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

  }
Esempio n. 2
0
  @Override
  public void run(final Plan p) {
    PlanImpl plan = (PlanImpl) p;
    double dayTime = 0.0;
    double carDayTime = 0.0;
    double ptDayTime = 0.0;
    double wlkDayTime = 0.0;
    double bikeDayTime = 0.0;
    double othersDayTime = 0.0;
    for (PlanElement pe : plan.getPlanElements())
      if (pe instanceof Leg) {
        Leg bl = (Leg) pe;

        ActTypeZrh legIntent = (ActTypeZrh) this.getLegIntent(plan, bl);

        double time = bl.getTravelTime() / 60.0;
        if (time < 0) time = 0;
        // if (bl.getDepartureTime() < 86400) {
        dayTime += time;
        String mode = bl.getMode();
        if (TransportMode.car.equals(mode)) {
          carTime += time;
          carDayTime += time;
          switch (legIntent) {
            case home:
              carHomeTime += time;
              break;
            case work:
              carWorkTime += time;
              break;
            case education:
              carEducTime += time;
              break;
            case shopping:
              carShopTime += time;
              break;
            case leisure:
              carLeisTime += time;
              break;
            default:
              carOtherTime += time;
              break;
          }
          carLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.pt.equals(mode)) {
          ptTime += time;
          ptDayTime += time;
          switch (legIntent) {
            case home:
              ptHomeTime += time;
              break;
            case work:
              ptWorkTime += time;
              break;
            case education:
              ptEducTime += time;
              break;
            case shopping:
              ptShopTime += time;
              break;
            case leisure:
              ptLeisTime += time;
              break;
            default:
              ptOtherTime += time;
              break;
          }
          ptLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.walk.equals(mode)) {
          wlkTime += time;
          wlkDayTime += time;
          switch (legIntent) {
            case home:
              wlkHomeTime += time;
              break;
            case work:
              wlkWorkTime += time;
              break;
            case education:
              wlkEducTime += time;
              break;
            case shopping:
              wlkShopTime += time;
              break;
            case leisure:
              wlkLeisTime += time;
              break;
            default:
              wlkOtherTime += time;
              break;
          }
          wlkLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.bike.equals(mode)) {
          bikeTime += time;
          bikeDayTime += time;
          switch (legIntent) {
            case home:
              bikeHomeTime += time;
              break;
            case work:
              bikeWorkTime += time;
              break;
            case education:
              bikeEducTime += time;
              break;
            case shopping:
              bikeShopTime += time;
              break;
            case leisure:
              bikeLeisTime += time;
              break;
            default:
              bikeOtherTime += time;
              break;
          }
          bikeLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else {
          othersTime += time;
          othersDayTime += time;
          switch (legIntent) {
            case home:
              othersHomeTime += time;
              break;
            case work:
              othersWorkTime += time;
              break;
            case education:
              othersEducTime += time;
              break;
            case shopping:
              othersShopTime += time;
              break;
            case leisure:
              othersLeisTime += time;
              break;
            default:
              othersOtherTime += time;
              break;
          }
          othersLegTimeCounts[Math.min(100, (int) time / 2)]++;
        }
      }
    for (int i = 0; i <= Math.min(100, (int) dayTime); i++) totalDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) othersDayTime); i++) othersDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) carDayTime); i++) carDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) ptDayTime); i++) ptDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) wlkDayTime); i++) wlkDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) bikeDayTime); i++) bikeDayEnRouteTimeCounts[i]++;
  }