/**
   * Helper method to complete a plan with *wh in a consistent way. Assuming that the first activity
   * is the home activity.
   *
   * @param plan
   * @param workCoord
   * @param jobLocation
   * @author nagel
   */
  public static void completePlanToHwh(
      PlanImpl plan, Coord workCoord, ActivityFacility jobLocation) {

    // complete the first activity (home) by setting end time.
    ActivityImpl act = (ActivityImpl) plan.getFirstActivity();
    act.setEndTime(7. * 3600.); // tnicolai: make configurable: see actType1.setOpeningTime(7*3600)
    // gather coordinate and facility id needed for last activity
    Coord homeCoord = act.getCoord();
    Id homeId = act.getFacilityId();

    // set Leg
    plan.createAndAddLeg(TransportMode.car);

    // set second activity (work)
    act = plan.createAndAddActivity(Constants.ACT_WORK, workCoord);
    act.setFacilityId(jobLocation.getId());
    act.setMaximumDuration(
        8. * 3600.); // tnicolai: make configurable: actType1.setTypicalDuration(8*60*60);

    // set Leg
    plan.createAndAddLeg(TransportMode.car);

    // set last activity (=first activity) and complete home-work-home plan.
    plan.createAndAddActivity(Constants.ACT_HOME, homeCoord);
    act = (ActivityImpl) plan.getLastActivity();
    act.setFacilityId(homeId);
  }
 @Test
 public void testRandomChoice() {
   ChooseRandomSingleLegMode algo =
       new ChooseRandomSingleLegMode(
           new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk},
           MatsimRandom.getRandom());
   PlanImpl plan =
       new org.matsim.core.population.PlanImpl(
           PersonImpl.createPerson(Id.create(1, Person.class)));
   plan.createAndAddActivity("home", new Coord((double) 0, (double) 0));
   LegImpl leg = plan.createAndAddLeg(TransportMode.car);
   plan.createAndAddActivity("work", new Coord((double) 0, (double) 0));
   boolean foundCarMode = false;
   boolean foundPtMode = false;
   boolean foundWalkMode = false;
   for (int i = 0; i < 5; i++) {
     algo.run(plan);
     String mode = leg.getMode();
     if (TransportMode.car.equals(mode)) {
       foundCarMode = true;
     } else if (TransportMode.pt.equals(mode)) {
       foundPtMode = true;
     } else if (TransportMode.walk.equals(mode)) {
       foundWalkMode = true;
     } else {
       fail("unexpected mode: " + mode);
     }
   }
   assertTrue("expected to find car-mode", foundCarMode);
   assertTrue("expected to find pt-mode", foundPtMode);
   assertTrue("expected to find walk-mode", foundWalkMode);
 }
 @Test
 public void testIgnoreCarAvailability_Always() {
   ChooseRandomSingleLegMode algo =
       new ChooseRandomSingleLegMode(
           new String[] {TransportMode.car, TransportMode.pt, TransportMode.bike}, new Random(1));
   algo.setIgnoreCarAvailability(false);
   Person person = PersonImpl.createPerson(Id.create(1, Person.class));
   PersonUtils.setCarAvail(person, "always");
   PlanImpl plan = new org.matsim.core.population.PlanImpl(person);
   plan.createAndAddActivity("home", new Coord((double) 0, (double) 0));
   plan.createAndAddLeg(TransportMode.pt);
   plan.createAndAddActivity("work", new Coord((double) 0, (double) 0));
   algo.run(plan);
   assertEquals(
       "unexpected leg mode in leg 1.",
       TransportMode.car,
       ((Leg) plan.getPlanElements().get(1)).getMode());
   algo.run(plan);
   assertEquals(
       "unexpected leg mode in leg 1.",
       TransportMode.pt,
       ((Leg) plan.getPlanElements().get(1)).getMode());
   algo.run(plan);
   assertEquals(
       "unexpected leg mode in leg 1.",
       TransportMode.car,
       ((Leg) plan.getPlanElements().get(1)).getMode());
   algo.run(plan);
   assertEquals(
       "unexpected leg mode in leg 1.",
       TransportMode.bike,
       ((Leg) plan.getPlanElements().get(1)).getMode());
 }
 @Test
 public void testHandlePlan_OnlySingleLegChanged() {
   ChooseRandomSingleLegMode algo =
       new ChooseRandomSingleLegMode(
           new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk},
           MatsimRandom.getRandom());
   PlanImpl plan =
       new org.matsim.core.population.PlanImpl(
           PersonImpl.createPerson(Id.create(1, Person.class)));
   plan.createAndAddActivity("home", new Coord(0, 0));
   LegImpl leg1 = plan.createAndAddLeg(TransportMode.car);
   plan.createAndAddActivity("work", new Coord((double) 0, (double) 0));
   LegImpl leg2 = plan.createAndAddLeg(TransportMode.car);
   plan.createAndAddActivity("home", new Coord((double) 0, (double) 0));
   String oldMode1 = leg1.getMode();
   String oldMode2 = leg2.getMode();
   for (int i = 0; i < 5; i++) {
     algo.run(plan);
     int cntChanges = 0;
     String mode1 = leg1.getMode();
     String mode2 = leg2.getMode();
     if (!mode1.equals(oldMode1)) {
       cntChanges++;
       oldMode1 = mode1;
     }
     if (!mode2.equals(oldMode2)) {
       cntChanges++;
       oldMode2 = mode2;
     }
     assertEquals(1, cntChanges);
   }
 }
  /**
   * Tests if the scoring function correctly handles {@link PersonMoneyEvent}. It generates one
   * person with one plan having two activities (home, work) and a car-leg in between. It then tests
   * the scoring function by calling several methods on an instance of the scoring function with the
   * aforementioned plan.
   */
  @Test
  public void testAddMoney() {
    Fixture f = new Fixture();

    // score the same plan twice
    PersonImpl person1 = new PersonImpl(Id.create(1, Person.class));
    PlanImpl plan1 = person1.createAndAddPlan(true);
    Activity act1a =
        plan1.createAndAddActivity("home", (Id<Link>) null); // , 0, 7.0*3600, 7*3600, false);
    act1a.setEndTime(f.secondLegStartTime);
    Leg leg1 = plan1.createAndAddLeg(TransportMode.car); // , 7*3600, 100, 7*3600+100);
    leg1.setDepartureTime(f.secondLegStartTime);
    leg1.setTravelTime(f.secondLegTravelTime);
    Route route2 = new GenericRouteImpl(null, null);
    leg1.setRoute(route2);
    route2.setDistance(20000.0);
    Activity act1b =
        plan1.createAndAddActivity(
            "work",
            (Id<Link>) null); // , 7.0*3600+100, Time.UNDEFINED_TIME, Time.UNDEFINED_TIME, false);
    act1b.setStartTime(f.secondLegStartTime + f.secondLegTravelTime);
    ScoringFunction sf1 = getScoringFunctionInstance(f, person1);
    sf1.handleActivity(act1a);
    sf1.handleLeg(leg1);
    sf1.handleActivity(act1b);

    sf1.finish();
    double score1 = sf1.getScore();

    ScoringFunction sf2 = getScoringFunctionInstance(f, person1);
    sf2.handleActivity(act1a);
    sf2.addMoney(1.23);
    sf2.handleLeg(leg1);
    sf2.addMoney(-2.46);
    sf2.handleActivity(act1b);
    sf2.addMoney(4.86);
    sf2.addMoney(-0.28);
    sf2.finish();
    double score2 = sf2.getScore();

    assertEquals(1.23 - 2.46 + 4.86 - 0.28, score2 - score1, EPSILON);
  }
 @Test
 public void testHandlePlan_DifferentThanLastMode() {
   ChooseRandomSingleLegMode algo =
       new ChooseRandomSingleLegMode(
           new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk},
           MatsimRandom.getRandom());
   PlanImpl plan =
       new org.matsim.core.population.PlanImpl(
           PersonImpl.createPerson(Id.create(1, Person.class)));
   plan.createAndAddActivity("home", new Coord((double) 0, (double) 0));
   LegImpl leg = plan.createAndAddLeg(TransportMode.car);
   plan.createAndAddActivity("work", new Coord((double) 0, (double) 0));
   String oldMode = leg.getMode();
   for (int i = 0; i < 5; i++) {
     algo.run(plan);
     String mode = leg.getMode();
     assertTrue(!oldMode.equals(mode));
     oldMode = mode;
   }
 }
 @Test
 public void testHandlePlanWithoutLeg() {
   ChooseRandomSingleLegMode algo =
       new ChooseRandomSingleLegMode(
           new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk},
           MatsimRandom.getRandom());
   PlanImpl plan =
       new org.matsim.core.population.PlanImpl(
           PersonImpl.createPerson(Id.create(1, Person.class)));
   plan.createAndAddActivity("home", new Coord(0, 0));
   algo.run(plan);
   // no specific assert, but there should also be no NullPointerException or similar stuff that
   // could theoretically happen
 }
