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 ...@@ -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.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation; 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.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.relevance.impl.SimpleQoIBasedImpactFunction;
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND; private static final long SCALING = Time.SECOND;
...@@ -95,51 +96,21 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -95,51 +96,21 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
}); });
long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue(); Object value = pSimilarPointInformation.get(0).getValue();
boolean differentValue = false; boolean differentValue = false;
for (T t : pSimilarPointInformation) { 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())) { if (!value.equals(t.getValue())) {
differentValue = true; differentValue = true;
} }
} }
if (differentValue) { if (differentValue) {
long difference = maxTimestamp - minTimestamp; SimpleQoIBasedImpactFunction<T> impactFunction = new SimpleQoIBasedImpactFunction<>(pSimilarPointInformation, accuracy);
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);
}
Map<Object, Double> weight = new HashMap<>(); Map<Object, Double> weight = new HashMap<>();
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
double impact = calculateImpact(1 - accuracy, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING); double impact = impactFunction.calculateImpact(t);
double sumImpact = 0; double sumImpact = 0;
...@@ -167,7 +138,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -167,7 +138,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
} }
maxTimestamp = -1; long maxTimestamp = -1;
T maxFitting = null; T maxFitting = null;
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate(); long timestamp = t.getDetectionDate();
...@@ -207,7 +178,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -207,7 +178,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
return maxFitting; return maxFitting;
} else { } else {
maxTimestamp = -1; long maxTimestamp = -1;
T maxFitting = null; T maxFitting = null;
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL); long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);
...@@ -223,140 +194,4 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -223,140 +194,4 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
return maxFitting; 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 @@ ...@@ -21,7 +21,6 @@
package de.tud.kom.p2psim.impl.vehicular.caching.decision; package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
...@@ -31,20 +30,15 @@ import java.util.Map.Entry; ...@@ -31,20 +30,15 @@ import java.util.Map.Entry;
import de.tudarmstadt.maki.simonstrator.api.Time; 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.TemporalDependencyMatrix;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty; 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.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes; 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.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation; 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 { public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND; 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 accuracy = 1;
private double costWrongKeep = 1; private double costWrongKeep = 1;
...@@ -138,47 +132,15 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -138,47 +132,15 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
} }
if (differentValue) { 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;
VectoralProperty currentProperty = null; VectoralProperty currentProperty = null;
List<Double> bValues = new ArrayList<>(); VectoralQoIBasedImpactFunction<T> impactFunction = new VectoralQoIBasedImpactFunction<>(pSimilarPointInformation, accuracy, _lastDecision);
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;
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
RoadInformation roadInformation = ((RoadInformation) t); RoadInformation roadInformation = ((RoadInformation) t);
VectoralProperty property = (VectoralProperty) roadInformation.getValue(); 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(); TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
dependencyMatrix = modifyDependencyMatrix(dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING), impact); dependencyMatrix = modifyDependencyMatrix(dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING), impact);
...@@ -220,36 +182,6 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -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 pT
* @param pRoadInformation * @param pRoadInformation
...@@ -276,174 +208,4 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -276,174 +208,4 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
return result; 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);
}
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