@Override public Id<Link> getDestinationLinkId() { return route.getEndLinkId(); }
@Override public final Id<Link> getDestinationLinkId() { return this.getCurrentLeg().getRoute().getEndLinkId() ; } @Override
@Override public Id<Link> getDestinationLinkId() { return getCurrentLeg().getRoute().getEndLinkId(); }
@Override public void run(final Plan plan) { for (PlanElement pe : plan.getPlanElements()) { if (pe instanceof Leg) { Leg leg = (Leg) pe; Route route = leg.getRoute(); if (route != null) { double dist = RouteUtils.calcDistanceExcludingStartEndLink((NetworkRoute) route, this.network); if (route.getEndLinkId() != null && route.getStartLinkId() != route.getEndLinkId()) { dist += this.network.getLinks().get(route.getEndLinkId()).getLength(); } this.sumLength += dist; this.cntTrips++; } } } }
/** * 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 ); }
out.write("\""); out.write(" end_link=\""); out.write(route.getEndLinkId().toString()); out.write("\""); out.write(" trav_time=\"");
public GenericRouteSerializable(Route route) { distance = route.getDistance(); endLinkIdString = route.getEndLinkId().toString(); routeDescription = route.getRouteDescription(); startLinkIdString = route.getStartLinkId().toString(); travelTime = route.getTravelTime(); }
out.write("\""); out.write(" end_link=\""); out.write(route.getEndLinkId().toString()); out.write("\""); out.write(" trav_time=\"");
private static List<Leg> extractDriverSubTrip( final Id originId, final Id destinationId, final List<Leg> driverTrip) { final ArrayList<Leg> subTrip = new ArrayList<Leg>(); boolean inSubTrip = false; for ( final Leg l : driverTrip ) { if (l.getRoute().getStartLinkId().equals( originId )) inSubTrip = true; if (inSubTrip) subTrip.add( l ); if (l.getRoute().getEndLinkId().equals( destinationId )) break; } return subTrip; }
/** * 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 ) ; }
@Override public void handleEvent(PersonEntersVehicleEvent event) { Double startWaitingTime = agentsWaitingData.get(event.getPersonId()); if(startWaitingTime!=null) { int legs = 0, currentLeg = agentsCurrentLeg.get(event.getPersonId()); PLAN_ELEMENTS: for(PlanElement planElement:population.getPersons().get(event.getPersonId()).getSelectedPlan().getPlanElements()) if(planElement instanceof Leg) { if(currentLeg==legs) { Route route = (((Leg)planElement).getRoute()); ExperimentalTransitRoute eRoute = (ExperimentalTransitRoute) new ExperimentalTransitRouteFactory().createRoute(route.getStartLinkId(), route.getEndLinkId()); eRoute.setStartLinkId(route.getStartLinkId()); eRoute.setEndLinkId(route.getEndLinkId()); eRoute.setRouteDescription(route.getRouteDescription()); WaitTimeData data = waitTimes.get(new Tuple<Id<TransitLine>, Id<TransitRoute>>(eRoute.getLineId(), eRoute.getRouteId())).get(eRoute.getAccessStopId()); data.addWaitTime((int) (startWaitingTime/timeSlot), event.getTime()-startWaitingTime); agentsWaitingData.remove(event.getPersonId()); break PLAN_ELEMENTS; } else legs++; } } }
@Override public void handleEvent(PersonStuckEvent event) { Double startWaitingTime = agentsWaitingData.get(event.getPersonId()); if(startWaitingTime!=null) { int legs = 0, currentLeg = agentsCurrentLeg.get(event.getPersonId()); PLAN_ELEMENTS: for(PlanElement planElement:population.getPersons().get(event.getPersonId()).getSelectedPlan().getPlanElements()) if(planElement instanceof Leg) { if(currentLeg==legs) { Route route = ((Leg)planElement).getRoute(); ExperimentalTransitRoute eRoute = (ExperimentalTransitRoute) new ExperimentalTransitRouteFactory().createRoute(route.getStartLinkId(), route.getEndLinkId()); eRoute.setStartLinkId(route.getStartLinkId()); eRoute.setEndLinkId(route.getEndLinkId()); eRoute.setRouteDescription(route.getRouteDescription()); WaitTimeData data = waitTimes.get(new Tuple<Id<TransitLine>, Id<TransitRoute>>(eRoute.getLineId(), eRoute.getRouteId())).get(eRoute.getAccessStopId()); if(data!=null) data.addWaitTime((int) (startWaitingTime/timeSlot), event.getTime()-startWaitingTime); agentsWaitingData.remove(event.getPersonId()); break PLAN_ELEMENTS; } else legs++; } } }
@Override public void handleLeg(Leg leg) { if(leg.getRoute() instanceof NetworkRoute){ NetworkRoute nRoute = (NetworkRoute) leg.getRoute(); Id vehicleId = nRoute.getVehicleId(); CarrierVehicle vehicle = getVehicle(vehicleId); if(vehicle == null) throw new IllegalStateException("vehicle with id " + vehicleId + " is missing"); if(!employedVehicles.contains(vehicle)){ employedVehicles.add(vehicle); } double distance = 0.0; double toll = 0.; if(leg.getRoute() instanceof NetworkRoute){ Link startLink = network.getLinks().get(leg.getRoute().getStartLinkId()); distance += startLink.getLength(); for(Id linkId : ((NetworkRoute) leg.getRoute()).getLinkIds()){ distance += network.getLinks().get(linkId).getLength(); } distance += network.getLinks().get(leg.getRoute().getEndLinkId()).getLength(); } double distanceCosts = distance*getDistanceParameter(vehicle); assert distanceCosts >= 0.0 : "distanceCosts must be positive"; score += (-1) * distanceCosts; double timeCosts = leg.getTravelTime()*getTimeParameter(vehicle); assert timeCosts >= 0.0 : "timeCosts must be positive"; score += (-1) * timeCosts; } }
/** * TODO Note that in MATSim leg departure times may be meaningless; the only thing that truly matters is the * activity end time. kai, jul'14 */ @Override public void notifyMobsimInitialized(@SuppressWarnings("rawtypes") MobsimInitializedEvent e) { Collection<MobsimAgent> agents = ((QSim)e.getQueueSimulation()).getAgents().values(); String mode = passengerEngine.getMode(); for (MobsimAgent mobsimAgent : agents) { if (mobsimAgent instanceof PlanAgent) { Plan plan = ((PlanAgent)mobsimAgent).getCurrentPlan(); for (PlanElement elem : plan.getPlanElements()) { if (elem instanceof Leg) { Leg leg = (Leg)elem; if (leg.getMode().equals(mode)) { Id<Link> fromLinkId = leg.getRoute().getStartLinkId(); Id<Link> toLinkId = leg.getRoute().getEndLinkId(); double departureTime = leg.getDepartureTime(); passengerEngine.prebookTrip(0, (MobsimPassengerAgent)mobsimAgent, fromLinkId, toLinkId, departureTime); } } } } } } }
Id<Link> endLinkId = this.currRoute.getEndLinkId(); this.currRoute.setStartLinkId(startLinkId); this.currRoute.setEndLinkId(endLinkId); if (this.currRoute.getEndLinkId() != null) {
@Test public void testReadFile_v5_multipleSuccessiveLegsWithRoutes() { Scenario s = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Assert.assertEquals(0, s.getPopulation().getPersons().size()); new PopulationReader(s).readFile("test/input/org/matsim/core/utils/io/MatsimFileTypeGuesserTest/population_v5_multipleLegsWithRoutes.xml"); Assert.assertEquals(1, s.getPopulation().getPersons().size()); Person person = s.getPopulation().getPersons().get(Id.create(1, Person.class)); Assert.assertNotNull(person); Plan plan = person.getSelectedPlan(); List<PlanElement> planElements = plan.getPlanElements(); Assert.assertEquals(5, planElements.size()); Assert.assertTrue(planElements.get(0) instanceof Activity); Assert.assertTrue(planElements.get(1) instanceof Leg); Assert.assertEquals(Id.create("1", Link.class), ((Leg) planElements.get(1)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("2", Link.class), ((Leg) planElements.get(1)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(2) instanceof Leg); Assert.assertEquals(Id.create("2", Link.class), ((Leg) planElements.get(2)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("4", Link.class), ((Leg) planElements.get(2)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(3) instanceof Leg); Assert.assertEquals(Id.create("4", Link.class), ((Leg) planElements.get(3)).getRoute().getStartLinkId()); Assert.assertEquals(Id.create("6", Link.class), ((Leg) planElements.get(3)).getRoute().getEndLinkId()); Assert.assertTrue(planElements.get(4) instanceof Activity); }
/** * In rare cases, Dijkstra may choose to go along two walk links to get from one location to another. * Test, that still only one walk leg with the correct start and end points/links is returned. */ @Test public void testDoubleWalk() { WalkFixture f = new WalkFixture(); f.routerConfig.setMarginalUtilityOfTravelTimePt_utl_s(-1.0 / 3600.0 - 6.0/3600.0); f.routerConfig.setUtilityOfLineSwitch_utl(0.2); // must be relatively low in this example, otherwise it's cheaper to walk the whole distance... TransitRouter router = createTransitRouter(f.schedule, f.routerConfig, routerType); List<Leg> legs = router.calcRoute(new FakeFacility(f.coord1), new FakeFacility(f.coord7), 990, null); assertEquals(5, legs.size()); assertEquals(TransportMode.transit_walk, legs.get(0).getMode()); assertEquals(TransportMode.pt, legs.get(1).getMode()); assertEquals(f.stop1.getId(), ((ExperimentalTransitRoute) legs.get(1).getRoute()).getAccessStopId()); assertEquals(f.stop2.getId(), ((ExperimentalTransitRoute) legs.get(1).getRoute()).getEgressStopId()); assertEquals(f.stop1.getLinkId(), legs.get(1).getRoute().getStartLinkId()); assertEquals(f.stop2.getLinkId(), legs.get(1).getRoute().getEndLinkId()); assertEquals(TransportMode.transit_walk, legs.get(2).getMode()); assertEquals(TransportMode.pt, legs.get(3).getMode()); assertEquals(f.stop2.getLinkId(), legs.get(2).getRoute().getStartLinkId()); assertEquals(f.stop6.getLinkId(), legs.get(2).getRoute().getEndLinkId()); assertEquals(f.stop6.getId(), ((ExperimentalTransitRoute) legs.get(3).getRoute()).getAccessStopId()); assertEquals(f.stop7.getId(), ((ExperimentalTransitRoute) legs.get(3).getRoute()).getEgressStopId()); assertEquals(f.stop6.getLinkId(), legs.get(3).getRoute().getStartLinkId()); assertEquals(f.stop7.getLinkId(), legs.get(3).getRoute().getEndLinkId()); assertEquals(TransportMode.transit_walk, legs.get(4).getMode()); }
@Test public void testReadRoute_consequentLinks() throws SAXException, ParserConfigurationException, IOException { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Population population = scenario.getPopulation(); MatsimXmlParser parser = new PopulationReaderMatsimV5(scenario); XmlParserTestHelper tester = new XmlParserTestHelper(parser); tester.startTag("population"); tester.startTag("person", new String[][]{{"id", "1"}}); tester.startTag("plan", new String[][]{{"selected", "no"}}); tester.startTag("act", new String[][]{{"type", "h"}, {"x", "-25000"}, {"y", "0"}, {"link", "1"}, {"end_time", "06:00"}}); tester.endTag(); tester.startTag("leg", new String[][]{{"mode", "car"}}); tester.startTag("route", new String[][]{{"type", "links"}}); tester.endTag("1 2"); tester.endTag(); tester.startTag("act", new String[][]{{"type", "w"}, {"x", "10000"}, {"y", "0"}, {"link", "2"}, {"dur", "00:10"}}); tester.endTag(); tester.endTag(); tester.endTag(); tester.endTag(); Person person1 = population.getPersons().get(Id.create("1", Person.class)); Plan plan1 = person1.getPlans().get(0); Leg leg1a = (Leg) plan1.getPlanElements().get(1); Route route1a = leg1a.getRoute(); Assert.assertEquals("different startLink for first leg.", "1", route1a.getStartLinkId().toString()); Assert.assertEquals("different endLink for first leg.", "2", route1a.getEndLinkId().toString()); Assert.assertTrue(route1a instanceof NetworkRoute); NetworkRoute nr = (NetworkRoute) route1a; Assert.assertEquals(0, nr.getLinkIds().size()); }
@Test public void testReadRoute_sameLinkRoute() throws SAXException, ParserConfigurationException, IOException { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Population population = scenario.getPopulation(); MatsimXmlParser parser = new PopulationReaderMatsimV5(scenario); XmlParserTestHelper tester = new XmlParserTestHelper(parser); tester.startTag("population"); tester.startTag("person", new String[][]{{"id", "1"}}); tester.startTag("plan", new String[][]{{"selected", "no"}}); tester.startTag("act", new String[][]{{"type", "h"}, {"x", "-25000"}, {"y", "0"}, {"link", "1"}, {"end_time", "06:00"}}); tester.endTag(); tester.startTag("leg", new String[][]{{"mode", "car"}}); tester.startTag("route", new String[][]{{"type", "links"}}); tester.endTag("1"); tester.endTag(); tester.startTag("act", new String[][]{{"type", "w"}, {"x", "10000"}, {"y", "0"}, {"link", "1"}, {"dur", "00:10"}}); tester.endTag(); tester.endTag(); tester.endTag(); tester.endTag(); Person person1 = population.getPersons().get(Id.create("1", Person.class)); Plan plan1 = person1.getPlans().get(0); Leg leg1a = (Leg) plan1.getPlanElements().get(1); Route route1a = leg1a.getRoute(); Assert.assertEquals("different startLink for first leg.", "1", route1a.getStartLinkId().toString()); Assert.assertEquals("different endLink for first leg.", "1", route1a.getEndLinkId().toString()); Assert.assertTrue(route1a instanceof NetworkRoute); NetworkRoute nr = (NetworkRoute) route1a; Assert.assertEquals(0, nr.getLinkIds().size()); }
Assert.assertEquals("wrong end link", endLinkId, route2.getEndLinkId().toString()); Assert.assertEquals("wrong travel time", travTime, route2.getTravelTime(), 1e-9); Assert.assertEquals("wrong distance", dist, route2.getDistance(), 1e-9);