@Override
  public void notifyAfterMobsim(AfterMobsimEvent event) {
    Network network = event.getServices().getScenario().getNetwork();
    DescriptiveStatistics error = new DescriptiveStatistics();
    DescriptiveStatistics errorAbs = new DescriptiveStatistics();
    DescriptivePiStatistics errorWeighted = new WSMStatsFactory().newInstance();

    TDoubleArrayList errorVals = new TDoubleArrayList();
    TDoubleArrayList caps = new TDoubleArrayList();
    TDoubleArrayList speeds = new TDoubleArrayList();

    for (Count count : counts.getCounts().values()) {
      if (!count.getId().toString().startsWith(ODCalibrator.VIRTUAL_ID_PREFIX)) {
        double obsVal = 0;
        for (int i = 1; i < 25; i++) {
          obsVal += count.getVolume(i).getValue();
        }

        if (obsVal > 0) {
          double simVal = calculator.getOccupancy(count.getId());
          simVal *= factor;

          double err = (simVal - obsVal) / obsVal;

          error.addValue(err);
          errorAbs.addValue(Math.abs(err));
          errorWeighted.addValue(Math.abs(err), 1 / obsVal);

          Link link = network.getLinks().get(count.getId());
          errorVals.add(Math.abs(err));
          caps.add(link.getCapacity());
          speeds.add(link.getFreespeed());
        }
      }
    }

    logger.info(
        String.format(
            "Relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            error.getMean(),
            error.getVariance(),
            error.getStandardDeviation(),
            error.getMin(),
            error.getMax()));
    logger.info(
        String.format(
            "Absolute relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorAbs.getMean(),
            errorAbs.getVariance(),
            errorAbs.getStandardDeviation(),
            errorAbs.getMin(),
            errorAbs.getMax()));
    logger.info(
        String.format(
            "Absolute weigthed relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorWeighted.getMean(),
            errorWeighted.getVariance(),
            errorWeighted.getStandardDeviation(),
            errorWeighted.getMin(),
            errorWeighted.getMax()));

    String outdir = event.getServices().getControlerIO().getIterationPath(event.getIteration());

    try {
      TDoubleDoubleHashMap map = Correlations.mean(caps.toArray(), errorVals.toArray());
      StatsWriter.writeHistogram(
          map, "capacity", "counts", String.format("%s/countsError.capacity.txt", outdir));

      map = Correlations.mean(speeds.toArray(), errorVals.toArray());
      StatsWriter.writeHistogram(
          map, "speed", "counts", String.format("%s/countsError.speed.txt", outdir));

      StatsWriter.writeHistogram(
          Histogram.createHistogram(error, new LinearDiscretizer(0.1), false),
          "Error",
          "Frequency",
          String.format("%s/countsError.hist.txt", outdir));
      StatsWriter.writeHistogram(
          Histogram.createHistogram(errorAbs, new LinearDiscretizer(0.1), false),
          "Error (absolute)",
          "Frequency",
          String.format("%s/countsErrorAbs.hist.txt", outdir));
      StatsWriter.writeHistogram(
          Histogram.createHistogram(errorWeighted, new LinearDiscretizer(0.1), true),
          "Error (weighted)",
          "Frequency",
          String.format("%s/countsErrorWeighted.hist.txt", outdir));

      CountsCompare2GeoJSON.write(calculator, counts, factor, network, outdir);
      NetworkLoad2GeoJSON.write(
          event.getServices().getScenario().getNetwork(),
          calculator,
          factor,
          outdir + "/network.json");
    } catch (Exception e) {
      e.printStackTrace();
    }

    String rootOutDir = event.getServices().getControlerIO().getOutputPath();
    boolean append = false;
    if (event.getIteration() > 0) {
      append = true;
    }
    writeErrorFile(error, String.format("%s/countsError.txt", rootOutDir), append);
    writeErrorFile(errorAbs, String.format("%s/countsAbsError.txt", rootOutDir), append);
  }
示例#2
0
  public static void main(String args[]) throws IOException {
    String countsInFile =
        "/Volumes/johannes/sge/prj/fpd/data/counts/counts.2014.net20140909.5.24h.xml";
    String countsOutFile =
        "/Users/johannes/gsv/fpd/telefonica/032016/analysis/20160623/countIds.xml";
    String mappingFile = "/Volumes/johannes/sge/prj/fpd/data/counts/counts2014-directions.csv";

    Counts<Link> counts = new Counts();
    MatsimCountsReader countsReader = new MatsimCountsReader(counts);
    countsReader.readFile(countsInFile);

    BufferedReader txtReader = new BufferedReader(new FileReader(mappingFile));
    String line = txtReader.readLine();

    Map<String, Record> records = new HashMap<>();

    while ((line = txtReader.readLine()) != null) {
      String[] tokens = line.split("\t");
      String id = tokens[1];
      String name = tokens[2];
      double kfzR1 = parseDouble(tokens[15]);
      double kfzR2 = parseDouble(tokens[16]);
      double svR1 = parseDouble(tokens[18]);
      double svR2 = parseDouble(tokens[19]);

      Record record = new Record();
      record.id = id;
      record.name = name;
      record.pkwR1 = kfzR1 - svR1;
      record.pkwR2 = kfzR2 - svR2;

      if (records.containsKey(record.name)) {
        logger.warn(String.format("Entry for %s already exists - removing both.", record.name));
        records.remove(record.name);
      } else {
        records.put(record.name, record);
      }
    }

    Set<Count> remove = new HashSet<>();

    for (Count<Link> count : counts.getCounts().values()) {
      Record record = records.get(count.getCsLabel());
      if (record != null) {
        double pkwR1_h = record.pkwR1 / 24.0;
        double pkwR2_h = record.pkwR2 / 24.0;

        double diffR1 = Math.abs(pkwR1_h - count.getVolume(1).getValue());
        double diffR2 = Math.abs(pkwR2_h - count.getVolume(1).getValue());

        if (diffR1 < diffR2) {
          if (diffR1 > 10) {
            logger.warn(String.format("Difference %s is greater than 10.", diffR1));
          }
          count.setCsId(record.id + "_R1");
        } else {
          if (diffR2 > 10) {
            logger.warn(String.format("Difference %s is greater than 10.", diffR2));
          }
          count.setCsId(record.id + "_R2");
        }
      } else {
        logger.warn(String.format("No count station with name %s found.", count.getCsLabel()));
        remove.add(count);
      }
    }

    for (Count<Link> count : remove) {
      counts.getCounts().remove(count.getId());
    }

    CountsWriter writer = new CountsWriter(counts);
    writer.write(countsOutFile);
  }