// Main
 public static void main(String[] args) throws IOException {
   final String CSV_SEPARATOR = "\t";
   EventsManager events = EventsUtils.createEventsManager();
   TypeActivityAnalyzer typeActivityAnalyzer = new TypeActivityAnalyzer(300, 30 * 3600);
   events.addHandler(typeActivityAnalyzer);
   new EventsReaderXMLv1(events).parse(args[0]);
   typeActivityAnalyzer.finishActivities();
   PrintWriter writer = new PrintWriter(new File("./data/durationsByType.txt"));
   for (String type : typeActivityAnalyzer.getDurations().keySet()) {
     for (Entry<Double, Integer> count : typeActivityAnalyzer.getDurations().get(type).entrySet())
       writer.println(
           Time.writeTime(count.getKey())
               + CSV_SEPARATOR
               + type
               + CSV_SEPARATOR
               + count.getValue());
   }
   writer.close();
   writer = new PrintWriter(new File("./data/performingByType.txt"));
   for (String type : typeActivityAnalyzer.getPeoplePerforming().keySet()) {
     for (Entry<Double, Integer> count :
         typeActivityAnalyzer.getPeoplePerforming().get(type).entrySet())
       writer.println(
           Time.writeTime(count.getKey())
               + CSV_SEPARATOR
               + type
               + CSV_SEPARATOR
               + count.getValue());
   }
   writer.close();
 }
  private static void startLeg(final Leg leg, final BufferedWriter out) throws IOException {
    out.write("\t\t\t<leg mode=\"");
    out.write(leg.getMode());
    out.write("\"");
    if (leg.getDepartureTime() != Time.UNDEFINED_TIME) {
      out.write(" dep_time=\"");
      out.write(Time.writeTime(leg.getDepartureTime()));
      out.write("\"");
    }
    if (leg.getTravelTime() != Time.UNDEFINED_TIME) {
      out.write(" trav_time=\"");
      out.write(Time.writeTime(leg.getTravelTime()));
      out.write("\"");
    }
    //		if (leg instanceof LegImpl) {
    //			LegImpl l = (LegImpl)leg;
    //			if (l.getDepartureTime() + l.getTravelTime() != Time.UNDEFINED_TIME) {
    //				out.write(" arr_time=\"");
    //				out.write(Time.writeTime(l.getDepartureTime() + l.getTravelTime()));
    //				out.write("\"");
    //			}
    //		}
    // arrival time is in dtd, but no longer evaluated in code (according to not being in API).
    // kai, jun'16

    out.write(">\n");
  }
 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;
  }
