@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 testSetWriteEventsAlways() {
    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);
    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());
    assertTrue(
        new File(controler.getControlerIO().getIterationFilename(1, Controler.FILENAME_EVENTS_XML))
            .exists());
  }
 @Test
 public void testConstructor_EventsManagerTypeImmutable() {
   Controler controler = new Controler(new String[] {"test/scenarios/equil/config.xml"});
   try {
     controler.getConfig().setParam("parallelEventHandling", "numberOfThreads", "2");
     Assert.fail("Expected exception");
   } catch (Exception e) {
     log.info("catched expected exception", e);
   }
   try {
     controler.getConfig().setParam("parallelEventHandling", "estimatedNumberOfEvents", "200000");
     Assert.fail("Expected exception");
   } catch (Exception e) {
     log.info("catched expected exception", e);
   }
 }
  @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());
  }
 public static void main(String[] args) {
   final Config config = ConfigUtils.createConfig();
   config.controler().setLastIteration(1);
   config
       .controler()
       .setOverwriteFileSetting(
           OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles);
   Controler controler = new Controler(ScenarioUtils.createScenario(config));
   controler.addOverridingModule(
       new AbstractModule() {
         @Override
         public void install() {
           bindCarTravelDisutilityFactory().to(InterestingTravelDisutilityFactory.class);
           bindCarTravelTime().to(InterestingTravelTime.class);
           addControlerListenerBinding().to(InterestingControlerListener.class);
         }
       });
   controler.run();
 }
  @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);
    }
  }
  /** @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());
  }
 @Test
 public void testConstructor() {
   Controler controler = new Controler(new String[] {"test/scenarios/equil/config.xml"});
   assertNotNull(
       controler.getScenario().getNetwork()); // is required, e.g. for changing the factories
   assertNotNull(controler.getScenario().getPopulation());
   assertEquals(23, controler.getScenario().getNetwork().getLinks().size());
   assertEquals(15, controler.getScenario().getNetwork().getNodes().size());
   assertEquals(100, controler.getScenario().getPopulation().getPersons().size());
   assertNotNull(controler.getEvents());
 }
  /**
   * 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);
  }
Exemple #10
0
  @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());
  }
Exemple #11
0
 // TODO show tree
 void showTree(Controler program) {
   program.getTree();
 }
Exemple #12
0
  /**
   * 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 #13
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);
  }
  public void testTransitRouteCopy() {
    Config config = super.loadConfig(null);
    config.scenario().setUseTransit(true);
    config.scenario().setUseVehicles(true);
    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(config);

    Id<Node> nodeId1 = Id.create("1", Node.class);
    Id<Node> nodeId2 = Id.create("2", Node.class);
    Id<Node> nodeId3 = Id.create("3", Node.class);
    Id<Link> linkId1 = Id.create("1", Link.class);
    Id<Link> linkId2 = Id.create("2", Link.class);

    // build network
    Network network = scenario.getNetwork();
    NetworkFactory nBuilder = network.getFactory();
    Node node1 = nBuilder.createNode(nodeId1, scenario.createCoord(0, 0));
    Node node2 = nBuilder.createNode(nodeId2, scenario.createCoord(1000, 0));
    Node node3 = nBuilder.createNode(nodeId3, scenario.createCoord(2000, 0));
    network.addNode(node1);
    network.addNode(node2);
    network.addNode(node3);
    Link link1 = nBuilder.createLink(linkId1, node1, node2);
    Link link2 = nBuilder.createLink(linkId2, node2, node3);
    network.addLink(link1);
    network.addLink(link2);

    // build schedule
    TransitSchedule schedule = scenario.getTransitSchedule();
    TransitScheduleFactory sBuilder = schedule.getFactory();

    TransitStopFacility stopF1 =
        sBuilder.createTransitStopFacility(
            Id.create("1", TransitStopFacility.class), scenario.createCoord(1000.0, 0), false);
    TransitStopFacility stopF2 =
        sBuilder.createTransitStopFacility(
            Id.create("2", TransitStopFacility.class), scenario.createCoord(2000.0, 0), false);
    stopF1.setLinkId(link1.getId());
    stopF2.setLinkId(link2.getId());
    schedule.addStopFacility(stopF1);
    schedule.addStopFacility(stopF2);

    TransitLine tLine1 = sBuilder.createTransitLine(Id.create("1", TransitLine.class));

    TransitRouteStop stop1 = sBuilder.createTransitRouteStop(stopF1, 0, 0);
    TransitRouteStop stop2 = sBuilder.createTransitRouteStop(stopF2, 100, 100);
    ArrayList<TransitRouteStop> stops = new ArrayList<TransitRouteStop>();
    stops.add(stop1);
    stops.add(stop2);

    NetworkRoute netRoute = new LinkNetworkRouteImpl(link1.getId(), link2.getId());
    netRoute.setLinkIds(link1.getId(), Collections.<Id<Link>>emptyList(), link2.getId());
    TransitRoute tRoute1 =
        sBuilder.createTransitRoute(Id.create("1", TransitRoute.class), netRoute, stops, "bus");

    tRoute1.addDeparture(sBuilder.createDeparture(Id.create("1", Departure.class), 7.0 * 3600));
    tLine1.addRoute(tRoute1);
    schedule.addTransitLine(tLine1);

    // build vehicles
    new CreateVehiclesForSchedule(schedule, scenario.getVehicles()).run();

    // build population
    Population population = scenario.getPopulation();
    PopulationFactory pBuilder = population.getFactory();
    Person person1 = pBuilder.createPerson(Id.create("1", Person.class));
    Plan plan = pBuilder.createPlan();
    Activity homeAct = pBuilder.createActivityFromLinkId("h", linkId1);
    homeAct.setEndTime(7.0 * 3600);
    plan.addActivity(homeAct);
    Leg leg = pBuilder.createLeg(TransportMode.pt);
    ExperimentalTransitRoute tRoute = new ExperimentalTransitRoute(stopF1, tLine1, tRoute1, stopF2);
    leg.setRoute(tRoute);
    plan.addLeg(leg);
    plan.addActivity(pBuilder.createActivityFromLinkId("w", linkId2));
    person1.addPlan(plan);
    population.addPerson(person1);

    // prepare config
    config.controler().setFirstIteration(0);
    config.controler().setLastIteration(1);

    ActivityParams params = new ActivityParams("h");
    params.setTypicalDuration(16.0 * 3600);
    config.planCalcScore().addActivityParams(params);
    params = new ActivityParams("w");
    params.setTypicalDuration(8.0 * 3600);
    config.planCalcScore().addActivityParams(params);

    StrategySettings tam = new StrategySettings(Id.create(1, StrategySettings.class));
    tam.setStrategyName("TimeAllocationMutator");
    tam.setWeight(1.0);
    config.strategy().addStrategySettings(tam);

    // run
    Controler controler = new Controler(scenario);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.setCreateGraphs(false);
    controler.run();

    // checks
    assertEquals(1, population.getPersons().size());
    assertEquals(2, person1.getPlans().size());
    assertEquals(
        ExperimentalTransitRoute.class,
        ((Leg) person1.getPlans().get(0).getPlanElements().get(1)).getRoute().getClass());
    assertEquals(
        ExperimentalTransitRoute.class,
        ((Leg) person1.getPlans().get(1).getPlanElements().get(1)).getRoute().getClass());
  }
Exemple #15
0
 public void showView(HasWidgets container) {
   super.showView(container);
 }