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); }
@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 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 ) {
public static Plan printPlan(MobsimAgent agent1) { final Plan plan = getModifiablePlan(agent1); return printPlan(plan) ; }
public static Activity findNextActivityWithType( MobsimAgent agent, String type ) { int index = indexOfNextActivityWithType( agent, type ) ; return (Activity) getModifiablePlan(agent).getPlanElements().get(index) ; }
public void flushEverythingBeyondCurrent(MobsimAgent agent) { List<PlanElement> pes = WithinDayAgentUtils.getModifiablePlan(agent).getPlanElements() ; Integer index = WithinDayAgentUtils.getCurrentPlanElementIndex(agent) ; for ( int ii=pes.size()-1 ; ii>index ; ii-- ) { pes.remove(ii) ; } } public void rescheduleCurrentActivityEndtime(MobsimAgent agent, double newEndTime) {
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; } }
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); }
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,
private void printPlan(String str ,MobsimAgent agent1) { Plan plan = WithinDayAgentUtils.getModifiablePlan(agent1) ; log.debug(str + plan ); ; for ( PlanElement pe : plan.getPlanElements() ) { log.debug(" " + pe ); ; } } }
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) {
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 String getModeOfCurrentOrNextTrip(MobsimAgent agent) { Trip trip ; if ( isAtRealActivity( agent ) ) { Activity activity = (Activity) WithinDayAgentUtils.getCurrentPlanElement(agent) ; trip = editTrips.findTripAfterActivity(WithinDayAgentUtils.getModifiablePlan(agent), activity) ; } else { trip = editTrips.findCurrentTrip(agent) ; } return tripRouter.getMainModeIdentifier().identifyMainMode(trip.getTripElements()) ; } public void flushEverythingBeyondCurrent(MobsimAgent agent) {
public final Activity replaceActivity(MobsimAgent agent, int index, Activity newAct, String upstreamMode, String downstreamMode ) { System.err.println("here310"); WithinDayAgentUtils.printPlan(agent) ; System.err.println("here320"); Plan plan = WithinDayAgentUtils.getModifiablePlan(agent) ; WithinDayAgentUtils.printPlan(plan) ; System.err.println("here330"); WithinDayAgentUtils.printPlan(plan) ; WithinDayAgentUtils.resetCaches(agent); this.mobsim.rescheduleActivityEnd(agent); return origAct ;
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) {
public static Integer getCurrentPlanElementIndex( MobsimAgent agent ) { return WithinDayAgentUtils.getCurrentPlanElementIndex( agent ) ; } }
public static Leg getModifiableCurrentLeg(MobsimAgent agent) { PlanElement currentPlanElement = getCurrentPlanElement(agent); if (!(currentPlanElement instanceof Leg)) { return null; } return (Leg) currentPlanElement; }
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 static PlanElement getCurrentPlanElement(MobsimAgent agent) { return getModifiablePlan(agent).getPlanElements().get(getCurrentPlanElementIndex(agent)); }
@Override public boolean doReplanning(MobsimAgent withinDayAgent) { this.planRouter.run(WithinDayAgentUtils.getModifiablePlan(withinDayAgent)); return true; } }