@Test
  public void testLegAttributesIO() {
    final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig());

    final Person person = population.getFactory().createPerson(Id.createPersonId("Donald Trump"));
    population.addPerson(person);

    final Plan plan = population.getFactory().createPlan();
    person.addPlan(plan);
    final Leg leg = population.getFactory().createLeg("SUV");
    plan.addActivity(
        population.getFactory().createActivityFromLinkId("speech", Id.createLinkId(1)));
    plan.addLeg(leg);
    plan.addActivity(population.getFactory().createActivityFromLinkId("tweet", Id.createLinkId(2)));

    leg.getAttributes().putAttribute("mpg", 0.000001d);

    final String file = utils.getOutputDirectory() + "/population.xml";
    new PopulationWriter(population).writeV6(file);

    final Scenario readScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new PopulationReader(readScenario).readFile(file);

    final Person readPerson =
        readScenario.getPopulation().getPersons().get(Id.createPersonId("Donald Trump"));
    final Leg readLeg = (Leg) readPerson.getSelectedPlan().getPlanElements().get(1);

    Assert.assertEquals(
        "Unexpected Double attribute in " + readLeg.getAttributes(),
        leg.getAttributes().getAttribute("mpg"),
        readLeg.getAttributes().getAttribute("mpg"));
  }
  @Test
  public void testActivityAttributesIO() {
    final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig());

    final Person person = population.getFactory().createPerson(Id.createPersonId("Donald Trump"));
    population.addPerson(person);

    final Plan plan = population.getFactory().createPlan();
    person.addPlan(plan);
    final Activity act = population.getFactory().createActivityFromCoord("speech", new Coord(0, 0));
    plan.addActivity(act);

    act.getAttributes().putAttribute("makes sense", false);
    act.getAttributes().putAttribute("length", 1895L);

    final String file = utils.getOutputDirectory() + "/population.xml";
    new PopulationWriter(population).writeV6(file);

    final Scenario readScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new PopulationReader(readScenario).readFile(file);

    final Person readPerson =
        readScenario.getPopulation().getPersons().get(Id.createPersonId("Donald Trump"));
    final Activity readAct = (Activity) readPerson.getSelectedPlan().getPlanElements().get(0);

    Assert.assertEquals(
        "Unexpected boolean attribute in " + readAct.getAttributes(),
        act.getAttributes().getAttribute("makes sense"),
        readAct.getAttributes().getAttribute("makes sense"));

    Assert.assertEquals(
        "Unexpected Long attribute in " + readAct.getAttributes(),
        act.getAttributes().getAttribute("length"),
        readAct.getAttributes().getAttribute("length"));
  }
  @Test
  public void testCoord3dIO() {
    final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig());

    final Person person = population.getFactory().createPerson(Id.createPersonId("Donald Trump"));
    population.addPerson(person);

    final Plan plan = population.getFactory().createPlan();
    person.addPlan(plan);
    plan.addActivity(population.getFactory().createActivityFromCoord("speech", new Coord(0, 0)));
    plan.addActivity(
        population.getFactory().createActivityFromCoord("tweet", new Coord(0, 0, -100)));

    final String file = utils.getOutputDirectory() + "/population.xml";
    new PopulationWriter(population).writeV6(file);

    final Scenario readScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new PopulationReader(readScenario).readFile(file);

    final Person readPerson =
        readScenario.getPopulation().getPersons().get(Id.createPersonId("Donald Trump"));
    final Activity readSpeach = (Activity) readPerson.getSelectedPlan().getPlanElements().get(0);
    final Activity readTweet = (Activity) readPerson.getSelectedPlan().getPlanElements().get(1);

    Assert.assertFalse(
        "did not expect Z value in " + readSpeach.getCoord(), readSpeach.getCoord().hasZ());

    Assert.assertTrue("did expect T value in " + readTweet.getCoord(), readTweet.getCoord().hasZ());

    Assert.assertEquals(
        "unexpected Z value in " + readTweet.getCoord(),
        -100,
        readTweet.getCoord().getZ(),
        MatsimTestUtils.EPSILON);
  }
