@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; }
/** * Re-plans a future route. The route is given by its leg. It is expected that the * leg's route is not null and that the start- and end link Ids are set properly. * * If the start- and or end-location of the leg have changed, use relocateFutureLegRoute(...)! * * @return true when replacing the route worked, false when something went wrong */ public boolean replanFutureLegRoute(Leg leg, Person person ) { // just a pointer to that other method, but this one (which does not change the destination) is still ok also with access/egress // routing return relocateFutureLegRoute( leg, leg.getRoute().getStartLinkId(), leg.getRoute().getEndLinkId(), person ) ; }
List<Id<Link>> oldLinkIds = getRouteLinkIds(oldRoute); Id<Link> currentLinkId = oldLinkIds.get(currentLinkIndex); Path path = this.pathCalculator.calcLeastCostPath(startLink.getToNode(), endLink.getFromNode(), time, person, vehicle) ; spliceNewPathIntoOldRoute(currentLinkIndex, toLinkId, oldRoute, NetworkUtils.getLinkIds(path.links), currentLinkId) ;
EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory() ); assertEquals(true, ed.relocateFutureLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), activityH1.getLinkId(), activityW1.getLinkId(), plan.getPerson() )); assertEquals(true, ed.relocateFutureLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), activityW1.getLinkId(), activityH2.getLinkId(), plan.getPerson() )); legWH.setRoute(null); legWH.setMode(TransportMode.walk); assertEquals(true, ed.relocateFutureLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), activityH1.getLinkId(), activityW1.getLinkId(), plan.getPerson() )); assertEquals(true, ed.relocateFutureLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), activityW1.getLinkId(), activityH2.getLinkId(), plan.getPerson() ));
EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory() ); assertEquals(true, ed.replanFutureLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson() ) ); // 1-->3 assertEquals(true, ed.replanFutureLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson() ) ); // 3-->9
@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
@Inject Replanner(QSim qSim2, TripRouter tripRouter, Map<String,TravelTime> travelTimes ) { Scenario scenario = qSim2.getScenario(); this.travelTimes = travelTimes ; { TravelTime travelTime = TravelTimeUtils.createFreeSpeedTravelTime(); TravelDisutility travelDisutility = TravelDisutilityUtils.createFreespeedTravelTimeAndDisutility(scenario.getConfig().planCalcScore()); LeastCostPathCalculator pathCalculator = new FastAStarLandmarksFactory().createPathCalculator(scenario.getNetwork(), travelDisutility, travelTime); this.editRoutes = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); } this.editTrips = new EditTrips(tripRouter, qSim2.getScenario() ) ; this.editPlans = new EditPlans(qSim2, tripRouter, editTrips, scenario.getPopulation().getFactory() ) ; }
/** * We create a new Plan which contains only the Leg that should be replanned and its previous and next * Activities. By doing so the PlanAlgorithm will only change the Route of that Leg. * * Use currentNodeIndex from a DriverAgent if possible! * * Otherwise code it as following: * startLink - Node1 - routeLink1 - Node2 - routeLink2 - Node3 - endLink * The currentNodeIndex has to Point to the next Node * (which is the endNode of the current Link) */ public boolean replanCurrentLegRoute(Leg leg, Person person, int currentLinkIndex, double time ) { // just a pointer to that other method, but this one (which does not change the destination) is still ok also with access/egress // routing Route route = leg.getRoute(); // if the route type is not supported: if (!(route instanceof NetworkRoute)) { log.warn( "route not instance of NetworkRoute"); return false; } // (cannot move the above test down into relocateCurrentLegRoute, since it also hedges against route==null. kai, nov'17) return relocateCurrentLegRoute(leg, person, currentLinkIndex, route.getEndLinkId(), time ); }
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) {
LeastCostPathCalculator.Path result = this.replanner.editRoutes(RoutingMode.carFreespeed).getPathCalculator().calcLeastCostPath( currentLink.getFromNode(), destLink.getFromNode(), now, null, null );
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); }
EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), -1, 8.0*3600 ); ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 100, 8.0*3600 ); fail("expected ArrayIndexOutOfBoundsException."); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 0, 8.0*3600 )); // HW, start Link assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 1, 8.0*3600 )); // HW, en-route assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 2, 8.0*3600 )); // HW, end Link assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 0, 8.0*3600 )); // WH, start Link assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 1, 8.0*3600 )); // WH, en-route assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 2, 8.0*3600 )); // WH, en-route assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 3, 8.0*3600 )); // WH, en-route assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 4, 8.0*3600 )); // WH, end Link
public void testReplanCurrentLegRouteTen() { // expect EditRoutes to return false if the Route in the leg is not a NetworkRoute createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Leg legHW = (Leg) plan.getPlanElements().get(firstCarLeg); Leg legWH = (Leg) plan.getPlanElements().get(scndCarLeg); legHW.setRoute(null); legHW.setMode(TransportMode.walk); legWH.setRoute(null); legWH.setMode(TransportMode.walk); assertEquals(false, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 0, 8.0*3600 )); assertEquals(false, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 0, 8.0*3600 )); }
public void testReplanCurrentLegRouteSeven() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityH2; activityH2 = (Activity) plan.getPlanElements().get(4); activityH2.setLinkId(Id.create("l4", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 2, 8.0*3600 )); // WH, en-route assertEquals(true, checkRouteValidity((NetworkRoute)((Leg)plan.getPlanElements().get(scndCarLeg)).getRoute())); } public void testReplanCurrentLegRouteEight()
public void testReplanCurrentLegRouteSix() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityH2; activityH2 = (Activity) plan.getPlanElements().get(4); activityH2.setLinkId(Id.create("l4", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 1, 8.0*3600 )); // WH, en-route assertEquals(true, checkRouteValidity((NetworkRoute)((Leg)plan.getPlanElements().get(scndCarLeg)).getRoute())); } public void testReplanCurrentLegRouteSeven()
public void testReplanCurrentLegRouteFour() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityW1; activityW1 = (Activity) plan.getPlanElements().get(2); activityW1.setLinkId(Id.create("l2", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 2, 8.0*3600 )); // HW, end Link final NetworkRoute route = (NetworkRoute)((Leg)plan.getPlanElements().get(firstCarLeg)).getRoute(); assertEquals(true, checkRouteValidity(route)); } public void testReplanCurrentLegRouteFive()
public void testReplanCurrentLegRouteFive() { // create new routes for WH-trip createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityH2; activityH2 = (Activity) plan.getPlanElements().get(4); activityH2.setLinkId(Id.create("l4", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 0, 8.0*3600 )); // WH, start Link assertEquals(true, checkRouteValidity((NetworkRoute)((Leg)plan.getPlanElements().get(scndCarLeg)).getRoute())); } public void testReplanCurrentLegRouteSix()
public void testReplanCurrentLegRouteEight() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityH2; activityH2 = (Activity) plan.getPlanElements().get(4); activityH2.setLinkId(Id.create("l4", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 3, 8.0*3600 ) ); // WH, en-route assertEquals(true, checkRouteValidity((NetworkRoute)((Leg)plan.getPlanElements().get(scndCarLeg)).getRoute())); } public void testReplanCurrentLegRouteNine()
public void testReplanCurrentLegRouteNine() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityH2; activityH2 = (Activity) plan.getPlanElements().get(4); activityH2.setLinkId(Id.create("l4", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(scndCarLeg), plan.getPerson(), 4, 8.0*3600 )); // WH, end Link assertEquals(true, checkRouteValidity((NetworkRoute)((Leg)plan.getPlanElements().get(scndCarLeg)).getRoute())); } public void testReplanCurrentLegRouteTen()
public void testReplanCurrentLegRouteThree() { createScenario(); // reset scenario EditRoutes ed = new EditRoutes(scenario.getNetwork(), pathCalculator, scenario.getPopulation().getFactory()); int firstCarLeg = 1 ; // 1-->3 int scndCarLeg = 3 ; // 3-->9 if ( scenario.getConfig().plansCalcRoute().isInsertingAccessEgressWalk() ) { firstCarLeg = 3 ; // 1-->3 scndCarLeg = 9 ; // 3-->9 } Activity activityW1; activityW1 = (Activity) plan.getPlanElements().get(2); activityW1.setLinkId(Id.create("l2", Link.class)); // move Activity location assertEquals(true, ed.replanCurrentLegRoute((Leg) plan.getPlanElements().get(firstCarLeg), plan.getPerson(), 1, 8.0*3600 ) ); // HW, en-route final NetworkRoute route = (NetworkRoute)((Leg)plan.getPlanElements().get(firstCarLeg)).getRoute(); assertEquals(true, checkRouteValidity(route)); } public void testReplanCurrentLegRouteFour()