Exemple #5
0
  public void write(String outputFilenameBase) {
    SimpleWriter writer = new SimpleWriter(outputFilenameBase + "txt");
    writer.writeln("timeBin\twaitTimeOutofBus\twaitTimeInBus");
    StackedBarChart chart =
        new StackedBarChart(
            "arrivalTimeAtBusStop<->WaitTimeOutOfBus or WaitTimeInBus",
            "arrivalTimeAtBusStops",
            "waitTime[s]",
            PlotOrientation.VERTICAL);
    Object[] bins = wtobSums.keySet().toArray();
    // System.out.println("bins length=\t" + bins.length);
    int firstBin = (Integer) bins[0], lastBin = (Integer) bins[bins.length - 1];
    String[] times = new String[lastBin - firstBin + 1];
    double[][] values = new double[2][lastBin - firstBin + 1];
    for (int i = firstBin; i <= lastBin; i++) {
      // System.out.println(">>>>> i=\t" + i);
      int wtob = (int) (wtobCounts.containsKey(i) ? wtobSums.get(i) / wtobCounts.get(i) : 0);
      int wib = (int) (wibCounts.containsKey(i) ? wibSums.get(i) / wibCounts.get(i) : 0);
      String time = Time.writeTime(i * binSize);
      writer.writeln(time + "\t" + Time.writeTime(wtob) + "\t" + Time.writeTime(wib));
      times[i - firstBin] = time;
      values[0][i - firstBin] = wtob;
      values[1][i - firstBin] = wib;
    }
    writer.close();

    chart.addSeries(new String[] {"waitTimeOutOfBus", "waitTimeInBus"}, times, values);
    chart.saveAsPng(outputFilenameBase + "png", 1024, 768);
  }
 @StringGetter(END_TIME)
 public String getSimulationEndTimeAsString() {
   if (simulationEndTime != Double.MAX_VALUE) {
     return Time.writeTime(simulationEndTime);
   } else {
     return Time.writeTime(Time.UNDEFINED_TIME);
   }
 }
 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();
  }
}
Exemple #10
0
  /**
   * Writes the gathered data tab-separated into a text stream.
   *
   * @param stream The data stream where to write the gathered data.
   */
  public void write(final PrintStream stream) {
    stream.print("time\ttime\tdepartures_all\tarrivals_all\tstuck_all\ten-route_all");
    for (String legMode : this.data.keySet()) {
      stream.print(
          "\tdepartures_"
              + legMode
              + "\tarrivals_"
              + legMode
              + "\tstuck_"
              + legMode
              + "\ten-route_"
              + legMode);
    }
    stream.print("\n");
    int allEnRoute = 0;
    int[] modeEnRoute = new int[this.data.size()];
    for (int i = 0; i < this.allModesData.countsDep.length; i++) {
      // data about all modes
      allEnRoute =
          allEnRoute
              + this.allModesData.countsDep[i]
              - this.allModesData.countsArr[i]
              - this.allModesData.countsStuck[i];
      stream.print(Time.writeTime(i * this.binSize) + "\t" + i * this.binSize);
      stream.print(
          "\t"
              + this.allModesData.countsDep[i]
              + "\t"
              + this.allModesData.countsArr[i]
              + "\t"
              + this.allModesData.countsStuck[i]
              + "\t"
              + allEnRoute);

      // data about single modes
      int mode = 0;
      for (ModeData modeData : this.data.values()) {
        modeEnRoute[mode] =
            modeEnRoute[mode]
                + modeData.countsDep[i]
                - modeData.countsArr[i]
                - modeData.countsStuck[i];
        stream.print(
            "\t"
                + modeData.countsDep[i]
                + "\t"
                + modeData.countsArr[i]
                + "\t"
                + modeData.countsStuck[i]
                + "\t"
                + modeEnRoute[mode]);
        mode++;
      }

      // new line
      stream.print("\n");
    }
  }
  @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 writeAct(final Activity act, final BufferedWriter out) throws IOException {
   out.write("\t\t\t<act type=\"");
   out.write(act.getType());
   out.write("\"");
   if (act.getLinkId() != null) {
     out.write(" link=\"");
     out.write(act.getLinkId().toString());
     out.write("\"");
   }
   if (act.getFacilityId() != null) {
     out.write(" facility=\"");
     out.write(act.getFacilityId().toString());
     out.write("\"");
   }
   if (act.getCoord() != null) {
     final Coord coord = coordinateTransformation.transform(act.getCoord());
     out.write(" x=\"");
     out.write(Double.toString(coord.getX()));
     out.write("\" y=\"");
     out.write(Double.toString(coord.getY()));
     out.write("\"");
   }
   if (act.getStartTime() != Time.UNDEFINED_TIME) {
     out.write(" start_time=\"");
     out.write(Time.writeTime(act.getStartTime()));
     out.write("\"");
   }
   if (act != null) {
     Activity a = act;
     if (a.getMaximumDuration() != Time.UNDEFINED_TIME) {
       out.write(" max_dur=\"");
       out.write(Time.writeTime(a.getMaximumDuration()));
       out.write("\"");
     }
   }
   if (act.getEndTime() != Time.UNDEFINED_TIME) {
     out.write(" end_time=\"");
     out.write(Time.writeTime(act.getEndTime()));
     out.write("\"");
   }
   out.write(" />\n");
 }
 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")));
   }
 }
Exemple #15
0
 @Override
 public void run(Person person) {
   try {
     Plan plan = person.getSelectedPlan();
     this.transitLegsRemover.run(plan);
     //				for (Plan plan : person.getPlans()) {
     Activity prevAct = null;
     for (PlanElement pe : plan.getPlanElements()) {
       if (pe instanceof Activity) {
         Activity act = (Activity) pe;
         if (prevAct != null) {
           List<Leg> legs =
               router.calcRoute(
                   new FakeFacility(prevAct.getCoord()),
                   new FakeFacility(act.getCoord()),
                   act.getStartTime(),
                   person);
           out.write(
               person.getId()
                   + " "
                   + prevAct.getCoord()
                   + " -> "
                   + act.getCoord()
                   + " @ "
                   + Time.writeTime(act.getStartTime())
                   + " :\n");
           if (legs != null) {
             for (Leg l : legs) {
               out.write("  " + l.getMode());
               if (l.getRoute() instanceof ExperimentalTransitRoute) {
                 ExperimentalTransitRoute r = (ExperimentalTransitRoute) l.getRoute();
                 out.write(" " + r.getRouteDescription());
               }
               out.write("\n");
             }
           }
         }
         prevAct = act;
       }
     }
     //				}
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 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")));
   }
 }
