private void startLeg(final Attributes atts) { this.currleg = this.currplan.createAndAddLeg(atts.getValue("mode").toLowerCase(Locale.ROOT).intern()); this.currleg.setDepartureTime(Time.parseTime(atts.getValue("dep_time"))); this.currleg.setTravelTime(Time.parseTime(atts.getValue("trav_time"))); this.currleg.setArrivalTime(Time.parseTime(atts.getValue("arr_time"))); }
private void startAct(final Attributes atts) { ActivityImpl act = null; if (atts.getValue("link") != null) { final Id<Link> linkId = Id.create(atts.getValue("link"), Link.class); act = this.currplan.createAndAddActivity(atts.getValue("type"), linkId); if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) { final Coord coord = parseCoord(atts); act.setCoord(coord); } } else if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) { final Coord coord = parseCoord(atts); act = this.currplan.createAndAddActivity(atts.getValue("type"), coord); } else { throw new IllegalArgumentException( "Either the coords or the link must be specified for an Act."); } act.setStartTime(Time.parseTime(atts.getValue("start_time"))); act.setMaximumDuration(Time.parseTime(atts.getValue("dur"))); act.setEndTime(Time.parseTime(atts.getValue("end_time"))); if (this.routeNodes != null) { this.currroute.setLinkIds( this.prevAct.getLinkId(), NetworkUtils.getLinkIds( RouteUtils.getLinksFromNodes(NetworkUtils.getNodes(this.network, this.routeNodes))), act.getLinkId()); this.routeNodes = null; this.currroute = null; } this.prevAct = act; }
private void startLeg(final Attributes atts) { String mode = atts.getValue("mode").toLowerCase(Locale.ROOT); if (mode.equals("undef")) { mode = "undefined"; } this.currleg = this.currplan.createAndAddLeg(mode.intern()); this.currleg.setDepartureTime(Time.parseTime(atts.getValue("dep_time"))); this.currleg.setTravelTime(Time.parseTime(atts.getValue("trav_time"))); this.currleg.setArrivalTime(Time.parseTime(atts.getValue("arr_time"))); }
private void startAct(final Attributes atts) { Coord coord = null; if (atts.getValue("link") != null) { Id<Link> linkId = Id.create(atts.getValue("link"), Link.class); this.curract = this.currplan.createAndAddActivity(atts.getValue(ATTR_TYPE), linkId); if ((atts.getValue("x") != null) && (atts.getValue("y") != null)) { coord = new Coord( Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y"))); this.curract.setCoord(coord); } } else if ((atts.getValue("x") != null) && (atts.getValue("y") != null)) { coord = new Coord(Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y"))); this.curract = this.currplan.createAndAddActivity(atts.getValue(ATTR_TYPE), coord); } else { throw new IllegalArgumentException( "In this version of MATSim either the coords or the link must be specified for an Act."); } this.curract.setStartTime(Time.parseTime(atts.getValue("start_time"))); this.curract.setMaximumDuration(Time.parseTime(atts.getValue("dur"))); this.curract.setEndTime(Time.parseTime(atts.getValue("end_time"))); String fId = atts.getValue("facility"); if (fId != null) { this.curract.setFacilityId(Id.create(fId, ActivityFacility.class)); } if (this.routeDescription != null) { Id<Link> startLinkId = null; if (this.prevAct.getLinkId() != null) { startLinkId = this.prevAct.getLinkId(); } Id<Link> endLinkId = null; if (this.curract.getLinkId() != null) { endLinkId = this.curract.getLinkId(); } this.currRoute.setStartLinkId(startLinkId); this.currRoute.setEndLinkId(endLinkId); if (this.currRoute instanceof NetworkRoute) { ((NetworkRoute) this.currRoute) .setLinkIds( startLinkId, NetworkUtils.getLinkIds( RouteUtils.getLinksFromNodes( NetworkUtils.getNodes(this.network, this.routeDescription))), endLinkId); } else { this.currRoute.setRouteDescription(this.routeDescription.trim()); } this.routeDescription = null; this.currRoute = null; } }
/** @author ychen */ public class EventDepTimeFilter extends EventFilterA { private static double criterionMAX = Time.parseTime("08:00"); private static double criterionMIN = Time.parseTime("06:00"); @Override public boolean judge(Event event) { if (event.getClass().equals(PersonDepartureEvent.class)) { return event.getTime() < criterionMAX && event.getTime() > criterionMIN; } return isResult(); } }
@Test public void testGetDeparturesAtStop() throws SAXException, ParserConfigurationException, IOException { final String inputDir = "test/input/" + TransitScheduleReaderTest.class.getCanonicalName().replace('.', '/') + "/"; ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig()); Network network = scenario.getNetwork(); new MatsimNetworkReader(scenario).readFile(inputDir + INPUT_TEST_FILE_NETWORK); TransitScheduleFactory builder = new TransitScheduleFactoryImpl(); TransitSchedule schedule = builder.createTransitSchedule(); new TransitScheduleReaderV1(schedule, network) .readFile(inputDir + INPUT_TEST_FILE_TRANSITSCHEDULE); TransitLine line = schedule.getTransitLines().get(Id.create("T1", TransitLine.class)); CreateTimetableForStop timetable = new CreateTimetableForStop(line); assertNotNull("could not get transit line.", line); double[] departures = timetable.getDeparturesAtStop( schedule.getFacilities().get(Id.create("stop3", TransitStopFacility.class))); for (double d : departures) { System.out.println("Departure at " + Time.writeTime(d)); } assertEquals("wrong number of departures.", 3, departures.length); double baseDepartureTime = Time.parseTime("07:00:00") + Time.parseTime("00:03:00"); assertEquals( "wrong departure time for 1st departure.", baseDepartureTime, departures[0], MatsimTestCase.EPSILON); assertEquals( "wrong departure time for 2nd departure.", baseDepartureTime + 600, departures[1], MatsimTestCase.EPSILON); assertEquals( "wrong departure time for 3rd departure.", baseDepartureTime + 1200, departures[2], MatsimTestCase.EPSILON); }
@StringSetter(END_TIME) public void setSimulationEndTime(String simulationEndTime) { double parsedTime = Time.parseTime(simulationEndTime); if (parsedTime != Time.UNDEFINED_TIME) { this.simulationEndTime = parsedTime; } else { parsedTime = Double.MAX_VALUE; } }
private void startRoute(final Attributes atts) { this.currroute = ((PopulationFactoryImpl) this.plans.getFactory()) .createRoute(NetworkRoute.class, this.prevAct.getLinkId(), this.prevAct.getLinkId()); this.currleg.setRoute(this.currroute); if (atts.getValue("dist") != null) { this.currroute.setDistance(Double.parseDouble(atts.getValue("dist"))); } if (atts.getValue("trav_time") != null) { this.currroute.setTravelTime(Time.parseTime(atts.getValue("trav_time"))); } }
private void startRoute(final Attributes atts) { Class<? extends Route> routeType = GenericRouteImpl.class; if ("pt".equals(this.currleg.getMode())) { routeType = ExperimentalTransitRoute.class; } if ("car".equals(this.currleg.getMode())) { routeType = NetworkRoute.class; } this.currRoute = ((PopulationFactoryImpl) this.plans.getFactory()).createRoute(routeType, null, null); this.currleg.setRoute(this.currRoute); if (atts.getValue("dist") != null) { this.currRoute.setDistance(Double.parseDouble(atts.getValue("dist"))); } if (atts.getValue("trav_time") != null) { this.currRoute.setTravelTime(Time.parseTime(atts.getValue("trav_time"))); } }
@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); }
public void cleanUpScenario(Scenario sc) { LOG.info("Cleaning up scenario..."); /* TODO Still need to figure out what cleaning up must happen. */ /* Search for location-less activities, and sample its locations from * the kernel density estimates. */ LOG.info("Sampling locations for those without known zones..."); int locationsFixed = 0; int knownLocations = 0; for (Person person : sc.getPopulation().getPersons().values()) { Plan plan = person.getSelectedPlan(); if (plan != null) { for (PlanElement pe : plan.getPlanElements()) { if (pe instanceof Activity) { Activity act = (Activity) pe; Coord coord = act.getCoord(); if (coord.getX() == 0.0 || coord.getY() == 0.0) { ((ActivityImpl) act).setCoord(sampleActivityLocation(act.getType())); locationsFixed++; } else { knownLocations++; } } } } } LOG.info("Number of known locations: " + knownLocations); LOG.info("Number of locations fixed: " + locationsFixed); LOG.info("Done sampling locations."); /* Ensure each activity, except the last, has both a start and end time. */ LOG.info("Ensure each activity has an end time..."); for (Person person : sc.getPopulation().getPersons().values()) { Plan plan = person.getSelectedPlan(); if (plan != null) { List<PlanElement> list = plan.getPlanElements(); for (int i = 0; i < list.size() - 2; i += 2) { PlanElement pe = list.get(i); if (pe instanceof Activity) { Activity act = (Activity) pe; double endTime = act.getEndTime(); if (endTime < 0.0) { double legStart = ((Leg) list.get(i + 1)).getDepartureTime(); act.setEndTime(legStart); } } else { LOG.warn("Every second PlanElement should be of type 'Activity'."); } } } } LOG.info("Done fixing activity end times."); /* Ensure that the home location/coordinate for all members in the * household are the same for all their "h" activities. */ LOG.info("Fix home locations for each household member. "); int homeLocationsFixed = 0; int nonTravellers = 0; for (Household hh : sc.getHouseholds().getHouseholds().values()) { Object o = sc.getHouseholds() .getHouseholdAttributes() .getAttribute(hh.getId().toString(), "transportZone"); if (o instanceof String) { String zoneId = (String) o; Coord homeCoord = null; if (zoneId != null && !zoneId.equalsIgnoreCase("")) { /* There is known home zone. */ Point p = this.zoneMap.get(zoneId).sampleRandomInteriorPoint(); homeCoord = CoordUtils.createCoord(p.getX(), p.getY()); } else { /* There is no transport zone. */ homeCoord = sampleActivityLocation("h"); } /* Now assign the home coordinate to ALL home activities for * all members of the household. */ for (Id<Person> id : hh.getMemberIds()) { Person person = sc.getPopulation().getPersons().get(id); Plan plan = person.getSelectedPlan(); if (plan != null) { for (PlanElement pe : person.getSelectedPlan().getPlanElements()) { if (pe instanceof ActivityImpl) { ActivityImpl act = (ActivityImpl) pe; if (act.getType().equalsIgnoreCase("h")) { act.setCoord(homeCoord); homeLocationsFixed++; } } } } else { /* The member does not have ANY plan. We 'fix' this by * adding aPlan with a single home-based activity for * which not times are specified. */ Plan stayHomePlan = sc.getPopulation().getFactory().createPlan(); Activity act = sc.getPopulation().getFactory().createActivityFromCoord("h", homeCoord); stayHomePlan.addActivity(act); person.addPlan(stayHomePlan); nonTravellers++; } } } } LOG.info("Total number of home locations fixed: " + homeLocationsFixed); LOG.info(" Total number of non-travellers: " + nonTravellers); LOG.info("Done fixing home locations."); /* TODO Check what to do with those household members that are not * travelling. Are they just given a single 'h' activities, and * left at that? */ /* Look for erroneous activity times that can/should be fixed in * the raw travel diary input data. This will typically include very * long activity times or leg durations. */ LOG.info("Checking leg durations:"); int remainingLegOddities = 0; double legDurationThreshold = Time.parseTime("03:00:00"); for (Person p : sc.getPopulation().getPersons().values()) { Plan plan = p.getSelectedPlan(); if (plan != null) { for (PlanElement pe : plan.getPlanElements()) { if (pe instanceof Leg) { double duration = ((Leg) pe).getTravelTime(); if (duration < 0 || duration > legDurationThreshold) { LOG.warn( " -> Odd leg duration: " + p.getId().toString() + " (" + Time.writeTime(duration) + ")"); remainingLegOddities++; } } } } } LOG.info("Done checking leg durations. (" + remainingLegOddities + " remain)"); /* Parse the activity duration times in an effort to pick up * erroneous travel time data that results in negative activity * durations. Then fix in input data. */ LOG.info("Checking activity durations (from leg times):"); int remainingActivityOddities = 0; double activityDurationThreshold = Time.parseTime("16:00:00"); for (Person p : sc.getPopulation().getPersons().values()) { Plan plan = p.getSelectedPlan(); if (plan != null) { for (int i = 1; i < plan.getPlanElements().size() - 2; i += 2) { PlanElement pe1 = plan.getPlanElements().get(i); PlanElement pe2 = plan.getPlanElements().get(i + 2); if (pe1 instanceof Leg && pe2 instanceof Leg) { Leg l1 = (Leg) pe1; Leg l2 = (Leg) pe2; double act = l2.getDepartureTime() - (l1.getDepartureTime() + l1.getTravelTime()); if (act < 0 || act > activityDurationThreshold) { LOG.warn( " -> Odd activity duration: " + p.getId().toString() + " (" + Time.writeTime(act) + ")"); remainingActivityOddities++; } } else { LOG.error("PlanElements not of type Leg!!"); LOG.error(" pe1: " + pe1.getClass().toString()); LOG.error(" pe2: " + pe2.getClass().toString()); } } } } LOG.info("Done checking activity durations. (" + remainingActivityOddities + " remain)"); /* TODO Fix plans for night workers. They typically start with 'h', * but should actually start with 'w'. */ /* TODO Consider what to do with repeating activities, especially * consecutive home activities. */ /* Convert all activity locations to a projected coordinate system. */ LOG.info("Converting all activity locations to EPSG:3857..."); CoordinateTransformation ct = TransformationFactory.getCoordinateTransformation("WGS84", "EPSG:3857"); for (Person person : sc.getPopulation().getPersons().values()) { Plan plan = person.getSelectedPlan(); if (plan != null) { for (PlanElement pe : plan.getPlanElements()) { if (pe instanceof Activity) { Activity act = (Activity) pe; ((ActivityImpl) act).setCoord(ct.transform(act.getCoord())); } } } } LOG.info("Done converting activity locations."); LOG.info("Done cleaning up scenario."); }
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."); }
public PassengerTTAnaEventHandler(String vehId, String startTime, String stopTime) { this(vehId, Time.parseTime(startTime), Time.parseTime(stopTime)); }