@SuppressWarnings("unchecked") private G randomDraw(G graph, double p, long randomSeed) { long n = graph.getVertices().size(); long M = n * (n - 1) / 2; int m = (int) (p * M); Random random = new Random(randomSeed); List<V> vertices = new ArrayList<V>((Collection<? extends V>) graph.getVertices()); ProgressLogger.init(m, 1, 5); for (int i = 0; i < m; i++) { E edge = null; while (edge == null) { V vi = vertices.get(random.nextInt((int) n)); V vj = vertices.get(random.nextInt((int) n)); edge = builder.addEdge(graph, vi, vj); } ProgressLogger.step(); // if(i % 10000 == 0) // logger.info(String.format("Created %1$s of %2$s edges.", i+1, m)); } ProgressLogger.termiante(); return graph; }
@Override public void apply(Collection<? extends Person> persons) { LandUseData landUseData = (LandUseData) dataPool.get(LandUseDataLoader.KEY); // ZoneLayer<Map<String, Object>> zoneLayer = // landUseData.getNuts3Layer(); ZoneLayer<Map<String, Object>> zoneLayer = landUseData.getModenaLayer(); List<Zone<Map<String, Object>>> zones = new ArrayList<>(zoneLayer.getZones()); TObjectDoubleHashMap<Zone<?>> zoneProba = new TObjectDoubleHashMap<>(); double sum = 0; for (Zone<Map<String, Object>> zone : zoneLayer.getZones()) { sum += (Double) zone.getAttribute().get(LandUseData.POPULATION_KEY); } for (Zone<Map<String, Object>> zone : zoneLayer.getZones()) { double inhabs = (Double) zone.getAttribute().get(LandUseData.POPULATION_KEY); double p = inhabs / sum; zoneProba.put(zone, p); } logger.info("Assigning facilities to zones..."); int unassigned = 0; FacilityData facilityData = (FacilityData) dataPool.get(FacilityDataLoader.KEY); Map<Zone<?>, List<ActivityFacility>> zoneFacilities = new IdentityHashMap<>(zones.size()); List<ActivityFacility> homeFacils = facilityData.getFacilities(ActivityTypes.HOME); ProgressLogger.init(homeFacils.size(), 2, 10); for (ActivityFacility facility : homeFacils) { Zone<?> zone = zoneLayer.getZone(MatsimCoordUtils.coordToPoint(facility.getCoord())); if (zone != null) { List<ActivityFacility> facilities = zoneFacilities.get(zone); if (facilities == null) { facilities = new ArrayList<>(); zoneFacilities.put(zone, facilities); } facilities.add(facility); } else { unassigned++; } ProgressLogger.step(); } if (unassigned > 0) { logger.warn(String.format("%s facilities are out if zone bounds.", unassigned)); } logger.info("Assigning facilities to persons..."); ProgressLogger.init(persons.size(), 2, 10); List<Person> shuffledPersons = new ArrayList<>(persons); Collections.shuffle(shuffledPersons, random); TObjectDoubleIterator<Zone<?>> it = zoneProba.iterator(); int j = 0; for (int i = 0; i < zoneProba.size(); i++) { it.advance(); int n = (int) Math.ceil(persons.size() * it.value()); List<ActivityFacility> facilities = zoneFacilities.get(it.key()); if (facilities != null) { if (n + j > persons.size()) { n = persons.size() - j; } for (int k = j; k < (j + n); k++) { ActivityFacility f = facilities.get(random.nextInt(facilities.size())); ((PlainPerson) shuffledPersons.get(k)) .setUserData(SwitchHomeLocation.USER_FACILITY_KEY, f); ProgressLogger.step(); } j += n; } } logger.info("Checking for homeless persons..."); int cnt = 0; for (Person person : shuffledPersons) { if (((PlainPerson) person).getUserData(SwitchHomeLocation.USER_FACILITY_KEY) == null) { ActivityFacility f = homeFacils.get(random.nextInt(homeFacils.size())); ((PlainPerson) person).setUserData(SwitchHomeLocation.USER_FACILITY_KEY, f); cnt++; } } if (cnt > 0) { logger.info(String.format("Assigend %s persons a random home.", cnt)); } ProgressLogger.termiante(); }
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."); }
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; }