diff --git a/VadereManager/src/org/vadere/manager/traci/commandHandler/VadereCommandHandler.java b/VadereManager/src/org/vadere/manager/traci/commandHandler/VadereCommandHandler.java index bc02d5a09b9f3bbcf2568e9db6dece6ec2cca4c1..c87a80e2b9cc3b0d9d8e214580c5b6ace55e37ba 100644 --- a/VadereManager/src/org/vadere/manager/traci/commandHandler/VadereCommandHandler.java +++ b/VadereManager/src/org/vadere/manager/traci/commandHandler/VadereCommandHandler.java @@ -98,7 +98,7 @@ public class VadereCommandHandler extends CommandHandler { //todo[random]: use Random object from context for now. This should be replaced by the meta seed. VadereContext ctx = VadereContext.get(state.getTopography()); Random rnd = (Random) ctx.get("random"); - TargetChangerController tcc = new TargetChangerController(state.getTopography(), tc, rnd); + TargetChangerController tcc = state.getMainModel().get().getTargetChangerControllerFactory().create(state.getTopography(), tc, rnd); manager.getRemoteSimulationRun().addTargetChangerController(tcc); state.getTopography().addTargetChanger(tc); cmd.setOK(); diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetChangerControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetChangerControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..36fcfe3447f37ccbf2664198b855172c4f96ac4f --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetChangerControllerFactory.java @@ -0,0 +1,23 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.GroupTargetChangerController; +import org.vadere.simulator.control.scenarioelements.TargetChangerController; +import org.vadere.simulator.models.groups.GroupIterator; +import org.vadere.state.scenario.TargetChanger; +import org.vadere.state.scenario.Topography; + +import java.util.Random; + +public class GroupTargetChangerControllerFactory extends TargetChangerControllerFactory { + + private final GroupIterator groupIterator; + + public GroupTargetChangerControllerFactory(GroupIterator groupIterator) { + this.groupIterator = groupIterator; + } + + @Override + public TargetChangerController create(Topography topography, TargetChanger target, Random random) { + return new GroupTargetChangerController(topography, target, random, groupIterator); + } +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..aef6da308d1a2d6273779fb525637e16bede6636 --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/GroupTargetControllerFactory.java @@ -0,0 +1,21 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.GroupTargetController; +import org.vadere.simulator.control.scenarioelements.TargetController; +import org.vadere.simulator.models.groups.GroupIterator; +import org.vadere.state.scenario.Target; +import org.vadere.state.scenario.Topography; + +public class GroupTargetControllerFactory extends TargetControllerFactory { + + private final GroupIterator groupIterator; + + public GroupTargetControllerFactory(GroupIterator groupIterator) { + this.groupIterator = groupIterator; + } + + @Override + public TargetController create(Topography topography, Target target) { + return new GroupTargetController(topography, target, groupIterator); + } +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetChangerControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetChangerControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..a2c3f4ff29ccc1c5bd93cc40cd50198d5a472fb8 --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetChangerControllerFactory.java @@ -0,0 +1,16 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.SingleTargetChangerController; +import org.vadere.simulator.control.scenarioelements.TargetChangerController; +import org.vadere.state.scenario.TargetChanger; +import org.vadere.state.scenario.Topography; + +import java.util.Random; + +public class SingleTargetChangerControllerFactory extends TargetChangerControllerFactory { + + @Override + public TargetChangerController create(Topography topography, TargetChanger targetChanger, Random random) { + return new SingleTargetChangerController(topography, targetChanger, random); + } +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..3a33d71e0122a521cdfa085deed439d8bf81f0ab --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/SingleTargetControllerFactory.java @@ -0,0 +1,14 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.SingleTargetController; +import org.vadere.simulator.control.scenarioelements.TargetController; +import org.vadere.state.scenario.Target; +import org.vadere.state.scenario.Topography; + +public class SingleTargetControllerFactory extends TargetControllerFactory { + + @Override + public TargetController create(Topography topography, Target target) { + return new SingleTargetController(topography, target); + } +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/TargetChangerControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/TargetChangerControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..f1c3563457fe6ab491176f937389598a29b0c18e --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/TargetChangerControllerFactory.java @@ -0,0 +1,11 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.TargetChangerController; +import org.vadere.state.scenario.TargetChanger; +import org.vadere.state.scenario.Topography; + +import java.util.Random; + +public abstract class TargetChangerControllerFactory { + abstract public TargetChangerController create(Topography topography, TargetChanger targetChanger, Random random); +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/factory/TargetControllerFactory.java b/VadereSimulator/src/org/vadere/simulator/control/factory/TargetControllerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..02b1de2b26dbff81f047696609cea92efad90d08 --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/factory/TargetControllerFactory.java @@ -0,0 +1,9 @@ +package org.vadere.simulator.control.factory; + +import org.vadere.simulator.control.scenarioelements.TargetController; +import org.vadere.state.scenario.Target; +import org.vadere.state.scenario.Topography; + +public abstract class TargetControllerFactory { + abstract public TargetController create(Topography topography, Target target); +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetChangerController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetChangerController.java new file mode 100644 index 0000000000000000000000000000000000000000..1f89f9648a0d905846f151d053a2f86bd146869c --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetChangerController.java @@ -0,0 +1,39 @@ +package org.vadere.simulator.control.scenarioelements; + +import org.vadere.simulator.models.groups.Group; +import org.vadere.simulator.models.groups.GroupIterator; +import org.vadere.state.scenario.Agent; +import org.vadere.state.scenario.Pedestrian; +import org.vadere.state.scenario.TargetChanger; +import org.vadere.state.scenario.Topography; + +import java.util.Iterator; +import java.util.Random; + +public class GroupTargetChangerController extends TargetChangerController { + // Member Variables + private final GroupIterator groupIterator; + // Constructors + + + public GroupTargetChangerController(Topography topography, TargetChanger targetChanger, Random random, GroupIterator groupIterator) { + super(topography, targetChanger, random); + this.groupIterator = groupIterator; + } + + @Override + protected void changeTargets(Agent agent) { + assert agent instanceof Pedestrian : "GroupModel used but Agent not Pedestrian"; + Iterator iterator = groupIterator.getGroupIterator(); + while (iterator.hasNext()) { + Group group = iterator.next(); + if (group.isMember((Pedestrian) agent)) { + for (Pedestrian ped: group.getMembers()) { + changerAlgorithm.setAgentTargetList(ped); + processedAgents.put(ped.getId(), ped); + } + break; + } + } + } +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetController.java new file mode 100644 index 0000000000000000000000000000000000000000..6d9c919952fd8df18e194067b619d612fea27b1f --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/GroupTargetController.java @@ -0,0 +1,38 @@ +package org.vadere.simulator.control.scenarioelements; + +import org.vadere.simulator.models.groups.Group; +import org.vadere.simulator.models.groups.GroupIterator; +import org.vadere.state.scenario.Agent; +import org.vadere.state.scenario.Pedestrian; +import org.vadere.state.scenario.Target; +import org.vadere.state.scenario.Topography; + +import java.util.Iterator; + +public class GroupTargetController extends TargetController { + + private final GroupIterator groupIterator; + + public GroupTargetController(Topography topography, Target target, GroupIterator groupIterator) { + super(topography, target); + this.groupIterator = groupIterator; + } + + @Override + protected boolean checkRemove(Agent agent) { + if (!super.checkRemove(agent)) { + assert agent instanceof Pedestrian : "GroupModel used but Agent not Pedestrian"; + Iterator iterator = groupIterator.getGroupIterator(); + while (iterator.hasNext()) { + Group group = iterator.next(); + if (group.isMember((Pedestrian) agent)) { + group.checkNextTarget(target.getNextSpeed()); + break; + } + } + return false; + } + return true; + } + +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetChangerController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetChangerController.java new file mode 100644 index 0000000000000000000000000000000000000000..ebe8771a1c15b919693453142c456926491edd7e --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetChangerController.java @@ -0,0 +1,20 @@ +package org.vadere.simulator.control.scenarioelements; + +import org.vadere.state.scenario.Agent; +import org.vadere.state.scenario.TargetChanger; +import org.vadere.state.scenario.Topography; + +import java.util.Random; + +public class SingleTargetChangerController extends TargetChangerController { + + public SingleTargetChangerController(Topography topography, TargetChanger targetChanger, Random random) { + super(topography, targetChanger, random); + } + + @Override + protected void changeTargets(Agent agent) { + changerAlgorithm.setAgentTargetList(agent); + processedAgents.put(agent.getId(), agent); + } +} \ No newline at end of file diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetController.java new file mode 100644 index 0000000000000000000000000000000000000000..be8a507b5c171c75d43d7f4a56fe0929af9da3aa --- /dev/null +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/SingleTargetController.java @@ -0,0 +1,43 @@ +package org.vadere.simulator.control.scenarioelements; + +import org.vadere.state.scenario.Agent; +import org.vadere.state.scenario.Target; +import org.vadere.state.scenario.Topography; + +public class SingleTargetController extends TargetController { + + public SingleTargetController(Topography topography, Target target) { + super(topography, target); + } + + @Override + protected boolean checkRemove(Agent agent) { + if (!super.checkRemove(agent)) { + checkNextTarget(agent, target.getNextSpeed()); + return false; + } + return true; + } + + // TODO [priority=high] [task=deprecation] removing the target from the list is deprecated, but still very frequently used everywhere. + public void checkNextTarget(Agent agent, double nextSpeed) { + final int nextTargetListIndex = agent.getNextTargetListIndex(); + + // Deprecated target list usage + if (nextTargetListIndex <= -1 && !agent.getTargets().isEmpty()) { + agent.getTargets().removeFirst(); + } + + // The right way (later this first check should not be necessary anymore): + if (agent.hasNextTarget()) { + agent.incrementNextTargetListIndex(); + } + + // set a new desired speed, if possible. you can model street networks with differing + // maximal speeds with this. + if (nextSpeed >= 0) { + agent.setFreeFlowSpeed(nextSpeed); + } + } + +} diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetChangerController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetChangerController.java index b2a08090690c83e04d513adb1f3bf22ee6b120d0..509eec15ba7d67b8c7bac0dce1452fcd34acd71d 100644 --- a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetChangerController.java +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetChangerController.java @@ -1,10 +1,6 @@ package org.vadere.simulator.control.scenarioelements; import org.vadere.simulator.control.scenarioelements.targetchanger.TargetChangerAlgorithm; -import org.vadere.simulator.control.simulation.SimulationState; -import org.vadere.simulator.models.groups.GroupIterator; -import org.vadere.simulator.models.groups.cgm.CentroidGroup; -import org.vadere.simulator.models.groups.cgm.CentroidGroupModel; import org.vadere.simulator.projects.dataprocessing.processor.util.ModelFilter; import org.vadere.state.scenario.Agent; import org.vadere.state.scenario.DynamicElement; @@ -34,39 +30,32 @@ import java.util.*; * * */ -public class TargetChangerController extends ScenarioElementController implements ModelFilter { +public abstract class TargetChangerController extends ScenarioElementController implements ModelFilter { // Static Variables - private static final Logger log = Logger.getLogger(TargetChangerController.class); + protected static final Logger log = Logger.getLogger(TargetChangerController.class); private static final int BINOMIAL_DISTRIBUTION_SUCCESS_VALUE = 1; // Member Variables public final TargetChanger targetChanger; - private final GroupIterator groupIterator; - private Topography topography; - private Map processedAgents; + protected final Topography topography; + protected final Map processedAgents; private Random random; - private TargetChangerAlgorithm changerAlgorithm; + protected TargetChangerAlgorithm changerAlgorithm; // Constructors - public TargetChangerController(Topography topography, TargetChanger targetChanger, Random random, GroupIterator groupIterator) { + public TargetChangerController(Topography topography, TargetChanger targetChanger, Random random) { this.changerAlgorithm = TargetChangerAlgorithm.create(targetChanger, topography); this.changerAlgorithm.throwExceptionOnInvalidInput(targetChanger); this.changerAlgorithm.init(random); - this.targetChanger = targetChanger; this.topography = topography; this.processedAgents = new HashMap<>(); this.random = random; - this.groupIterator = groupIterator; - } - - public TargetChangerController(Topography topography, TargetChanger targetChanger, Random random) { - this(topography, targetChanger, random, null); } // Getters @@ -87,72 +76,15 @@ public class TargetChangerController extends ScenarioElementController implement continue; } - if (hasAgentReachedTargetChangerArea(agent) && processedAgents.containsKey(agent.getId()) == false) { + if (hasAgentReachedTargetChangerArea(agent) && !processedAgents.containsKey(agent.getId())) { logEnteringTimeOfAgent(agent, simTimeInSec); notifyListenersTargetChangerAreaReached(agent); - - if (agent instanceof Pedestrian) { - Pedestrian p = (Pedestrian) agent; - if (p.isAgentsInGroup()) { - changerAlgorithm.setAgentTargetList(p); - for (Pedestrian ped : p.getPedGroupMembers()) { - processedAgents.put(ped.getId(), ped); - } - } else { - changerAlgorithm.setAgentTargetList(p); - } - } else { - changerAlgorithm.setAgentTargetList(agent); - } - processedAgents.put(agent.getId(), agent); + changeTargets(agent); } } } - //new update method to include groups - public void update(SimulationState state) { - for (DynamicElement element : getDynamicElementsNearTargetChangerArea()) { - - final Agent agent; - if (element instanceof Agent) { - agent = (Agent) element; - } else { - log.error("The given object is not a subtype of Agent."); - continue; - } - - if (hasAgentReachedTargetChangerArea(agent) && processedAgents.containsKey(agent.getId()) == false) { - logEnteringTimeOfAgent(agent, state.getSimTimeInSec()); - notifyListenersTargetChangerAreaReached(agent); - - if (agent instanceof Pedestrian) { - Pedestrian p = (Pedestrian) agent; - //TODO tmp better GroupTargetChangerController like GroupSourceController or CentroidGroup to DynamicElement - if (p.isAgentsInGroup()) { - getModel(state, CentroidGroupModel.class).ifPresentOrElse( - (model) - -> { - CentroidGroupModel cgm = (CentroidGroupModel) model; - CentroidGroup group = cgm.getGroup(p); - group.setGroupTargetList(this.targetChanger.getAttributes().getNextTarget()); - }, - () - -> { - log.error("no group Model found but Agent in Group"); - }); - for (Pedestrian ped : p.getPedGroupMembers()) { - processedAgents.put(ped.getId(), ped); - } - } else { - changerAlgorithm.setAgentTargetList(p); - } - } else { - changerAlgorithm.setAgentTargetList(agent); - } - processedAgents.put(agent.getId(), agent); - } - } - } + protected abstract void changeTargets(Agent agent); private Collection getDynamicElementsNearTargetChangerArea() { final Rectangle2D areaBounds = targetChanger.getShape().getBounds2D(); diff --git a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetController.java b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetController.java index 699df1aacbf2e693d07b779b9328c461288cd272..bf0daa6a184c3b59aa83dea0ea8d831b06bf8377 100644 --- a/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetController.java +++ b/VadereSimulator/src/org/vadere/simulator/control/scenarioelements/TargetController.java @@ -1,9 +1,5 @@ package org.vadere.simulator.control.scenarioelements; -import org.vadere.simulator.control.simulation.SimulationState; -import org.vadere.simulator.models.groups.cgm.CentroidGroup; -import org.vadere.simulator.models.groups.cgm.CentroidGroupModel; -import org.vadere.simulator.projects.dataprocessing.processor.util.ModelFilter; import org.vadere.state.scenario.Agent; import org.vadere.state.scenario.Car; import org.vadere.state.scenario.DynamicElement; @@ -22,14 +18,14 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -public class TargetController extends ScenarioElementController implements ModelFilter { +public abstract class TargetController extends ScenarioElementController { private static final Logger log = Logger.getLogger(TargetController.class); public final Target target; - private Topography topography; + protected final Topography topography; - public TrafficLightPhase phase = TrafficLightPhase.GREEN; + public TrafficLightPhase phase; public TargetController(Topography topography, Target target) { this.target = target; @@ -42,7 +38,7 @@ public class TargetController extends ScenarioElementController implements Model } } - public void update(SimulationState state) { + public void update(double simTimeInSec) { if (target.isTargetPedestrian()) { return; } @@ -63,9 +59,9 @@ public class TargetController extends ScenarioElementController implements Model notifyListenersTargetReached(agent); if (target.getWaitingTime() <= 0) { - checkRemove(agent, state); + checkRemove(agent); } else { - waitingBehavior(state, agent); + waitingBehavior(simTimeInSec, agent); } } } @@ -85,30 +81,30 @@ public class TargetController extends ScenarioElementController implements Model return elementsInRange; } - private void waitingBehavior(SimulationState state, final Agent agent) { + private void waitingBehavior(double simTimeInSec, final Agent agent) { final int agentId = agent.getId(); // individual waiting behaviour, as opposed to waiting at a traffic light if (target.getAttributes().isIndividualWaiting()) { final Map enteringTimes = target.getEnteringTimes(); if (enteringTimes.containsKey(agentId)) { - if (state.getSimTimeInSec() - enteringTimes.get(agentId) > target + if (simTimeInSec - enteringTimes.get(agentId) > target .getWaitingTime()) { enteringTimes.remove(agentId); - checkRemove(agent, state); + checkRemove(agent); } } else { final int parallelWaiters = target.getParallelWaiters(); if (parallelWaiters <= 0 || (parallelWaiters > 0 && enteringTimes.size() < parallelWaiters)) { - enteringTimes.put(agentId, state.getSimTimeInSec()); + enteringTimes.put(agentId, simTimeInSec); } } } else { // traffic light switching based on waiting time. Light starts green. - phase = getCurrentTrafficLightPhase(state.getSimTimeInSec()); + phase = getCurrentTrafficLightPhase(simTimeInSec); if (phase == TrafficLightPhase.GREEN) { - checkRemove(agent, state); + checkRemove(agent); } } } @@ -162,33 +158,13 @@ public class TargetController extends ScenarioElementController implements Model return isNextTargetForAgent; } - private void checkRemove(Agent agent, SimulationState state) { + protected boolean checkRemove(Agent agent) { if (target.isAbsorbing()) { changeTargetOfFollowers(agent); topography.removeElement(agent); - } else { - if (agent instanceof Pedestrian) { - Pedestrian p = (Pedestrian) agent; - //TODO better GroupTargetController like GroupSourceController or make CentroidGroup a DynamicElement - if (p.isAgentsInGroup()) { - getModel(state, CentroidGroupModel.class).ifPresentOrElse( - (model) - -> { - CentroidGroupModel cgm = (CentroidGroupModel) model; - CentroidGroup group = cgm.getGroup(p); - group.checkNextTarget(target.getNextSpeed()); - }, - () - -> { - log.error("no group Model found but Agent in Group"); - }); - } else { - agent.checkNextTarget(target.getNextSpeed()); - } - } else { - agent.checkNextTarget(target.getNextSpeed()); - } + return true; } + return false; } private void changeTargetOfFollowers(Agent agent) { diff --git a/VadereSimulator/src/org/vadere/simulator/control/simulation/Simulation.java b/VadereSimulator/src/org/vadere/simulator/control/simulation/Simulation.java index 625bcb477a0ab655db94f034668bd55d7f25e51f..8e32aa33a7b3e0aabc658783a8f84ce5b710f616 100644 --- a/VadereSimulator/src/org/vadere/simulator/control/simulation/Simulation.java +++ b/VadereSimulator/src/org/vadere/simulator/control/simulation/Simulation.java @@ -1,6 +1,8 @@ package org.vadere.simulator.control.simulation; import org.vadere.simulator.control.factory.SourceControllerFactory; +import org.vadere.simulator.control.factory.TargetChangerControllerFactory; +import org.vadere.simulator.control.factory.TargetControllerFactory; import org.vadere.simulator.control.psychology.cognition.models.ICognitionModel; import org.vadere.simulator.control.psychology.perception.StimulusController; import org.vadere.simulator.control.psychology.perception.models.IPerceptionModel; @@ -84,6 +86,8 @@ public class Simulation implements ControllerProvider{ private final Topography topography; private final ProcessorManager processorManager; private final SourceControllerFactory sourceControllerFactory; + private final TargetControllerFactory targetControllerFactory; + private final TargetChangerControllerFactory targetChangerControllerFactory; private SimulationResult simulationResult; private final StimulusController stimulusController; private final ScenarioCache scenarioCache; @@ -120,6 +124,8 @@ public class Simulation implements ControllerProvider{ this.models = mainModel.getSubmodels(); this.sourceControllerFactory = mainModel.getSourceControllerFactory(); + this.targetControllerFactory = mainModel.getTargetControllerFactory(); + this.targetChangerControllerFactory = mainModel.getTargetChangerControllerFactory(); // TODO [priority=normal] [task=bugfix] - the attributesCar are missing in initialize' parameters this.dynamicElementFactory = mainModel; @@ -159,11 +165,11 @@ public class Simulation implements ControllerProvider{ } for (Target target : topography.getTargets()) { - targetControllers.add(new TargetController(topography, target)); + targetControllers.add(this.targetControllerFactory.create(topography, target)); } for (TargetChanger targetChanger : topography.getTargetChangers()) { - targetChangerControllers.add(new TargetChangerController(topography, targetChanger, random)); + targetChangerControllers.add(this.targetChangerControllerFactory.create(topography, targetChanger, random)); } for (AbsorbingArea absorbingArea : topography.getAbsorbingAreas()) { @@ -430,7 +436,7 @@ public class Simulation implements ControllerProvider{ // new targets. this.targetControllers.clear(); for (Target target : this.topographyController.getTopography().getTargets()) { - targetControllers.add(new TargetController(this.topographyController.getTopography(), target)); + targetControllers.add(this.targetControllerFactory.create(this.topographyController.getTopography(), target)); } for (SourceController sourceController : this.sourceControllers) { @@ -438,11 +444,11 @@ public class Simulation implements ControllerProvider{ } for (TargetController targetController : this.targetControllers) { - targetController.update(this.getSimulationState()); + targetController.update(simTimeInSec); } for (TargetChangerController targetChangerController : this.targetChangerControllers) { - targetChangerController.update(this.getSimulationState()); + targetChangerController.update(simTimeInSec); } for (AbsorbingAreaController absorbingAreaController : this.absorbingAreaControllers) { diff --git a/VadereSimulator/src/org/vadere/simulator/models/MainModel.java b/VadereSimulator/src/org/vadere/simulator/models/MainModel.java index 4fc351c70b876eb8b23dc02f0f1376bd1fd03932..9ae484d8e2c2c949ac1eda41afd9ea00b8667f14 100644 --- a/VadereSimulator/src/org/vadere/simulator/models/MainModel.java +++ b/VadereSimulator/src/org/vadere/simulator/models/MainModel.java @@ -1,7 +1,6 @@ package org.vadere.simulator.models; -import org.vadere.simulator.control.factory.SingleSourceControllerFactory; -import org.vadere.simulator.control.factory.SourceControllerFactory; +import org.vadere.simulator.control.factory.*; import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.scenario.Topography; @@ -19,6 +18,14 @@ public interface MainModel extends Model, DynamicElementFactory { return new SingleSourceControllerFactory(); } + default TargetControllerFactory getTargetControllerFactory() { + return new SingleTargetControllerFactory(); + } + + default TargetChangerControllerFactory getTargetChangerControllerFactory() { + return new SingleTargetChangerControllerFactory(); + } + @Override default int registerDynamicElementId(final Topography topography, int id) { int pedId; diff --git a/VadereSimulator/src/org/vadere/simulator/models/groups/Group.java b/VadereSimulator/src/org/vadere/simulator/models/groups/Group.java index 4faa6090237426e3e3e210dee80bd64ecff4aa37..df9817ee97c54a9479fc4bdcf5a04be7abd3d941 100644 --- a/VadereSimulator/src/org/vadere/simulator/models/groups/Group.java +++ b/VadereSimulator/src/org/vadere/simulator/models/groups/Group.java @@ -32,4 +32,6 @@ public interface Group { void setPotentialFieldTarget(IPotentialFieldTarget potentialFieldTarget); IPotentialFieldTarget getPotentialFieldTarget(); + + void checkNextTarget(double nextSpeed); } diff --git a/VadereSimulator/src/org/vadere/simulator/models/groups/GroupIterator.java b/VadereSimulator/src/org/vadere/simulator/models/groups/GroupIterator.java index a38aec1156ca729e235a6f4b393169e028648649..b1206b44f17a90dde85c2e51369a27a61d710790 100644 --- a/VadereSimulator/src/org/vadere/simulator/models/groups/GroupIterator.java +++ b/VadereSimulator/src/org/vadere/simulator/models/groups/GroupIterator.java @@ -3,6 +3,5 @@ package org.vadere.simulator.models.groups; import java.util.Iterator; public interface GroupIterator { - public Iterator getGroupIterator(); - + Iterator getGroupIterator(); } diff --git a/VadereSimulator/src/org/vadere/simulator/models/groups/cgm/CentroidGroup.java b/VadereSimulator/src/org/vadere/simulator/models/groups/cgm/CentroidGroup.java index 965bc438d04980eed4d02b156def70b904188fc7..6b7521f56483c366f2686012803d31634828718b 100644 --- a/VadereSimulator/src/org/vadere/simulator/models/groups/cgm/CentroidGroup.java +++ b/VadereSimulator/src/org/vadere/simulator/models/groups/cgm/CentroidGroup.java @@ -394,12 +394,6 @@ public class CentroidGroup implements Group { return potentialFieldTarget; } - public void setNextTarget(int nextTargetListIndex) { - for (Pedestrian p: this.getMembers()) { - p.setNextTargetListIndex(nextTargetListIndex); - } - } - public void checkNextTarget(double nextSpeed) { //is called by targetcontroller //increments indx of targetlist in case of non absorbing target @@ -412,13 +406,4 @@ public class CentroidGroup implements Group { } } } - - public void setGroupTargetList(LinkedList target) { - //is called by targetchanger controller - for (Pedestrian p: this.getMembers()) { - p.setTargets(target); - p.setIsCurrentTargetAnAgent(false); - } - setNextTarget(0); - } } diff --git a/VadereSimulator/src/org/vadere/simulator/models/osm/OptimalStepsModel.java b/VadereSimulator/src/org/vadere/simulator/models/osm/OptimalStepsModel.java index 36b05735530c736ce26c933fa4e1df1943a978f7..33081cb9855ae33e704758e87afcf7cab6bbfc64 100644 --- a/VadereSimulator/src/org/vadere/simulator/models/osm/OptimalStepsModel.java +++ b/VadereSimulator/src/org/vadere/simulator/models/osm/OptimalStepsModel.java @@ -2,9 +2,7 @@ package org.vadere.simulator.models.osm; import org.jetbrains.annotations.NotNull; import org.vadere.annotation.factories.models.ModelClass; -import org.vadere.simulator.control.factory.GroupSourceControllerFactory; -import org.vadere.simulator.control.factory.SingleSourceControllerFactory; -import org.vadere.simulator.control.factory.SourceControllerFactory; +import org.vadere.simulator.control.factory.*; import org.vadere.simulator.models.*; import org.vadere.simulator.models.groups.cgm.CentroidGroupModel; import org.vadere.simulator.models.groups.cgm.CentroidGroupPotential; @@ -291,16 +289,40 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { @Override public SourceControllerFactory getSourceControllerFactory() { - Optional opCentroidGroupModel = models.stream() - .filter(ac -> ac instanceof CentroidGroupModel) - .map(ac -> (CentroidGroupModel) ac).findAny(); - if (opCentroidGroupModel.isPresent()) { - return new GroupSourceControllerFactory(opCentroidGroupModel.get()); + Optional cgm = getCentroidGroupModel(); + if (cgm.isPresent()) { + return new GroupSourceControllerFactory(cgm.get()); } return new SingleSourceControllerFactory(); } + @Override + public TargetControllerFactory getTargetControllerFactory() { + Optional cgm = getCentroidGroupModel(); + if (cgm.isPresent()) { + return new GroupTargetControllerFactory(cgm.get()); + } + + return new SingleTargetControllerFactory(); + } + + @Override + public TargetChangerControllerFactory getTargetChangerControllerFactory() { + Optional cgm = getCentroidGroupModel(); + if (cgm.isPresent()) { + return new GroupTargetChangerControllerFactory(cgm.get()); + } + + return new SingleTargetChangerControllerFactory(); + } + + private Optional getCentroidGroupModel() { + return models.stream() + .filter(ac -> ac instanceof CentroidGroupModel) + .map(ac -> (CentroidGroupModel) ac).findAny(); + } + /** * Compares the time of the next possible move. */ diff --git a/VadereSimulator/tests/org/vadere/simulator/control/TargetChangerControllerTest.java b/VadereSimulator/tests/org/vadere/simulator/control/TargetChangerControllerTest.java index 816bd34c35df6dbbcd01134e37496047fac07f7d..9d0d5ec7faba77bf843f396652927fdd5c0a3973 100644 --- a/VadereSimulator/tests/org/vadere/simulator/control/TargetChangerControllerTest.java +++ b/VadereSimulator/tests/org/vadere/simulator/control/TargetChangerControllerTest.java @@ -3,6 +3,7 @@ package org.vadere.simulator.control; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.vadere.simulator.control.scenarioelements.SingleTargetChangerController; import org.vadere.simulator.control.scenarioelements.TargetChangerController; import org.vadere.simulator.control.scenarioelements.targetchanger.TargetChangerAlgorithm; import org.vadere.state.attributes.scenario.AttributesAgent; @@ -165,7 +166,7 @@ public class TargetChangerControllerTest { int seed = 0; Random random = new Random(seed); - return new TargetChangerController(topography, targetChanger, random); + return new SingleTargetChangerController(topography, targetChanger, random); } @After @@ -699,7 +700,7 @@ public class TargetChangerControllerTest { attributesTargetChanger.setProbabilitiesToChangeTarget(probability); TargetChanger targetChanger = new TargetChanger(attributesTargetChanger); - TargetChangerController controllerUnderTest = new TargetChangerController(topography, targetChanger, new Random(0)); + TargetChangerController controllerUnderTest = new SingleTargetChangerController(topography, targetChanger, new Random(0)); assertEquals(peds.get(0).getNextTargetId(), 1); assertEquals(peds.get(1).getNextTargetId(), 1); diff --git a/VadereState/src/org/vadere/state/scenario/Agent.java b/VadereState/src/org/vadere/state/scenario/Agent.java index e2e0b78833f5692fc80de63b2a9b433595bbdf43..6d6886b046f39ba434bd136413823e944e29f814 100644 --- a/VadereState/src/org/vadere/state/scenario/Agent.java +++ b/VadereState/src/org/vadere/state/scenario/Agent.java @@ -286,25 +286,4 @@ public abstract class Agent extends DynamicElement { this.followers = followers; } - // TODO [priority=high] [task=deprecation] removing the target from the list is deprecated, but still very frequently used everywhere. - public void checkNextTarget(double nextSpeed) { - final int nextTargetListIndex = this.getNextTargetListIndex(); - - // Deprecated target list usage - if (nextTargetListIndex <= -1 && !this.getTargets().isEmpty()) { - this.getTargets().removeFirst(); - } - - // The right way (later this first check should not be necessary anymore): - if (this.hasNextTarget()) { - this.incrementNextTargetListIndex(); - } - - // set a new desired speed, if possible. you can model street networks with differing - // maximal speeds with this. - if (nextSpeed >= 0) { - this.setFreeFlowSpeed(nextSpeed); - } - } - }