MATSIM
Classes | Functions
Package tutorial.programming.withinDayReplanningFromPlans

Classes

class  MyWithinDayMobsimListener
 
class  RunWithinDayReplanningFromPlansExample
 

Functions

static List< MobsimAgentgetAgentsToReplan (Netsim mobsim)
 
boolean doReplanning (MobsimAgent agent, Netsim mobsim)
 

Detailed Description

Definition: Withinday replanning means that agents can replan while they are on their way, technically while the mobsim is running.

At this point, there are at least two approaches:

In the end, each approach will be able to emulate the other one, so it may come down to a matter of taste.

This package contains the first variant. The other one is described in another tutorial package.

This package contains (as of dec'10):

Please address yourself to Christoph Dobler for support.

Function Documentation

static List<MobsimAgent> tutorial.programming.withinDayReplanningFromPlans.getAgentsToReplan ( Netsim  mobsim)
staticprivate

Definition at line 92 of file MyWithinDayMobsimListener.java.

References org.matsim.core.mobsim.qsim.interfaces.NetsimNetwork.getNetsimLinks(), org.matsim.core.mobsim.qsim.interfaces.Netsim.getNetsimNetwork(), org.matsim.core.mobsim.qsim.interfaces.Netsim.getSimTimer(), and org.matsim.core.mobsim.framework.MobsimTimer.getTimeOfDay().

92  {
93 
94  List<MobsimAgent> set = new ArrayList<MobsimAgent>();
95 
96  // don't do anything for most time steps:
97  if (Math.floor(mobsim.getSimTimer().getTimeOfDay()) != 22000.0) {
98  return set;
99  }
100 
101  // find agents that are en-route (more interesting case)
102  for (NetsimLink link:mobsim.getNetsimNetwork().getNetsimLinks().values()){
103  for (MobsimVehicle vehicle : link.getAllNonParkedVehicles()) {
104  MobsimDriverAgent agent=vehicle.getDriver();
105  System.out.println(agent.getId());
106  if ( true ) { // some condition ...
107  System.out.println("found agent");
108  set.add(agent);
109  }
110  }
111  }
112 
113  return set;
114 
115  }

Here is the call graph for this function:

boolean tutorial.programming.withinDayReplanningFromPlans.doReplanning ( MobsimAgent  agent,
Netsim  mobsim 
)
private

Definition at line 117 of file MyWithinDayMobsimListener.java.

References org.matsim.api.core.v01.TransportMode.car, org.matsim.api.core.v01.population.PopulationFactory.createActivityFromLinkId(), org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils.getCurrentPlanElement(), org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils.getCurrentPlanElementIndex(), org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils.getCurrentRouteLinkIdIndex(), org.matsim.api.core.v01.population.Route.getEndLinkId(), org.matsim.api.core.v01.population.Population.getFactory(), org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils.getModifiablePlan(), org.matsim.api.core.v01.population.Plan.getPlanElements(), org.matsim.api.core.v01.Scenario.getPopulation(), org.matsim.api.core.v01.population.Leg.getRoute(), org.matsim.core.mobsim.qsim.interfaces.Netsim.getScenario(), org.matsim.core.mobsim.qsim.interfaces.Netsim.getSimTimer(), org.matsim.core.mobsim.framework.MobsimTimer.getTimeOfDay(), org.matsim.withinday.utils.EditRoutes.relocateCurrentLegRoute(), org.matsim.withinday.utils.EditRoutes.relocateFutureLegRoute(), org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils.resetCaches(), and org.matsim.api.core.v01.population.Activity.setMaximumDuration().

117  {
118  double now = mobsim.getSimTimer().getTimeOfDay() ;
119 
120  Plan plan = WithinDayAgentUtils.getModifiablePlan( agent ) ;
121 
122  if (plan == null) {
123  log.info( " we don't have a modifiable plan; returning ... ") ;
124  return false;
125  }
126  if ( !(WithinDayAgentUtils.getCurrentPlanElement(agent) instanceof Leg) ) {
127  log.info( "agent not on leg; returning ... ") ;
128  return false ;
129  }
130  if (!((Leg) WithinDayAgentUtils.getCurrentPlanElement(agent)).getMode().equals(TransportMode.car)) {
131  log.info( "not a car leg; can only replan car legs; returning ... ") ;
132  return false;
133  }
134 
135  List<PlanElement> planElements = plan.getPlanElements() ;
136  final Integer planElementsIndex = WithinDayAgentUtils.getCurrentPlanElementIndex(agent);
137 
138  if ( !(planElements.get(planElementsIndex+1) instanceof Activity || !(planElements.get(planElementsIndex+2) instanceof Leg)) ) {
139  log.error( "this version of withinday replanning cannot deal with plans where legs and acts do not alternate; returning ...") ;
140  return false ;
141  }
142 
143  // now the real work begins. This, as an example, changes the activity (i.e. the destination of the current leg) and then
144  // re-splices the plan
145 
146  Id<Link> newDestinationLinkId = Id.create("22", Link.class) ;
147  Activity newAct = mobsim.getScenario().getPopulation().getFactory().createActivityFromLinkId("w", newDestinationLinkId ) ;
148  newAct.setMaximumDuration(3600);
149 
150  planElements.set( planElementsIndex+1, newAct );
151 
152  // =============================================================================================================
153  // =============================================================================================================
154  // EditRoutes at this point only works for car routes
155 
156  TravelTime travelTime = travelTimes.get( TransportMode.car ) ;
157 
158  TravelDisutility travelDisutility = travelDisutilityFactories.get( TransportMode.car ).createTravelDisutility(travelTime) ;
159 
160  Network network = scenario.getNetwork() ;
161 
162  LeastCostPathCalculator pathCalculator = pathCalculatorFactory.createPathCalculator(network, travelDisutility, travelTime ) ;
163 
164  EditRoutes editRoutes = new EditRoutes( scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory() ) ;
165 
166  // new Route for current Leg.
167  final Leg leg = (Leg) plan.getPlanElements().get(planElementsIndex);
168  final Person person = ((HasPerson) agent).getPerson();
169  final Integer linkIdx = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(agent);
170 
171  editRoutes.relocateCurrentLegRoute(leg, person, linkIdx, newDestinationLinkId, now) ;
172 
173  // the route _from_ the modified activity also needs to be replanned:
174  Leg futureLeg = (Leg) plan.getPlanElements().get(planElementsIndex + 2);
175  EditRoutes.relocateFutureLegRoute(futureLeg, newDestinationLinkId, futureLeg.getRoute().getEndLinkId(), person,
176  scenario.getNetwork(), tripRouter);
177 
178  // =============================================================================================================
179  // =============================================================================================================
180 
181  // finally reset the cached Values of the PersonAgent - they may have changed!
182  WithinDayAgentUtils.resetCaches(agent);
183 
184  return true;
185  }

Here is the call graph for this function: