Beispiel #1
0
  private static NumericMatrix getMatrix(Collection<PlainPerson> persons, LegPredicate pred) {
    NumericMatrix m = new NumericMatrix();

    for (Person person : persons) {
      for (Episode episode : person.getEpisodes()) {
        for (int i = 0; i < episode.getLegs().size(); i++) {
          Segment leg = episode.getLegs().get(i);
          if (pred.test(leg)) {
            Segment prev = episode.getActivities().get(i);
            Segment next = episode.getActivities().get(i + 1);

            String origin = prev.getAttribute(SetZones.ZONE_KEY);
            String dest = next.getAttribute(SetZones.ZONE_KEY);

            if (origin != null && dest != null) {
              m.add(origin, dest, 1);

              String touched = leg.getAttribute(DBG_TOUCHED);
              int cnt = 0;
              if (touched != null) cnt = Integer.parseInt(touched);
              leg.setAttribute(DBG_TOUCHED, String.valueOf(cnt + 1));
            }
          }
        }
      }
    }

    return m;
  }
  public static void main(String args[]) throws IOException {
    String in = "/Users/jillenberger/work/matrix2014/data/matrices/tomtom.de.txt";
    String out = "/Users/jillenberger/work/matrix2014/data/matrices/tomtom.de.100KM.txt";
    String zoneFile = "/Users/jillenberger/work/matrix2014/data/zones/nuts3.psm.gk3.geojson";
    String idKey = "NO";

    NumericMatrix m = NumericMatrixIO.read(in);
    ZoneCollection zones = ZoneGeoJsonIO.readFromGeoJSON(zoneFile, idKey);
    DistanceCalculator dCalc = CartesianDistanceCalculator.getInstance();
    double threshold = 100000;

    Set<String> keys = m.keys();
    for (String i : keys) {
      Zone z_i = zones.get(i);
      for (String j : keys) {
        Zone z_j = zones.get(j);

        double d = dCalc.distance(z_i.getGeometry().getCentroid(), z_j.getGeometry().getCentroid());
        if (d < threshold) {
          m.set(i, j, null);
        }
      }
    }

    NumericMatrixIO.write(m, out);
  }
  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.");
  }
Beispiel #4
0
  public NumericMatrix generate(
      Network network, ZoneCollection zones, String zoneIdKey, int nThreads) {
    this.network = network;

    logger.info("Initializing node mappings...");
    initMappings(zones, zoneIdKey);

    logger.info("Calculation travel times...");
    ExecutorService executor = Executors.newFixedThreadPool(nThreads);
    Set<Future<Worker>> futures = new HashSet<>();

    for (Zone zone : zones.getZones()) {
      Worker worker = new Worker(zone.getAttribute(zoneIdKey));
      futures.add(executor.submit(worker, worker));
    }

    ProgressLogger.init(futures.size(), 1, 10);

    HashMatrix<String, CellData> sumMatrix = new HashMatrix<>();
    int errors = 0;

    for (Future<Worker> future : futures) {
      try {
        Worker worker = future.get();
        HashMatrix<String, CellData> m = worker.getMatrix();
        Set<String> keys = m.keys();

        for (String i : keys) {
          for (String j : keys) {

            CellData cData = m.get(i, j);
            if (cData != null) {

              CellData cDataSum = sumMatrix.get(i, j);
              if (cDataSum == null) {
                cDataSum = new CellData();
                sumMatrix.set(i, j, cDataSum);
              }

              cDataSum.count += cData.count;
              cDataSum.ttSum += cData.ttSum;
            }
          }
        }

        errors += worker.getErrors();

        ProgressLogger.step();
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }

    ProgressLogger.terminate();

    if (errors > 0) {
      logger.info(String.format("%s errors occured.", errors));
    }

    logger.info("Collection results...");

    NumericMatrix ttMatrix = new NumericMatrix();
    Set<String> keys = sumMatrix.keys();
    for (String i : keys) {
      for (String j : keys) {
        CellData cData = sumMatrix.get(i, j);
        double avr = cData.ttSum / cData.count;
        ttMatrix.set(i, j, avr);
      }
    }

    return ttMatrix;
  }