/** @author mrieser */
  @Test
  public void testSetDumpDataAtEnd_false() {
    final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
    config.controler().setLastIteration(0);
    config.controler().setWritePlansInterval(0);

    final Controler controler = new Controler(config);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });

    controler.setDumpDataAtEnd(false);
    controler.run();

    assertFalse(
        new File(controler.getControlerIO().getOutputFilename(Controler.FILENAME_POPULATION))
            .exists());
  }
    @Override
    public void run() {
      final Config config =
          ControlerTest.this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
      config.controler().setLastIteration(1);

      controler = new Controler(config);
      final CrashingMobsimFactory testFactory = new CrashingMobsimFactory();
      controler.addOverridingModule(
          new AbstractModule() {
            @Override
            public void install() {
              bindMobsim()
                  .toProvider(
                      new Provider<Mobsim>() {
                        @Override
                        public Mobsim get() {
                          return testFactory.createMobsim(
                              controler.getScenario(), controler.getEvents());
                        }
                      });
            }
          });
      controler.getConfig().controler().setCreateGraphs(false);
      controler.setDumpDataAtEnd(false);
      controler.run();
    }
  /** @author mrieser */
  @Test
  public void testSetWriteEventsXml() {
    final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
    config.controler().setLastIteration(0);
    config.controler().setWritePlansInterval(0);
    config.controler().setEventsFileFormats(EnumSet.of(EventsFileFormat.xml));

    final Controler controler = new Controler(config);
    controler.getConfig().controler().setWriteEventsInterval(1);
    assertEquals(1, controler.getConfig().controler().getWriteEventsInterval());
    controler.getConfig().controler().setCreateGraphs(false);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();

    assertTrue(
        new File(controler.getControlerIO().getIterationFilename(0, Controler.FILENAME_EVENTS_XML))
            .exists());
  }
  @Test
  public void testOneSnapshotWriterInConfig() {
    final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
    config.controler().setLastIteration(0);
    config.controler().setWriteEventsInterval(0);
    config.controler().setWritePlansInterval(0);
    config.qsim().setSnapshotPeriod(10);
    config.qsim().setSnapshotStyle(SnapshotStyle.equiDist);
    ;

    final Controler controler = new Controler(config);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.setDumpDataAtEnd(false);
    controler.run();

    assertTrue(new File(controler.getControlerIO().getIterationFilename(0, "T.veh.gz")).exists());
  }
  /**
   * Tests that a custom scoring function factory doesn't get overwritten in the initialization
   * process of the Controler.
   *
   * @author mrieser
   */
  @Test
  public void testSetScoringFunctionFactory() {
    final Config config = this.utils.loadConfig(null);
    config.controler().setLastIteration(0);

    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(config);
    // create a very simple network with one link only and an empty population
    Network network = scenario.getNetwork();
    Node node1 = network.getFactory().createNode(Id.create(1, Node.class), new Coord(0, 0));
    Node node2 = network.getFactory().createNode(Id.create(2, Node.class), new Coord(100, 0));
    network.addNode(node1);
    network.addNode(node2);
    Link link = network.getFactory().createLink(Id.create(1, Link.class), node1, node2);
    link.setLength(100);
    link.setFreespeed(1);
    link.setCapacity(3600.0);
    link.setNumberOfLanes(1);

    final Controler controler = new Controler(scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.setScoringFunctionFactory(new DummyScoringFunctionFactory());

    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();

    assertTrue(
        "Custom ScoringFunctionFactory was not set.",
        controler.getScoringFunctionFactory() instanceof DummyScoringFunctionFactory);
  }
  @Test
  public void test_ExceptionOnMissingFacilitiesFile() {
    try {
      final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
      config.controler().setLastIteration(0);
      config.controler().setWriteEventsInterval(0);
      config.controler().setWritePlansInterval(0);
      config.facilities().setInputFile("dummy/non-existing/network.xml");

      final Controler controler = new Controler(config);
      controler.addOverridingModule(
          new AbstractModule() {
            @Override
            public void install() {
              bindMobsim()
                  .toProvider(
                      new Provider<Mobsim>() {
                        @Override
                        public Mobsim get() {
                          return new FakeMobsim();
                        }
                      });
            }
          });
      controler.getConfig().controler().setCreateGraphs(false);
      controler.setDumpDataAtEnd(false);
      controler.run();
      Assert.fail("expected exception, got none.");

      // note: I moved loadScenario in the controler from run() into the constructor to mirror the
      // loading sequence one has
      // when calling new Controler(scenario).  In consequence, it fails already in the constructor;
      // one could stop after that.
      // kai, apr'15

    } catch (RuntimeException e) {
      log.info("catched expected exception.", e);
    }
  }
  @Test
  public void testKMLSnapshotWriterOnQSim() {
    final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
    config.controler().setLastIteration(2);
    config.controler().setWriteEventsInterval(0);
    config.controler().setWritePlansInterval(0);
    config.controler().setMobsim("qsim");
    config.controler().setSnapshotFormat(Arrays.asList("googleearth"));
    config.qsim().setSnapshotPeriod(600);
    config.qsim().setSnapshotStyle(SnapshotStyle.equiDist);

    final Controler controler = new Controler(config);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.setDumpDataAtEnd(false);
    controler.run();

    assertTrue(
        new File(controler.getControlerIO().getIterationFilename(0, "googleearth.kmz")).exists());
    assertTrue(
        new File(controler.getControlerIO().getIterationFilename(1, "googleearth.kmz")).exists());
    assertTrue(
        new File(controler.getControlerIO().getIterationFilename(2, "googleearth.kmz")).exists());
  }
  /** @author mrieser */
  @Test
  public void testSetWriteEventsNever() {
    final Config config = this.utils.loadConfig("test/scenarios/equil/config_plans1.xml");
    config.controler().setLastIteration(1);
    config.controler().setWritePlansInterval(0);

    final Controler controler = new Controler(config);
    assertFalse(
        "Default for Controler.writeEventsInterval should be different from the interval we plan to use, otherwise it's hard to decide if it works correctly.",
        0 == controler.getConfig().controler().getWriteEventsInterval());
    controler.getConfig().controler().setWriteEventsInterval(0);
    assertEquals(0, controler.getConfig().controler().getWriteEventsInterval());
    controler.getConfig().controler().setCreateGraphs(false);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();

    assertFalse(
        new File(controler.getControlerIO().getIterationFilename(0, Controler.FILENAME_EVENTS_XML))
            .exists());
    assertFalse(
        new File(controler.getControlerIO().getIterationFilename(1, Controler.FILENAME_EVENTS_XML))
            .exists());
  }
  /**
   * Tests that plans with missing act locations are completed (=xy2links and routed) before the
   * mobsim starts.
   *
   * @author mrieser
   */
  @Test
  public void testCalcMissingActLinks() {
    Config config = this.utils.loadConfig(null);
    Fixture f = new Fixture(config);

    /* Create a person with two plans, driving from link 1 to link 3, starting at 7am.  */
    Population population = f.scenario.getPopulation();
    PopulationFactory factory = population.getFactory();
    Person person1 = null;
    Activity act1a = null;
    Activity act1b = null;
    Activity act2a = null;
    Activity act2b = null;
    Leg leg1 = null;
    Leg leg2 = null;

    person1 = PersonImpl.createPerson(Id.create(1, Person.class));
    // --- plan 1 ---
    Plan plan1 = factory.createPlan();
    person1.addPlan(plan1);
    double x1 = -50.0;
    act1a = factory.createActivityFromCoord("h", new Coord(x1, 10.0));
    act1a.setEndTime(7.0 * 3600);
    plan1.addActivity(act1a);
    leg1 = factory.createLeg(TransportMode.car);
    plan1.addLeg(leg1);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    double y1 = -10.0;
    act1b = factory.createActivityFromCoord("h", new Coord(1075.0, y1));
    plan1.addActivity(act1b);
    // --- plan 2 ---
    Plan plan2 = factory.createPlan();
    person1.addPlan(plan2);
    double x = -50.0;
    double y = -10.0;
    act2a = factory.createActivityFromCoord("h", new Coord(x, y));
    act2a.setEndTime(7.9 * 3600);
    plan2.addActivity(act2a);
    leg2 = factory.createLeg(TransportMode.car);
    plan2.addLeg(leg2);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    act2b = factory.createActivityFromCoord("h", new Coord(1111.1, 10.0));
    plan2.addActivity(act2b);
    population.addPerson(person1);

    // Complete the configuration for our test case
    // - set scoring parameters
    ActivityParams actParams = new ActivityParams("h");
    actParams.setTypicalDuration(8 * 3600);
    actParams.setPriority(1.0);
    config.planCalcScore().addActivityParams(actParams);
    // - define iterations
    config.controler().setLastIteration(0);
    // - make sure we don't use threads, as they are not deterministic
    config.global().setNumberOfThreads(1);

    // Now run the simulation
    Controler controler = new Controler(f.scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();
    /* if something goes wrong, there will be an exception we don't catch and the test fails,
     * otherwise, everything is fine. */

    // check that BOTH plans have their act-locations calculated
    assertEquals(f.link1.getId(), act1a.getLinkId());
    assertEquals(f.link3.getId(), act1b.getLinkId());
    assertEquals(f.link1.getId(), act2a.getLinkId());
    assertEquals(f.link3.getId(), act2b.getLinkId());

    // check that BOTH plans have a route set, even when we only run 1 iteration where only one of
    // them is used.
    // assertNotNull(leg1.getRoute());
    // assertNotNull(leg2.getRoute());
    // but do not assume that the leg will be the same instance...
    for (Plan plan : new Plan[] {plan1, plan2}) {
      assertEquals(
          "unexpected plan length in " + plan.getPlanElements(), 3, plan.getPlanElements().size());
      assertNotNull(
          "null route in plan " + plan.getPlanElements(),
          ((Leg) plan.getPlanElements().get(1)).getRoute());
    }
  }
Exemple #10
0
  /**
   * Tests that the travel times are correctly calculated during the simulation.
   *
   * @author mrieser
   */
  @Test
  public void testTravelTimeCalculation() {
    Config config = this.utils.loadConfig(null);
    Fixture f = new Fixture(config);

    /* Create 2 persons driving from link 1 to link 3, both starting at the
     * same time at 7am.  */
    Population population = f.scenario.getPopulation();
    PopulationFactory factory = population.getFactory();
    Person person1 = null;

    person1 = factory.createPerson(Id.create("1", Person.class));
    Plan plan1 = factory.createPlan();
    person1.addPlan(plan1);
    Activity a1 = factory.createActivityFromLinkId("h", f.link1.getId());
    a1.setEndTime(7.0 * 3600);
    plan1.addActivity(a1);
    Leg leg1 = factory.createLeg(TransportMode.car);
    plan1.addLeg(leg1);
    NetworkRoute route1 =
        ((PopulationFactoryImpl) f.scenario.getPopulation().getFactory())
            .createRoute(NetworkRoute.class, f.link1.getId(), f.link3.getId());
    leg1.setRoute(route1);
    ArrayList<Id<Link>> linkIds = new ArrayList<Id<Link>>();
    linkIds.add(f.link2.getId());
    route1.setLinkIds(f.link1.getId(), linkIds, f.link3.getId());
    plan1.addActivity(factory.createActivityFromLinkId("h", f.link3.getId()));
    population.addPerson(person1);

    Person person2 = factory.createPerson(Id.create("2", Person.class));
    Plan plan2 = factory.createPlan();
    person2.addPlan(plan2);
    Activity a2 = factory.createActivityFromLinkId("h", f.link1.getId());
    a2.setEndTime(7.0 * 3600);
    plan2.addActivity(a2);
    Leg leg2 = factory.createLeg(TransportMode.car);
    plan2.addLeg(leg2);
    NetworkRoute route2 =
        ((PopulationFactoryImpl) f.scenario.getPopulation().getFactory())
            .createRoute(NetworkRoute.class, f.link1.getId(), f.link3.getId());
    leg2.setRoute(route2);
    route2.setLinkIds(f.link1.getId(), linkIds, f.link3.getId());
    plan2.addActivity(factory.createActivityFromLinkId("h", f.link3.getId()));
    population.addPerson(person2);

    // Complete the configuration for our test case
    // - set scoring parameters
    ActivityParams actParams = new ActivityParams("h");
    actParams.setTypicalDuration(8 * 3600);
    actParams.setPriority(1.0);
    config.planCalcScore().addActivityParams(actParams);
    // - define iterations
    config.controler().setLastIteration(0);
    // - make sure we don't use threads, as they are not deterministic
    config.global().setNumberOfThreads(0);

    // Now run the simulation
    Controler controler = new Controler(f.scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.setDumpDataAtEnd(false);
    controler.run();

    // test if we got the right result
    // the actual result is 151sec, not 150, as each vehicle "loses" 1sec in the buffer
    assertEquals(
        "TravelTimeCalculator has wrong result",
        151.0,
        controler.getLinkTravelTimes().getLinkTravelTime(f.link2, 7 * 3600, null, null),
        0.0);

    // now test that the ReRoute-Strategy also knows about these travel times...
    config.controler().setLastIteration(1);
    ConfigGroup strategyParams = config.getModule("strategy");
    strategyParams.addParam("maxAgentPlanMemorySize", "4");
    strategyParams.addParam("ModuleProbability_1", "1.0");
    strategyParams.addParam("Module_1", "ReRoute");
    // Run the simulation again
    controler = new Controler(f.scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler
        .getConfig()
        .controler()
        .setOverwriteFileSetting(
            OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.run();

    // test that the plans have the correct times
    assertEquals(
        "ReRoute seems to have wrong travel times.",
        151.0,
        ((Leg) (person1.getPlans().get(1).getPlanElements().get(1))).getTravelTime(),
        0.0);
  }