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

Added additionally required decision strategies

parent 12002fdc
...@@ -59,6 +59,8 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor; ...@@ -59,6 +59,8 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class VehicleMovementModel implements MovementModel, EventHandler, FutureLocationSensor { public class VehicleMovementModel implements MovementModel, EventHandler, FutureLocationSensor {
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN);
private boolean _reuseComponents = false; private boolean _reuseComponents = false;
private static VehicleMovementModel MOVEMENT; private static VehicleMovementModel MOVEMENT;
...@@ -95,7 +97,6 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -95,7 +97,6 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
private List<Position> intersections; private List<Position> intersections;
/** /**
* Constructor for the movement model using the sumo TraCI API * Constructor for the movement model using the sumo TraCI API
* @param timeBetweenMoveOperations The time between two movement operations. * @param timeBetweenMoveOperations The time between two movement operations.
...@@ -168,7 +169,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -168,7 +169,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
components.add(comp); components.add(comp);
freeComponents.add(comp); freeComponents.add(comp);
comp.updateCurrentLocation(new PositionVector(Double.NaN, Double.NaN)); comp.updateCurrentLocation(DEFAULT_LOCATION);
} }
/** /**
...@@ -199,7 +200,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -199,7 +200,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
initialized = true; initialized = true;
} }
// Initial placement // Initial placement
actuator.updateCurrentLocation(new PositionVector(Double.NaN, Double.NaN)); actuator.updateCurrentLocation(DEFAULT_LOCATION);
try { try {
final SiSComponent sis = actuator.getHost().getComponent(SiSComponent.class); final SiSComponent sis = actuator.getHost().getComponent(SiSComponent.class);
...@@ -340,6 +341,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -340,6 +341,12 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
} }
} }
if (Time.getCurrentTime() < 5 * Time.SECOND) {
for (SimLocationActuator simLocationActuator : freeComponents) {
simLocationActuator.updateCurrentLocation(DEFAULT_LOCATION);
}
}
// Reschedule next step // Reschedule next step
Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0); Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0);
} }
...@@ -371,7 +378,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future ...@@ -371,7 +378,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler, Future
} catch (ComponentNotAvailableException e) { } catch (ComponentNotAvailableException e) {
e.printStackTrace(); e.printStackTrace();
} }
simLocationActuator.updateCurrentLocation(new PositionVector(Double.NaN, Double.NaN)); simLocationActuator.updateCurrentLocation(DEFAULT_LOCATION);
if (_reuseComponents) { if (_reuseComponents) {
freeComponents.add(simLocationActuator); freeComponents.add(simLocationActuator);
......
...@@ -78,10 +78,6 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi ...@@ -78,10 +78,6 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
for (List<PointInformation> similarEntries : similarCacheEntries.values()) { for (List<PointInformation> similarEntries : similarCacheEntries.values()) {
PointInformation correctInformation = _decisionStrategy.decideOnCorrectInformation(similarEntries); PointInformation correctInformation = _decisionStrategy.decideOnCorrectInformation(similarEntries);
if (correctInformation == null) {
System.out.println();
}
decidedInformation.add((T) correctInformation); decidedInformation.add((T) correctInformation);
} }
...@@ -163,4 +159,8 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi ...@@ -163,4 +159,8 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
} }
} }
public CacheDecisionStrategy getDecisionStrategy() {
return _decisionStrategy;
}
} }
...@@ -28,7 +28,7 @@ import java.util.Map; ...@@ -28,7 +28,7 @@ import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
public enum CacheDecisionStrategyType { public enum CacheDecisionStrategyType {
DEFAULT(NewestCacheDecisionStrategy.class), NEWEST(NewestCacheDecisionStrategy.class), TTL(TTLbasedCacheDecisionStrategy.class), MAJORITY(MajorityVotingCacheDecisionStrategy.class); DEFAULT(NewestCacheDecisionStrategy.class), NEWEST(NewestCacheDecisionStrategy.class), TTL(TTLbasedCacheDecisionStrategy.class), MAJORITY(MajorityVotingCacheDecisionStrategy.class), OPTIMAL(OptimalCacheDecisionStrategy.class), RANDOM(RandomCacheDecisionStrategy.class);
private final Class<? extends CacheDecisionStrategy> decisionStrategy; private final Class<? extends CacheDecisionStrategy> decisionStrategy;
private final Map<String, String> params = new HashMap<>(); private final Map<String, String> params = new HashMap<>();
......
...@@ -33,7 +33,6 @@ public class MajorityVotingCacheDecisionStrategy implements CacheDecisionStrateg ...@@ -33,7 +33,6 @@ public class MajorityVotingCacheDecisionStrategy implements CacheDecisionStrateg
@Override @Override
public <T extends PointInformation> T decideOnCorrectInformation( public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) { List<T> pSimilarPointInformation) {
Map<Object, Integer> voting = new HashMap<>(); Map<Object, Integer> voting = new HashMap<>();
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
if (!voting.containsKey(t.getValue())) { if (!voting.containsKey(t.getValue())) {
...@@ -59,7 +58,7 @@ public class MajorityVotingCacheDecisionStrategy implements CacheDecisionStrateg ...@@ -59,7 +58,7 @@ public class MajorityVotingCacheDecisionStrategy implements CacheDecisionStrateg
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate(); long timestamp = t.getDetectionDate();
if (t.getValue().equals(maxEntry.getValue()) && timestamp > maxTimestamp) { if (t.getValue().equals(maxEntry.getKey()) && timestamp > maxTimestamp) {
maxTimestamp = timestamp; maxTimestamp = timestamp;
maxFitting = t; maxFitting = t;
} }
......
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List;
import de.tud.kom.p2psim.impl.topology.movement.VehicleMovementModel;
import de.tudarmstadt.maki.simonstrator.api.Time;
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.roadnetwork.RoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
private RoadNetwork _roadNetwork;
private boolean _lastJamState = false;
private long _lastChange = 0;
public OptimalCacheDecisionStrategy() {
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
if (_roadNetwork == null) {
_roadNetwork = VehicleMovementModel.getRoadNetwork();
}
RoadNetworkEdge edge = _roadNetwork.getEdge("A5.8");
boolean jam = false;
if (edge.getMaxSpeed() < 1) {
jam = true;
}
if (jam != _lastJamState) {
_lastChange = Time.getCurrentTime();
}
long maxTimestamp = -1;
T maxFitting = null;
T maxFittingIncludingTimestamp = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (t.getValue().equals(jam) && timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
if (timestamp > _lastChange) {
maxFittingIncludingTimestamp = t;
}
}
}
if (maxFitting == null) {
maxTimestamp = -1;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
}
if (maxFittingIncludingTimestamp == null) {
maxTimestamp = -1;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (timestamp > maxTimestamp && timestamp > _lastChange) {
maxTimestamp = timestamp;
maxFittingIncludingTimestamp = t;
}
}
}
if (maxFittingIncludingTimestamp != null) {
return maxFittingIncludingTimestamp;
}
return maxFitting;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.vehicular.caching.decision;
import java.util.List;
import java.util.Random;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class RandomCacheDecisionStrategy implements CacheDecisionStrategy {
private Random _random = Randoms.getRandom(getClass());
public RandomCacheDecisionStrategy() {
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
return pSimilarPointInformation.get(_random.nextInt(pSimilarPointInformation.size()));
}
}
...@@ -33,29 +33,48 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.Poin ...@@ -33,29 +33,48 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.Poin
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND; private static final long SCALING = Time.SECOND;
private static final double ACCURACY_FACTOR = 100000;
private long ttl = 300 * Time.SECOND / SCALING; private long ttl = 300 * Time.SECOND / SCALING;
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;
public TTLbasedCacheDecisionStrategy(Map<String, String> pParams) { public TTLbasedCacheDecisionStrategy(Map<String, String> pParams) {
for (Entry<String, String> param : pParams.entrySet()) { for (Entry<String, String> param : pParams.entrySet()) {
switch (param.getKey()) { switch (param.getKey()) {
case "ACCURACY": case "ACCURACY":
accuracy = Double.valueOf(param.getValue()); accuracy = Double.valueOf(param.getValue());
break; break;
case "COST_RATIO":
double ratio = Double.valueOf(param.getValue());
costWrongChange = 2 / (ratio + 1);
costWrongKeep = 2 - costWrongChange;
break;
default: default:
break; break;
} }
} }
} }
public double getCostWrongChange() {
return costWrongChange;
}
public double getCostWrongKeep() {
return costWrongKeep;
}
@Override @Override
public <T extends PointInformation> T decideOnCorrectInformation( public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) { List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) { if (pSimilarPointInformation.size() == 1) {
return pSimilarPointInformation.get(0); T decision = pSimilarPointInformation.get(0);
_lastDecision = decision.getValue();
return decision;
} else if (pSimilarPointInformation.size() == 0) { } else if (pSimilarPointInformation.size() == 0) {
return null; return null;
} }
...@@ -91,9 +110,14 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -91,9 +110,14 @@ 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 / 10); rate = Math.min(rate, ttl / 20);
double b = determineB(rate, 1 - accuracy, ttl, costWrongKeep, costWrongChange); 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<>();
...@@ -110,7 +134,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -110,7 +134,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
weight.put(t.getValue(), sumImpact); weight.put(t.getValue(), sumImpact);
} }
double maxWeight = 0; double maxWeight = -1;
Object maxValue = null; Object maxValue = null;
for (Object key : weight.keySet()) { for (Object key : weight.keySet()) {
...@@ -120,7 +144,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -120,7 +144,7 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
} }
maxTimestamp = 0; maxTimestamp = -1;
T maxFitting = null; T maxFitting = null;
for (T t : pSimilarPointInformation) { for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate(); long timestamp = t.getDetectionDate();
...@@ -131,9 +155,11 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -131,9 +155,11 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
} }
_lastDecision = maxFitting.getValue();
return maxFitting; return maxFitting;
} else { } else {
maxTimestamp = 0; 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);
...@@ -144,6 +170,8 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -144,6 +170,8 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
} }
} }
_lastDecision = maxFitting.getValue();
return maxFitting; return maxFitting;
} }
} }
...@@ -193,28 +221,41 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy { ...@@ -193,28 +221,41 @@ public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
double leftSide; double leftSide;
double rightSide; double rightSide;
double step = 0.5; double step = 5;
if (errorProbability < 0.5) { if (errorProbability < 0.5) {
b = -1 * reversed; b = -2 * step * reversed;
} else { } else {
b = 1 * reversed; b = 2 * step * reversed;
} }
int similar = 0;
double lastDifference = -1;
do { do {
leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b); leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b);
rightSide = calculateWeightingForNewState(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)) { if (Double.isNaN(leftSide) || Double.isNaN(rightSide) || similar > 100) {
if (reversed != -1) { if (reversed != -1) {
return determineB(rate, errorProbability, ttl, costSlow, costFast, -1); double determineB = determineB(rate, errorProbability, ttl, costSlow, costFast, -1);
if (!Double.isNaN(determineB)) {
return determineB;
} else {
return b;
}
} else { } else {
return Double.NaN; return Double.NaN;
} }
} }
leftSide = Math.round(leftSide * 100000); leftSide = Math.round(leftSide * ACCURACY_FACTOR);
rightSide = Math.round(rightSide * 100000); rightSide = Math.round(rightSide * ACCURACY_FACTOR);
if (leftSide > rightSide) { if (leftSide > rightSide) {
if (b < 0) { if (b < 0) {
......
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