private void replanCurrentTripFromLeg(Activity newAct, final PlanElement currentPlanElement, final String routingMode, double now, MobsimAgent agent) { log.debug("entering replanCurrentTripFromLeg"); Leg currentLeg = (Leg) currentPlanElement ; if ( currentLeg.getRoute() instanceof NetworkRoute ) { replanCurrentLegWithNetworkRoute(newAct, routingMode, currentLeg, now, agent); } else { throw new ReplanningException("not implemented") ; // Does not feel so hard: // * with teleported legs, push forward to next facility // * with passenger legs, push forward to next possibility of exit } WithinDayAgentUtils.resetCaches(agent); } private void replanCurrentLegWithNetworkRoute(Activity newAct, String mainMode, Leg currentLeg, double now,
public final void rescheduleActivityEndtime( MobsimAgent agent, int index, double newEndTime ) { Activity activity = (Activity) WithinDayAgentUtils.getModifiablePlan(agent).getPlanElements().get(index) ; activity.setEndTime(newEndTime); WithinDayAgentUtils.resetCaches(agent); WithinDayAgentUtils.rescheduleActivityEnd(agent, mobsim); } public final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct, String upstreamMode, String downstreamMode ) {
private void replanCurrentLegWithNetworkRoute(Activity newAct, String mainMode, Leg currentLeg, double now, MobsimAgent agent) { log.debug("entering replanCurrentLegWithNetworkRoute") ; Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; List<PlanElement> planElements = plan.getPlanElements() ; Person person = plan.getPerson() ; // (1) get new trip from current position to new activity: List<? extends PlanElement> newTripElements = newTripToNewActivity(newAct, mainMode, now, agent, person, scenario); // (2) prune the new trip up to the current leg: pruneUpToCurrentLeg(currentLeg, newTripElements); // (2) modify current route: replaceRemainderOfCurrentRoute(currentLeg, newTripElements, agent); // (3) remove remainder of old trip after current leg in plan: int pos = WithinDayAgentUtils.getCurrentPlanElementIndex(agent) + 1 ; while ( !planElements.get(pos).equals(newAct) ) { planElements.remove(pos) ; } // (4) insert new trip after current leg: for ( int ijk = 1 ; ijk < newTripElements.size() ; ijk++ ) { planElements.add( pos, newTripElements.get(ijk) ) ; } WithinDayAgentUtils.resetCaches(agent); } private List<? extends PlanElement> newTripToNewActivity(Activity newAct, String mainMode, double now,
@Deprecated // yyyy but I don't have an easy replacement yet // (maybe just editTrips.replanCurrentTrip(...)?) protected final void reRouteCurrentLeg( MobsimAgent agent, double now ) { Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; PlanElement pe = plan.getPlanElements().get( WithinDayAgentUtils.getCurrentPlanElementIndex(agent)) ; if ( !(pe instanceof Leg) ) { return ; } int currentLinkIndex = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(agent) ; this.editRoutes.replanCurrentLegRoute((Leg)pe, ((HasPerson)agent).getPerson(), currentLinkIndex, now ) ; WithinDayAgentUtils.resetCaches(agent); } @Deprecated // I don't think that this is really needed for the bushfire applications. kai, nov'17
public final void replanCurrentLeg( MobsimAgent agent, double now ) { Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; PlanElement pe = plan.getPlanElements().get( WithinDayAgentUtils.getCurrentPlanElementIndex(agent)) ; if ( !(pe instanceof Leg) ) { return ; } int currentLinkIndex = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(agent) ; this.replanCurrentLegRoute((Leg)pe, ((HasPerson)agent).getPerson(), currentLinkIndex, now ) ; WithinDayAgentUtils.resetCaches(agent); }
public final boolean replanCurrentTrip(MobsimAgent agent, double now, String routingMode ) { log.debug("entering replanCurrentTrip with routingMode=" + routingMode) ; // I will treat that in the way that it will make the trip consistent with the activities. So if the activity in the // plan has changed, the trip will go to a new location. // what matters is the external interface, which is the method call. Everything below is internal so it does not matter so much. Trip trip = findCurrentTrip( agent ) ; final PlanElement currentPlanElement = WithinDayAgentUtils.getCurrentPlanElement(agent) ; final List<PlanElement> tripElements = trip.getTripElements(); int tripElementsIndex = tripElements.indexOf( currentPlanElement ) ; // final String currentMode = tripRouter.getMainModeIdentifier().identifyMainMode( tripElements ) ; if ( currentPlanElement instanceof Activity ) { // we are on a stage activity. Take it from there: replanCurrentTripFromStageActivity(tripElements, tripElementsIndex, agent, routingMode); } else { // we are on a leg replanCurrentTripFromLeg(trip.getDestinationActivity(), currentPlanElement, routingMode, now, agent); } WithinDayAgentUtils.resetCaches(agent); return true ; } private void replanCurrentTripFromLeg(Activity newAct, final PlanElement currentPlanElement, final String routingMode,
public boolean addActivityAtEnd(MobsimAgent agent, Activity activity, String routingMode) { log.debug("entering addActivityAtEnd with routingMode=" + routingMode) ; Plan plan = WithinDayAgentUtils.getModifiablePlan(agent); List<PlanElement> planElements = plan.getPlanElements(); boolean retVal1 = false; if (isAtRealActivity(agent)) { retVal1 = planElements.add(pf.createLeg(routingMode)); } final boolean retVal = planElements.add(activity); // (need the terminating activity in order to find the current trip. kai, nov'17) if (!isAtRealActivity(agent)) { retVal1 = editTrips.replanCurrentTrip(agent,mobsim.getSimTimer().getTimeOfDay(),routingMode); } WithinDayAgentUtils.resetCaches(agent); this.mobsim.rescheduleActivityEnd(agent); return (retVal1 && retVal); } public PlanElement removeActivity(MobsimAgent agent, int index, String mode) {
private static void replaceRemainderOfCurrentRoute(Leg currentLeg, List<? extends PlanElement> newTrip, MobsimAgent agent) { Leg newCurrentLeg = (Leg) newTrip.get(0) ; Gbl.assertNotNull(newCurrentLeg); // prune remaining route from current route: NetworkRoute oldNWRoute = (NetworkRoute) currentLeg.getRoute(); final Integer currentRouteLinkIdIndex = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(agent); final NetworkRoute newNWRoute = (NetworkRoute)newCurrentLeg.getRoute(); final List<Id<Link>> newLinksIds = newNWRoute.getLinkIds().subList(0,newNWRoute.getLinkIds().size()) ; EditRoutes.spliceNewPathIntoOldRoute(currentRouteLinkIdIndex, newNWRoute.getEndLinkId(), oldNWRoute, newLinksIds, agent.getCurrentLinkId()) ; // for (int ii = currentRouteLinkIdIndex; ii<oldNWRoute.getLinkIds().size() ; ii++ ) { // oldNWRoute.getLinkIds().remove(ii) ; // } // // // now add the new route (yyyyyy not sure if it starts with correct link) // oldNWRoute.getLinkIds().addAll( newNWRoute.getLinkIds() ) ; // // // also change the arrival link id: // oldNWRoute.setEndLinkId( newNWRoute.getEndLinkId() ) ; WithinDayAgentUtils.resetCaches(agent); } private static void pruneUpToCurrentLeg(Leg currentLeg, List<? extends PlanElement> newTrip) {
private boolean doReplanning(MobsimAgent mobsimAgent, double time, Mobsim mobsim) { PlanAgent planAgent = (PlanAgent) mobsimAgent; Id<Person> agentId = planAgent.getCurrentPlan().getPerson().getId(); PlanElement currentPlanElement = WithinDayAgentUtils.getCurrentPlanElement(mobsimAgent); if (currentPlanElement instanceof Activity) { Activity act = (Activity) currentPlanElement; if (encounteredActivities.contains(act)) { return false; } CarrierDriverAgent driver = carrierAgentTracker.getDriver(agentId); TourActivity plannedActivity = (TourActivity) driver.getPlannedTourElement(WithinDayAgentUtils.getCurrentPlanElementIndex(mobsimAgent)); if (plannedActivity instanceof Start){ encounteredActivities.add(act); return false; } else { double newEndTime = Math.max(time, plannedActivity.getTimeWindow().getStart()) + plannedActivity.getDuration(); // logger.info("[agentId="+ agentId + "][currentTime="+Time.writeTime(time)+"][actDuration="+plannedActivity.getDuration()+ // "[timeWindow="+ plannedActivity.getTimeWindow() + "][plannedActEnd="+ Time.writeTime(act.getEndTime()) + "][newActEnd="+Time.writeTime(newEndTime)+"]"); act.setMaximumDuration(Time.UNDEFINED_TIME); act.setEndTime(newEndTime); // WithinDayAgentUtils.calculateAndSetDepartureTime(mobsimAgent, act); WithinDayAgentUtils.resetCaches( mobsimAgent ); WithinDayAgentUtils.rescheduleActivityEnd(mobsimAgent,mobsim); encounteredActivities.add(act); return true ; } } return true; } }
@Override public boolean doReplanning(MobsimAgent withinDayAgent) { Plan executedPlan = WithinDayAgentUtils.getModifiablePlan(withinDayAgent); // If we don't have an executed plan if (executedPlan == null) return false; PlanElement currentPlanElement = WithinDayAgentUtils.getCurrentPlanElement(withinDayAgent); if (!(currentPlanElement instanceof Leg)) return false; Leg currentLeg = (Leg) currentPlanElement; int currentLinkIndex = WithinDayAgentUtils.getCurrentRouteLinkIdIndex(withinDayAgent); EditRoutes editRoutes = new EditRoutes( scenario.getNetwork(), pathCalculator, populationFactory ) ; editRoutes.replanCurrentLegRoute(currentLeg, executedPlan.getPerson(), currentLinkIndex, this.time ) ; // Finally reset the cached Values of the PersonAgent - they may have changed! WithinDayAgentUtils.resetCaches(withinDayAgent); return true; }
public PlanElement removeActivity(MobsimAgent agent, int index, String mode) { Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; List<PlanElement> planElements = plan.getPlanElements() ; checkIfNotInPastOrCurrent(agent, index); final Trip tripBefore = TripStructureUtils.findTripEndingAtActivity( (Activity) planElements.get(index),plan,tripRouter.getStageActivityTypes() ); final Trip tripAfter = TripStructureUtils.findTripStartingAtActivity( (Activity)planElements.get(index),plan,tripRouter.getStageActivityTypes() ); if ( mode==null ) { final String mainModeBefore = tripRouter.getMainModeIdentifier().identifyMainMode( tripBefore.getTripElements() ); final String mainModeAfter = tripRouter.getMainModeIdentifier().identifyMainMode( tripAfter.getTripElements() ); if ( mainModeBefore.equals( mainModeAfter ) ) { mode = mainModeBefore ; } else { throw new ReplanningException("mode not given and mode before removed activity != mode after removed activity; don't know which mode to use") ; } } PlanElement pe = planElements.remove(index) ; if ( checkIfTripHasAlreadyStarted( agent, tripBefore.getTripElements() ) ) { editTrips.replanCurrentTrip(agent, mobsim.getSimTimer().getTimeOfDay() , mode); } else { editTrips.insertEmptyTrip(plan, tripBefore.getOriginActivity(), tripAfter.getDestinationActivity(), mode ) ; } WithinDayAgentUtils.resetCaches(agent); this.mobsim.rescheduleActivityEnd(agent); return pe ; } public final void rescheduleActivityEndtime( MobsimAgent agent, int index, double newEndTime ) {
public void insertActivity(MobsimAgent agent, int index, Activity activity, String upstreamMode, String downstreamMode ) { Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; List<PlanElement> planElements = plan.getPlanElements() ; checkIfNotInPastOrCurrent(agent, index) ; planElements.add( index, activity ) ; { // activity before: Activity actBefore = findRealActBefore(agent, index); if ( actBefore != null ) { if ( WithinDayAgentUtils.indexOfPlanElement(agent, actBefore) < WithinDayAgentUtils.getCurrentPlanElementIndex(agent) ) { // we are already under way editTrips.replanCurrentTrip(agent, this.mobsim.getSimTimer().getTimeOfDay(), upstreamMode ); } else { // we are not yet under way; inserting empty trip: EditTrips.insertEmptyTrip(plan, actBefore, activity, upstreamMode, pf ) ; } } } { // activity after: Activity actAfter = findRealActAfter(agent, index); if ( actAfter != null ) { EditTrips.insertEmptyTrip(plan, activity, actAfter, downstreamMode, pf ) ; } } WithinDayAgentUtils.resetCaches(agent); this.mobsim.rescheduleActivityEnd(agent); }
WithinDayAgentUtils.resetCaches(agent); this.mobsim.rescheduleActivityEnd(agent); return origAct ;
Activity currentActivity = (Activity) WithinDayAgentUtils.getCurrentPlanElement(agent); currentActivity.setEndTime(e.getSimulationTime() + 60); WithinDayAgentUtils.resetCaches(agent); this.withinDayEngine.getActivityRescheduler().rescheduleActivityEnd(agent); ((QSim) e.getQueueSimulation()).getEventsManager().processEvent(new ReplanningEvent(e.getSimulationTime(), agent.getId(), "ActivityRescheduler")); WithinDayAgentUtils.resetCaches(agent); this.withinDayEngine.getActivityRescheduler().rescheduleActivityEnd(agent); ((QSim) e.getQueueSimulation()).getEventsManager().processEvent(new ReplanningEvent(e.getSimulationTime(), agent.getId(), "ActivityRescheduler"));