Skip to content

Commit

Permalink
Parallelize DynActivityEngine.doSimStep()
Browse files Browse the repository at this point in the history
  • Loading branch information
marecabo committed Jun 8, 2024
1 parent 4f5bbf1 commit f15f48f
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package org.matsim.contrib.dynagent.run;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
Expand Down Expand Up @@ -56,25 +56,31 @@ public void doSimStep(double time) {
dynAgents.addAll(newDynAgents);
newDynAgents.clear();

Iterator<DynAgent> dynAgentIter = dynAgents.iterator();
while (dynAgentIter.hasNext()) {
DynAgent agent = dynAgentIter.next();
Preconditions.checkState(agent.getState() == State.ACTIVITY);
agent.doSimStep(time);
// ask agents about the current activity end time;
double currentEndTime = agent.getActivityEndTime();

if (currentEndTime == Double.POSITIVE_INFINITY) { // agent says: stop simulating me
unregisterAgentAtActivityLocation(agent);
internalInterface.getMobsim().getAgentCounter().decLiving();
dynAgentIter.remove();
} else if (currentEndTime <= time) { // the agent wants to end the activity NOW
unregisterAgentAtActivityLocation(agent);
agent.endActivityAndComputeNextState(time);
internalInterface.arrangeNextAgentState(agent);
dynAgentIter.remove();
}
}
List<DynAgent> agentsToRemove = dynAgents.parallelStream()
.map(agent -> {

Preconditions.checkState(agent.getState() == State.ACTIVITY);
agent.doSimStep(time);
// ask agents about the current activity end time;
double currentEndTime = agent.getActivityEndTime();

if (currentEndTime == Double.POSITIVE_INFINITY) { // agent says: stop simulating me
unregisterAgentAtActivityLocation(agent);
internalInterface.getMobsim().getAgentCounter().decLiving();
return agent;
} else if (currentEndTime <= time) { // the agent wants to end the activity NOW
unregisterAgentAtActivityLocation(agent);
agent.endActivityAndComputeNextState(time);
internalInterface.arrangeNextAgentState(agent);
return agent;
}

return null;
})
.filter(Objects::nonNull)
.toList();

dynAgents.removeAll(agentsToRemove);
}

@Override
Expand Down

0 comments on commit f15f48f

Please sign in to comment.