Commit 037fd190 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add DynamicElement{Add,Removed}Listener to OSM and Group Model

Pedestrians which are hold within a Model (List, Queue) must be deleted,
if they are consumed by a target. For the group model the listeners were
moved to the CentroidGroupModel (from the CentroidGroupFactory) to allow
one factory per source.
parent c90ef93d
Pipeline #55228 passed with stage
in 46 seconds
......@@ -10,7 +10,7 @@ public class CentroidGroupFactory extends GroupFactory {
private CentroidGroup currentGroup;
public CentroidGroupFactory(CentroidGroupModel groupCollection,
GroupSizeDeterminator groupSizeDet) {
GroupSizeDeterminator groupSizeDet) {
this.groupCollection = groupCollection;
this.groupSizeDeterminator = groupSizeDet;
}
......@@ -43,7 +43,7 @@ public class CentroidGroupFactory extends GroupFactory {
.nextGroupSize());
}
@Override //listener methode (aufruf
//listener methode (aufruf
public void elementAdded(Pedestrian pedestrian) {
if (requiresNewGroup()) {
createNewGroup();
......@@ -51,9 +51,8 @@ public class CentroidGroupFactory extends GroupFactory {
assignToGroup(pedestrian);
}
@Override
public void elementRemoved(Pedestrian ped) {
CentroidGroup group = groupCollection.removeMember(ped);
System.out.printf("Remove ped %s from group %s %n", ped.getId(), group.getID());
System.out.printf("Remove ped %s from group %s %n", ped.getId(), group != null ? group.getID() : "noGroup");
}
}
package org.vadere.simulator.models.groups;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesCGM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.DynamicElementAddListener;
import org.vadere.state.scenario.DynamicElementRemoveListener;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
import org.vadere.state.scenario.Topography;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
@ModelClass
public class CentroidGroupModel implements GroupModel {
public class CentroidGroupModel
implements GroupModel, DynamicElementAddListener<Pedestrian>, DynamicElementRemoveListener<Pedestrian> {
private GroupSizeDeterminator groupSizeDeterminator;
private Map<Integer, CentroidGroupFactory> groupFactories;
......@@ -29,16 +32,16 @@ public class CentroidGroupModel implements GroupModel {
private AttributesCGM attributesCGM;
private AtomicInteger nextFreeGroupId;
public CentroidGroupModel() {
this.groupFactories = new TreeMap<>();
this.pedestrianGroupData = new HashMap<>();
this.nextFreeGroupId = new AtomicInteger(0);
}
@Override
public void initialize(List<Attributes> attributesList, Topography topography,
AttributesAgent attributesPedestrian, Random random) {
AttributesAgent attributesPedestrian, Random random) {
this.attributesCGM = Model.findAttributes(attributesList, AttributesCGM.class);
this.groupSizeDeterminator = new GroupSizeDeterminatorRandom(
attributesCGM.getGroupSizeDistribution(), random);
......@@ -95,10 +98,8 @@ public class CentroidGroupModel implements GroupModel {
@Override
public void preLoop(final double simTimeInSec) {
topography.getSources().stream().forEach( source -> {
topography.addElementAddedListener(Pedestrian.class, getGroupFactory(source.getId()));
topography.addElementRemovedListener(Pedestrian.class, getGroupFactory(source.getId()));
});
topography.addElementAddedListener(Pedestrian.class, this);
topography.addElementRemovedListener(Pedestrian.class, this);
}
@Override
......@@ -106,10 +107,21 @@ public class CentroidGroupModel implements GroupModel {
}
@Override
public void update(final double simTimeInSec) {}
public void update(final double simTimeInSec) {
}
public AttributesCGM getAttributesCGM() {
return attributesCGM;
}
@Override
public void elementAdded(Pedestrian pedestrian) {
// call GroupFactory for selected Source
getGroupFactory(pedestrian.getSource().getId()).elementAdded(pedestrian);
}
@Override
public void elementRemoved(Pedestrian pedestrian) {
getGroupFactory(pedestrian.getSource().getId()).elementRemoved(pedestrian);
}
}
package org.vadere.simulator.models.groups;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.state.attributes.Attributes;
......@@ -16,6 +12,10 @@ 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.List;
import java.util.Random;
@ModelClass
public class CentroidGroupPotential implements PotentialFieldAgent {
......@@ -24,8 +24,8 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
private final PotentialFieldAgent potentialFieldPedestrian;
public CentroidGroupPotential(CentroidGroupModel groupCollection,
PotentialFieldAgent pedestrianRepulsionPotential,
AttributesCGM attributesCGM) {
PotentialFieldAgent pedestrianRepulsionPotential,
AttributesCGM attributesCGM) {
this.attributesCGM = attributesCGM;
this.groupCollection = groupCollection;
......@@ -34,7 +34,7 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override
public double getAgentPotential(VPoint pos, Agent pedestrian,
Collection<? extends Agent> closePedestrians) {
Collection<? extends Agent> closePedestrians) {
double result = 0;
if (!(pedestrian instanceof Pedestrian))
......@@ -67,16 +67,16 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
result = attributesCGM.getLeaderAttractionFactor()
* Math.pow(
Math.pow(distanceToCentroid[0], 2)
+ Math.pow(distanceToCentroid[1], 2),
2);
Math.pow(distanceToCentroid[0], 2)
+ Math.pow(distanceToCentroid[1], 2),
2);
}
return result;
}
private double getPedestrianRepulsionPotential(Pedestrian ped, VPoint pos,
Collection<? extends Agent> closePedestrians) {
Collection<? extends Agent> closePedestrians) {
double potential = 0;
for (Agent neighborBody : closePedestrians) {
......@@ -90,8 +90,8 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override
public Vector2D getAgentPotentialGradient(VPoint pos,
Vector2D velocity, Agent pedestrian,
Collection<? extends Agent> closePedestrians) {
Vector2D velocity, Agent pedestrian,
Collection<? extends Agent> closePedestrians) {
// TODO [priority=low] [task=refactoring] not implemented
throw new UnsupportedOperationException("this method is not jet implemented.");
// return new Vector2D(0, 0);
......@@ -99,14 +99,14 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override
public double getAgentPotential(VPoint pos, Agent pedestrian,
Agent otherPedestrian) {
Agent otherPedestrian) {
System.out.printf("Ped1: %s, Ped1: %s %n", pedestrian.getId(), otherPedestrian.getId());
CentroidGroup group = groupCollection.getGroup(pedestrian);
CentroidGroup groupOther = groupCollection.getGroup(otherPedestrian);
double potential = potentialFieldPedestrian.getAgentPotential(pos,
pedestrian, otherPedestrian);
if (group!= null && group.equals(groupOther)) {
if (group.equals(groupOther)) {
potential *= attributesCGM.getGroupMemberRepulsionFactor();
}
......@@ -115,14 +115,14 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override
public Collection<? extends Agent> getRelevantAgents(VCircle relevantArea,
Agent pedestrian, Topography scenario) {
Agent pedestrian, Topography scenario) {
return potentialFieldPedestrian.getRelevantAgents(relevantArea,
pedestrian, scenario);
}
@Override
public void initialize(List<Attributes> attributesList, Topography topography,
AttributesAgent attributesPedestrian, Random random) {
AttributesAgent attributesPedestrian, Random random) {
// TODO [priority=medium] [task=refactoring] should be used to initialize the Model
}
......
......@@ -5,13 +5,10 @@ import org.vadere.state.scenario.DynamicElementRemoveListener;
import org.vadere.state.scenario.Pedestrian;
//wenn ped erzeugt und entfernet werden
public abstract class GroupFactory
implements DynamicElementAddListener<Pedestrian>, DynamicElementRemoveListener<Pedestrian>{
public abstract class GroupFactory {
public abstract int getOpenPersons();
@Override
public abstract void elementAdded(Pedestrian element);
public abstract void elementAdded(Pedestrian pedestrian);
@Override
public abstract void elementRemoved(Pedestrian element);
public abstract void elementRemoved(Pedestrian ped);
}
package org.vadere.simulator.models.groups;
import org.vadere.simulator.models.Model;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
public interface GroupModel extends Model {
......
......@@ -18,14 +18,15 @@ import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerPowell;
import org.vadere.simulator.models.osm.updateScheme.ParallelWorkerOSM;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
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.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.DynamicElementRemoveListener;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.state.types.OptimizationType;
......@@ -46,23 +47,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@ModelClass(isMainModel = true)
public class OptimalStepsModel implements MainModel, PotentialFieldModel {
public class OptimalStepsModel implements MainModel, PotentialFieldModel, DynamicElementRemoveListener<Pedestrian> {
/**
* Compares the time of the next possible move.
*/
private class ComparatorPedestrianOSM implements Comparator<PedestrianOSM> {
@Override
public int compare(PedestrianOSM ped1, PedestrianOSM ped2) {
// TODO [priority=low] [task=refactoring] use Double.compare() oder compareTo()
if (ped1.getTimeOfNextStep() < ped2.getTimeOfNextStep()) {
return -1;
} else {
return 1;
}
}
}
private AttributesOSM attributesOSM;
private AttributesAgent attributesPedestrian;
......@@ -76,10 +62,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
private double lastSimTimeInSec;
private int pedestrianIdCounter;
private PriorityQueue<PedestrianOSM> pedestrianEventsQueue;
private ExecutorService executorService;
private List<Model> models = new LinkedList<>();
public OptimalStepsModel() {
this.pedestrianIdCounter = 0;
this.speedAdjusters = new LinkedList<>();
......@@ -87,7 +71,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
@Override
public void initialize(List<Attributes> modelAttributesList, Topography topography,
AttributesAgent attributesPedestrian, Random random) {
AttributesAgent attributesPedestrian, Random random) {
this.attributesOSM = Model.findAttributes(modelAttributesList, AttributesOSM.class);
this.topography = topography;
......@@ -110,19 +94,19 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField(
modelAttributesList, topography, attributesOSM.getPedestrianPotentialModel());
Optional<CentroidGroupModel> opCentroidGroupModel = models.stream().
filter(ac -> ac instanceof CentroidGroupModel).map(ac -> (CentroidGroupModel)ac).findAny();
filter(ac -> ac instanceof CentroidGroupModel).map(ac -> (CentroidGroupModel) ac).findAny();
if (opCentroidGroupModel.isPresent()) {
CentroidGroupModel centroidGroupModel = opCentroidGroupModel.get();
centroidGroupModel.setPotentialFieldTarget(iPotentialTargetGrid);
this.potentialFieldPedestrian =
new CentroidGroupPotential(centroidGroupModel,
potentialFieldPedestrian, centroidGroupModel.getAttributesCGM());
SpeedAdjuster speedAdjusterCGM = new CentroidGroupSpeedAdjuster(centroidGroupModel);
this.speedAdjusters.add(speedAdjusterCGM);
}
......@@ -148,6 +132,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
this.executorService = null;
}
topography.addElementRemovedListener(Pedestrian.class, this);
models.add(this);
}
......@@ -196,7 +182,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
}
@Override
public void postLoop(final double simTimeInSec) {}
public void postLoop(final double simTimeInSec) {
}
@Override
public void update(final double simTimeInSec) {
......@@ -312,4 +299,27 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
public PotentialFieldAgent getPotentialFieldAgent() {
return potentialFieldPedestrian;
}
@Override
public void elementRemoved(Pedestrian ped) {
PedestrianOSM osmPed = (PedestrianOSM) ped;
pedestrianEventsQueue.remove(osmPed);
// System.out.printf("Remove ped %s from pedestrianEventsQueue%n", ped.getId());
}
/**
* Compares the time of the next possible move.
*/
private class ComparatorPedestrianOSM implements Comparator<PedestrianOSM> {
@Override
public int compare(PedestrianOSM ped1, PedestrianOSM ped2) {
// TODO [priority=low] [task=refactoring] use Double.compare() oder compareTo()
if (ped1.getTimeOfNextStep() < ped2.getTimeOfNextStep()) {
return -1;
} else {
return 1;
}
}
}
}
Supports Markdown
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