Exemple #17
0
  private static void writeAnArray(final int[][] anArray, final String filename) {

    File outFile = null;
    BufferedWriter out = null;

    outFile = new File(filename);

    try {
      out = new BufferedWriter(new FileWriter(outFile));

      boolean timesAvailable = true;
      int timeIndex = 0;

      out.write("#");
      for (int ii = 0; ii < Activities.values().length; ii++) {
        out.write(Activities.values()[ii] + "\t");
      }
      out.newLine();

      while (timesAvailable) {

        timesAvailable = false;

        out.write(Time.writeTime(timeIndex * TIME_BIN_SIZE) + "\t");
        for (int aa = 0; aa < anArray.length; aa++) {

          //					if (numDeps[aa][timeIndex] != null) {
          if (timeIndex < anArray[aa].length) {
            out.write(Integer.toString(anArray[aa][timeIndex]));
            timesAvailable = true;
          } else {
            out.write("0");
          }
          out.write("\t");
        }
        out.newLine();
        timeIndex++;
      }
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  private double calculateFlightDuration(DgOagLine l, String route) {
    double duration = l.getFlightDurationSeconds();
    if (duration > 24.0 * 3600) {
      log.warn(
          "Flight "
              + l.getFlightNumber()
              + " has a duration of "
              + Time.writeTime(duration)
              + " hh:mm:ss that is considered as not realistic, substracting 24 h...");
      duration -= (24.0 * 3600.0);
    }

    if (DgCreateSfFlightScenario.doCreateStars) {
      duration = this.calculateFlightDurationWithStar(l.getDestinationAirport(), duration);
    }

    if (!this.routeDurationMap.containsKey(route)) {
      this.routeDurationMap.put(route, duration);
    }
    return duration;
  }
 private static void startRoute(final Route route, final BufferedWriter out) throws IOException {
   out.write("\t\t\t\t<route ");
   out.write("type=\"");
   out.write(route.getRouteType());
   out.write("\"");
   out.write(" start_link=\"");
   out.write(route.getStartLinkId().toString());
   out.write("\"");
   out.write(" end_link=\"");
   out.write(route.getEndLinkId().toString());
   out.write("\"");
   out.write(" trav_time=\"");
   out.write(Time.writeTime(route.getTravelTime()));
   out.write("\"");
   out.write(" distance=\"");
   out.write(Double.toString(route.getDistance()));
   out.write("\"");
   out.write(">");
   String rd = route.getRouteDescription();
   if (rd != null) {
     out.write(rd);
   }
 }
  @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 PassengerTTAnaEventHandler(String vehId, String startTime, String stopTime) {
   this(vehId, Time.parseTime(startTime), Time.parseTime(stopTime));
 }
 private String convertSeconds2dateTimeFormat(double endOfTimeInterval) {
   String date = "2012-04-13 ";
   String time = Time.writeTime(endOfTimeInterval, Time.TIMEFORMAT_HHMM);
   String dateTimeString = date + time;
   return dateTimeString;
 }
Exemple #23
0
  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.");
  }
Exemple #24
0
  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.");
  }
  @Override
  public String printResults() {

    double averageWaitingTime = 0.0;
    double averageTravelTime = 0.0;

    double sumWaitingTime = 0.0;
    double sumTravelTime = 0.0;

    int numberOfAgentsCountedSoFar = 0;

    for (AgentCountBox agentCountBox : this.completeAgents) {
      averageWaitingTime =
          (numberOfAgentsCountedSoFar * averageWaitingTime + agentCountBox.getWaitingTime())
              / (numberOfAgentsCountedSoFar + 1);
      averageTravelTime =
          (numberOfAgentsCountedSoFar * averageTravelTime + agentCountBox.getTravelTimeInVehicle())
              / (numberOfAgentsCountedSoFar + 1);

      sumWaitingTime += agentCountBox.getWaitingTime();
      sumTravelTime += agentCountBox.getTravelTimeInVehicle();

      numberOfAgentsCountedSoFar++;
    }

    log.info(
        "Veh "
            + this.vehId
            + " from "
            + Time.writeTime(this.startTime)
            + " to "
            + Time.writeTime(this.stopTime)
            + " counted "
            + numberOfAgentsCountedSoFar
            + " agents who (in average) had to wait for "
            + ((int) averageWaitingTime)
            + "s and travelled "
            + ((int) averageTravelTime)
            + "s - sumWaiting "
            + ((int) sumWaitingTime)
            + "s, sumTT "
            + ((int) sumTravelTime)
            + "s, "
            + this.numberOfAdditionalTripsPerformed
            + " agents drive a bus stop ahead resulting in additional trips");

    return this.vehId
        + ", "
        + Time.writeTime(this.startTime)
        + ", "
        + Time.writeTime(this.stopTime)
        + ", "
        + numberOfAgentsCountedSoFar
        + ", "
        + ((int) averageWaitingTime)
        + ", "
        + ((int) averageTravelTime)
        + ", "
        + ((int) sumWaitingTime)
        + ", "
        + ((int) sumTravelTime)
        + ", "
        + this.numberOfAdditionalTripsPerformed;
  }