Commit de715009 authored by Tobias Meuser's avatar Tobias Meuser Committed by Jose Ignacio Monreal Bailey
Browse files

First really working version for adhoc

parent ae21935b
......@@ -40,6 +40,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.Avai
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.JamInformation;
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.RoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
......@@ -88,6 +89,7 @@ implements CachingComponent, ConnectivityListener {
for (T t : cacheEntries) {
Object position = getEdgeOrPosition(t);
if (!similarCacheEntries.containsKey(position)) {
similarCacheEntries.put(position, new ArrayList<>());
}
......
......@@ -23,7 +23,7 @@ package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.NumericVectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralJamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.VectoralJamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
......
......@@ -23,8 +23,8 @@ package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List;
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.vector.TemporalDependencyMatrix;
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;
......
......@@ -22,12 +22,12 @@ package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.JamProperty;
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;
public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
......@@ -46,11 +46,7 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE);
double actualSpeed = edge.getCurrentSpeedState();
if (actualSpeed == 24d) {
System.out.println();
}
double actualSpeed = edge.getCurrentSpeed();
JamProperty jamProperty = edge.getJamProperty();
......@@ -61,6 +57,7 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
maxTimestamp = -1;
}
double difference = Double.MAX_VALUE;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
......@@ -69,9 +66,15 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty) currentValue).getMostProbableValue();
}
if (currentValue.equals(actualSpeed) && timestamp > maxTimestamp) {
maxTimestamp = timestamp;
if (timestamp >= maxTimestamp) {
if (currentValue.equals(actualSpeed)) {
maxFitting = t;
difference = 0;
} else if (currentValue instanceof Number && Math.abs(((Number)currentValue).doubleValue() - actualSpeed) < difference) {
maxFitting = t;
difference = Math.abs(((Number)currentValue).doubleValue() - actualSpeed);
}
}
}
......@@ -81,7 +84,7 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (timestamp > maxTimestamp) {
if (timestamp >= maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
......
......@@ -27,14 +27,15 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import cern.colt.Arrays;
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.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.VectoralJamProperty;
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 edu.emory.mathcs.backport.java.util.Arrays;
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
......@@ -117,10 +118,6 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
}
if (differentValue) {
if (Time.getCurrentTime() == 463500000) {
System.out.println();
}
long difference = maxTimestamp - minTimestamp;
if (difference == 0) {
......@@ -176,8 +173,8 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
long timestamp = t.getDetectionDate();
Object currentValue = t.getValue();
if (currentValue instanceof VectoralJamProperty) {
currentValue = ((VectoralJamProperty)currentValue).getMostProbableValue();
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty)currentValue).getMostProbableValue();
}
if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
......@@ -186,6 +183,26 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
}
}
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;
}
_lastDecision = maxFitting.getValue();
return maxFitting;
......
......@@ -21,15 +21,16 @@
package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.TemporalDependencyMatrix;
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.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;
......@@ -100,11 +101,11 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue();
if (value instanceof VectoralProperty) {
value = ((VectoralProperty) value).getMostProbableValue();
value = ((VectoralProperty) value).getMostProbableIndex();
}
boolean differentValue = false;
List<Double> possibleValues = new ArrayList<>();
List<Integer> possibleValues = new ArrayList<>();
for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
......@@ -122,13 +123,16 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
Object currentValue = t.getValue();
if (currentValue instanceof VectoralProperty) {
VectoralProperty currentProperty = (VectoralProperty) currentValue;
currentValue = currentProperty.getMostProbableValue();
currentValue = currentProperty.getMostProbableIndex();
if (!value.equals(currentValue)) {
differentValue = true;
}
if (!possibleValues.contains(currentValue)) {
possibleValues.add((Double) currentValue);
for (int i = 0; i < currentProperty.getValueProbabilities().length; i++) {
if (!possibleValues.contains(i)) {
possibleValues.add(i);
}
}
}
}
......@@ -143,26 +147,33 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = getTTL(pSimilarPointInformation.get(0));
double numberOfMessages = ttl / rate;
double numberOfMessages = ttl / rate + 1;
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);
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)) {
b += temp;
count++;
bValues.add(temp);
}
}
if (count > 0) {
b /= count;
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) {
RoadInformation roadInformation = ((RoadInformation) t);
VectoralProperty property = (VectoralProperty) roadInformation.getValue();
......@@ -214,12 +225,12 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
* @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;
}
// if (pT instanceof RoadInformation) {
// RoadInformation roadInformation = ((RoadInformation) pT);
// VectoralProperty property = (VectoralProperty) roadInformation.getValue();
// double accuracy = property.getProbabilityForIndex(property.getMostProbableIndex());
// return accuracy;
// }
return this.accuracy;
}
......@@ -296,17 +307,17 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
}
public double determineB(RoadNetworkEdge pRoadNetworkEdge, double pPossibleValue, double change, double rate, double errorProbability, double pNumberOfMessages, double costSlow, double costFast) {
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 == ((VectoralJamProperty)_lastDecision).getMostProbableValue()) {
if (pPossibleValue == ((VectoralProperty)_lastDecision).getMostProbableIndex()) {
return Double.NaN;
}
} else {
if (pPossibleValue == pRoadNetworkEdge.getCurrentSpeedState()) {
if (pPossibleValue == pTemplate.getDefaultIndex()) {
return Double.NaN;
}
}
......@@ -316,6 +327,10 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if ((int)pNumberOfMessages <= optimalAmount) {
return Double.POSITIVE_INFINITY;
}
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
......@@ -331,15 +346,15 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
}
do {
VectoralJamProperty valueAfterN = calculateMostProbableValue(pRoadNetworkEdge, pPossibleValue, optimalAmount, rate, errorProbability, pNumberOfMessages, b);
VectoralProperty valueAfterN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount, rate, errorProbability, pNumberOfMessages, b);
double probableValueAfterN = Double.NaN;
if (valueAfterN != null) {
probableValueAfterN = valueAfterN.getMostProbableValue();
probableValueAfterN = valueAfterN.getMostProbableIndex();
}
VectoralJamProperty valueBeforeN = calculateMostProbableValue(pRoadNetworkEdge, pPossibleValue, optimalAmount - 1, rate, errorProbability, pNumberOfMessages, b);
VectoralProperty valueBeforeN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount - 1, rate, errorProbability, pNumberOfMessages, b);
double probableValueBeforeN = Double.NaN;
if (valueBeforeN != null) {
probableValueBeforeN = valueBeforeN.getMostProbableValue();
probableValueBeforeN = valueBeforeN.getMostProbableIndex();
}
if (probableValueAfterN == pPossibleValue && probableValueAfterN != probableValueBeforeN) {
......@@ -378,16 +393,16 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
} while (true);
}
public VectoralJamProperty calculateMostProbableValue(RoadNetworkEdge pRoadNetworkEdge, double pNewValue, int optimalMessageAmount, double rate, double errorProbability, double pNumberOfMessages, double b) {
VectoralJamProperty currentProperty = null;
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++) {
VectoralJamProperty jamProperty = new VectoralJamProperty();
VectoralProperty jamProperty = pTemplate.clone();
if (_lastDecision != null) {
jamProperty.setGaussianSpeedWithAccuracy(((VectoralJamProperty)_lastDecision).getMostProbableValue(), accuracy);
jamProperty.set(((VectoralProperty)_lastDecision).getMostProbableIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
} else {
jamProperty.setGaussianSpeedWithAccuracy(pRoadNetworkEdge.getCurrentSpeedState(), accuracy);
jamProperty.set(pTemplate.getDefaultIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
}
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
......@@ -398,18 +413,18 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralJamProperty) jamProperty.age(1, temporalDependencyMatrix);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralJamProperty) currentProperty.combine(jamProperty);
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
}
for (int a = 0; a <= optimalMessageAmount; a++) {
VectoralJamProperty jamProperty = new VectoralJamProperty();
jamProperty.setGaussianSpeedWithAccuracy(pNewValue, accuracy);
VectoralProperty jamProperty = pTemplate.clone();
jamProperty.setGaussianWithAccuracy(pNewValue, accuracy);
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
......@@ -420,10 +435,10 @@ public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrat
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralJamProperty) jamProperty.age(1, temporalDependencyMatrix);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralJamProperty) currentProperty.combine(jamProperty);
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
......
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