Example #1
0
  public static void main(String[] args) throws IOException {
    String in = args[0];
    String rootDir = args[1];

    logger.info("Loading persons...");
    XMLHandler reader = new XMLHandler(new PlainFactory());
    reader.setValidating(false);
    reader.readFile(in);
    Collection<PlainPerson> persons = (Set<PlainPerson>) reader.getPersons();
    logger.info(String.format("Loaded %s persons.", persons.size()));

    logger.info("Restoring original activity types...");
    TaskRunner.run(new RestoreActTypes(), persons, true);
    logger.info("Replacing misc types...");
    new ReplaceMiscType().apply(persons);
    logger.info("Imputing month attributes...");
    new ImputeMonth().apply(persons);
    logger.info("Assigning leg purposes...");
    TaskRunner.run(new SetLegPurposes(), persons);
    logger.info("Inferring wecommuter purpose...");
    TaskRunner.run(new InfereWeCommuter(100000), persons);

    Map<String, LegPredicate> modePreds = new LinkedHashMap<>();
    modePreds.put("car", new LegKeyValuePredicate(CommonKeys.LEG_MODE, "car"));

    Map<String, LegPredicate> purposePreds = new LinkedHashMap<>();
    purposePreds.put(
        ActivityTypes.BUSINESS,
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.BUSINESS));
    purposePreds.put(
        ActivityTypes.EDUCATION,
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.EDUCATION));

    PredicateORComposite leisurePred = new PredicateORComposite();
    leisurePred.addComponent(
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.LEISURE));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "visit"));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "gastro"));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "culture"));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "private"));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "pickdrop"));
    leisurePred.addComponent(new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, "sport"));

    purposePreds.put(ActivityTypes.LEISURE, leisurePred);
    purposePreds.put(
        ActivityTypes.SHOP, new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.SHOP));
    purposePreds.put(
        ActivityTypes.WORK, new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.WORK));
    purposePreds.put(
        ActivityTypes.VACATIONS_SHORT,
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.VACATIONS_SHORT));
    purposePreds.put(
        ActivityTypes.VACATIONS_LONG,
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, ActivityTypes.VACATIONS_LONG));
    purposePreds.put(
        InfereWeCommuter.WECOMMUTER,
        new LegKeyValuePredicate(CommonKeys.LEG_PURPOSE, InfereWeCommuter.WECOMMUTER));

    Map<String, LegPredicate> dayPreds = new LinkedHashMap<>();
    dayPreds.put(
        CommonValues.MONDAY, new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.MONDAY));
    dayPreds.put(
        CommonValues.FRIDAY, new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.FRIDAY));
    dayPreds.put(
        CommonValues.SATURDAY, new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.SATURDAY));
    dayPreds.put(
        CommonValues.SUNDAY, new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.SUNDAY));
    PredicateORComposite dimidoPred = new PredicateORComposite();
    dimidoPred.addComponent(new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.TUESDAY));
    dimidoPred.addComponent(new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.WEDNESDAY));
    dimidoPred.addComponent(new PersonKeyValuePredicate(CommonKeys.DAY, CommonValues.THURSDAY));
    dayPreds.put(DIMIDO, dimidoPred);

    Map<String, LegPredicate> seasonPreds = new LinkedHashMap<>();
    PredicateORComposite summerPred = new PredicateORComposite();
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.APRIL));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.MAY));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.JUNE));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.JULY));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.AUGUST));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.SEPTEMBER));
    summerPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.OCTOBER));
    seasonPreds.put(SUMMER, summerPred);
    PredicateORComposite winterPred = new PredicateORComposite();
    winterPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.NOVEMBER));
    winterPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.DECEMBER));
    winterPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.JANUARY));
    winterPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.FEBRUARY));
    winterPred.addComponent(new PersonKeyValuePredicate(MiDKeys.PERSON_MONTH, MiDValues.MARCH));
    seasonPreds.put(WINTER, winterPred);

    Map<String, LegPredicate> directionPreds = new LinkedHashMap<>();
    directionPreds.put(
        DirectionPredicate.OUTWARD, new DirectionPredicate(DirectionPredicate.OUTWARD));
    directionPreds.put(
        DirectionPredicate.RETURN, new DirectionPredicate(DirectionPredicate.RETURN));
    directionPreds.put(
        DirectionPredicate.INTERMEDIATE, new DirectionPredicate(DirectionPredicate.INTERMEDIATE));

    logger.info("Extracting full matrix...");
    NumericMatrixTxtIO.write(
        getMatrix(persons, modePreds.get("car")), String.format("%s/car.txt.gz", rootDir));

    for (Map.Entry<String, LegPredicate> mode : modePreds.entrySet()) {
      for (Map.Entry<String, LegPredicate> purpose : purposePreds.entrySet()) {
        for (Map.Entry<String, LegPredicate> day : dayPreds.entrySet()) {
          for (Map.Entry<String, LegPredicate> season : seasonPreds.entrySet()) {
            for (Map.Entry<String, LegPredicate> direction : directionPreds.entrySet()) {

              PredicateANDComposite comp = new PredicateANDComposite();
              comp.addComponent(mode.getValue());
              comp.addComponent(purpose.getValue());
              comp.addComponent(day.getValue());
              comp.addComponent(season.getValue());
              comp.addComponent(direction.getValue());

              StringBuilder builder = new StringBuilder();
              builder.append(mode.getKey());
              builder.append(DIM_SEPARATOR);
              builder.append(purpose.getKey());
              builder.append(DIM_SEPARATOR);
              builder.append(day.getKey());
              builder.append(DIM_SEPARATOR);
              builder.append(season.getKey());
              builder.append(DIM_SEPARATOR);
              builder.append(direction.getKey());

              logger.info(String.format("Extracting matrix %s...", builder.toString()));
              NumericMatrix m = getMatrix(persons, comp);

              String out = String.format("%s/%s.txt.gz", rootDir, builder.toString());
              NumericMatrixTxtIO.write(m, out);
            }
          }
        }
      }
    }

    int cnt = 0;
    for (Person person : persons) {
      for (Episode episode : person.getEpisodes()) {
        for (Segment leg : episode.getLegs()) {
          String touched = leg.getAttribute(DBG_TOUCHED);
          if (touched == null) {
            leg.setAttribute(DBG_TOUCHED, "0");
            cnt++;
          }
        }
      }
    }
    logger.info(String.format("%s trips are untouched.", cnt));

    XMLWriter writer = new XMLWriter();
    writer.write(rootDir + "/plans.xml.gz", persons);
    logger.info("Done.");
  }
  public static final void main(String args[]) throws IOException {
    String outdir = "/home/johannes/gsv/miv-matrix/qs2013/";
    String matrixFile = "/home/johannes/gsv/miv-matrix/qs2013/matrix.txt";
    String zoneFile = "/home/johannes/gsv/gis/zones/geojson/nuts3.psm.gk3.geojson";
    String inhabFile = "/home/johannes/gsv/miv-matrix/qs2013/inhabitants.csv";

    final NumericMatrix carVol = new NumericMatrix();
    final NumericMatrix railVol = new NumericMatrix();
    final NumericMatrix airVol = new NumericMatrix();

    logger.info("Loading zones...");
    ZoneCollection zones = ZoneGeoJsonIO.readFromGeoJSON(zoneFile, "NO");
    TObjectDoubleHashMap<String> zoneRho = calcDensity(inhabFile, zones);

    logger.info("Reading matrix...");
    MatrixReader mReader = new MatrixReader();
    mReader.read(
        matrixFile,
        new MatrixReader.RowHandler() {
          @Override
          public void handleRow(
              String from,
              String to,
              String purpose,
              String year,
              String mode,
              String direction,
              String day,
              String season,
              double volume) {
            if (mode.equalsIgnoreCase("M")) {
              carVol.add(from, to, volume);
            } else if (mode.equalsIgnoreCase("B")) {
              railVol.add(from, to, volume);
            } else if (mode.equalsIgnoreCase("F")) {
              airVol.add(from, to, volume);
            }
          }
        });

    logger.info("Calculating shares per od-pair...");
    NumericMatrix carShare = new NumericMatrix();
    NumericMatrix railShare = new NumericMatrix();
    NumericMatrix airShare = new NumericMatrix();
    NumericMatrix odCounts = new NumericMatrix();

    Discretizer discr = FixedSampleSizeDiscretizer.create(zoneRho.values(), 1, 20);
    //        Discretizer discr = new DummyDiscretizer();

    Set<String> keys = carVol.keys();
    keys.addAll(railVol.keys());
    keys.addAll(airVol.keys());

    ProgressLogger.init(keys.size(), 2, 10);

    for (String from : keys) {
      for (String to : keys) {
        Double car = carVol.get(from, to);
        if (car == null) car = 0.0;
        Double rail = railVol.get(from, to);
        if (rail == null) rail = 0.0;
        Double air = airVol.get(from, to);
        if (air == null) air = 0.0;

        if (car > 0 && rail > 0 && air > 0) {
          double total = car + rail + air;

          double fromRho = zoneRho.get(from);
          double toRho = zoneRho.get(to);

          fromRho = discr.discretize(fromRho);
          toRho = discr.discretize(toRho);

          String fromRhoStr = String.valueOf(fromRho);
          String toRhoStr = String.valueOf(toRho);
          odCounts.add(fromRhoStr, toRhoStr, 1);
          //                    odCounts.add(fromRhoStr, toRhoStr, total);

          carShare.add(fromRhoStr, toRhoStr, car / total);
          railShare.add(fromRhoStr, toRhoStr, rail / total);
          airShare.add(fromRhoStr, toRhoStr, air / total);
        }
      }
      ProgressLogger.step();
    }
    ProgressLogger.terminate();

    logger.info("Calculating averages...");
    keys = odCounts.keys();
    for (String from : keys) {
      for (String to : keys) {
        Double count = odCounts.get(from, to);
        if (count != null) {
          carShare.multiply(from, to, 1 / count);
          railShare.multiply(from, to, 1 / count);
          airShare.multiply(from, to, 1 / count);
        }
      }
    }

    logger.info("Writing matrices...");
    NumericMatrixTxtIO.write(carShare, String.format("%s/carShare.txt", outdir));
    NumericMatrixTxtIO.write(railShare, String.format("%s/railShare.txt", outdir));
    NumericMatrixTxtIO.write(airShare, String.format("%s/airShare.txt", outdir));
    logger.info("Done.");
  }