Commit 17348905 authored by Maxim Dudin's avatar Maxim Dudin
Browse files

Merge branch 'walkingByEachOtherProcessor' into target_changer_prob_list_MD

# Conflicts:
#	Scenarios/Demos/roVer/scenarios/mf_base.scenario
#	Scenarios/Demos/roVer/scenarios/mf_detailed.scenario
#	Scenarios/Demos/roVer/scenarios/mf_underground.scenario
#	Scenarios/Demos/roVer/scenarios/roVerTest001.scenario
#	Scenarios/Demos/roVer/scenarios/roVerTest002.scenario
#	Scenarios/Demos/roVer/scenarios/scenario001.scenario
#	Scenarios/Demos/roVer/scenarios/scenario002.scenario
#	Scenarios/Demos/roVer/scenarios/separation_slow_fast.scenario
parents 5a511624 041df0c1
{
"name" : "mf_base",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
{
"name" : "mf_detailed",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
{
"name" : "mf_underground",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
{
"name" : "roVerTest002",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
{
"name" : "scenario001",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......@@ -15,6 +15,10 @@
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.NoDataKeyOutputFile",
"filename" : "overlapCount.txt",
"processors" : [ 4 ]
}, {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.TimestepPedestriansNearbyIdOutputFile",
"filename" : "pedsNearby.txt",
"processors" : [ 8 ]
} ],
"processors" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.processor.PedestrianOverlapProcessor",
......@@ -32,6 +36,9 @@
}, {
"type" : "org.vadere.simulator.projects.dataprocessing.processor.FootStepTargetIDProcessor",
"id" : 6
}, {
"type" : "org.vadere.simulator.projects.dataprocessing.processor.PedestriansNearbyProcessor",
"id" : 8
} ],
"isTimestamped" : true,
"isWriteMetaData" : false
......
{
"name" : "scenario002",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
{
"name" : "separation_slow_fast",
"description" : "",
"release" : "1.12",
"release" : "1.11",
"processWriters" : {
"files" : [ {
"type" : "org.vadere.simulator.projects.dataprocessing.outputfile.EventtimePedestrianIdOutputFile",
......
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.List;
public class PedestriansNearbyData {
private final int pedId1;
private final int pedId2;
private int durationTimesteps;
private int startTimestep;
private List<VPoint> trajectory;
private boolean printTrajectory;
/*private PedestriansNearbyData() {
this.pedId1 = -1;
this.pedId2 = -1;
this.durationTimesteps = 1;
this.startTimestep = 1;
this.trajectory = new ArrayList<>();
}*/
public int getStartTimestep() {
return startTimestep;
}
public PedestriansNearbyData(int ped1, int ped2, final int durationTimesteps, int startTimestep, List<VPoint> contactTrajectory, boolean printTrajectory){
this.pedId1 = Math.min(ped1, ped2);
this.pedId2 = Math.max(ped1, ped2);
this.durationTimesteps = durationTimesteps;
this.startTimestep = startTimestep;
this.trajectory = contactTrajectory;
this.printTrajectory = printTrajectory;
}
public int getPedId1() {
return pedId1;
}
public int getPedId2() {
return pedId2;
}
public int getDurationTimesteps() {
return durationTimesteps;
}
public List<VPoint> getTrajectory() {
return trajectory;
}
public void addToContactTrajectory(VPoint point) {
trajectory.add(point);
}
public void addToContactTrajectory(List<VPoint> points) {
trajectory.addAll(points);
}
public PedestriansNearbyData getUpdatedData(PedestriansNearbyData newData, int sampleEveryNthStep) {
List<VPoint> traj = getTrajectory();
traj.addAll(newData.getTrajectory());
return new PedestriansNearbyData(getPedId1(), getPedId2(), getDurationTimesteps() + sampleEveryNthStep, getStartTimestep(),traj, printTrajectory);
}
public String[] toStrings(){
if (!printTrajectory) {
return new String[]{Integer.toString(durationTimesteps)};
}
StringBuilder ret = new StringBuilder();
List<VPoint> traj = getTrajectory();
for (int i = 0; i < traj.size(); i++) {
VPoint p = traj.get(i);
ret.append(p.x).append(" ").append(p.y).append("\n");
if (i != traj.size() -1) {
ret.append("- - - - ");
}
}
return new String[]{Integer.toString(durationTimesteps), ret.toString()};
}
}
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimestepPedestrianIdOverlapOutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimestepPedestriansNearbyIdOutputFile;
import org.vadere.state.attributes.AttributesSimulation;
import java.util.Objects;
@OutputFileMap(outputFileClass = TimestepPedestriansNearbyIdOutputFile.class)
public class TimestepPedestriansNearbyIdKey implements DataKey<TimestepPedestriansNearbyIdKey> {
private final int timeStep;
private final int pedId1; //smaller id
private final int pedId2; //bigger id
public TimestepPedestriansNearbyIdKey(int timeStep, int pedA, int pedB) {
this.timeStep = timeStep;
this.pedId1 = Math.min(pedA, pedB);
this.pedId2 = Math.max(pedA, pedB);
}
public static String[] getHeaders(){
return new String[]{TimestepKey.getHeader(), PedestrianIdKey.getHeader(), "pedestrianNearbyId"};
}
public int getTimeStep() {
return timeStep;
}
public int getPedId1() {
return pedId1;
}
public int getPedId2() {
return pedId2;
}
public String[] toStrings(){
return new String[]{Integer.toString(timeStep), Integer.toString(pedId1), Integer.toString(pedId2)};
}
public boolean isContinuationOf(PedestriansNearbyData other, int toleranceTimesteps) {
return other.getPedId1() == getPedId1() &&
other.getPedId2() == getPedId2() &&
(other.getStartTimestep() + other.getDurationTimesteps() <= getTimeStep()) &&
(other.getStartTimestep() + other.getDurationTimesteps() + toleranceTimesteps >= getTimeStep());
}
public boolean isAccountedForBy(PedestriansNearbyData other) {
return equals(other) ||
(other.getPedId1() == getPedId1() &&
other.getPedId2() == getPedId2() &&
getTimeStep() >= other.getStartTimestep() &&
getTimeStep() < other.getStartTimestep() + other.getDurationTimesteps());
}
@Override
public int compareTo(@NotNull TimestepPedestriansNearbyIdKey o) {
int result = Integer.compare(this.timeStep, o.timeStep);
if (result == 0) {
result = Integer.compare(this.pedId1, o.pedId1);
if (result == 0){
result = Integer.compare(this.pedId2, o.pedId2);
}
}
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimestepPedestriansNearbyIdKey that = (TimestepPedestriansNearbyIdKey) o;
return timeStep == that.timeStep &&
((pedId1 == that.pedId1 &&
pedId2 == that.pedId2) || (pedId2 == that.pedId1 &&
pedId1 == that.pedId2));
}
@Override
public int hashCode() {
return Objects.hash(timeStep, pedId1, pedId2);
}
@Override
public String toString() {
return "TimestepPedestrianIdOverlapKey{" +
"timeStep=" + timeStep +
", pedId1=" + pedId1 +
", pedId2=" + pedId2 +
'}';
}
}
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.outputfiles.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdOverlapKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestriansNearbyIdKey;
@OutputFileClass(dataKeyMapping = TimestepPedestriansNearbyIdKey.class)
public class TimestepPedestriansNearbyIdOutputFile extends OutputFile<TimestepPedestriansNearbyIdKey>{
public TimestepPedestriansNearbyIdOutputFile() {
super(TimestepPedestriansNearbyIdKey.getHeaders());
}
@Override
public String[] toStrings(final TimestepPedestriansNearbyIdKey key){
return key.toStrings();
}
}
......@@ -105,6 +105,10 @@ public abstract class DataProcessor<K extends DataKey<K>, V> {
this.data.put(key, value);
}
protected void removeKey(final K key) {
this.data.remove(key);
}
public void preLoop(final SimulationState state) { data.clear(); }
protected abstract void doUpdate(final SimulationState state);
......
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.annotation.factories.dataprocessors.DataProcessorClass;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.PedestriansNearbyData;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestriansNearbyIdKey;
import org.vadere.state.attributes.processor.AttributesPedestrianNearbyProcessor;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.LinkedCellsGrid;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Maxim Dudin
*/
@DataProcessorClass()
public class PedestriansNearbyProcessor extends DataProcessor<TimestepPedestriansNearbyIdKey, PedestriansNearbyData> {
private double maxDistance; // todo adjustable with json
private int sampleEveryNthStep;
private int allowedAbsenceTimestepsIfContactReturns;
private int minTimespanOfContactTimesteps;
private boolean printContacTrajectories;
public PedestriansNearbyProcessor() {
super("durationTimesteps", "xCoord-Contact", "yCoord-Contact");
setAttributes(new AttributesPedestrianNearbyProcessor());
}
@Override
protected void doUpdate(final SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
int timeStep = state.getStep();
if (timeStep % sampleEveryNthStep != 0) {
return;
}
for (Pedestrian ped : peds) {
// get all Pedestrians with at most maxDistance away
// this reduces the amount of overlap tests
VPoint pedPos = ped.getPosition();
List<DynamicElement> dynElemNneighbours = getDynElementsAtPosition(state.getTopography(), ped.getPosition(), maxDistance);
List<PedestriansNearbyData> pedsNearby = dynElemNneighbours
.parallelStream()
.filter(p -> ped.getId() != p.getId())
.map(p -> {
VPoint pointOfContact = new VPoint(ped.getPosition().getX() - p.getPosition().getX(), ped.getPosition().getY() - p.getPosition().getY());
List<VPoint> trajectory = new ArrayList<>();
trajectory.add(pointOfContact);
return new PedestriansNearbyData(ped.getId(), p.getId(), sampleEveryNthStep, timeStep, trajectory, printContacTrajectories);})
.collect(Collectors.toList());
pedsNearby.forEach(o -> this.putValue(new TimestepPedestriansNearbyIdKey(timeStep, o.getPedId1(), o.getPedId2()), o));
}
}
@Override
public void postLoop(final SimulationState state) {
List<TimestepPedestriansNearbyIdKey> toBeRemoved = new ArrayList<>();
for (TimestepPedestriansNearbyIdKey key : getKeys()) {
if (getValue(key).getDurationTimesteps() < minTimespanOfContactTimesteps) {
toBeRemoved.add(key);
}
}
for (TimestepPedestriansNearbyIdKey toRemove: toBeRemoved) {
removeKey(toRemove);
}
}
public String[] toStrings(final TimestepPedestriansNearbyIdKey key) {
return this.hasValue(key) ? this.getValue(key).toStrings() : new String[]{"N/A", "N/A"};
}
@Override
protected void putValue(final TimestepPedestriansNearbyIdKey key, final PedestriansNearbyData value) {
for (TimestepPedestriansNearbyIdKey alreadyExisting : getKeys()) {
PedestriansNearbyData currentVal = getValue(alreadyExisting);
if (key.isAccountedForBy(currentVal)) {
return;
} else if (key.isContinuationOf(currentVal, allowedAbsenceTimestepsIfContactReturns)) {
super.putValue(alreadyExisting, currentVal.getUpdatedData(value, sampleEveryNthStep));
return;
}
}
super.putValue(key, value);
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
AttributesPedestrianNearbyProcessor att = (AttributesPedestrianNearbyProcessor) this.getAttributes();
maxDistance = att.getMaxDistanceForANearbyPedestrian();
sampleEveryNthStep = att.getSampleEveryNthStep();
allowedAbsenceTimestepsIfContactReturns = att.getAllowedAbsenceTimestepsIfContactReturns();
minTimespanOfContactTimesteps = att.getMinTimespanOfContactTimesteps();
printContacTrajectories = att.getPrintContactTrajectories();
}
private List<DynamicElement> getDynElementsAtPosition(final Topography topography, VPoint sourcePosition, double radius) {
LinkedCellsGrid<DynamicElement> dynElements = topography.getSpatialMap(DynamicElement.class);
return dynElements.getObjects(sourcePosition, radius);
}
}
package org.vadere.state.attributes.processor;
/**
* @author Maxim Dudin
*
*/
public class AttributesPedestrianNearbyProcessor extends AttributesProcessor {
private double maxDistanceForANearbyPedestrian = 1.5;
private int sampleEveryNthStep = 1;
private int allowedAbsenceTimestepsIfContactReturns = 0;
private int minTimespanOfContactTimesteps = 1;
private boolean printContactTrajectories = false;
public double getMaxDistanceForANearbyPedestrian() {
return maxDistanceForANearbyPedestrian;
}
public int getSampleEveryNthStep() {
return sampleEveryNthStep;
}
public int getAllowedAbsenceTimestepsIfContactReturns() {
return allowedAbsenceTimestepsIfContactReturns;
}
public int getMinTimespanOfContactTimesteps() {
return minTimespanOfContactTimesteps;
}
public boolean getPrintContactTrajectories() {
return printContactTrajectories;
}
}
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