Commit 323dda39 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

First really working version for ad-hoc

parent 904c0cf9
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 24.04.2018
*
*/
public class JamInformationContainer {
public static long EVENT_DURATION = 0;
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 23.04.2018
*
*/
public enum ProbabilityDistribution {
SIMPLE, GAUSSIAN
}
......@@ -32,4 +32,6 @@ public interface RoadProperty extends LocationBasedEnvironmentProperty {
RoadNetworkEdge getEdge();
EnvironmentProperty getDefaultProperty();
long getDetectionDate();
}
......@@ -22,7 +22,10 @@
package de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.vector.TemporalDependencyMatrix;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
......@@ -34,16 +37,67 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road
*/
public class VectoralJamProperty extends NumericVectoralProperty {
public static final double SCALING = 6;
private static final int DIMENSIONS = 15;
private static final int DIMENSIONS = 10;
private static final double TEMPORAL_CHANGE = 0.05;
private static Map<Double, Double> _requiredStandardDeviation = new HashMap<>();
private static TemporalDependencyMatrix _temporalDependencyMatrix;
public VectoralJamProperty(Location pLocation, RoadNetworkEdge pEdge) {
super(pLocation, pEdge, DIMENSIONS);
}
public void setSpeed(double pSpeed, double pDeviation) {
setProbabilities(pSpeed / SCALING, pDeviation / SCALING);
/**
*
*/
public VectoralJamProperty() {
this(null, null);
}
public void setSpeed(double pSpeed) {
double[] valueProbabilities = getValueProbabilities();
Arrays.fill(valueProbabilities, 0);
valueProbabilities[(int) (pSpeed / SCALING)] = 1;
}
public void setSpeed(double pSpeed, double pAccuracy, ProbabilityDistribution pDist) {
switch (pDist) {
case SIMPLE:
setSimpleSpeed(pSpeed, pAccuracy);
break;
case GAUSSIAN:
setGaussianSpeedWithAccuracy(pSpeed, pAccuracy);
break;
default:
throw new AssertionError("Unknown ProbablilityDistribution " + pDist);
}
}
private void setSimpleSpeed(double pSpeed, double pAccuracy) {
double[] valueProbabilities = getValueProbabilities();
Arrays.fill(valueProbabilities, 0);
int index = (int) (pSpeed / SCALING);
valueProbabilities[index] += pAccuracy;
if (index + 1 < valueProbabilities.length) {
valueProbabilities[index + 1] += (1 - pAccuracy) / 2.0;
} else {
valueProbabilities[index - 1] += (1 - pAccuracy) / 2.0;
}
if (index - 1 >= 1) {
valueProbabilities[index - 1] += (1 - pAccuracy) / 2.0;
} else {
valueProbabilities[index + 1] += (1 - pAccuracy) / 2.0;
}
}
public void setGaussianSpeedWithAccuracy(double pSpeed, double pAccuracy) {
double deviation = determineCorrectDeviation(pSpeed, pAccuracy);
setGaussianProbabilities(pSpeed / SCALING, deviation / SCALING);
}
public void setGaussianSpeed(double pSpeed, double pDeviation) {
setGaussianProbabilities(pSpeed / SCALING, pDeviation / SCALING);
}
@Override
......@@ -54,6 +108,11 @@ public class VectoralJamProperty extends NumericVectoralProperty {
return vectoralJamProperty;
}
@Override
public VectoralJamProperty age(long pAge) {
return (VectoralJamProperty) super.age(pAge);
}
@Override
public EnvironmentProperty getDefaultProperty() {
return new VectoralJamProperty(getLocation(), getEdge());
......@@ -66,16 +125,28 @@ public class VectoralJamProperty extends NumericVectoralProperty {
@Override
public TemporalDependencyMatrix getDependencyMatrix() {
if (_temporalDependencyMatrix == null) {
TemporalDependencyMatrix temporalDependencyMatrix = new TemporalDependencyMatrix(DIMENSIONS);
double TEMPORAL_CHANGE = 1 / ((double) JamInformationContainer.EVENT_DURATION / Time.SECOND) / 2.0;
for (int x = 0; x < DIMENSIONS; x++) {
double[] probabilities = new double[DIMENSIONS];
probabilities[Math.max(0, x - 1)] += TEMPORAL_CHANGE;
probabilities[Math.min(DIMENSIONS - 1, x + 1)] += TEMPORAL_CHANGE;
if (x > 1) {
probabilities[x - 1] += TEMPORAL_CHANGE;
} else {
probabilities[x + 1] += TEMPORAL_CHANGE;
}
if (x < DIMENSIONS - 1) {
probabilities[x + 1] += TEMPORAL_CHANGE;
} else {
probabilities[x - 1] += TEMPORAL_CHANGE;
}
probabilities[x] += (1 - 2 * TEMPORAL_CHANGE);
temporalDependencyMatrix.setTransitionProbability(x, probabilities);
}
return temporalDependencyMatrix;
_temporalDependencyMatrix = temporalDependencyMatrix;
}
return _temporalDependencyMatrix;
}
public double getProbabilityForValue(Object pValue) {
......@@ -123,4 +194,38 @@ public class VectoralJamProperty extends NumericVectoralProperty {
}
return super.equals(pObj);
}
public double determineCorrectDeviation(double speed, double pAccuracy) {
if (!_requiredStandardDeviation.containsKey(speed)) {
if (pAccuracy < 1) {
VectoralJamProperty vectoralJamProperty = new VectoralJamProperty(null, null);
double deviation = 10;
boolean reduceChange = false;
double currentChange = deviation / 2;
do {
vectoralJamProperty.setGaussianSpeed(speed, deviation);
if (vectoralJamProperty.getProbabilityForValue(speed) > pAccuracy) {
deviation += currentChange;
} else {
deviation -= currentChange;
reduceChange = true;
}
if (reduceChange) {
currentChange /= 2.0;
}
} while (Math.round(vectoralJamProperty.getProbabilityForValue(speed) * 10000) != Math
.round(pAccuracy * 10000));
_requiredStandardDeviation.put(speed, deviation);
} else {
_requiredStandardDeviation.put(speed, 0d);
}
}
return _requiredStandardDeviation.get(speed);
}
}
\ No newline at end of file
......@@ -59,7 +59,7 @@ public abstract class VectoralProperty implements RoadProperty, Cloneable {
_valueProbabilities = pValueProbabilities;
}
protected void setProbabilities(double pMean, double pDeviation) {
protected void setGaussianProbabilities(double pMean, double pDeviation) {
if (pDeviation > 0) {
double sum = 0;
for (int i = 0; i < _valueProbabilities.length; i++) {
......@@ -112,7 +112,7 @@ public abstract class VectoralProperty implements RoadProperty, Cloneable {
public abstract Object getValueAtIndex(int pIndex);
public Object getMostProbableValue() {
public int getMostProbableIndex() {
double max = -1;
int index = 0;
......@@ -124,7 +124,11 @@ public abstract class VectoralProperty implements RoadProperty, Cloneable {
}
}
return getValueAtIndex(index);
return index;
}
public Object getMostProbableValue() {
return getValueAtIndex(getMostProbableIndex());
}
public abstract TemporalDependencyMatrix getDependencyMatrix();
......@@ -170,6 +174,10 @@ public abstract class VectoralProperty implements RoadProperty, Cloneable {
return result;
}
public double getProbabilityForIndex(int pIndex) {
return getValueProbabilities()[pIndex];
}
public abstract double getProbabilityForValue(Object pValue);
public VectoralProperty combine(VectoralProperty pVectoralProperty) {
......
......@@ -22,6 +22,8 @@
package de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.vector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
......@@ -32,6 +34,8 @@ public class TemporalDependencyMatrix implements Cloneable {
// Value x | y --> Transition probability from state x to state y
private double[][] _dependencies;
private Map<Long, TemporalDependencyMatrix> _agedMatrices = new HashMap<>();
public TemporalDependencyMatrix(int dimension) {
_dependencies = new double[dimension][dimension];
}
......@@ -51,7 +55,13 @@ public class TemporalDependencyMatrix implements Cloneable {
}
public TemporalDependencyMatrix age(long pAge) {
double[][] current = cloneArray(_dependencies);
if (!_agedMatrices.containsKey(pAge)) {
double[][] current = new double[_dependencies.length][];
for (int i = 0; i < current.length; i++) {
current[i] = new double[_dependencies[i].length];
current[i][i] = 1;
}
for (long t = 0; t < pAge; t++) {
double[][] next = createEmptyArray(current);
......@@ -67,7 +77,9 @@ public class TemporalDependencyMatrix implements Cloneable {
current = next;
}
return new TemporalDependencyMatrix(current);
_agedMatrices.put(pAge, new TemporalDependencyMatrix(current));
}
return _agedMatrices.get(pAge);
}
public double[][] cloneArray(double[][] pDependencies) {
......@@ -104,4 +116,12 @@ public class TemporalDependencyMatrix implements Cloneable {
return new TemporalDependencyMatrix(dependencies);
}
/**
* @param pI
* @return
*/
public double getChangeProbability(int pI) {
return 1 - _dependencies[pI][pI];
}
}
......@@ -22,9 +22,7 @@
package de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.plugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import de.tudarmstadt.maki.simonstrator.api.Host;
......@@ -32,6 +30,7 @@ import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.EnvironmentProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.ProbabilityDistribution;
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.location.Location;
......@@ -54,13 +53,29 @@ public class VectoralJamEnvironmentSensorPlugin implements EnvironmentSensorPlug
private double _accuracy;
private Map<Double, Double> _requiredStandardDeviation = new HashMap<>();
private static Random _random = Randoms.getRandom(VectoralJamEnvironmentSensorPlugin.class);
private static Random _accuracyRandom = Randoms.getRandom(VectoralJamEnvironmentSensorPlugin.class + "Accuracy");
private ProbabilityDistribution _probabilityDistribution = ProbabilityDistribution.SIMPLE;
@XMLConfigurableConstructor({ "accuracy" })
public VectoralJamEnvironmentSensorPlugin(double pAccuracy) {
setAccuracy(pAccuracy);
double deviation = 0.2 * pAccuracy;
if (deviation + pAccuracy > 1) {
deviation = 1 - pAccuracy;
}
if (pAccuracy - deviation < 0.6) {
deviation = pAccuracy - 0.6;
}
double accuracy = pAccuracy + _accuracyRandom.nextGaussian() * deviation;
if (accuracy < pAccuracy - deviation) {
accuracy = pAccuracy - deviation;
}
if (accuracy > pAccuracy + deviation) {
accuracy = pAccuracy + deviation;
}
setAccuracy(accuracy);
}
@Override
......@@ -95,17 +110,10 @@ public class VectoralJamEnvironmentSensorPlugin implements EnvironmentSensorPlug
speed = (int) (edge.getOriginalMaxSpeed() / VectoralJamProperty.SCALING) * VectoralJamProperty.SCALING;
}
if (!_requiredStandardDeviation.containsKey(speed)) {
determineCorrectDeviation(speed);
}
double measuredSpeed = measureValue(speed, _requiredStandardDeviation.get(speed));
double measuredSpeed = measureValue(speed);
VectoralJamProperty vectoralJamProperty = new VectoralJamProperty(location, edge);
if (!_requiredStandardDeviation.containsKey(measuredSpeed)) {
determineCorrectDeviation(measuredSpeed);
}
vectoralJamProperty.setSpeed(measuredSpeed, _requiredStandardDeviation.get(measuredSpeed));
vectoralJamProperty.setSpeed(measuredSpeed, _accuracy, _probabilityDistribution);
List<EnvironmentProperty> properties = new ArrayList<>();
EnvironmentProperty property = vectoralJamProperty;
......@@ -116,9 +124,9 @@ public class VectoralJamEnvironmentSensorPlugin implements EnvironmentSensorPlug
}
}
public double measureValue(double pRealSpeed, double pDeviation) {
public double measureValue(double pRealSpeed) {
VectoralJamProperty vectoralJamProperty = new VectoralJamProperty(null, null);
vectoralJamProperty.setSpeed(pRealSpeed, pDeviation);
vectoralJamProperty.setSpeed(pRealSpeed, _accuracy, _probabilityDistribution);
double nextDouble = _random.nextDouble();
double currentAggregate = 0;
......@@ -133,36 +141,6 @@ public class VectoralJamEnvironmentSensorPlugin implements EnvironmentSensorPlug
throw new AssertionError("Unexpected!");
}
public void determineCorrectDeviation(double speed) {
if (_accuracy < 1) {
VectoralJamProperty vectoralJamProperty = new VectoralJamProperty(null, null);
double deviation = 10;
boolean reduceChange = false;
double currentChange = deviation / 2;
do {
vectoralJamProperty.setSpeed(speed, deviation);
if (vectoralJamProperty.getProbabilityForValue(speed) > _accuracy) {
deviation += currentChange;
} else {
deviation -= currentChange;
reduceChange = true;
}
if (reduceChange) {
currentChange /= 2.0;
}
} while (Math.round(vectoralJamProperty.getProbabilityForValue(speed) * 10000) != Math
.round(_accuracy * 10000));
_requiredStandardDeviation.put(speed, deviation);
} else {
_requiredStandardDeviation.put(speed, 0d);
}
}
@Override
public VectoralJamEnvironmentSensorPlugin clone() throws CloneNotSupportedException {
return new VectoralJamEnvironmentSensorPlugin(_accuracy);
......
......@@ -41,4 +41,12 @@ public class RoadInformation extends EnvironmentInformation<RoadProperty>
return getValue().getEdge();
}
@Override
public <T> T getAttribute(AvailableInformationAttributes pKey) {
if (AvailableInformationAttributes.EDGE.equals(pKey)) {
return (T) getEdge();
}
return super.getAttribute(pKey);
}
}
......@@ -26,6 +26,7 @@ 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.RoadProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralJamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController;
......@@ -80,6 +81,37 @@ public class RoadNetworkEdge {
}
}
public double getCurrentSpeedState() {
double actualSpeed = -1;
JamProperty jamProperty = getJamProperty();
if (jamProperty != null) {
actualSpeed = jamProperty.getAverageSpeed();
} else {
actualSpeed = getCorrespondingState(getOriginalMaxSpeed());
}
return actualSpeed;
}
public JamProperty getJamProperty() {
List<RoadProperty> activeProperties = getActiveProperties();
for (RoadProperty roadProperty : activeProperties) {
if (roadProperty instanceof JamProperty) {
return (JamProperty) roadProperty;
}
}
return null;
}
/**
* @param pExpectation
* @return
*/
public static double getCorrespondingState(double pExpectation) {
return ((int) (pExpectation / VectoralJamProperty.SCALING)) * VectoralJamProperty.SCALING;
}
public void removeRoadProperty(RoadProperty pProperty) {
_activeProperties.remove(pProperty);
......
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