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

First really working version for ad-hoc

parent 12e90c74
...@@ -55,7 +55,28 @@ public class RunnerController implements ActionListener { ...@@ -55,7 +55,28 @@ public class RunnerController implements ActionListener {
* @param configFile * @param configFile
*/ */
private void runSimulator() { private void runSimulator() {
new SimulationThread(selectedFile, det.getChosenSeed()).start(); SimulationThread simulationThread = new SimulationThread(selectedFile, det.getChosenSeed());
simulationThread.start();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
StackTraceElement[] stackTrace = simulationThread.getStackTrace();
System.out.println();
System.out.println();
for (int i = 0; i < stackTrace.length; i++) {
System.out.println(stackTrace[i]);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
});
// thread.start();
} }
public void setLastOpened(LastOpened lastOpened) { public void setLastOpened(LastOpened lastOpened) {
......
...@@ -29,6 +29,7 @@ import de.tudarmstadt.maki.simonstrator.api.Host; ...@@ -29,6 +29,7 @@ import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException; import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface; import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName; import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener; import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
...@@ -98,7 +99,6 @@ implements CachingComponent, ConnectivityListener { ...@@ -98,7 +99,6 @@ implements CachingComponent, ConnectivityListener {
.values()) { .values()) {
PointInformation correctInformation = _decisionStrategy PointInformation correctInformation = _decisionStrategy
.decideOnCorrectInformation(similarEntries); .decideOnCorrectInformation(similarEntries);
decidedInformation.add((T) correctInformation); decidedInformation.add((T) correctInformation);
} }
......
...@@ -60,7 +60,7 @@ public class AveragingCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -60,7 +60,7 @@ public class AveragingCacheDecisionStrategy implements CacheDecisionStrategy {
double value = sum / count; double value = sum / count;
if (cloned instanceof VectoralJamProperty) { if (cloned instanceof VectoralJamProperty) {
((VectoralJamProperty) cloned).setSpeed(((int)(value / VectoralJamProperty.SCALING)) * VectoralJamProperty.SCALING, 0); ((VectoralJamProperty) cloned).setSpeed(((int)(value / VectoralJamProperty.SCALING)) * VectoralJamProperty.SCALING);
} else { } else {
throw new AssertionError("Unknown data type " + cloned.getClass().getSimpleName()); throw new AssertionError("Unknown data type " + cloned.getClass().getSimpleName());
} }
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
package de.tud.kom.p2psim.impl.vehicular.caching.decision; package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.Arrays;
import java.util.List; import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Time; import de.tudarmstadt.maki.simonstrator.api.Time;
...@@ -71,7 +70,21 @@ public class MajorityVotingVectoralCacheDecisionStrategy implements CacheDecisio ...@@ -71,7 +70,21 @@ public class MajorityVotingVectoralCacheDecisionStrategy implements CacheDecisio
VectoralProperty agedProperty = currentProperty.age((Time.getCurrentTime() - maxTimestamp) / SCALING, dependencyMatrix); VectoralProperty agedProperty = currentProperty.age((Time.getCurrentTime() - maxTimestamp) / SCALING, dependencyMatrix);
return (T) new RoadInformation(agedProperty); RoadInformation roadInformation = new RoadInformation(agedProperty);
copyAttributes((RoadInformation) pSimilarPointInformation.get(0), roadInformation);
return (T) roadInformation;
}
/**
* @param pT
* @param pRoadInformation
*/
private void copyAttributes(RoadInformation pSrc, RoadInformation pDest) {
for (AvailableInformationAttributes attribute : pSrc.getAvailableAttributes()) {
pDest.setAttribute(attribute, pSrc.getAttribute(attribute));
}
} }
} }
...@@ -22,9 +22,12 @@ package de.tud.kom.p2psim.impl.vehicular.caching.decision; ...@@ -22,9 +22,12 @@ package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List; import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
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.roadnetwork.RoadNetworkEdge; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy { public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
...@@ -42,18 +45,31 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -42,18 +45,31 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
} }
RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE); RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE);
double actualSpeed = edge.getCurrentSpeedState();
if (actualSpeed == 24d) {
System.out.println();
}
JamProperty jamProperty = edge.getJamProperty();
boolean jam = false; long maxTimestamp;
if (edge.getLastStepMeanSpeed() < 5) { if (jamProperty != null) {
jam = true; maxTimestamp = jamProperty.getDetectionDate();
} else {
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();
if (t.getValue().equals(jam) && timestamp > maxTimestamp) { Object currentValue = t.getValue();
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty) currentValue).getMostProbableValue();
}
if (currentValue.equals(actualSpeed) && timestamp > maxTimestamp) {
maxTimestamp = timestamp; maxTimestamp = timestamp;
maxFitting = t; maxFitting = t;
} }
......
...@@ -20,16 +20,21 @@ ...@@ -20,16 +20,21 @@
package de.tud.kom.p2psim.impl.vehicular.caching.decision; package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.Arrays; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import cern.colt.Arrays;
import de.tudarmstadt.maki.simonstrator.api.Time; import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.RoadProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralJamProperty;
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;
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND; private static final long SCALING = Time.SECOND;
...@@ -79,6 +84,15 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -79,6 +84,15 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} else if (pSimilarPointInformation.size() == 0) { } else if (pSimilarPointInformation.size() == 0) {
return null; 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 minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0; long maxTimestamp = 0;
...@@ -103,6 +117,10 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -103,6 +117,10 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
if (differentValue) { if (differentValue) {
if (Time.getCurrentTime() == 463500000) {
System.out.println();
}
long difference = maxTimestamp - minTimestamp; long difference = maxTimestamp - minTimestamp;
if (difference == 0) { if (difference == 0) {
...@@ -111,7 +129,8 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -111,7 +129,8 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING); double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
rate = Math.min(rate, ttl / 20); long ttl = (long)pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.TTL) / SCALING;
rate = Math.min(rate, ttl / 10.0);
double b; double b;
if (Boolean.FALSE.equals(_lastDecision)) { if (Boolean.FALSE.equals(_lastDecision)) {
...@@ -121,18 +140,24 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -121,18 +140,24 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
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 = calculateImpact(1 - accuracy, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING);
double sumImpact = 0; double sumImpact = 0;
if (weight.containsKey(t.getValue())) { Object currentValue = t.getValue();
sumImpact = weight.get(t.getValue());
if (currentValue instanceof VectoralJamProperty) {
currentValue = ((VectoralJamProperty) currentValue).getMostProbableValue();
}
if (weight.containsKey(currentValue)) {
sumImpact = weight.get(currentValue);
} }
sumImpact += impact; sumImpact += impact;
weight.put(t.getValue(), sumImpact); weight.put(currentValue, sumImpact);
} }
double maxWeight = -1; double maxWeight = -1;
...@@ -150,7 +175,12 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -150,7 +175,12 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate(); long timestamp = t.getDetectionDate();
if (t.getValue().equals(maxValue) && timestamp > maxTimestamp) { Object currentValue = t.getValue();
if (currentValue instanceof VectoralJamProperty) {
currentValue = ((VectoralJamProperty)currentValue).getMostProbableValue();
}
if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
maxTimestamp = timestamp; maxTimestamp = timestamp;
maxFitting = t; maxFitting = t;
} }
...@@ -178,8 +208,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -178,8 +208,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) { public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) {
long currentTime = Time.getCurrentTime() / SCALING; long age = maxTimestamp - time;
long age = currentTime - time;
if (errorProbability == 0) { if (errorProbability == 0) {
if (time == maxTimestamp) { if (time == maxTimestamp) {
return 1; return 1;
...@@ -190,6 +219,12 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -190,6 +219,12 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
return 1; return 1;
} else if (errorProbability == 0.5) { } else if (errorProbability == 0.5) {
return (errorProbability - 1) / ttl * age + errorProbability; 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)); return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * ttl)) / (1 - Math.exp(b * ttl));
...@@ -217,6 +252,10 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -217,6 +252,10 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast); int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
boolean first = true; boolean first = true;
double leftSide; double leftSide;
...@@ -290,16 +329,16 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -290,16 +329,16 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) { public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0; double impact = 0;
for (int a = optimalMessageAmount + 1; a < Math.max(Math.floor(ttl / rate), optimalMessageAmount + 2); a++) { 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, 0); impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
} }
return impact; return impact;
} }
public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) { public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0; double impact = 0;
for (int a = 0; a <= optimalMessageAmount; a++) { for (int a = 0; a < optimalMessageAmount; a++) {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, 0); impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
} }
return impact; return impact;
} }
......
...@@ -20,18 +20,21 @@ ...@@ -20,18 +20,21 @@
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.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; 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.VectoralJamProperty;
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.vector.TemporalDependencyMatrix; import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.vector.TemporalDependencyMatrix;
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 edu.emory.mathcs.backport.java.util.Collections; import edu.emory.mathcs.backport.java.util.Collections;
public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrategy { public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrategy {
...@@ -39,12 +42,14 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -39,12 +42,14 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
private static final double ACCURACY_FACTOR = 100000; 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;
private double costWrongChange = 1; private double costWrongChange = 1;
private Object _lastDecision = false; private Object _lastDecision = null;
public TTLbasedVectoralCacheDecisionStrategy(Map<String, String> pParams) { public TTLbasedVectoralCacheDecisionStrategy(Map<String, String> pParams) {
for (Entry<String, String> param : pParams.entrySet()) { for (Entry<String, String> param : pParams.entrySet()) {
...@@ -94,7 +99,13 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -94,7 +99,13 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
long minTimestamp = Long.MAX_VALUE; long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0; long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue(); Object value = pSimilarPointInformation.get(0).getValue();
if (value instanceof VectoralProperty) {
value = ((VectoralProperty) value).getMostProbableValue();
}
boolean differentValue = false; boolean differentValue = false;
List<Double> possibleValues = new ArrayList<>();
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) { if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Unable to perform TTL-based majority voting witout TTL"); throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
...@@ -108,8 +119,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -108,8 +119,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
maxTimestamp = timestamp; maxTimestamp = timestamp;
} }
if (!value.equals(t.getValue())) { Object currentValue = t.getValue();
differentValue = true; if (currentValue instanceof VectoralProperty) {
VectoralProperty currentProperty = (VectoralProperty) currentValue;
currentValue = currentProperty.getMostProbableValue();
if (!value.equals(currentValue)) {
differentValue = true;
}
if (!possibleValues.contains(currentValue)) {
possibleValues.add((Double) currentValue);
}
} }
} }
...@@ -122,24 +142,37 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -122,24 +142,37 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING); double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = (long)pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.TTL) / SCALING; long ttl = getTTL(pSimilarPointInformation.get(0));
rate = Math.min(rate, ttl / 10); double numberOfMessages = ttl / rate;
double b = determineB(rate, 1 - accuracy, ttl, costWrongKeep, costWrongChange);
VectoralProperty currentProperty = null; VectoralProperty currentProperty = null;
double b = 0;
int count = 0;
for (Double possibleValue : possibleValues) {
double temp = determineB(((RoadInformation)pSimilarPointInformation.get(0)).getEdge(), possibleValue, getChangeRate(pSimilarPointInformation.get(0), rate), rate, 1 - accuracy, numberOfMessages, costWrongKeep, costWrongChange);
if (!Double.isNaN(temp)) {
b += temp;
count++;
}
}
if (count > 0) {
b /= count;
} else {
b = Double.NEGATIVE_INFINITY;
}
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, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING) / (accuracy); double impact = calculateImpact(1 - accuracy, numberOfMessages, (((t.getDetectionDate() - maxTimestamp) / SCALING + ttl) / (double)ttl) * numberOfMessages, b) / (accuracy);
TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix(); TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
dependencyMatrix = dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING); dependencyMatrix = modifyDependencyMatrix(dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING), impact);
dependencyMatrix = modifyDependencyMatrix(dependencyMatrix, impact);
VectoralProperty agedProperty = property.age(1, dependencyMatrix); VectoralProperty agedProperty = property.age(1, dependencyMatrix);
if (currentProperty != null) { if (currentProperty != null) {
currentProperty = currentProperty.combine(agedProperty); currentProperty = currentProperty.combine(agedProperty);
} else { } else {
...@@ -147,7 +180,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -147,7 +180,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
} }
} }
return (T) new RoadInformation(currentProperty); 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 { } else {
maxTimestamp = -1; maxTimestamp = -1;
T maxFitting = null; T maxFitting = null;
...@@ -166,6 +209,46 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -166,6 +209,46 @@ 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.getProbabilityForValue(property.getMostProbableValue());
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
*/
private void copyAttributes(RoadInformation pSrc, RoadInformation pDest) {
for (AvailableInformationAttributes attribute : pSrc.getAvailableAttributes()) {
pDest.setAttribute(attribute, pSrc.getAttribute(attribute));
}
}
private TemporalDependencyMatrix modifyDependencyMatrix( private TemporalDependencyMatrix modifyDependencyMatrix(
TemporalDependencyMatrix pDependencyMatrix, double pImpact) { TemporalDependencyMatrix pDependencyMatrix, double pImpact) {
TemporalDependencyMatrix result = pDependencyMatrix.clone(); TemporalDependencyMatrix result = pDependencyMatrix.clone();
...@@ -175,34 +258,34 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -175,34 +258,34 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
double finalPercentages = 1.0 / dependencies[i].length; double finalPercentages = 1.0 / dependencies[i].length;
for (int j = 0; j < dependencies[i].length; j++) { for (int j = 0; j < dependencies[i].length; j++) {
dependencies[i][j] = finalPercentages + (dependencies[i][j] - finalPercentages) * pImpact; dependencies[i][j] = finalPercentages + (pDependencyMatrix.getDependencies()[i][j] - finalPercentages) * pImpact;
} }
} }
return result; return result;
} }
public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) { public double calculateImpact(double errorProbability, double pNumberOfMessages, double pMessageNumber, double b) {
long age = maxTimestamp - time; double age = pNumberOfMessages - pMessageNumber;
if (errorProbability == 0) { if (errorProbability == 0) {
if (time == maxTimestamp) { if (pMessageNumber == pNumberOfMessages) {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
} else if (errorProbability == 1) { } else if (errorProbability == 1) {
return 1; return 1;
} else if (errorProbability == 0.5) { } else if (errorProbability == 0.5 || b == 0) {
return (errorProbability - 1) / ttl * age + errorProbability; return (1 - errorProbability) / pNumberOfMessages * age + errorProbability;
} else if (b == Double.NEGATIVE_INFINITY) { } else if (b == Double.NEGATIVE_INFINITY) {
if (time == maxTimestamp) { if (pMessageNumber == pNumberOfMessages) {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
} }
return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * ttl)) / (1 - Math.exp(b * ttl)); return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * pNumberOfMessages)) / (1 - Math.exp(b * pNumberOfMessages));
} }
public double getChangeProbability(long ttl) { public double getChangeProbability(long ttl) {
...@@ -213,109 +296,139 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat ...@@ -213,109 +296,139 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability)); 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) { public double determineB(RoadNetworkEdge pRoadNetworkEdge, double pPossibleValue, double change, double rate, double errorProbability, double pNumberOfMessages, 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) { if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
return Double.NaN; return Double.NaN;
} }
if (_lastDecision != null) {
if (pPossibleValue == ((VectoralJamProperty)_lastDecision).getMostProbableValue()) {
return Double.NaN;
}
} else {
if (pPossibleValue == pRoadNetworkEdge.getCurrentSpeedState()) {
return Double.NaN;
}
}
double b; double b;
double p_c = getChangeProbability((long) (ttl / rate)); double p_c = change;
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast); int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if (optimalAmount == 1) { if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY; return Double.NEGATIVE_INFINITY;
} }
boolean first = true; boolean first = true;
double leftSide; double step = 10;
double rightSide;
double step = 5;
if (errorProbability < 0.5) { if (errorProbability < 0.5) {
b = -2 * step * reversed; b = -1 * step;
} else { } else {
b = 2 * step * reversed; b = 1 * step;
} }
int similar = 0;
double lastDifference = -1;
do { do {
leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b); VectoralJamProperty valueAfterN = calculateMostProbableValue(pRoadNetworkEdge, pPossibleValue, optimalAmount, rate, errorProbability, pNumberOfMessages, b);
rightSide = calculateWeightingForNewState(optimalAmount, rate, errorProbability, ttl, b); double probableValueAfterN = Double.NaN;
if (valueAfterN != null) {
probableValueAfterN = valueAfterN.getMostProbableValue();
}
VectoralJamProperty valueBeforeN = calculateMostProbableValue(pRoadNetworkEdge, pPossibleValue, optimalAmount - 1, rate, errorProbability, pNumberOfMessages, b);
double probableValueBeforeN = Double.NaN;
if (valueBeforeN != null) {
probableValueBeforeN = valueBeforeN.getMostProbableValue();
}
if (Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR)) == lastDifference) { if (probableValueAfterN == pPossibleValue && probableValueAfterN != probableValueBeforeN) {
similar++; return b;
} else {
lastDifference = Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR));
similar = 0;
} }
if (Double.isNaN(leftSide) || Double.isNaN(rightSide) || similar > 100) { if (!Double.isNaN(probableValueBeforeN) && !Double.isNaN(probableValueBeforeN) && step >= MIN_STEP) {
if (reversed != -1) { if (probableValueAfterN != pPossibleValue) {
double determineB = determineB(rate, errorProbability, ttl, costSlow, costFast, -1); if (b < 0) {
if (!Double.isNaN(determineB)) { b -= step;
return determineB; if (!first) {
step *= 0.5;
}
} else { } else {
return b; b -= step;
}
} 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; step *= 0.5;
first = false;
} }
} else { } else if (probableValueAfterN == pPossibleValue && probableValueAfterN == probableValueBeforeN) {
b -= step; if (b > 0) {
step *= 0.5; b += step;
first = false; if (!first) {
} step *= 0.5;
} else if (leftSide < rightSide) { }
if (b > 0) { } else {
b += step; b += step;
if (!first) {
step *= 0.5; step *= 0.5;
first = false;
} }
} else { } else {
b += step; return Double.NaN;
step *= 0.5;
first = false;
} }
} else { } else {
break; return Double.NaN;
} }
} while (true); } while (true);
return b;
} }
public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) { public VectoralJamProperty calculateMostProbableValue(RoadNetworkEdge pRoadNetworkEdge, double pNewValue, int optimalMessageAmount, double rate, double errorProbability, double pNumberOfMessages, double b) {
double impact = 0; VectoralJamProperty currentProperty = null;
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); for (int a = optimalMessageAmount + 1; a <= pNumberOfMessages; a++) {
VectoralJamProperty jamProperty = new VectoralJamProperty();
if (_lastDecision != null) {
jamProperty.setGaussianSpeedWithAccuracy(((VectoralJamProperty)_lastDecision).getMostProbableValue(), accuracy);
} else {
jamProperty.setGaussianSpeedWithAccuracy(pRoadNetworkEdge.getCurrentSpeedState(), 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 = (VectoralJamProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralJamProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
} }
return impact;
} for (int a = 0; a <= optimalMessageAmount; a++) {
VectoralJamProperty jamProperty = new VectoralJamProperty();
jamProperty.setGaussianSpeedWithAccuracy(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 = (VectoralJamProperty) jamProperty.age(1, temporalDependencyMatrix);
public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) { if (currentProperty != null) {
double impact = 0; currentProperty = (VectoralJamProperty) currentProperty.combine(jamProperty);
for (int a = 0; a < optimalMessageAmount; a++) { } else {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING); currentProperty = jamProperty;
}
} }
return impact;
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