The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

Commit 752f6a9b authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Implement PedOSMStrideLengthProc and add tests

parent a4991ac2
package org.vadere.simulator.models.osm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizer;
import org.vadere.simulator.models.osm.stairOptimization.StairStepOptimizer;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeEventDriven;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeParallel;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeSequential;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.PotentialFieldTargetRingExperiment;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
......@@ -29,23 +23,25 @@ import org.vadere.util.geometry.Vector2D;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class PedestrianOSM extends Pedestrian {
private final AttributesOSM attributesOSM;
private final transient StepCircleOptimizer stepCircleOptimizer;
private final transient UpdateSchemeOSM updateScheme;
private transient IPotentialFieldTarget potentialFieldTarget;
private transient PotentialFieldObstacle potentialFieldObstacle;
private transient PotentialFieldAgent potentialFieldPedestrian;
private final transient Topography topography;
private final double stepLength;
private final double stepDeviation;
private List<SpeedAdjuster> speedAdjusters;
private final double minStepLength;
private transient IPotentialFieldTarget potentialFieldTarget;
private transient PotentialFieldObstacle potentialFieldObstacle;
private transient PotentialFieldAgent potentialFieldPedestrian;
private List<SpeedAdjuster> speedAdjusters;
private double durationNextStep;
private VPoint nextPosition;
private VPoint lastPosition;
......@@ -60,17 +56,17 @@ public class PedestrianOSM extends Pedestrian {
// calculated by (current position - last position)/(period of time).
private double speedByAbsoluteDistance;
private List<Double>[] strides;
private LinkedList<Double>[] strides; // strides[0] = length strides[1] = time
private StairStepOptimizer stairStepOptimizer;
@SuppressWarnings("unchecked")
PedestrianOSM(AttributesOSM attributesOSM,
AttributesAgent attributesPedestrian, Topography topography,
Random random, IPotentialFieldTarget potentialFieldTarget,
PotentialFieldObstacle potentialFieldObstacle,
PotentialFieldAgent potentialFieldPedestrian,
List<SpeedAdjuster> speedAdjusters,
StepCircleOptimizer stepCircleOptimizer) {
AttributesAgent attributesPedestrian, Topography topography,
Random random, IPotentialFieldTarget potentialFieldTarget,
PotentialFieldObstacle potentialFieldObstacle,
PotentialFieldAgent potentialFieldPedestrian,
List<SpeedAdjuster> speedAdjusters,
StepCircleOptimizer stepCircleOptimizer) {
super(attributesPedestrian, random);
......@@ -98,9 +94,9 @@ public class PedestrianOSM extends Pedestrian {
this.minStepLength = 0;
}
this.strides = (ArrayList<Double>[]) (new ArrayList<?>[2]);
this.strides[0] = new ArrayList<>();
this.strides[1] = new ArrayList<>();
this.strides = (LinkedList<Double>[]) (new LinkedList<?>[2]);
this.strides[0] = new LinkedList<>();
this.strides[1] = new LinkedList<>();
}
private static UpdateSchemeOSM createUpdateScheme(UpdateType updateType, PedestrianOSM pedestrian) {
......@@ -198,7 +194,7 @@ public class PedestrianOSM extends Pedestrian {
if (attributesOSM.isDynamicStepLength()) {
return attributesOSM.getStepLengthIntercept()
+ attributesOSM.getStepLengthSlopeSpeed()
* getDesiredSpeed()
* getDesiredSpeed()
+ stepDeviation;
} else {
return stepLength;
......@@ -259,55 +255,55 @@ public class PedestrianOSM extends Pedestrian {
return timeOfNextStep;
}
public VPoint getNextPosition() {
return nextPosition;
public void setTimeOfNextStep(double timeOfNextStep) {
this.timeOfNextStep = timeOfNextStep;
}
public VPoint getLastPosition() {
return lastPosition;
public VPoint getNextPosition() {
return nextPosition;
}
public double getTimeCredit() {
return timeCredit;
public void setNextPosition(VPoint nextPosition) {
this.nextPosition = nextPosition;
}
public Collection<? extends Agent> getRelevantPedestrians() {
return relevantPedestrians;
public VPoint getLastPosition() {
return lastPosition;
}
public double getDurationNextStep() {
return durationNextStep;
public void setLastPosition(VPoint lastPosition) {
this.lastPosition = lastPosition;
}
public AttributesOSM getAttributesOSM() {
return attributesOSM;
public double getTimeCredit() {
return timeCredit;
}
public List<Double>[] getStrides() {
return strides;
public void setTimeCredit(double timeCredit) {
this.timeCredit = timeCredit;
}
// Setters...
public void setNextPosition(VPoint nextPosition) {
this.nextPosition = nextPosition;
public Collection<? extends Agent> getRelevantPedestrians() {
return relevantPedestrians;
}
public void setLastPosition(VPoint lastPosition) {
this.lastPosition = lastPosition;
public double getDurationNextStep() {
return durationNextStep;
}
public void setTimeCredit(double timeCredit) {
this.timeCredit = timeCredit;
public void setDurationNextStep(double durationNextStep) {
this.durationNextStep = durationNextStep;
}
public void setTimeOfNextStep(double timeOfNextStep) {
this.timeOfNextStep = timeOfNextStep;
public AttributesOSM getAttributesOSM() {
return attributesOSM;
}
public void setDurationNextStep(double durationNextStep) {
this.durationNextStep = durationNextStep;
public LinkedList<Double>[] getStrides() {
return strides;
}
public Topography getTopography() {
......@@ -317,7 +313,7 @@ public class PedestrianOSM extends Pedestrian {
public double getMinStepLength() {
return minStepLength;
}
@Override
public PedestrianOSM clone() {
throw new RuntimeException("clone is not supported for PedestrianOSM; it seems hard to implement.");
......
......@@ -3,68 +3,52 @@ package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdKey;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Mario Teixeira Parente
*
*/
public class PedestrianOSMStrideLengthProcessor extends DataProcessor<TimestepPedestrianIdKey, Double> {
private OptimalStepsModel osm;
public PedestrianOSMStrideLengthProcessor() {
super("strideLength");
this.osm = null;
}
@Override
protected void doUpdate(final SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
// TODO: if osm != null then compute stridelength
peds.forEach(ped -> this.putValue(new TimestepPedestrianIdKey(state.getStep(), ped.getId()), this.osm == null ? Double.NaN : 0.0));
// TODO Use this comment from the old implementation for this implementation
// @Override
// public Map<String, Table> getOutputTables() {
// outputTables.clear();
//
// List<PedestrianOSM> pedestrians = ListUtils.select(
// topography.getElements(Pedestrian.class), PedestrianOSM.class);
// for (PedestrianOSM pedestrian : pedestrians) {
//
// List<Double>[] pedStrides = pedestrian.getStrides();
// if (pedStrides.length > 0 && !pedStrides[0].isEmpty()) {
//
// Table strides = new Table("strideLength", "strideTime");
//
// for (int i = 0; i < pedStrides[0].size(); i++) {
// strides.addRow();
// strides.addColumnEntry("strideLength", pedStrides[0].get(i));
// strides.addColumnEntry("strideTime", pedStrides[1].get(i));
// }
//
// outputTables.put(String.valueOf(pedestrian.getId()), strides);
// }
//
// pedestrian.clearStrides();
//
// }
//
// return outputTables;
// }
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
Model model = manager.getMainModel();
if (model instanceof OptimalStepsModel)
this.osm = (OptimalStepsModel) model;
}
private OptimalStepsModel osm;
public PedestrianOSMStrideLengthProcessor() {
super("strideLength");
this.osm = null;
}
@Override
protected void doUpdate(final SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
if (osm != null) {
List<PedestrianOSM> osmPeds = peds.stream().map(p -> ((PedestrianOSM) p)).collect(Collectors.toList());
osmPeds.forEach(ped -> {
LinkedList<Double> strideLengths = ped.getStrides()[0];
double length = strideLengths.isEmpty() ? 0.0 : strideLengths.getLast();
this.putValue(new TimestepPedestrianIdKey(state.getStep(), ped.getId()), length);
});
} else {
peds.forEach(ped -> this.putValue(new TimestepPedestrianIdKey(state.getStep(), ped.getId()), Double.NaN));
}
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
Model model = manager.getMainModel();
if (model instanceof OptimalStepsModel)
this.osm = (OptimalStepsModel) model;
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.junit.Before;
import org.junit.Test;
public class PedestrianOSMStrideLengthProcessorTest extends ProcessorTest {
@Before
public void setup() {
processorTestEnv = new PedestrianOSMStrideLengthProcessorTestEnv();
super.setup();
}
@Test
public void doUpdate() throws Exception {
super.doUpdate();
}
@Test
public void init() throws Exception {
super.doUpdate();
}
}
\ No newline at end of file
package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.simulator.models.sfm.SocialForceModel;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdKey;
import org.vadere.simulator.projects.dataprocessing.writer.VadereWriterFactory;
import org.vadere.state.scenario.Pedestrian;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PedestrianOSMStrideLengthProcessorTestEnv extends ProcessorTestEnv<TimestepPedestrianIdKey, Double> {
PedestrianOSMStrideLengthProcessorTestEnv() {
testedProcessor = processorFactory.createDataProcessor(PedestrianOSMStrideLengthProcessor.class);
testedProcessor.setId(nextProcessorId());
outputFile = outputFileFactory.createDefaultOutputfileByDataKey(
TimestepPedestrianIdKey.class,
testedProcessor.getId()
);
outputFile.setVadereWriterFactory(VadereWriterFactory.getStringWriterFactory());
}
public void loadWrongModel() {
MainModel model = mock(SocialForceModel.class, Mockito.RETURNS_DEEP_STUBS);
when(manager.getMainModel()).thenReturn(model);
clearStates();
}
@Override
public void loadDefaultSimulationStateMocks() {
MainModel model = mock(OptimalStepsModel.class, Mockito.RETURNS_DEEP_STUBS);
when(manager.getMainModel()).thenReturn(model);
addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
List<Pedestrian> peds = new LinkedList<>();
peds.add(getPedMock(1, 0.456));
peds.add(getPedMock(2, 0.321));
peds.add(getPedMock(3, 0.765));
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(peds);
int step = state.getStep();
addToExpectedOutput(new TimestepPedestrianIdKey(step, 1), 0.456);
addToExpectedOutput(new TimestepPedestrianIdKey(step, 2), 0.321);
addToExpectedOutput(new TimestepPedestrianIdKey(step, 3), 0.765);
}
});
addSimState(new SimulationStateMock(2) {
@Override
public void mockIt() {
List<Pedestrian> peds = new LinkedList<>();
peds.add(getPedMock(1, 0.456, 0.333));
peds.add(getPedMock(2, 0.321, 0.444));
peds.add(getPedMock(3, 0.765, 0.555));
when(state.getTopography().getElements(Pedestrian.class)).thenReturn(peds);
int step = state.getStep();
addToExpectedOutput(new TimestepPedestrianIdKey(step, 1), 0.333);
addToExpectedOutput(new TimestepPedestrianIdKey(step, 2), 0.444);
addToExpectedOutput(new TimestepPedestrianIdKey(step, 3), 0.555);
}
});
}
@SuppressWarnings("unchecked")
private PedestrianOSM getPedMock(int id, Double... strides) {
PedestrianOSM ped = mock(PedestrianOSM.class, Mockito.RETURNS_DEEP_STUBS);
when(ped.getId()).thenReturn(id);
LinkedList<Double> strideList = new LinkedList<>(Arrays.asList(strides));
LinkedList<Double>[] tmp = (LinkedList<Double>[]) (new LinkedList<?>[2]);
tmp[0] = strideList;
when(ped.getStrides()).thenReturn(tmp);
return ped;
}
@Override
List<String> getExpectedOutputAsList() {
List<String> outputList = new ArrayList<>();
expectedOutput.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.forEach(e -> {
StringJoiner sj = new StringJoiner(getDelimiter());
sj.add(Integer.toString(e.getKey().getTimestep()))
.add(Integer.toString(e.getKey().getPedestrianId()))
.add(Double.toString(e.getValue()));
outputList.add(sj.toString());
});
return outputList;
}
}
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