private boolean handleArrival( Id person_id, double time, double departureTime, List<Integer> indices, List<Leg> toLegs, List<Leg> fromLegs) { int count = legCounter.get(person_id); boolean handled = false; if (indices.contains(count + 1)) { Leg toLeg = (Leg) scenario .getPopulation() .getPersons() .get(person_id) .getSelectedPlan() .getPlanElements() .get(count); toLeg.setDepartureTime(departureTime); toLeg.setTravelTime(time - departureTime); toLegs.add(toLeg); handled = true; } else if (indices.contains(count - 1)) { Leg fromLeg = (Leg) scenario .getPopulation() .getPersons() .get(person_id) .getSelectedPlan() .getPlanElements() .get(count); fromLeg.setDepartureTime(departureTime); fromLeg.setTravelTime(time - departureTime); fromLegs.add(fromLeg); indices.remove((Object) (count - 1)); handled = true; } return handled; }
/** * Tests if the scoring function correctly handles {@link PersonMoneyEvent}. It generates one * person with one plan having two activities (home, work) and a car-leg in between. It then tests * the scoring function by calling several methods on an instance of the scoring function with the * aforementioned plan. */ @Test public void testAddMoney() { Fixture f = new Fixture(); // score the same plan twice PersonImpl person1 = new PersonImpl(Id.create(1, Person.class)); PlanImpl plan1 = person1.createAndAddPlan(true); Activity act1a = plan1.createAndAddActivity("home", (Id<Link>) null); // , 0, 7.0*3600, 7*3600, false); act1a.setEndTime(f.secondLegStartTime); Leg leg1 = plan1.createAndAddLeg(TransportMode.car); // , 7*3600, 100, 7*3600+100); leg1.setDepartureTime(f.secondLegStartTime); leg1.setTravelTime(f.secondLegTravelTime); Route route2 = new GenericRouteImpl(null, null); leg1.setRoute(route2); route2.setDistance(20000.0); Activity act1b = plan1.createAndAddActivity( "work", (Id<Link>) null); // , 7.0*3600+100, Time.UNDEFINED_TIME, Time.UNDEFINED_TIME, false); act1b.setStartTime(f.secondLegStartTime + f.secondLegTravelTime); ScoringFunction sf1 = getScoringFunctionInstance(f, person1); sf1.handleActivity(act1a); sf1.handleLeg(leg1); sf1.handleActivity(act1b); sf1.finish(); double score1 = sf1.getScore(); ScoringFunction sf2 = getScoringFunctionInstance(f, person1); sf2.handleActivity(act1a); sf2.addMoney(1.23); sf2.handleLeg(leg1); sf2.addMoney(-2.46); sf2.handleActivity(act1b); sf2.addMoney(4.86); sf2.addMoney(-0.28); sf2.finish(); double score2 = sf2.getScore(); assertEquals(1.23 - 2.46 + 4.86 - 0.28, score2 - score1, EPSILON); }
@Override public List<Leg> calcRoute( final Coord fromCoord, final Coord toCoord, final double departureTime, final Person person) { // find possible start stops Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNodes(person, fromCoord, departureTime); // find possible end stops Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNodes(person, toCoord, departureTime); // find routes between start and end stops Path p = this.dijkstra.calcLeastCostPath(wrappedFromNodes, wrappedToNodes, person); if (p == null) { return null; } double directWalkCost = CoordUtils.calcEuclideanDistance(fromCoord, toCoord) / this.config.getBeelineWalkSpeed() * (0 - this.config.getMarginalUtilityOfTravelTimeWalk_utl_s()); double pathCost = p.travelCost + wrappedFromNodes.get(p.nodes.get(0)).initialCost + wrappedToNodes.get(p.nodes.get(p.nodes.size() - 1)).initialCost; if (directWalkCost < pathCost) { List<Leg> legs = new ArrayList<Leg>(); Leg leg = new LegImpl(TransportMode.transit_walk); double walkDistance = CoordUtils.calcEuclideanDistance(fromCoord, toCoord); Route walkRoute = new GenericRouteImpl(null, null); walkRoute.setDistance(walkDistance); leg.setRoute(walkRoute); leg.setTravelTime(walkDistance / this.config.getBeelineWalkSpeed()); legs.add(leg); return legs; } return convertPathToLegList(departureTime, p, fromCoord, toCoord, person); }
public Fixture() { firstLegStartTime = 7 * 3600; firstLegTravelTime = 30 * 60; thirdLegTravelTime = 30 * 60; secondLegStartTime = 10 * 3600; secondLegTravelTime = 15 * 60; thirdLegStartTime = 13 * 3600; fourthLegStartTime = 16 * 3600; fourthLegTravelTime = 15 * 60; // home act end 7am // work 7:30 to 10:00 // work 10:15 to 13:00 // work 13:30 to 16:00 // home 15:15 to ... this.config = ConfigUtils.createConfig(); PlanCalcScoreConfigGroup scoring = this.config.planCalcScore(); scoring.setBrainExpBeta(2.0); scoring.setConstantCar(0.0); scoring.setConstantPt(0.0); scoring.setConstantWalk(0.0); scoring.setConstantBike(0.0); scoring.setEarlyDeparture_utils_hr(0.0); scoring.setLateArrival_utils_hr(0.0); scoring.setMarginalUtlOfWaiting_utils_hr(0.0); scoring.setPerforming_utils_hr(0.0); scoring.setTraveling_utils_hr(0.0); scoring.setTravelingPt_utils_hr(0.0); scoring.setTravelingWalk_utils_hr(0.0); scoring.setTravelingBike_utils_hr(0.0); scoring.setMarginalUtilityOfMoney(1.); scoring.setMonetaryDistanceCostRateCar(0.0); scoring.setMonetaryDistanceCostRatePt(0.0); // setup activity types h and w for scoring PlanCalcScoreConfigGroup.ActivityParams params = new PlanCalcScoreConfigGroup.ActivityParams("h"); params.setTypicalDuration(15 * 3600); scoring.addActivityParams(params); params = new PlanCalcScoreConfigGroup.ActivityParams("w"); params.setTypicalDuration(3 * 3600); scoring.addActivityParams(params); this.scenario = ScenarioUtils.createScenario(config); this.network = (NetworkImpl) this.scenario.getNetwork(); Node node1 = this.network.createAndAddNode(Id.create("1", Node.class), new CoordImpl(0.0, 0.0)); Node node2 = this.network.createAndAddNode(Id.create("2", Node.class), new CoordImpl(500.0, 0.0)); Node node3 = this.network.createAndAddNode(Id.create("3", Node.class), new CoordImpl(5500.0, 0.0)); Node node4 = this.network.createAndAddNode(Id.create("4", Node.class), new CoordImpl(6000.0, 0.0)); Node node5 = this.network.createAndAddNode(Id.create("5", Node.class), new CoordImpl(11000.0, 0.0)); Node node6 = this.network.createAndAddNode(Id.create("6", Node.class), new CoordImpl(11500.0, 0.0)); Node node7 = this.network.createAndAddNode(Id.create("7", Node.class), new CoordImpl(16500.0, 0.0)); Node node8 = this.network.createAndAddNode(Id.create("8", Node.class), new CoordImpl(17000.0, 0.0)); Node node9 = this.network.createAndAddNode(Id.create("9", Node.class), new CoordImpl(22000.0, 0.0)); Node node10 = this.network.createAndAddNode(Id.create("10", Node.class), new CoordImpl(22500.0, 0.0)); Link link1 = this.network.createAndAddLink(Id.create("1", Link.class), node1, node2, 500, 25, 3600, 1); Link link2 = this.network.createAndAddLink( Id.create("2", Link.class), node2, node3, 25000, 50, 3600, 1); Link link3 = this.network.createAndAddLink(Id.create("3", Link.class), node3, node4, 500, 25, 3600, 1); this.network.createAndAddLink(Id.create("4", Link.class), node4, node5, 5000, 50, 3600, 1); Link link5 = this.network.createAndAddLink(Id.create("5", Link.class), node5, node6, 500, 25, 3600, 1); this.network.createAndAddLink(Id.create("6", Link.class), node6, node7, 5000, 50, 3600, 1); Link link7 = this.network.createAndAddLink(Id.create("7", Link.class), node7, node8, 500, 25, 3600, 1); this.network.createAndAddLink(Id.create("8", Link.class), node8, node9, 5000, 50, 3600, 1); Link link9 = this.network.createAndAddLink( Id.create("9", Link.class), node9, node10, 500, 25, 3600, 1); this.person = new PersonImpl(Id.create("1", Person.class)); this.plan = this.person.createAndAddPlan(true); ActivityImpl firstActivity = this.plan.createAndAddActivity("h", link1.getId()); firstActivity.setEndTime(firstLegStartTime); Leg leg = this.plan.createAndAddLeg(TransportMode.car); leg.setDepartureTime(firstLegStartTime); leg.setTravelTime(firstLegTravelTime); NetworkRoute route1 = new LinkNetworkRouteImpl(link1.getId(), link3.getId()); route1.setLinkIds(link1.getId(), Arrays.asList(link2.getId()), link3.getId()); route1.setTravelTime(firstLegTravelTime); route1.setDistance(RouteUtils.calcDistance(route1, this.network)); leg.setRoute(route1); ActivityImpl secondActivity = this.plan.createAndAddActivity("w", link3.getId()); secondActivity.setStartTime(firstLegStartTime + firstLegTravelTime); secondActivity.setEndTime(secondLegStartTime); leg = this.plan.createAndAddLeg(TransportMode.pt); leg.setDepartureTime(secondLegStartTime); leg.setTravelTime(secondLegTravelTime); Route route2 = new GenericRouteImpl(link3.getId(), link5.getId()); route2.setTravelTime(secondLegTravelTime); route2.setDistance(20000.0); leg.setRoute(route2); ActivityImpl thirdActivity = this.plan.createAndAddActivity("w", link5.getId()); thirdActivity.setStartTime(secondLegStartTime + secondLegTravelTime); thirdActivity.setEndTime(thirdLegStartTime); leg = this.plan.createAndAddLeg(TransportMode.walk); leg.setDepartureTime(thirdLegStartTime); leg.setTravelTime(thirdLegTravelTime); Route route3 = new GenericRouteImpl(link5.getId(), link7.getId()); route3.setTravelTime(thirdLegTravelTime); route3.setDistance(CoordUtils.calcDistance(link5.getCoord(), link7.getCoord())); leg.setRoute(route3); ActivityImpl fourthActivity = this.plan.createAndAddActivity("w", link7.getId()); fourthActivity.setStartTime(thirdLegStartTime + thirdLegTravelTime); fourthActivity.setEndTime(fourthLegStartTime); leg = this.plan.createAndAddLeg(TransportMode.bike); leg.setDepartureTime(fourthLegStartTime); leg.setTravelTime(fourthLegTravelTime); Route route4 = new GenericRouteImpl(link7.getId(), link9.getId()); route4.setTravelTime(fourthLegTravelTime); route4.setDistance(CoordUtils.calcDistance(link7.getCoord(), link9.getCoord())); leg.setRoute(route4); ActivityImpl fifthActivity = this.plan.createAndAddActivity("h", link9.getId()); fifthActivity.setStartTime(fourthLegStartTime + fourthLegTravelTime); this.scenario.getPopulation().addPerson(this.person); }
@Test @Ignore public void testAveraging() { // yy this test is probably not doing anything with respect to some of the newer statistics, // such as money. kai, mar'14 KNAnalysisEventsHandler testee = new KNAnalysisEventsHandler(this.scenario); EventsManager events = EventsUtils.createEventsManager(); events.addHandler(testee); Leg leg = PopulationUtils.createLeg(TransportMode.car); leg.setDepartureTime(Time.parseTime("07:10:00")); leg.setTravelTime(Time.parseTime("07:30:00") - leg.getDepartureTime()); testee.handleEvent( new PersonDepartureEvent( leg.getDepartureTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); testee.handleEvent( new PersonArrivalEvent( leg.getDepartureTime() + leg.getTravelTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); leg = PopulationUtils.createLeg(TransportMode.car); leg.setDepartureTime(Time.parseTime("07:00:00")); leg.setTravelTime(Time.parseTime("07:10:00") - leg.getDepartureTime()); testee.handleEvent( new PersonDepartureEvent( leg.getDepartureTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); testee.handleEvent( new PersonArrivalEvent( leg.getDepartureTime() + leg.getTravelTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); leg = PopulationUtils.createLeg(TransportMode.car); leg.setDepartureTime(Time.parseTime("31:12:00")); leg.setTravelTime(Time.parseTime("31:22:00") - leg.getDepartureTime()); testee.handleEvent( new PersonDepartureEvent( leg.getDepartureTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); testee.handleEvent( new PersonArrivalEvent( leg.getDepartureTime() + leg.getTravelTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); leg = PopulationUtils.createLeg(TransportMode.car); leg.setDepartureTime(Time.parseTime("30:12:00")); leg.setTravelTime(Time.parseTime("30:12:01") - leg.getDepartureTime()); testee.handleEvent( new PersonDepartureEvent( leg.getDepartureTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); testee.handleEvent( new PersonArrivalEvent( leg.getDepartureTime() + leg.getTravelTime(), DEFAULT_PERSON_ID, DEFAULT_LINK_ID, leg.getMode())); this.runTest(testee); }
private void processDiary() { LOG.info("Processing diary..."); PopulationFactory pf = this.sc.getPopulation().getFactory(); Map<String, Integer> chainCount = new TreeMap<>(); int noTrips = 0; int noFirstTrip = 0; int noZoneInfo = 0; int zoneInfo = 0; Counter counter = new Counter(" person # "); for (Id<Person> pId : this.tripMap.keySet()) { List<String[]> list = tripMap.get(pId); Map<String, String[]> map = new TreeMap<>(); for (String[] sa : list) { String tripNumber = sa[23]; if (!tripNumber.equals("")) { String tripId = String.format("%02d", Integer.parseInt(tripNumber)); map.put(tripId, sa); } } if (map.size() == 0) { noTrips++; } else { /* Try and do some magic with the diary. */ Plan plan = pf.createPlan(); /* Check the first activity. */ String chain = ""; String[] sa = map.get("01"); if (sa == null) { /* There is no first trip numbered '01'. */ LOG.warn(pId.toString() + ": " + map.keySet().toString()); noFirstTrip++; } else { chain += "h"; String homeZone = sa[7]; Coord homeCoord = sampleCoord(homeZone, "h"); Activity firstHome = pf.createActivityFromCoord("h", homeCoord); double homeEnd = 0.0; try { homeEnd = Time.parseTime(sa[25]); } catch (NumberFormatException e) { LOG.error(" TIME: ===> " + pId.toString() + ": " + sa[25]); } firstHome.setEndTime(homeEnd); plan.addActivity(firstHome); } /* Parse the chain. */ Iterator<String> it = map.keySet().iterator(); while (it.hasNext()) { String trip = it.next(); String[] tripSa = map.get(trip); String tripPurpose = tripSa[33]; String matsimTrip = DiaryEnums.TripPurpose.parseFromCode( tripPurpose.equals("") || tripPurpose.equals(" ") ? 0 : Integer.parseInt(tripPurpose)) .getMatsimActivityCode(); chain += "-" + matsimTrip; /* Add the trip to plan. */ String recordedMode = tripSa[34]; if (recordedMode.contains(";")) { LOG.error("Multiple modes for " + pId.toString() + ": " + recordedMode); throw new RuntimeException(); } String matsimMode = DiaryEnums.ModeOfTravel.parseFromCode( tripSa[34].equals("") || tripSa[34].equalsIgnoreCase(" ") ? 0 : Integer.parseInt(tripSa[34])) .getMatsimMode(); Leg leg = pf.createLeg(matsimMode); double tripStartTime = 0.0; double tripEndTime = 0.0; try { tripStartTime = Time.parseTime(tripSa[25]); } catch (NumberFormatException e) { LOG.error(" TIME: ===> " + pId.toString() + ": " + tripSa[25]); } try { tripEndTime = Time.parseTime(tripSa[27]); } catch (NumberFormatException e) { LOG.error(" TIME: ===> " + pId.toString() + ": " + tripSa[27]); } leg.setDepartureTime(tripStartTime); leg.setTravelTime(tripEndTime - tripStartTime); plan.addLeg(leg); /* Add the destination activity. */ if (tripSa[32].equals("") || tripSa[32].equals(" ")) { if (!matsimTrip.equalsIgnoreCase("h")) { zoneInfo++; } else { noZoneInfo++; } } else { zoneInfo++; } Coord actCoord = sampleCoord(tripSa[32], matsimTrip); Activity act = pf.createActivityFromCoord(matsimTrip, actCoord); act.setStartTime(tripEndTime); plan.addActivity(act); } /* Check and add chain. */ if (!chainCount.containsKey(chain)) { chainCount.put(chain, 1); } else { int oldCount = chainCount.get(chain); chainCount.put(chain, oldCount + 1); } /* Finally, associate the plan with the person. */ sc.getPopulation().getPersons().get(pId).addPlan(plan); } counter.incCounter(); } counter.printCounter(); LOG.info(" Number of persons with no trips: " + noTrips); LOG.info(" Number of persons with no first trips: " + noFirstTrip); LOG.info(" Number of destinations without zone info: " + noZoneInfo); LOG.info(" Number of destinations with zone info: " + zoneInfo); /* Report the activity chain types. */ SortedSet<Entry<String, Integer>> set = entriesSortedByValues(chainCount); Iterator<Entry<String, Integer>> iter = set.iterator(); while (iter.hasNext()) { Entry<String, Integer> entry = iter.next(); LOG.info(" " + entry.getKey() + " (" + entry.getValue() + ")"); } LOG.info("Done processing diary."); }
protected List<Leg> convertPathToLegList( double departureTime, Path p, Coord fromCoord, Coord toCoord, Person person) { List<Leg> legs = new ArrayList<Leg>(); Leg leg; double walkDistance, walkWaitTime, travelTime = 0; Route walkRoute; Coord coord = fromCoord; TransitRouteStop stop = null; double time = departureTime; for (Link link : p.links) { TransitRouterNetworkLink l = (TransitRouterNetworkLink) link; if (l.route != null) { // in line link double ttime = ttCalculator.getLinkTravelTime(l, time, person, null); travelTime += ttime; time += ttime; } else if (l.fromNode.route != null) { // inside link leg = new LegImpl(TransportMode.pt); ExperimentalTransitRoute ptRoute = new ExperimentalTransitRoute( stop.getStopFacility(), l.fromNode.line, l.fromNode.route, l.fromNode.stop.getStopFacility()); leg.setRoute(ptRoute); leg.setTravelTime(travelTime); legs.add(leg); travelTime = 0; stop = l.fromNode.stop; coord = l.fromNode.stop.getStopFacility().getCoord(); } else if (l.toNode.route != null) { // wait link leg = new LegImpl(TransportMode.transit_walk); walkDistance = CoordUtils.calcEuclideanDistance(coord, l.toNode.stop.getStopFacility().getCoord()); walkWaitTime = walkDistance / this.config.getBeelineWalkSpeed() + ttCalculator.getLinkTravelTime( l, time + walkDistance / this.config.getBeelineWalkSpeed(), person, null); walkRoute = new GenericRouteImpl( stop == null ? null : stop.getStopFacility().getLinkId(), l.toNode.stop.getStopFacility().getLinkId()); walkRoute.setDistance(walkDistance); leg.setRoute(walkRoute); leg.setTravelTime(walkWaitTime); legs.add(leg); stop = l.toNode.stop; time += walkWaitTime; } } leg = new LegImpl(TransportMode.transit_walk); walkDistance = CoordUtils.calcEuclideanDistance(coord, toCoord); walkWaitTime = walkDistance / this.config.getBeelineWalkSpeed(); walkRoute = new GenericRouteImpl(stop == null ? null : stop.getStopFacility().getLinkId(), null); walkRoute.setDistance(walkDistance); leg.setRoute(walkRoute); leg.setTravelTime(walkWaitTime); legs.add(leg); return legs; }