21 package org.matsim.withinday.replanning.parallel;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
36 import java.util.HashMap;
38 import java.util.Map.Entry;
39 import java.util.Queue;
40 import java.util.TreeMap;
41 import java.util.concurrent.BrokenBarrierException;
42 import java.util.concurrent.CyclicBarrier;
72 protected Map<Id<WithinDayReplanner>, Queue<ReplanningTask>>
replanningTasks =
new TreeMap<>();
80 counter =
new Counter(counterText);
92 this.timeStepStartBarrier = barrier;
96 this.betweenReplannerBarrier = barrier;
100 this.timeStepEndBarrier = barrier;
105 queue.add(replanningTask);
109 this.withinDayReplanners.put(withinDayReplanner.
getId(), withinDayReplanner);
110 this.replanningTasks.put(withinDayReplanner.
getId(), queue);
114 this.withinDayReplanners.remove(replannerId);
115 this.replanningTasks.remove(replannerId);
119 this.counter.
reset();
121 withinDayReplanner.reset();
126 this.simulationRunning =
true;
130 this.simulationRunning =
false;
137 private void doReplanning() throws InterruptedException, BrokenBarrierException {
142 Queue<ReplanningTask> queue = entry.getValue();
146 if (withinDayReplannerId == null) {
147 log.error(
"WithinDayReplanner Id is null!");
149 }
else if (withinDayReplanner == null) {
150 log.error(
"WithinDayReplanner is null!");
155 withinDayReplanner.
setTime(time);
159 replanningTask = queue.poll();
162 if (replanningTask == null)
break;
166 if (withinDayAgent == null) {
167 log.error(
"WithinDayAgent is null!");
171 boolean replanningSuccessful = withinDayReplanner.
doReplanning(withinDayAgent);
173 if (!replanningSuccessful) {
174 log.error(
"Replanning was not successful! Replanner " + withinDayReplanner.getClass().toString() +
181 if (eventsManager != null) {
183 withinDayReplanner.getClass().getSimpleName());
195 this.betweenReplannerBarrier.await();
201 while (simulationRunning) {
212 timeStepEndBarrier.await();
214 timeStepStartBarrier.await();
216 if (!simulationRunning) {
222 }
catch (InterruptedException | BrokenBarrierException e) {
final void removeWithinDayReplanner(Id< WithinDayReplanner > replannerId)
final Id< WithinDayReplanner > getId()
abstract boolean doReplanning(MobsimAgent withinDayAgent)
final void setEventsManager(EventsManager eventsManager)
Map< Id< WithinDayReplanner >, Queue< ReplanningTask > > replanningTasks
CyclicBarrier timeStepStartBarrier
ReplanningRunnable(String counterText)
void processEvent(final Event event)
CyclicBarrier timeStepEndBarrier
final void resetReplanners()
final void setBetweenReplannerBarrier(CyclicBarrier barrier)
static final String writeTime(final double seconds, final String timeformat)
volatile boolean simulationRunning
final void setCyclicTimeStepStartBarrier(CyclicBarrier barrier)
final void setCyclicTimeStepEndBarrier(CyclicBarrier barrier)
final void addReplanningTask(ReplanningTask replanningTask)
CyclicBarrier betweenReplannerBarrier
Map< Id< WithinDayReplanner >, WithinDayReplanner<? extends AgentSelector > > withinDayReplanners
final void setTime(double time)
final void addWithinDayReplanner(WithinDayReplanner<? extends AgentSelector > withinDayReplanner, Queue< ReplanningTask > queue)
EventsManager eventsManager
static final void printCurrentThreadCpuTime()
final void setTime(double time)