Example #1
0
  @Override
  public void apply(Person person1) {
    PlainPerson person = (PlainPerson) person1;
    String str = person.getAttribute("homeCoord");
    ActivityFacility fac;
    if (str != null) {
      Coord coord = string2Coord(str);
      fac = facilities.getClosest(coord, ActivityTypes.HOME);

    } else {
      fac = facilities.randomFacility(ActivityTypes.HOME);
    }
    person.setUserData(SwitchHomeLocation.USER_FACILITY_KEY, fac);
  }
  /* (non-Javadoc)
   * @see playground.johannes.gsv.synPop.analysis.AnalyzerTask#analyze(java.util.Collection, java.util.Map)
   */
  @Override
  public void analyze(Collection<PlainPerson> persons, Map<String, DescriptiveStatistics> results) {
    TDoubleArrayList ages = new TDoubleArrayList();
    TDoubleArrayList incomes = new TDoubleArrayList();

    for (PlainPerson person : persons) {
      String aStr = person.getAttribute(CommonKeys.PERSON_AGE);
      String iStr = person.getAttribute(CommonKeys.HH_INCOME);
      //			String mStr = person.getAttribute(CommonKeys.HH_MEMBERS);

      //			if(aStr != null && iStr != null && mStr != null) {
      if (aStr != null && iStr != null) {
        double age = Double.parseDouble(aStr);
        double income = Double.parseDouble(iStr);
        //				double members = Double.parseDouble(mStr);

        ages.add(age);
        //				incomes.add(income/members);
        incomes.add(income);
      }
    }

    try {
      //			TDoubleDoubleHashMap hist = Histogram.createHistogram(ages.toNativeArray(), new
      // LinearDiscretizer(5), false);
      TDoubleDoubleHashMap hist =
          Histogram.createHistogram(ages.toNativeArray(), new DummyDiscretizer(), false);
      TXTWriter.writeMap(hist, "age", "n", getOutputDirectory() + "/age.txt");

      hist = Histogram.createHistogram(incomes.toNativeArray(), new LinearDiscretizer(500), false);
      TXTWriter.writeMap(hist, "income", "n", getOutputDirectory() + "/income.txt");

      TXTWriter.writeScatterPlot(
          ages, incomes, "age", "income", getOutputDirectory() + "/age.income.txt");

      TXTWriter.writeMap(
          Correlations.mean(ages.toNativeArray(), incomes.toNativeArray()),
          "age",
          "income",
          getOutputDirectory() + "/age.income.mean.txt");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #3
0
  @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();
  }