Ejemplo n.º 8
0
  private Population getTestPopulation() {
    Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    Network network = scenario.getNetwork();
    new MatsimNetworkReader(scenario.getNetwork()).readFile("test/scenarios/equil/network.xml");

    Link link1 = network.getLinks().get(Id.create(1, Link.class));
    Link link20 = network.getLinks().get(Id.create(20, Link.class));

    Population population = scenario.getPopulation();

    Person person;
    PlanImpl plan;
    LegImpl leg;
    NetworkRoute route;

    person = PopulationUtils.createPerson(Id.create("1", Person.class));
    plan = PersonUtils.createAndAddPlan(person, true);
    ActivityImpl a = plan.createAndAddActivity("h", link1.getId());
    a.setEndTime(7.0 * 3600);
    leg = plan.createAndAddLeg(TransportMode.car);
    route = new LinkNetworkRouteImpl(link1.getId(), link20.getId());
    route.setLinkIds(link1.getId(), NetworkUtils.getLinkIds("6 15"), link20.getId());
    leg.setRoute(route);
    plan.createAndAddActivity("w", link20.getId());
    population.addPerson(person);

    person = PopulationUtils.createPerson(Id.create("2", Person.class));
    plan = PersonUtils.createAndAddPlan(person, true);
    ActivityImpl a2 = plan.createAndAddActivity("h", link1.getId());
    a2.setEndTime(7.0 * 3600 + 5.0 * 60);
    leg = plan.createAndAddLeg(TransportMode.car);
    route = new LinkNetworkRouteImpl(link1.getId(), link20.getId());
    route.setLinkIds(link1.getId(), NetworkUtils.getLinkIds("6 15"), link20.getId());
    leg.setRoute(route);
    plan.createAndAddActivity("w", link20.getId());
    population.addPerson(person);

    person = PopulationUtils.createPerson(Id.create("3", Person.class));
    plan = PersonUtils.createAndAddPlan(person, true);
    ActivityImpl a3 = plan.createAndAddActivity("h", link1.getId());
    a3.setEndTime(7.0 * 3600 + 10.0 * 60);
    leg = plan.createAndAddLeg(TransportMode.car);
    route = new LinkNetworkRouteImpl(link1.getId(), link20.getId());
    route.setLinkIds(link1.getId(), NetworkUtils.getLinkIds("5 14"), link20.getId());
    leg.setRoute(route);
    plan.createAndAddActivity("w", link20.getId());
    population.addPerson(person);

    return population;
  }
 /**
  * Helper method to start a plan by inserting the home location. This is really only useful
  * together with "completePlanToHwh", which completes the plan, and benefits from the fact that
  * the Strings for the "home" and the "work" act are now concentrated here.
  *
  * @param plan
  * @param homeCoord
  * @param homeLocation
  * @author nagel
  */
 public static void makeHomePlan(PlanImpl plan, Coord homeCoord, ActivityFacility homeLocation) {
   ActivityImpl act = plan.createAndAddActivity(Constants.ACT_HOME, homeCoord);
   act.setFacilityId(
       homeLocation.getId()); // tnicolai: added facility id to compute zone2zone trips
 }