private Coord sampleActivityLocation(String type) { Coord c = null; if (!this.propabilityMap.containsKey(type)) { /* First build the cumulative probability map. */ LOG.info("Building a cumulative probability map for activity type '" + type + "'"); /* a. Get the total number of activity observations. */ int total = 0; for (MyZone mz : this.zoneMap.values()) { Object o = mz.getObjectAttributes().getAttribute(mz.getId().toString(), type); if (o != null && o instanceof Integer) { total += (int) o; } } /* b. Calculate the cumulative probability for each zone. */ TreeMap<String, Double> map = new TreeMap<>(); int cumsum = 0; for (String zoneId : this.zoneMap.keySet()) { MyZone zone = this.zoneMap.get(zoneId); Object o = zone.getObjectAttributes().getAttribute(zoneId, type); if (o != null && o instanceof Integer) { cumsum += (int) o; double cumprob = ((double) cumsum) / ((double) total); map.put(zoneId, cumprob); } } this.propabilityMap.put(type, map); } /* Sample from the cumulative probabilities. */ double r = MatsimRandom.getLocalInstance().nextDouble(); String sampledZone = null; TreeMap<String, Double> theZoneMap = this.propabilityMap.get(type); Iterator<String> iterator = theZoneMap.navigableKeySet().iterator(); while (sampledZone == null && iterator.hasNext()) { String zoneId = iterator.next(); double d = theZoneMap.get(zoneId); if (r <= d) { sampledZone = zoneId; } } if (sampledZone == null) { LOG.error("Should never NOT find a sampled zone."); } Point p = this.zoneMap.get(sampledZone).sampleRandomInteriorPoint(); c = CoordUtils.createCoord(p.getX(), p.getY()); return c; }
public NavigableSet<K> navigableKeySet() { return realMap.navigableKeySet(); }