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");

  }
Ejemplo n.º 2
0
 @Override
 public Plan createCopyOfSelectedPlanAndMakeSelected() {
   return delegate.createCopyOfSelectedPlanAndMakeSelected();
 }