Example #1
0
 /*
  * return 0 if files are read and 1 if k values are created.
  * This is important to know for reading (case 0) or computation of maxDCScore (case 1)
  */
 public int run() {
   DestinationChoiceConfigGroup dccg =
       (DestinationChoiceConfigGroup)
           scenario.getConfig().getModule(DestinationChoiceConfigGroup.GROUP_NAME);
   String pkValuesFileName = dccg.getpkValuesFile();
   String fkValuesFileName = dccg.getfkValuesFile();
   String maxEpsValuesFileName = dccg.getMaxEpsFile();
   if (pkValuesFileName != null && fkValuesFileName != null && maxEpsValuesFileName != null) {
     ObjectAttributesXmlReader persKValuesReader =
         new ObjectAttributesXmlReader(this.personsKValues);
     ObjectAttributesXmlReader facKValuesReader =
         new ObjectAttributesXmlReader(this.facilitiesKValues);
     try {
       persKValuesReader.parse(pkValuesFileName);
       facKValuesReader.parse(fkValuesFileName);
       log.info("reading kvals from files:\n" + pkValuesFileName + "\n" + fkValuesFileName);
       return 0;
     } catch (UncheckedIOException e) {
       // reading was not successful
       log.error(
           "unsuccessful reading kvals from files!\nThe values are now computed"
               + " and following files are not considered!:\n"
               + pkValuesFileName
               + "\n"
               + fkValuesFileName);
       this.assignKValues();
       return 1;
     }
   } else {
     this.assignKValues();
     return 1;
   }
 }
  public void parse(String inputfolder) {
    /* Check that the given folder ends with a '/'. */
    inputfolder = inputfolder + (inputfolder.endsWith("/") ? "" : "/");

    /* Read population. */
    LOG.info("Reading population...");
    PopulationReaderMatsimV5 pr = new PopulationReaderMatsimV5(this.sc);
    pr.parse(inputfolder + "population.xml.gz");

    /* Read population attributes. */
    LOG.info("Reading person attributes...");
    ObjectAttributesXmlReader oar1 =
        new ObjectAttributesXmlReader(this.sc.getPopulation().getPersonAttributes());
    oar1.putAttributeConverter(IncomeImpl.class, new SAIncomeConverter());
    oar1.parse(inputfolder + "populationAttributes.xml.gz");

    /* Read households */
    LOG.info("Reading households...");
    HouseholdsReaderV10 hhr = new HouseholdsReaderV10(this.sc.getHouseholds());
    hhr.parse(inputfolder + "households.xml.gz");

    /* Read household attributes. */
    LOG.info("Reading household attributes...");
    ObjectAttributesXmlReader oar2 =
        new ObjectAttributesXmlReader(this.sc.getHouseholds().getHouseholdAttributes());
    oar2.putAttributeConverter(IncomeImpl.class, new SAIncomeConverter());
    oar2.putAttributeConverter(CoordImpl.class, new CoordConverter());
    oar2.parse(inputfolder + "householdAttributes.xml.gz");

    LOG.info("================================================================");
    LOG.info("Population size: " + sc.getPopulation().getPersons().size());
    LOG.info("Number of households: " + sc.getHouseholds().getHouseholds().size());
    LOG.info("================================================================");
  }
Example #3
0
  private static Population getPopulationWithCarLegWOCarAvail(
      String plansFile, String attributesFile, String outputFile) {

    Map<String, Boolean> agentIdString2CarAvail =
        new HashMap<String, Boolean>(); // car availability of all car users in selected plan
    Map<Id<Person>, Person> carUsersId2Person = new TreeMap<Id<Person>, Person>();
    Map<Id<Person>, Person> carUsersWoCarAvailable = new TreeMap<Id<Person>, Person>();

    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new MatsimPopulationReader(scenario).parse(plansFile);

    // collect agents using car on their tour
    for (Person person : scenario.getPopulation().getPersons().values()) {
      for (PlanElement pe : person.getSelectedPlan().getPlanElements()) {
        if (pe instanceof Leg) {
          Leg leg = (Leg) pe;
          if (leg.getMode() == TransportMode.car) {
            agentIdString2CarAvail.put(
                person.getId().toString(),
                false); // Assumption, that carAvail is false; carAvail will be checked later.
            carUsersId2Person.put(person.getId(), person);
          }
        }
      }
    }
    System.out.println("Number of agents using car: " + agentIdString2CarAvail.size());

    // read attributes -- carAvail?
    ObjectAttributes attributes = new ObjectAttributes();
    ObjectAttributesXmlReader attrReader = new ObjectAttributesXmlReader(attributes);
    attrReader.parse(attributesFile);

    // write car availability to car users.
    for (String agentIdString : agentIdString2CarAvail.keySet()) {
      System.out.println(
          agentIdString
              + ": "
              + attributes.getAttribute(agentIdString, SubpopulationName.carUsers));
      boolean carAvail =
          SubpopulationValues.carAvail.equals(
              attributes.getAttribute(agentIdString, SubpopulationName.carUsers));
      agentIdString2CarAvail.put(agentIdString, carAvail);
    }

    // Count car users with car avail
    int countCarUserswithCarAvail = 0;
    for (String agentIdString : agentIdString2CarAvail.keySet()) {
      if (agentIdString2CarAvail.get(agentIdString) == true) {
        countCarUserswithCarAvail++;
      }
    }
    System.out.println(
        countCarUserswithCarAvail
            + " of "
            + agentIdString2CarAvail.size()
            + " car users have a car available.");
    System.out.println("car user set size: " + carUsersId2Person.size());

    // separate all car users without car available
    for (Id<Person> carUserId : carUsersId2Person.keySet()) {
      if (agentIdString2CarAvail.get(carUserId.toString()) == false) {
        carUsersWoCarAvailable.put(carUserId, carUsersId2Person.get(carUserId));
      }
    }

    // write list of car users without car available
    BufferedWriter writer = IOUtils.getBufferedWriter(outputFile);
    try {
      writer.write(
          "There are "
              + carUsersWoCarAvailable.size()
              + " agents using a car, but having no car available: ");
      writer.newLine();

      for (Id<Person> carUserWoCarAvailId : carUsersWoCarAvailable.keySet()) {
        writer.write(carUserWoCarAvailId.toString());
        writer.newLine();
      }
      writer.flush();
      writer.close();

    } catch (IOException e) {
      e.printStackTrace();
    }

    // write population of car users without car available
    Population carUsersWoCarPop = scenario.getPopulation();
    carUsersWoCarPop.getPersons().clear();
    for (Person carUserWoCarAvail : carUsersWoCarAvailable.values()) {
      carUsersWoCarPop.addPerson(carUserWoCarAvail);
    }

    return carUsersWoCarPop;
  }