diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/DefaultCachingComponent.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/DefaultCachingComponent.java index 7779ca096d2a4c77573012443e88e20663bf5325..6584958a8570caf72de674c7d20ce86a7b09c827 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/DefaultCachingComponent.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/DefaultCachingComponent.java @@ -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<>()); } diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/AveragingCacheDecisionStrategy.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/AveragingCacheDecisionStrategy.java index 5d0fc023cf3a105d07ec22b541a77c82ecd73ae3..d52964b5153d1828350954766eaf70434bc9d2f8 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/AveragingCacheDecisionStrategy.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/AveragingCacheDecisionStrategy.java @@ -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; diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/MajorityVotingVectoralCacheDecisionStrategy.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/MajorityVotingVectoralCacheDecisionStrategy.java index d151617cece548ad78299540da8098f40bfe7ae6..2febfe2e5a03047ee782c78b946bc8e757917623 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/MajorityVotingVectoralCacheDecisionStrategy.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/MajorityVotingVectoralCacheDecisionStrategy.java @@ -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; diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/OptimalCacheDecisionStrategy.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/OptimalCacheDecisionStrategy.java index 671b70400977399b0a27daebf35336cafcd7836b..05d72cdb98494fb20858eabba30dfd8d22485e71 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/OptimalCacheDecisionStrategy.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/OptimalCacheDecisionStrategy.java @@ -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 { @@ -43,14 +43,10 @@ public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy { } else if (pSimilarPointInformation.size() == 0) { return null; } - - RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE); - double actualSpeed = edge.getCurrentSpeedState(); + RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE); - 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; - maxFitting = t; + + 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; } diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedCacheDecisionStrategy.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedCacheDecisionStrategy.java index d6015dac9ff72848667fd0849f49bd6c3b14b365..012090d3f3b73158eda0a4f544a4efb1b5a2d6ee 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedCacheDecisionStrategy.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedCacheDecisionStrategy.java @@ -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) { @@ -185,6 +182,26 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { 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; + } _lastDecision = maxFitting.getValue(); @@ -206,7 +223,7 @@ 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) { diff --git a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedVectoralCacheDecisionStrategy.java b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedVectoralCacheDecisionStrategy.java index 538fb8d71c2c1c9ce0943d599f0d76b2e08f1084..9cf6547750de2057d9b78a4f882f7e9acab4f4fb 100755 --- a/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedVectoralCacheDecisionStrategy.java +++ b/src/de/tud/kom/p2psim/impl/vehicular/caching/decision/TTLbasedVectoralCacheDecisionStrategy.java @@ -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 possibleValues = new ArrayList<>(); + List 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 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 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; }