Exemple #4
0
 private static void createPopulation(Scenario sc, int destination) {
   Population pop = sc.getPopulation();
   pop.getPersons().clear();
   PopulationFactory fac = pop.getFactory();
   double t = 0;
   for (int i = 0; i < NR_AGENTS / 2; i++) {
     Person pers = fac.createPerson(Id.create("b" + i, Person.class));
     Plan plan = fac.createPlan();
     pers.addPlan(plan);
     Activity act0;
     act0 = fac.createActivityFromLinkId("origin", Id.create("0", Link.class));
     act0.setEndTime(t);
     plan.addActivity(act0);
     Leg leg = fac.createLeg("walkct");
     plan.addLeg(leg);
     Activity act1 =
         fac.createActivityFromLinkId("destination", Id.create(destination, Link.class));
     plan.addActivity(act1);
     pop.addPerson(pers);
   }
   //		for (int i = NR_AGENTS / 2; i < NR_AGENTS; i++) {
   //			Person pers = fac.createPerson(Id.create("b" + i, Person.class));
   //			Plan plan = fac.createPlan();
   //			pers.addPlan(plan);
   //			Activity act0;
   //			act0 = fac.createActivityFromLinkId("origin",
   //					Id.create(destination + 1, Link.class));
   //			act0.setEndTime(t);
   //			plan.addActivity(act0);
   //			Leg leg = fac.createLeg("walkct");
   //			plan.addLeg(leg);
   //			Activity act1 = fac.createActivityFromLinkId("destination",
   //					Id.create(1, Link.class));
   //			plan.addActivity(act1);
   //			pop.addPerson(pers);
   //		}
 }
  @Override
  public void notifyShutdown(ShutdownEvent event) {
    Scenario newScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    Population newPopulation = newScenario.getPopulation();
    MoneyEventHandler moneyHandler = choiceGenerationControler.getMoneyEventHandler();
    CrowdingPsimHandler crowdingPsimHandler = choiceGenerationControler.getCrowdingPsimHandler();

    for (Id<Person> personId : population.getPersons().keySet()) {

      Id<Person> orgPersonId = Id.create(personId.toString().split("_")[0], Person.class);

      Double monetaryPayments = moneyHandler.getPersonId2amount().get(personId);
      if (monetaryPayments == null) {
        monetaryPayments = 0.0;
      }

      Plan planToAdd = null;
      if (personId.toString().contains("_")) {
        planToAdd = population.getPersons().get(personId).getSelectedPlan();
        planToAdd.getCustomAttributes().put("toll", monetaryPayments.toString());
        if (crowdingPsimHandler.getPerson2crowdingDisutility().containsKey(personId)) {
          planToAdd.setScore(
              planToAdd.getScore()
                  + crowdingPsimHandler.getPerson2crowdingDisutility().get(personId));
        }
      } else {
        planToAdd = initialPlans.get(personId);
        // planToAdd.getCustomAttributes().put("toll", monetaryPayments.toString());
      }

      if (newPopulation.getPersons().containsKey(orgPersonId)) {
        newPopulation.getPersons().get(orgPersonId).addPlan(planToAdd);
      } else {
        Person newPerson = newPopulation.getFactory().createPerson(orgPersonId);
        newPerson.addPlan(planToAdd);
        newPopulation.addPerson(newPerson);
      }
    }

    /*Write score table to SQL*/
    Integer relativeOutputDirectory =
        event.getServices().getConfig().controler().getOutputDirectory().split("/").length;

    String tableName = schema + ".scores_";
    String tableSuffix =
        event
            .getServices()
            .getConfig()
            .controler()
            .getOutputDirectory()
            .split("/")[relativeOutputDirectory - 1];
    tableSuffix = tableSuffix.replaceAll("\\.0x", "x");
    tableSuffix = tableSuffix.replaceAll("-", "_");
    tableSuffix = tableSuffix.replaceAll("\\.5", "5");
    tableSuffix = tableSuffix.replaceAll("\\.1", "1");
    tableSuffix = tableSuffix.replaceAll("\\.0", "0");
    tableName = tableName + tableSuffix;

    IndividualScoreFromPopulationSQLWriter sqlWriter =
        new IndividualScoreFromPopulationSQLWriter(event.getServices().getConfig(), newPopulation);
    sqlWriter.writeToDatabase(connectionPropertiesPath, schema, tableName);

    new PopulationWriter(newScenario.getPopulation())
        .write(
            choiceGenerationControler.getControler().getConfig().controler().getOutputDirectory()
                + "/output_plansJoin.xml");
  }
  public void CreateChoiceSets() {

    PopulationFactory populationFactory = population.getFactory();
    ArrayList<Person> newPersons = new ArrayList<>();

    /*Clean routes*/
    PlanRouteStripper planRouteStripper = new PlanRouteStripper();
    planRouteStripper.run(population);

    for (Id<Person> personId : population.getPersons().keySet()) {

      Plan plan = population.getPersons().get(personId).getSelectedPlan();
      plan.getCustomAttributes()
          .put(
              "toll",
              population
                  .getPersonAttributes()
                  .getAttribute(personId.toString(), "selectedPlanToll"));
      initialPlans.put(personId, plan);

      population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();

      Plan planTmp = population.getPersons().get(personId).getSelectedPlan();
      population.getPersons().get(personId).getPlans().clear();
      population.getPersons().get(personId).addPlan(planTmp);
    }

    /*Create optimal walk plan and substitute it in the planMap*/
    OptimalWalkPlanFinder optimalWalkPlanFinder = new OptimalWalkPlanFinder(controler.getConfig());

    for (Id<Person> personId : population.getPersons().keySet()) {

      /* Plan cloning for each mode*/
      HashMap<String, Plan> planMap = new HashMap<>();

      Leg firstLeg =
          (Leg) population.getPersons().get(personId).getSelectedPlan().getPlanElements().get(1);
      String initialMode = firstLeg.getMode();

      // If "No PT"
      if (initialMode == "pt" && simType.equals("noPT")) initialMode = "walk";

      planMap.put(initialMode, population.getPersons().get(personId).getSelectedPlan());

      // String[] modes =
      // controler.getScenario().getConfig().getModule("subtourModeChoice").getValue("modes").split(",");
      // String[] modes = {"car","walk"};

      ArrayList<String> relevantModes = new ArrayList<String>();
      relevantModes.add("car");
      if (!simType.equals("carOnly") && !simType.equals("noPT")) relevantModes.add("pt");

      for (String mode : relevantModes) {
        if (!initialMode.equals(mode) && !mode.equals("walk")) {
          population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
          Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan();

          for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) {
            Leg leg = (Leg) planForModeChange.getPlanElements().get(j);
            leg.setMode(mode);
          }

          planMap.put(mode, planForModeChange);
        }
      }

      /*Check if walk is a viable alternative and if so, add it to the choice set*/
      if (!initialMode.equals("walk")
          && optimalWalkPlanFinder.getWalkTravelTime(planMap.get(initialMode)) <= 3600.0
          && !simType.equals("carOnly")) {
        population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
        Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan();
        planForModeChange = optimalWalkPlanFinder.findOptimalWalkPlan(planForModeChange);
        for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) {
          Leg leg = (Leg) planForModeChange.getPlanElements().get(j);
          leg.setMode("walk");
        }

        planMap.put("walk", planForModeChange);
      }
      ;

      // String[] relevantModes = {"car","pt"};

      /* Departure time modification for each mode*/
      for (String mode : relevantModes) {
        Plan basePlan = planMap.get(mode);
        population.getPersons().get(personId).setSelectedPlan(basePlan);

        for (int i = 0; i < 6; i++) {
          population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
        }

        population.getPersons().get(personId).setSelectedPlan(basePlan);

        int planModCounter = 0;
        /* Departure time modification */
        for (Plan planToModify : population.getPersons().get(personId).getPlans()) {

          if (((Leg) planToModify.getPlanElements().get(1)).getMode().equals(mode)
              && !PersonUtils.isSelected(planToModify)) {

            // Home departure time + 1h
            if (planModCounter == 0) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() + 3600.0);
            }

            // Home departure time - 1h
            if (planModCounter == 1) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() - 3600.0);
            }

            // Work departure time + 1h
            if (planModCounter == 2) {
              Activity act = (Activity) planToModify.getPlanElements().get(2);
              act.setEndTime(act.getEndTime() + 3600.0);
            }

            // Work departure time - 1h
            if (planModCounter == 3) {
              Activity act = (Activity) planToModify.getPlanElements().get(2);
              act.setEndTime(act.getEndTime() - 3600.0);
            }

            // Home departure time + 1h, Work departure time +1h
            if (planModCounter == 4) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() + 3600.0);

              Activity act2 = (Activity) planToModify.getPlanElements().get(2);
              act2.setEndTime(act2.getEndTime() + 3600.0);
            }

            // Home departure time - 1h, Work departure time -1h
            if (planModCounter == 5) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() - 3600.0);

              Activity act2 = (Activity) planToModify.getPlanElements().get(2);
              act2.setEndTime(act2.getEndTime() - 3600.0);
            }

            //                      NOT REALISTIC
            //						//Home departure time +1h, Work departure time -1h
            //						if (planModCounter == 6) {
            //							Activity act = (Activity) planToModify.getPlanElements().get(0);
            //							act.setEndTime(act.getEndTime() + 3600.0);
            //
            //							Activity act2 = (Activity) planToModify.getPlanElements().get(2);
            //							act2.setEndTime(act2.getEndTime() - 3600.0);
            //						}
            //
            //						//Home departure time - 1h, Work departure time +1h
            //						if (planModCounter == 7) {
            //							Activity act = (Activity) planToModify.getPlanElements().get(0);
            //							act.setEndTime(act.getEndTime() - 3600.0);
            //
            //							Activity act2 = (Activity) planToModify.getPlanElements().get(2);
            //							act2.setEndTime(act2.getEndTime() + 3600.0);
            //						}

            planModCounter++;
          }
        }
      }

      /*Plan split to different persons*/
      int count = 0;
      population.getPersons().get(personId).setSelectedPlan(planMap.get(initialMode));
      for (Plan newPlan : population.getPersons().get(personId).getPlans()) {
        if (personId.toString().equals("1000")) System.out.println();
        if (!PersonUtils.isSelected(newPlan)) {
          count++;
          Person newPerson =
              populationFactory.createPerson(Id.createPersonId(personId.toString() + "_" + count));
          newPerson.addPlan(newPlan);
          newPerson.setSelectedPlan(newPlan);
          newPersons.add(newPerson);
          population
              .getPersonAttributes()
              .putAttribute(
                  newPerson.getId().toString(),
                  "income",
                  population.getPersonAttributes().getAttribute(personId.toString(), "income"));
          population
              .getPersonAttributes()
              .putAttribute(
                  newPerson.getId().toString(),
                  "betaFactor",
                  population.getPersonAttributes().getAttribute(personId.toString(), "betaFactor"));
        }
      }

      /*Clear all plans from initial person*/
      //			population.getPersons().get(personId).getPlans().clear();
      //			population.getPersons().get(personId).addPlan(initialPlans.get(personId));
      //			population.getPersons().get(personId).setSelectedPlan(initialPlans.get(personId));

      /*Clear all plans from initial person*/
      population.getPersons().get(personId).getPlans().clear();

      Person tempPerson = population.getFactory().createPerson(Id.create(20000, Person.class));
      tempPerson.addPlan(initialPlans.get(personId));
      tempPerson.setSelectedPlan(initialPlans.get(personId));
      tempPerson.createCopyOfSelectedPlanAndMakeSelected();
      Plan tempPlan = tempPerson.getSelectedPlan();
      population.getPersons().get(personId).addPlan(tempPlan);
      population.getPersons().get(personId).setSelectedPlan(tempPlan);
    }

    /*Add new agents to the simulation*/
    for (Person newPerson : newPersons) {
      population.addPerson(newPerson);
    }

    /*Write out new population file*/
    //		System.out.println("New number of persons: " + population.getPersons().size());
    //		new org.matsim.core.population.PopulationWriter(population,
    // controler.getScenario().getNetwork()).write("/Volumes/DATA 1 (WD 2
    // TB)/output_SelectExp1_5p_"+simType+"_1000it_Dwell/popText.xml");

  }