@Override
 public void startRow(final String[] row) throws IllegalArgumentException {
   if (!this.isFirstLine) {
     // For each station of tabular file
     String strStop = row[0];
     Id<Link> stopId = Id.create(strStop, Link.class);
     String str_cs_id = row[1];
     counts.createAndAddCount(stopId, str_cs_id);
     Count count = counts.getCount(stopId);
     Coord coord = new CoordImpl(row[2], row[3]);
     count.setCoord(coord);
     count.createVolume(1, Double.parseDouble(row[5])); // set the 24 hours volume to volume 1;
     // count.createVolume(1, Double.parseDouble(row[4]));  //boarding;
     // count.createVolume(1, Double.parseDouble(row[6]));  //alighting;
   } else {
     boolean equalsHeader = true;
     int i = 0;
     for (String s : row) {
       if (!s.equalsIgnoreCase(HEADER[i])) {
         equalsHeader = false;
         break;
       }
       i++;
     }
     if (!equalsHeader) {
       log.warn("the structure does not match. The header should be:  ");
       for (String g : HEADER) {
         System.out.print(g + " ");
       }
       System.out.println();
     }
     this.isFirstLine = false;
   }
 }
  public void writeResultsForSelectedStopIds(
      final String filename, final Counts occupCounts, final Collection<Id> stopIds) {
    SimpleWriter writer = new SimpleWriter(filename);

    final String TAB = "\t";
    final String NL = "\n";

    // write header
    writer.write("stopId\t");
    for (int i = 0; i < 24; i++) {
      writer.write("oc" + i + "-" + (i + 1) + TAB);
    }
    for (int i = 0; i < 24; i++) {
      writer.write("scalSim" + i + "-" + (i + 1) + TAB);
    }
    writer.write("coordinate\tcsId\n");

    // write content
    for (Id stopId : stopIds) {
      // get count data
      Count count = occupCounts.getCounts().get(stopId);
      if (!occupCounts.getCounts().containsKey(stopId)) {
        continue;
      }

      // get sim-Values
      int[] ocuppancy = this.occupancies.get(stopId);
      writer.write(stopId.toString() + TAB);
      for (int i = 0; i < ocuppancy.length; i++) {
        Volume v = count.getVolume(i + 1);
        if (v != null) {
          writer.write(v.getValue() + TAB);
        } else {
          writer.write("n/a" + TAB);
        }
      }
      for (int i = 0; i < ocuppancy.length; i++) {
        writer.write((ocuppancy != null ? ocuppancy[i] : 0) + TAB);
      }
      writer.write(count.getCoord().toString() + TAB + count.getCsId() + NL);
    }
    writer.write(this.occupancyRecord.toString());
    writer.close();
  }
  @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);
  }
Exemple #4
0
  public static <T> AnalyticalCalibrator<T> buildCalibrator(
      final Config config,
      final Counts<T> occupCounts,
      LookUpItemFromId<T> lookUp,
      Class<T> idType) {
    CadytsConfigGroup cadytsConfig =
        ConfigUtils.addOrGetModule(config, CadytsConfigGroup.GROUP_NAME, CadytsConfigGroup.class);

    // get timeBinSize_s and validate it
    int timeBinSize_s = cadytsConfig.getTimeBinSize();
    if (timeBinSize_s != 86400) {
      throw new RuntimeException("Time bin size has to be 86400!");
    }

    if (occupCounts.getCounts().size() == 0) {
      log.warn("Counts container is empty.");
    }

    AnalyticalCalibrator<T> matsimCalibrator =
        new AnalyticalCalibrator<T>(
            config.controler().getOutputDirectory() + "/cadyts.log",
            MatsimRandom.getLocalInstance().nextLong(),
            timeBinSize_s);

    matsimCalibrator.setRegressionInertia(cadytsConfig.getRegressionInertia());
    matsimCalibrator.setMinStddev(cadytsConfig.getMinFlowStddev_vehPerHour(), TYPE.FLOW_VEH_H);
    matsimCalibrator.setMinStddev(cadytsConfig.getMinFlowStddev_vehPerHour(), TYPE.COUNT_VEH);
    matsimCalibrator.setFreezeIteration(cadytsConfig.getFreezeIteration());
    matsimCalibrator.setPreparatoryIterations(cadytsConfig.getPreparatoryIterations());
    matsimCalibrator.setVarianceScale(cadytsConfig.getVarianceScale());

    matsimCalibrator.setBruteForce(cadytsConfig.useBruteForce());
    // I don't think this has an influence on any of the variants we are using. (Has an influence
    // only when plan choice is left
    // completely to cadyts, rather than just taking the score offsets.) kai, dec'13
    // More formally, one would need to use the selectPlan() method of AnalyticalCalibrator which we
    // are, however, not using. kai, mar'14
    if (matsimCalibrator.getBruteForce()) {
      log.warn(
          "setting bruteForce==true for calibrator, but this won't do anything in the way the cadyts matsim integration is set up. kai, mar'14");
    }

    matsimCalibrator.setStatisticsFile(
        config.controler().getOutputDirectory() + "/calibration-stats.txt");

    int linkCnt = 0;
    int odCount = 0;

    double odWeightFactor =
        Double.parseDouble(
            config.getParam(GsvConfigGroup.GSV_CONFIG_MODULE_NAME, "odWeightFactor"));
    for (Map.Entry<Id<T>, Count<T>> entry : occupCounts.getCounts().entrySet()) {
      // (loop over all counting "items" (usually locations/stations)

      T item = lookUp.getItem(Id.create(entry.getKey(), idType));

      Count count = entry.getValue();

      double value = 0;
      for (int i = 1; i < 25; i++) {
        value += count.getVolume(i).getValue();
      }

      if (entry.getKey().toString().startsWith(ODCalibrator.VIRTUAL_ID_PREFIX)) {
        final double stddev =
            max(
                matsimCalibrator.getMinStddev(SingleLinkMeasurement.TYPE.COUNT_VEH),
                sqrt(matsimCalibrator.getVarianceScale() * value));
        matsimCalibrator.addMeasurement(
            item, 0, 86400, value, stddev / odWeightFactor, SingleLinkMeasurement.TYPE.COUNT_VEH);
        odCount++;
      } else {
        matsimCalibrator.addMeasurement(
            item, 0, 86400, value, SingleLinkMeasurement.TYPE.COUNT_VEH);
        linkCnt++;
      }
    }

    log.info(String.format("Added %s link measurements to calibrator.", linkCnt));
    log.info(String.format("Added %s OD measurements to calibrator.", odCount));

    if (matsimCalibrator.getProportionalAssignment()) {
      throw new RuntimeException(
          "Gunnar says that this may not work so do not set to true. kai, sep'14");
    }
    return matsimCalibrator;
  }
Exemple #5
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);
  }