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

Current version

parent 44bee7a8
......@@ -57,7 +57,7 @@ SchedulerComponent, TimeComponent {
private long statusInterval = SCHEDULER_WAKEUP_INTERVAL_IN_VIRTUALTIME;
private static final int INITIAL_QUEUE_CAPACITY = 5000;
private static final int INITIAL_QUEUE_CAPACITY = 50000;
private long processedEventCounter;
......
......@@ -92,6 +92,8 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
private double _percentageOfKnownRoutes = 1;
private int _startTime;
/**
* Constructor for the movement model using the sumo TraCI API
* @param timeBetweenMoveOperations The time between two movement operations.
......@@ -117,6 +119,27 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
this.offsetY = Integer.parseInt(offsetY);
this.width = Integer.parseInt(width);
this.height = Integer.parseInt(height);
Thread current = Thread.currentThread();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
StackTraceElement[] stackTrace = current.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();
}
/**
......@@ -154,6 +177,10 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
_percentageOfKnownRoutes = pPercentageOfKnownRoutes;
}
public void setStartTime(long pStartTime) {
_startTime = (int) (pStartTime / Time.SECOND);
}
public void setReuseComponents(boolean pReuseComponents) {
_reuseComponents = pReuseComponents;
......@@ -221,6 +248,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
TraciSimulationController simulationController = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile);
_controller = simulationController;
_controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height);
simulationController.setStartTime(_startTime);
_controller.init();
_controller.nextStep();
......@@ -262,6 +290,8 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
*/
long currentTime = Time.getCurrentTime() / timestepConversion;
System.out.println("Performing movement for step " + currentTime);
while (_controller.getStep() - _controller.getStart() < currentTime) {
if (!_controller.nextStep()) {
return;
......
......@@ -75,6 +75,8 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endX;
private double _endY;
private int _startTime = 0;
private Map<String, VehicleInformationContainer> _positons = new HashMap<>();
private boolean _initalized = false;
......@@ -99,6 +101,10 @@ public class TraciSimulationController implements VehicleController, SimulationS
_configFile = pConfigFile;
}
public void setStartTime(int pStartTime) {
_startTime = pStartTime;
}
public static VehicleController getSimulationController() {
return CONTROLLER.values().iterator().next();
}
......@@ -127,6 +133,15 @@ public class TraciSimulationController implements VehicleController, SimulationS
Simulator.getInstance().addObserver(this);
_initalized = true;
for (int i = 0; i < _startTime; i++) {
System.out.println("Pre-Start Setup: " + i + " of " + _startTime + " steps done.");
try {
_connection.do_timestep();
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
}
......
......@@ -24,15 +24,18 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import de.tud.kom.p2psim.impl.vehicular.caching.decision.NewestCacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Time;
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.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.transition.TransitionEngine;
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.caching.CacheStateListener;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheSizeAwareCacheDecisionStrategy;
......@@ -43,6 +46,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;
......@@ -58,14 +62,17 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
private CacheReplacementStrategy _replacementStrategy;
private CacheDecisionStrategy _decisionStrategy;
CacheDecisionStrategy _defaultDecisionStrategy;
Map<Class<? extends Object>, CacheDecisionStrategy> _decisionStrategies = new HashMap<>();
private int _minObservations = 0;
private int _maxCacheSizePerEntry = Integer.MAX_VALUE;
private int _maxCacheSize = Integer.MAX_VALUE;
private double[] informationRatios = new double[] {1, 0.75, 0.5, 0.25, 0};
private List<CacheStateListener> _cacheStateListeners = new ArrayList<>();
public DefaultCachingComponent(Host pHost,
CacheInvalidationStrategy pInvalidationStrategy,
CacheReplacementStrategy pReplacementStrategy,
......@@ -79,7 +86,7 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
_invalidationStrategy = pInvalidationStrategy;
_replacementStrategy = pReplacementStrategy;
_decisionStrategy = pDecisionStrategy;
_defaultDecisionStrategy = pDecisionStrategy;
}
private TransitionEngine getTransitionEngine() throws AssertionError {
......@@ -90,9 +97,17 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
}
@Override
public void registerCacheStateListener(CacheStateListener pListener) {
_cacheStateListeners.add(pListener);
}
@Override
public <T extends PointInformation> List<T> getDecidedCacheEntries(
Class<T> pCacheEntryClass) {
CacheDecisionStrategy decisionStrategy = getCacheDecisionStrategy(pCacheEntryClass);
Set<RoadNetworkEdge> allEverActiveEdges = RoadNetwork.CURRENT_ROAD_NETWORK.getAllEverActiveEdges();
List<T> cacheEntries = getCacheEntries(pCacheEntryClass);
if (cacheEntries == null) {
......@@ -104,6 +119,7 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
for (T t : cacheEntries) {
Object position = getEdgeOrPosition(t);
if (!(position instanceof RoadNetworkEdge) || allEverActiveEdges.contains((position))) {
if (!similarCacheEntries.containsKey(position)) {
similarCacheEntries.put(position, new HashMap<>());
}
......@@ -113,12 +129,14 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
similarCacheEntries.get(position).get(t.getValue().getClass()).add(t);
}
}
List<T> decidedInformation = new ArrayList<>();
for (Map<Class<? extends Object>, List<PointInformation>> similarEdges : similarCacheEntries
.values()) {
for (List<PointInformation> similarInformation : similarEdges.values()) {
if (similarInformation.size() >= _minObservations) {
PointInformation correctInformation = _decisionStrategy
PointInformation correctInformation = decisionStrategy
.decideOnCorrectInformation(similarInformation);
decidedInformation.add((T) correctInformation);
......@@ -138,6 +156,13 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
@Override
public <T extends PointInformation> List<T> getDecidedCacheEntries(
Class<T> pCacheEntryClass, Class<?> pCacheValueClass, RoadNetworkEdge pEdge) {
CacheDecisionStrategy decisionStrategy = getCacheDecisionStrategy(pCacheEntryClass);
Set<RoadNetworkEdge> allEverActiveEdges = RoadNetwork.CURRENT_ROAD_NETWORK.getAllEverActiveEdges();
if (!allEverActiveEdges.contains(pEdge)) {
return null;
}
List<T> cacheEntries = getCacheEntries(pCacheEntryClass);
if (cacheEntries == null) {
......@@ -149,8 +174,6 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
for (T t : cacheEntries) {
Object position = getEdgeOrPosition(t);
// System.out.println(t.getDetectionDate() + " --> " + ((AggregatedInformation)t).getAggregationInformation().getMinTimestamp());
if (position.equals(pEdge) && (pCacheValueClass == null || t.getValue().getClass().equals(pCacheValueClass))) {
if (!similarCacheEntries.containsKey(t.getValue().getClass())) {
similarCacheEntries.put(t.getValue().getClass(), new ArrayList<PointInformation>());
......@@ -164,7 +187,7 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
for (List<PointInformation> similarInformation : similarCacheEntries.values()) {
if (similarInformation.size() >= _minObservations) {
PointInformation correctInformation = _decisionStrategy
PointInformation correctInformation = decisionStrategy
.decideOnCorrectInformation(similarInformation);
decidedInformation.add((T) correctInformation);
......@@ -174,6 +197,21 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
return decidedInformation;
}
private <T extends PointInformation> CacheDecisionStrategy getCacheDecisionStrategy(Class<T> pCacheEntryClass)
throws AssertionError {
if (!_decisionStrategies.containsKey(pCacheEntryClass)) {
CacheDecisionStrategy clone = _defaultDecisionStrategy.clone();
TransitionEngine tEngine = getTransitionEngine();
clone = tEngine.createMechanismProxy(CacheDecisionStrategy.class, clone, DECISION_STRATEGY + pCacheEntryClass.getSimpleName());
_decisionStrategies.put(pCacheEntryClass, clone);
}
CacheDecisionStrategy decisionStrategy = _decisionStrategies.get(pCacheEntryClass);
return decisionStrategy;
}
public void setMinObservations(int pMinObservations) {
this._minObservations = pMinObservations;
}
......@@ -217,29 +255,78 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
if (pCacheEntry instanceof AggregatedInformation && ((AggregatedInformation) pCacheEntry).isAggregated()) {
List<PointInformation> toBeRemoved = new ArrayList<>();
for (PointInformation pointInformation : entries) {
if (_replacementStrategy.replaceInformation(pointInformation, pCacheEntry)) {
toBeRemoved.add(pointInformation);
}
}
for (PointInformation pointInformation : toBeRemoved) {
entries.remove(pointInformation);
}
while (entries.size() > _maxCacheSizePerEntry - 1) {
entries.remove(0);
}
shrinkCache(_maxCacheSize - 1);
entries.add(pCacheEntry);
for (CacheStateListener cacheStateListener : _cacheStateListeners) {
cacheStateListener.entryStored(pCacheEntry);
}
}
private void shrinkCache(int maxSize) {
while (maxSize < getTotalCacheSize()) {
int maxSizeValue = 0;
List<PointInformation> removedValues = null;
List<PointInformation> removedList = null;
for (List<PointInformation> informationList : _cache.values()) {
Map<Object, List<PointInformation>> perPosition = new HashMap<>();
for (PointInformation pointInformation : informationList) {
Object position = getEdgeOrPosition(pointInformation);
if (!perPosition.containsKey(position)) {
perPosition.put(position, new ArrayList<>());
}
perPosition.get(position).add(pointInformation);
}
for (Entry<Object, List<PointInformation>> entry : perPosition.entrySet()) {
if (maxSizeValue < entry.getValue().size()) {
maxSizeValue = entry.getValue().size();
removedValues = entry.getValue();
removedList = informationList;
}
}
}
removedList.removeAll(removedValues);
CacheDecisionStrategy decisionStrategy = getCacheDecisionStrategy(removedValues.get(0).getClass());
PointInformation correctInformation = decisionStrategy
.decideOnCorrectInformation(removedValues);
for (CacheStateListener cacheStateListener : _cacheStateListeners) {
cacheStateListener.entriesRemoved(removedValues, correctInformation);
}
}
}
private int getTotalCacheSize() {
int sum = 0;
for (List<PointInformation> informationList : _cache.values()) {
sum += informationList.size();
}
return sum;
}
@Override
public void initialize() {
_cache.clear();
TransitionEngine tEngine = getTransitionEngine();
_decisionStrategy = tEngine.createMechanismProxy(CacheDecisionStrategy.class, _decisionStrategy, DECISION_STRATEGY);
}
@Override
......@@ -262,14 +349,20 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
@Override
public void performDecisionTransition(Class<? extends CacheDecisionStrategy> pCacheDecisionStrategy) {
getTransitionEngine().executeAtomicTransition(DECISION_STRATEGY, pCacheDecisionStrategy);
public void performDecisionTransition(Class<? extends Object> pInformationClass, Class<? extends CacheDecisionStrategy> pCacheDecisionStrategy) {
getTransitionEngine().executeAtomicTransition(DECISION_STRATEGY + pInformationClass.getSimpleName(), pCacheDecisionStrategy);
}
@Override
public void adjustCacheSizePerEntry(int pMaxCacheSizePerEntry) {
getTransitionEngine().alterLocalState(DECISION_STRATEGY, CacheSizeAwareCacheDecisionStrategy.class, "CacheSize", pMaxCacheSizePerEntry);
_maxCacheSizePerEntry = pMaxCacheSizePerEntry;
public void setMaxSize(int pMaxCacheSize) {
_maxCacheSize = pMaxCacheSize;
shrinkCache(_maxCacheSize);
}
@Override
public void adjustCacheSizePerEntry(Class<? extends Object> pInformationClass, int pMaxCacheSizePerEntry) {
getTransitionEngine().alterLocalState(DECISION_STRATEGY + pInformationClass.getSimpleName(), CacheSizeAwareCacheDecisionStrategy.class, "CacheSize", pMaxCacheSizePerEntry);
}
@Override
......@@ -307,10 +400,6 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
}
public CacheDecisionStrategy getDecisionStrategy() {
return _decisionStrategy;
}
@Override
public String getNodeDescription() {
return " " + getHost().getId();
......
......@@ -84,4 +84,9 @@ public class AveragingCacheDecisionStrategy extends AbstractCacheDecisionStrateg
public Map<CacheDecisionStrategyParameters, String> getParams() {
return _params;
}
@Override
public AveragingCacheDecisionStrategy clone() {
return new AveragingCacheDecisionStrategy(_params);
}
}
......@@ -83,4 +83,9 @@ public class MajorityVotingCacheDecisionStrategy extends AbstractCacheDecisionSt
return _params;
}
@Override
public MajorityVotingCacheDecisionStrategy clone() {
return new MajorityVotingCacheDecisionStrategy(_params);
}
}
......@@ -103,4 +103,9 @@ public class MajorityVotingVectoralCacheDecisionStrategy extends AbstractCacheDe
return _params;
}
@Override
public MajorityVotingVectoralCacheDecisionStrategy clone() {
return new MajorityVotingVectoralCacheDecisionStrategy(_params);
}
}
......@@ -56,4 +56,9 @@ public class NewestCacheDecisionStrategy extends AbstractCacheDecisionStrategy i
return _params;
}
@Override
public NewestCacheDecisionStrategy clone() {
return new NewestCacheDecisionStrategy(_params);
}
}
......@@ -107,4 +107,9 @@ public class OptimalCacheDecisionStrategy extends AbstractCacheDecisionStrategy
return _params;
}
@Override
public OptimalCacheDecisionStrategy clone() {
return new OptimalCacheDecisionStrategy(_params);
}
}
......@@ -58,4 +58,9 @@ public class RandomCacheDecisionStrategy extends AbstractCacheDecisionStrategy i
return _params;
}
@Override
public RandomCacheDecisionStrategy clone() {
return new RandomCacheDecisionStrategy(_params);
}
}
......@@ -200,4 +200,9 @@ public class TTLbasedCacheDecisionStrategy extends AbstractCacheDecisionStrategy
public void setCacheSize(int pCacheSize) {
_maxCacheSize = pCacheSize;
}
@Override
public TTLbasedCacheDecisionStrategy clone() {
return new TTLbasedCacheDecisionStrategy(_params);
}
}
......@@ -28,6 +28,7 @@ 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.RoadProperty;
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.transition.MechanismState;
......@@ -51,7 +52,6 @@ public class TTLbasedVectoralCacheDecisionStrategy extends AbstractCacheDecision
private double costWrongKeep = 1;
private double costWrongChange = 1;
@MechanismState(value = "CacheSize")
private int cacheSize = Integer.MAX_VALUE;
private Object _lastDecision = null;
......@@ -114,6 +114,7 @@ public class TTLbasedVectoralCacheDecisionStrategy extends AbstractCacheDecision
}
boolean differentValue = false;
if (differentValue) {
List<Integer> possibleValues = new ArrayList<>();
for (T t : pSimilarPointInformation) {
......@@ -179,6 +180,18 @@ public class TTLbasedVectoralCacheDecisionStrategy extends AbstractCacheDecision
_lastDecision = roadInformation.getValue();
return (T) roadInformation;
} else {
RoadProperty currentProperty = (RoadProperty) pSimilarPointInformation.get(pSimilarPointInformation.size() - 1).getValue();
RoadInformation roadInformation = new RoadInformation(currentProperty );
copyAttributes((RoadInformation) pSimilarPointInformation.get(pSimilarPointInformation.size() - 1), roadInformation);
addAggregationInformation(pSimilarPointInformation, roadInformation);
_lastDecision = roadInformation.getValue();
return (T) roadInformation;
}
}
/**
......@@ -216,4 +229,9 @@ public class TTLbasedVectoralCacheDecisionStrategy extends AbstractCacheDecision
cacheSize = pCacheSize;
}
@Override
public TTLbasedVectoralCacheDecisionStrategy clone() {
return new TTLbasedVectoralCacheDecisionStrategy(_params);
}
}
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