Commit c911a273 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Introduced impact separate function

parent 4c122520
......@@ -36,6 +36,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.relevance.impl.SimpleQoIBasedImpactFunction;
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
......@@ -85,66 +86,36 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
Collections.sort(pSimilarPointInformation, new Comparator<T>() {
@Override
public int compare(T pArg0, T pArg1) {
return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
}
});
long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue();
boolean differentValue = false;
for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
}
long timestamp = t.getDetectionDate();
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
}
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
if (!value.equals(t.getValue())) {
differentValue = true;
}
}
if (differentValue) {
long difference = maxTimestamp - minTimestamp;
if (difference == 0) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = (long)pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.TTL) / SCALING;
rate = Math.min(rate, ttl / 10.0);
double b;
if (Boolean.FALSE.equals(_lastDecision)) {
b = determineB(rate, 1 - accuracy, ttl, costWrongKeep, costWrongChange);
} else {
b = determineB(rate, 1 - accuracy, ttl, costWrongChange, costWrongKeep);
}
SimpleQoIBasedImpactFunction<T> impactFunction = new SimpleQoIBasedImpactFunction<>(pSimilarPointInformation, accuracy);
Map<Object, Double> weight = new HashMap<>();
for (T t : pSimilarPointInformation) {
double impact = calculateImpact(1 - accuracy, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING);
double impact = impactFunction.calculateImpact(t);
double sumImpact = 0;
Object currentValue = t.getValue();
if (currentValue instanceof VectoralJamProperty) {
currentValue = ((VectoralJamProperty) currentValue).getMostProbableValue();
}
......@@ -167,7 +138,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
}
}
maxTimestamp = -1;
long maxTimestamp = -1;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
......@@ -176,28 +147,28 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty)currentValue).getMostProbableValue();
}
if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
if (maxFitting.getValue() instanceof VectoralProperty) {
VectoralProperty vectoralProperty = ((VectoralProperty)maxFitting.getValue()).clone();
double[] valueProbabilities = vectoralProperty.getValueProbabilities();
Arrays.fill(valueProbabilities, 0);
double sum = 0;
for (Object key : weight.keySet()) {
valueProbabilities[vectoralProperty.getIndexForValue(key)] = weight.get(key);
sum += weight.get(key);
}
for (int i = 0; i < valueProbabilities.length; i++) {
valueProbabilities[i] /= sum;
}
RoadInformation roadInformation = new RoadInformation(vectoralProperty);
roadInformation.copyAttributes((RoadInformation)maxFitting);
maxFitting = (T) roadInformation;
......@@ -207,7 +178,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
return maxFitting;
} else {
maxTimestamp = -1;
long maxTimestamp = -1;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);
......@@ -223,140 +194,4 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
return maxFitting;
}
}
public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) {
long age = maxTimestamp - time;
if (errorProbability == 0) {
if (time == maxTimestamp) {
return 1;
} else {
return 0;
}
} else if (errorProbability == 1) {
return 1;
} else if (errorProbability == 0.5) {
return (errorProbability - 1) / ttl * age + errorProbability;
} else if (b == Double.NEGATIVE_INFINITY) {
if (time == maxTimestamp) {
return 1;
} else {
return 0;
}
}
return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * ttl)) / (1 - Math.exp(b * ttl));
}
public double getChangeProbability(long ttl) {
return 1 - Math.pow(0.5, 1 / (double) ttl);
}
public int getOptimalMessageAmountForSwitch(double changeProbability, double errorProbability, double costSlow, double costFast) {
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
}
public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast) {
return determineB(rate, errorProbability, ttl, costSlow, costFast, 1);
}
public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast, int reversed) {
if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
return Double.NaN;
}
double b;
double p_c = getChangeProbability((long) (ttl / rate));
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
boolean first = true;
double leftSide;
double rightSide;
double step = 5;
if (errorProbability < 0.5) {
b = -2 * step * reversed;
} else {
b = 2 * step * reversed;
}
int similar = 0;
double lastDifference = -1;
do {
leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b);
rightSide = calculateWeightingForNewState(optimalAmount, rate, errorProbability, ttl, b);
if (Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR)) == lastDifference) {
similar++;
} else {
lastDifference = Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR));
similar = 0;
}
if (Double.isNaN(leftSide) || Double.isNaN(rightSide) || similar > 100) {
if (reversed != -1) {
double determineB = determineB(rate, errorProbability, ttl, costSlow, costFast, -1);
if (!Double.isNaN(determineB)) {
return determineB;
} else {
return b;
}
} else {
return Double.NaN;
}
}
leftSide = Math.round(leftSide * ACCURACY_FACTOR);
rightSide = Math.round(rightSide * ACCURACY_FACTOR);
if (leftSide > rightSide) {
if (b < 0) {
b -= step;
if (!first) {
step *= 0.5;
}
} else {
b -= step;
step *= 0.5;
first = false;
}
} else if (leftSide < rightSide) {
if (b > 0) {
b += step;
if (!first) {
step *= 0.5;
}
} else {
b += step;
step *= 0.5;
first = false;
}
} else {
break;
}
} while (true);
return b;
}
public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0;
for (int a = optimalMessageAmount; a < Math.max(Math.floor(ttl / rate), optimalMessageAmount + 2); a++) {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
}
return impact;
}
public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0;
for (int a = 0; a < optimalMessageAmount; a++) {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
}
return impact;
}
}
......@@ -21,7 +21,6 @@
package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
......@@ -31,20 +30,15 @@ import java.util.Map.Entry;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.TemporalDependencyMatrix;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.measurement.MeasurementDistributionTypeContainer;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.relevance.impl.VectoralQoIBasedImpactFunction;
public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
private static final double ACCURACY_FACTOR = 100000;
private static final double MIN_STEP = 0.00001;
private double accuracy = 1;
private double costWrongKeep = 1;
......@@ -87,14 +81,14 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
Collections.sort(pSimilarPointInformation, new Comparator<T>() {
@Override
public int compare(T pArg0, T pArg1) {
return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
}
});
long minTimestamp = Long.MAX_VALUE;
......@@ -104,9 +98,9 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
value = ((VectoralProperty) value).getMostProbableIndex();
}
boolean differentValue = false;
List<Integer> possibleValues = new ArrayList<>();
for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
......@@ -124,11 +118,11 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
if (currentValue instanceof VectoralProperty) {
VectoralProperty currentProperty = (VectoralProperty) currentValue;
currentValue = currentProperty.getMostProbableIndex();
if (!value.equals(currentValue)) {
differentValue = true;
}
for (int i = 0; i < currentProperty.getValueProbabilities().length; i++) {
if (!possibleValues.contains(i)) {
possibleValues.add(i);
......@@ -136,53 +130,21 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
}
}
}
if (differentValue) {
long difference = maxTimestamp - minTimestamp;
if (difference == 0) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = getTTL(pSimilarPointInformation.get(0));
double numberOfMessages = ttl / rate + 1;
if (differentValue) {
VectoralProperty currentProperty = null;
List<Double> bValues = new ArrayList<>();
double b;
for (Integer possibleValue : possibleValues) {
double temp = determineB((VectoralProperty) pSimilarPointInformation.get(0).getValue(), ((RoadInformation)pSimilarPointInformation.get(0)).getEdge(), possibleValue, getChangeRate(pSimilarPointInformation.get(0), rate), rate, 1 - accuracy, numberOfMessages, costWrongKeep, costWrongChange);
if (!Double.isNaN(temp)) {
bValues.add(temp);
}
}
Collections.sort(bValues);
if (bValues.size() > 0) {
if (bValues.size() % 2 == 0) {
b = (bValues.get(bValues.size() / 2) + bValues.get(bValues.size() / 2 - 1)) / 2.0;
} else {
b = bValues.get(bValues.size() / 2);
}
} else {
b = Double.NEGATIVE_INFINITY;
}
int count = 0;
VectoralQoIBasedImpactFunction<T> impactFunction = new VectoralQoIBasedImpactFunction<>(pSimilarPointInformation, accuracy, _lastDecision);
for (T t : pSimilarPointInformation) {
RoadInformation roadInformation = ((RoadInformation) t);
VectoralProperty property = (VectoralProperty) roadInformation.getValue();
double impact = calculateImpact(1 - accuracy, numberOfMessages, (((t.getDetectionDate() - maxTimestamp) / SCALING + ttl) / (double)ttl) * numberOfMessages, b) / (accuracy);
double impact = impactFunction.calculateImpact(t);
TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
dependencyMatrix = modifyDependencyMatrix(dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING), impact);
VectoralProperty agedProperty = property.age(1, dependencyMatrix);
if (currentProperty != null) {
currentProperty = currentProperty.combine(agedProperty);
......@@ -190,17 +152,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
currentProperty = agedProperty;
}
}
if (Double.isNaN(currentProperty.getValueProbabilities()[0])) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
RoadInformation roadInformation = new RoadInformation(currentProperty);
copyAttributes((RoadInformation) pSimilarPointInformation.get(0), roadInformation);
_lastDecision = roadInformation.getValue();
return (T) roadInformation;
} else {
maxTimestamp = -1;
......@@ -220,36 +182,6 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
}
}
/**
* @param pT
* @return
*/
private <T extends PointInformation> double getAccuracy(T pT) {
// if (pT instanceof RoadInformation) {
// RoadInformation roadInformation = ((RoadInformation) pT);
// VectoralProperty property = (VectoralProperty) roadInformation.getValue();
// double accuracy = property.getProbabilityForIndex(property.getMostProbableIndex());
// return accuracy;
// }
return this.accuracy;
}
private <T extends PointInformation> double getChangeRate(T pT, double pRate) {
// if (pT instanceof RoadInformation) {
// RoadInformation roadInformation = ((RoadInformation) pT);
// VectoralProperty property = (VectoralProperty) roadInformation.getValue();
//
// TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
// return 1 - Math.pow(1 - dependencyMatrix.getChangeProbability(0), pRate * (SCALING / Time.SECOND));
// }
return getChangeProbability((long) (getTTL(pT) / pRate));
}
public <T extends PointInformation> long getTTL(
T pT) {
return (long)pT.getAttribute(AvailableInformationAttributes.TTL) / SCALING;
}
/**
* @param pT
* @param pRoadInformation
......@@ -263,187 +195,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
private TemporalDependencyMatrix modifyDependencyMatrix(
TemporalDependencyMatrix pDependencyMatrix, double pImpact) {
TemporalDependencyMatrix result = pDependencyMatrix.clone();
double[][] dependencies = result.getDependencies();
for (int i = 0; i < dependencies.length; i++) {
double finalPercentages = 1.0 / dependencies[i].length;
for (int j = 0; j < dependencies[i].length; j++) {
dependencies[i][j] = finalPercentages + (pDependencyMatrix.getDependencies()[i][j] - finalPercentages) * pImpact;
}
}
return result;
}
public double calculateImpact(double errorProbability, double pNumberOfMessages, double pMessageNumber, double b) {
double age = pNumberOfMessages - pMessageNumber;
if (errorProbability == 0) {
if (pMessageNumber == pNumberOfMessages) {
return 1;
} else {
return 0;
}
} else if (errorProbability == 1) {
return 1;
} else if (errorProbability == 0.5 || b == 0) {
return (1 - errorProbability) / pNumberOfMessages * age + errorProbability;
} else if (b == Double.NEGATIVE_INFINITY) {
if (pMessageNumber == pNumberOfMessages) {
return 1;
} else {
return 0;
}
}
return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * pNumberOfMessages)) / (1 - Math.exp(b * pNumberOfMessages));
}
public double getChangeProbability(long ttl) {
return 1 - Math.pow(0.5, 1 / (double) ttl);
}
public int getOptimalMessageAmountForSwitch(double changeProbability, double errorProbability, double costSlow, double costFast) {
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
}
public double determineB(VectoralProperty pTemplate, RoadNetworkEdge pRoadNetworkEdge, int pPossibleValue, double change, double rate, double errorProbability, double pNumberOfMessages, double costSlow, double costFast) {
if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
return Double.NaN;
}
if (_lastDecision != null) {
if (pPossibleValue == ((VectoralProperty)_lastDecision).getMostProbableIndex()) {
return Double.NaN;
}
} else {
if (pPossibleValue == pTemplate.getDefaultIndex()) {
return Double.NaN;
}
}
double b;
double p_c = change;
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if ((int)pNumberOfMessages <= optimalAmount) {
return Double.POSITIVE_INFINITY;
}
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
boolean first = true;
double step = 10;
if (errorProbability < 0.5) {
b = -1 * step;
} else {
b = 1 * step;
}
do {
VectoralProperty valueAfterN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount, rate, errorProbability, pNumberOfMessages, b);
double probableValueAfterN = Double.NaN;
if (valueAfterN != null) {
probableValueAfterN = valueAfterN.getMostProbableIndex();
}
VectoralProperty valueBeforeN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount - 1, rate, errorProbability, pNumberOfMessages, b);
double probableValueBeforeN = Double.NaN;
if (valueBeforeN != null) {
probableValueBeforeN = valueBeforeN.getMostProbableIndex();
}
if (probableValueAfterN == pPossibleValue && probableValueAfterN != probableValueBeforeN) {
return b;
}
if (!Double.isNaN(probableValueBeforeN) && !Double.isNaN(probableValueBeforeN) && step >= MIN_STEP) {
if (probableValueAfterN != pPossibleValue) {
if (b < 0) {
b -= step;
if (!first) {
step *= 0.5;
}
} else {
b -= step;
step *= 0.5;
first = false;
}
} else if (probableValueAfterN == pPossibleValue && probableValueAfterN == probableValueBeforeN) {
if (b > 0) {
b += step;
if (!first) {
step *= 0.5;
}
} else {
b += step;
step *= 0.5;
first = false;
}
} else {
return Double.NaN;
}
} else {
return Double.NaN;
}
} while (true);
return result;
}
public VectoralProperty calculateMostProbable(VectoralProperty pTemplate, RoadNetworkEdge pRoadNetworkEdge, int pNewValue, int optimalMessageAmount, double rate, double errorProbability, double pNumberOfMessages, double b) {
VectoralProperty currentProperty = null;
for (int a = optimalMessageAmount + 1; a <= pNumberOfMessages; a++) {
VectoralProperty jamProperty = pTemplate.clone();
if (_lastDecision != null) {
jamProperty.set(((VectoralProperty)_lastDecision).getMostProbableIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
} else {
jamProperty.set(pTemplate.getDefaultIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
}
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
double impact = calculateImpact(errorProbability, pNumberOfMessages, pNumberOfMessages - a, b);
if (Double.isNaN(impact)) {
return null;
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
}
for (int a = 0; a <= optimalMessageAmount; a++) {
VectoralProperty jamProperty = pTemplate.clone();
jamProperty.setGaussianWithAccuracy(pNewValue, accuracy);
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
double impact = calculateImpact(errorProbability, pNumberOfMessages, pNumberOfMessages - a, b);
if (Double.isNaN(impact)) {
return null;
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
}
return currentProperty;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment