Ejemplo n.º 1
0
    @Override
    public void notifyMobsimInitialized(final MobsimInitializedEvent e) {
      assertEquals(
          100, this.arp.getActivityPerformingAgents().size()); // all agents perform an activity
      assertEquals(0, this.arp.getActivityEndingAgents(0.0).size()); // no agent ends an activity

      QSim sim = (QSim) e.getQueueSimulation();
      for (MobsimAgent agent : sim.getAgents()) this.agents.put(agent.getId(), agent);
    }
Ejemplo n.º 2
0
  private void test1PersonStartingOnLane(boolean reduceCap) {
    fixture.create1PersonFromLink1Population();

    if (reduceCap) {
      fixture.sc.getConfig().qsim().setStartTime(3500.0);
      fixture.sc.getConfig().qsim().setEndTime(7200.0);
      LaneDefinitions20 lanes = fixture.sc.getLanes();
      Lane lane1 =
          lanes.getLanesToLinkAssignments().get(fixture.id1).getLanes().get(fixture.laneId1);
      lane1.setCapacityVehiclesPerHour(1800.0);
      Lane lane1ol =
          lanes
              .getLanesToLinkAssignments()
              .get(fixture.id1)
              .getLanes()
              .get(fixture.link1FirstLaneId);
      lane1ol.setCapacityVehiclesPerHour(1800.0);
    }

    EventsManager events = EventsUtils.createEventsManager();
    MixedLanesEventsHandler handler = new MixedLanesEventsHandler(this.fixture);
    events.addHandler(handler);
    QSim qsim = QSimUtils.createDefaultQSim(this.fixture.sc, events);
    qsim.run();
    Assert.assertNotNull(handler.lastAgentDepartureEvent);
    Assert.assertEquals(3600.0, handler.lastAgentDepartureEvent.getTime(), testUtils.EPSILON);

    Assert.assertNull(handler.lastLink1EnterEvent);

    Assert.assertNull(handler.lastLane1olEnterEvent);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT, handler.lastLane1olLeaveEvent.getTime(), testUtils.EPSILON);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT, handler.lastLane1EnterEvent.getTime(), testUtils.EPSILON);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1,
        handler.lastLane1LeaveEvent.getTime(),
        testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink2EnterEvent);
    Assert.assertEquals(
        this.fixture.pid1, handler.vehId2DriverId.get(handler.lastLink2EnterEvent.getVehicleId()));
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1,
        handler.lastLink2EnterEvent.getTime(),
        testUtils.EPSILON);

    Assert.assertNull(handler.lastLink3EnterEvent);
  }
Ejemplo n.º 3
0
  public void testArriveAtStop() {
    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig());

    NetworkImpl network = (NetworkImpl) scenario.getNetwork();
    Node node1 = network.createAndAddNode(Id.create("1", Node.class), new CoordImpl(0, 0));
    Node node2 = network.createAndAddNode(Id.create("2", Node.class), new CoordImpl(1000, 0));
    Node node3 = network.createAndAddNode(Id.create("3", Node.class), new CoordImpl(2000, 0));
    network.createAndAddLink(Id.create("1", Link.class), node1, node2, 1000.0, 10.0, 3600.0, 1);
    network.createAndAddLink(Id.create("2", Link.class), node2, node3, 1000.0, 10.0, 3600.0, 1);

    TransitScheduleFactory builder = new TransitScheduleFactoryImpl();
    PopulationFactory pb = scenario.getPopulation().getFactory();
    Person person = pb.createPerson(Id.create("1", Person.class));
    Plan plan = pb.createPlan();
    person.addPlan(plan);
    Activity homeAct = pb.createActivityFromLinkId("home", Id.create("1", Link.class));
    Leg leg = pb.createLeg(TransportMode.pt);
    TransitStopFacility stop1 =
        builder.createTransitStopFacility(
            Id.create("1", TransitStopFacility.class), scenario.createCoord(100, 100), false);
    TransitStopFacility stop2 =
        builder.createTransitStopFacility(
            Id.create("2", TransitStopFacility.class), scenario.createCoord(900, 100), false);
    TransitStopFacility stop3 =
        builder.createTransitStopFacility(
            Id.create("3", TransitStopFacility.class), scenario.createCoord(1900, 100), false);
    TransitLine line1 = builder.createTransitLine(Id.create("L1", TransitLine.class));
    leg.setRoute(new ExperimentalTransitRoute(stop1, line1, null, stop2));
    Activity workAct = pb.createActivityFromLinkId("work", Id.create("2", Link.class));
    plan.addActivity(homeAct);
    plan.addLeg(leg);
    plan.addActivity(workAct);

    QSim sim = (QSim) new QSimFactory().createMobsim(scenario, EventsUtils.createEventsManager());
    TransitAgent agent = TransitAgent.createTransitAgent(person, sim);
    sim.insertAgentIntoMobsim(agent);
    agent.endActivityAndComputeNextState(10);

    assertFalse(agent.getExitAtStop(stop1));
    assertTrue(agent.getExitAtStop(stop2));
    assertFalse(agent.getExitAtStop(stop3));
    assertTrue(agent.getExitAtStop(stop2)); // offering the same stop again should yield "true"
  }
Ejemplo n.º 4
0
  /**
   * Tests travel times in the same scenario as above but without lanes. Note, that they are
   * different in this example!
   *
   * @author thunig
   */
  @Test
  public void testLink2PersonsDriving() {
    log.info("starting testLink2PersonsDriving()");

    fixture.sc.getConfig().qsim().setUseLanes(false);

    fixture.create2PersonPopulation();
    EventsManager events = EventsUtils.createEventsManager();

    MixedLanesEventsHandler handler = new MixedLanesEventsHandler(fixture);
    events.addHandler(handler);

    QSim qsim = (QSim) QSimUtils.createDefaultQSim(fixture.sc, events);
    qsim.run();

    Assert.assertNotNull(handler.lastAgentDepartureEvent);
    Assert.assertEquals(3600.0, handler.lastAgentDepartureEvent.getTime(), testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink1EnterEvent);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT, handler.lastLink1EnterEvent.getTime(), testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink2EnterEvent);
    Assert.assertEquals(
        fixture.pid1, handler.vehId2DriverId.get(handler.lastLink2EnterEvent.getVehicleId()));
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + linkTTWithoutLanes,
        handler.lastLink2EnterEvent.getTime(),
        testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink3EnterEvent);
    Assert.assertEquals(
        fixture.pid2, handler.vehId2DriverId.get(handler.lastLink3EnterEvent.getVehicleId()));
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + linkTTWithoutLanes,
        handler.lastLink3EnterEvent.getTime(),
        testUtils.EPSILON);
  }
Ejemplo n.º 5
0
  private void testMixedLane2PersonsDriving(boolean reduceCap) {
    fixture.create2PersonPopulation();

    // if no capacity is reduced the delay of both agents should be zero
    double delayOfAgent1 = 0;
    double delayOfAgent2 = 0;

    if (reduceCap) {
      // reduce capacity on lane 1
      LaneDefinitions20 lanes = fixture.sc.getLanes();
      Lane lane1 =
          lanes.getLanesToLinkAssignments().get(fixture.id1).getLanes().get(fixture.laneId1);
      lane1.setCapacityVehiclesPerHour(1800.0);

      // the delay of the second agent on lane 1 should be two seconds if
      // capacity is reduced to 1800 veh/h
      delayOfAgent2 = 2;
    }

    EventsManager events = EventsUtils.createEventsManager();

    MixedLanesEventsHandler handler = new MixedLanesEventsHandler(this.fixture);
    events.addHandler(handler);

    QSim qsim = (QSim) QSimUtils.createDefaultQSim(this.fixture.sc, events);
    qsim.run();

    Assert.assertNotNull(handler.lastAgentDepartureEvent);
    Assert.assertEquals(3600.0, handler.lastAgentDepartureEvent.getTime(), testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink1EnterEvent);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT, handler.lastLink1EnterEvent.getTime(), testUtils.EPSILON);

    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT, handler.lastLane1olEnterEvent.getTime(), testUtils.EPSILON);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1ol,
        handler.lastLane1olLeaveEvent.getTime(),
        testUtils.EPSILON);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1ol,
        handler.lastLane1EnterEvent.getTime(),
        testUtils.EPSILON);
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1ol + laneTTlane1 + delayOfAgent2,
        handler.lastLane1LeaveEvent.getTime(),
        testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink2EnterEvent);
    Assert.assertEquals(
        this.fixture.pid1, handler.vehId2DriverId.get(handler.lastLink2EnterEvent.getVehicleId()));
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1ol + laneTTlane1 + delayOfAgent2,
        handler.lastLink2EnterEvent.getTime(),
        testUtils.EPSILON);

    Assert.assertNotNull(handler.lastLink3EnterEvent);
    Assert.assertEquals(
        this.fixture.pid2, handler.vehId2DriverId.get(handler.lastLink3EnterEvent.getVehicleId()));
    Assert.assertEquals(
        3600.0 + firstLinkOrLaneTT + laneTTlane1ol + laneTTlane1 + delayOfAgent1,
        handler.lastLink3EnterEvent.getTime(),
        testUtils.EPSILON);
  }
  private void run(
      final LinkDynamics linkDynamics,
      final TrafficDynamics trafficDynamics,
      final boolean isUsingFastCapacityUpdate) {

    MatsimRandom.reset();
    scenario = ScenarioUtils.loadScenario(ConfigUtils.createConfig());
    createNetwork();

    storeVehicleTypeInfo();

    scenario.getConfig().qsim().setMainModes(Arrays.asList(travelModes));
    scenario.getConfig().qsim().setEndTime(14 * 3600);
    scenario.getConfig().qsim().setLinkDynamics(linkDynamics.name());

    if (linkDynamics.equals(LinkDynamics.SeepageQ)) {
      scenario.getConfig().qsim().setSeepMode("bike");
      scenario.getConfig().qsim().setSeepModeStorageFree(false);
      scenario.getConfig().qsim().setRestrictingSeepage(true);
    }

    scenario
        .getConfig()
        .vspExperimental()
        .setVspDefaultsCheckingLevel(VspDefaultsCheckingLevel.abort);
    scenario.getConfig().qsim().setTrafficDynamics(trafficDynamics);

    scenario.getConfig().qsim().setUsingFastCapacityUpdate(isUsingFastCapacityUpdate);

    // equal modal split run
    Map<String, Integer> minSteps = new HashMap<String, Integer>();

    double pcu1 = modeVehicleTypes.get(travelModes[0]).getPcuEquivalents();
    double pcu2 = modeVehicleTypes.get(travelModes[1]).getPcuEquivalents();

    if (pcu1 == 1 && pcu2 == 0.25) { // car bike
      minSteps.put(travelModes[0], 1);
      minSteps.put(travelModes[1], 4);
    } else { // car truck
      minSteps.put(travelModes[0], 3);
      minSteps.put(travelModes[1], 1);
    }

    int reduceNoOfDataPointsInPlot = 4; // 1--> will generate all possible data points;

    double networkDensity = 3. * (1000. / 7.5);
    double sumOfPCUInEachStep =
        (modeVehicleTypes.get(travelModes[0]).getPcuEquivalents() * minSteps.get(travelModes[0]))
            + (modeVehicleTypes.get(travelModes[1]).getPcuEquivalents()
                * minSteps.get(travelModes[1]));
    int numberOfPoints =
        (int) Math.ceil(networkDensity / (reduceNoOfDataPointsInPlot * sumOfPCUInEachStep)) + 5;

    List<Map<String, Integer>> points2Run = new ArrayList<Map<String, Integer>>();

    for (int m = 1; m < numberOfPoints; m++) {
      Map<String, Integer> pointToRun = new HashMap<>();
      for (String mode : travelModes) {
        pointToRun.put(mode, minSteps.get(mode) * m * reduceNoOfDataPointsInPlot);
      }

      double density = 0;
      for (String mode : pointToRun.keySet()) {
        double pcu = this.modeVehicleTypes.get(mode).getPcuEquivalents();
        density += pcu * pointToRun.get(mode);
      }

      if (density <= networkDensity + 10) {
        System.out.println("Number of Agents - \t" + pointToRun.toString());
        points2Run.add(pointToRun);
      }
    }

    Map<Double, Map<String, Tuple<Double, Double>>> outData =
        new HashMap<Double, Map<String, Tuple<Double, Double>>>();

    for (Map<String, Integer> point2run : points2Run) {

      System.out.println("\n \n \t \t Running points " + point2run.toString() + "\n \n");

      int count = 0;
      person2Mode.clear();

      for (String mode : point2run.keySet()) {
        for (int ii = 0; ii < point2run.get(mode); ii++) {
          person2Mode.put(Id.createPersonId(count++), mode);
        }
        this.mode2FlowData
            .get(modeVehicleTypes.get(mode).getId())
            .setnumberOfAgents(point2run.get(mode));
      }

      EventsManager events = EventsUtils.createEventsManager();
      globalFlowDynamicsUpdator = new GlobalFlowDynamicsUpdator(mode2FlowData);
      events.addHandler(globalFlowDynamicsUpdator);

      final QSim qSim = new QSim(scenario, events);
      ActivityEngine activityEngine = new ActivityEngine(events, qSim.getAgentCounter());
      qSim.addMobsimEngine(activityEngine);
      qSim.addActivityHandler(activityEngine);
      QNetsimEngine netsimEngine = new QNetsimEngine(qSim);
      qSim.addMobsimEngine(netsimEngine);
      qSim.addDepartureHandler(netsimEngine.getDepartureHandler());

      final Map<String, VehicleType> travelModesTypes = new HashMap<String, VehicleType>();

      for (String mode : travelModes) {
        travelModesTypes.put(mode, modeVehicleTypes.get(mode));
      }

      AgentSource agentSource =
          new AgentSource() {
            @Override
            public void insertAgentsIntoMobsim() {
              for (Id<Person> personId : person2Mode.keySet()) {
                String travelMode = person2Mode.get(personId);
                double actEndTime = (MatsimRandom.getRandom().nextDouble()) * 900;

                MobsimAgent agent =
                    new MySimplifiedRoundAndRoundAgent(personId, actEndTime, travelMode);
                qSim.insertAgentIntoMobsim(agent);

                final Vehicle vehicle =
                    VehicleUtils.getFactory()
                        .createVehicle(
                            Id.create(agent.getId(), Vehicle.class),
                            travelModesTypes.get(travelMode));
                final Id<Link> linkId4VehicleInsertion = Id.createLinkId("home");
                qSim.createAndParkVehicleOnLink(vehicle, linkId4VehicleInsertion);
              }
            }
          };

      qSim.addAgentSource(agentSource);

      qSim.run();

      Map<String, Tuple<Double, Double>> mode2FlowSpeed =
          new HashMap<String, Tuple<Double, Double>>();
      for (int i = 0; i < travelModes.length; i++) {

        Tuple<Double, Double> flowSpeed =
            new Tuple<Double, Double>(
                this.mode2FlowData
                    .get(Id.create(travelModes[i], VehicleType.class))
                    .getPermanentFlow(),
                this.mode2FlowData
                    .get(Id.create(travelModes[i], VehicleType.class))
                    .getPermanentAverageVelocity());
        mode2FlowSpeed.put(travelModes[i], flowSpeed);
        outData.put(
            globalFlowDynamicsUpdator.getGlobalData().getPermanentDensity(), mode2FlowSpeed);
      }
    }

    /*
     *	Basically overriding the helper.getOutputDirectory() method, such that,
     *	if file directory does not exists or same file already exists, remove and re-creates the whole dir hierarchy so that
     *	all existing files are re-written
     *	else, just keep adding files in the directory.
     *	This is necessary in order to allow writing different tests results from JUnit parameterization.
     */

    String outDir =
        "test/output/"
            + CreateAutomatedFDTest.class.getCanonicalName().replace('.', '/')
            + "/"
            + helper.getMethodName()
            + "/";
    String fileName = linkDynamics + "_" + trafficDynamics + ".png";
    String outFile;
    // ZZ_TODO : what is there exists some different directory => changing method name will keep
    // collecting the old data.
    if (!new File(outDir).exists() || new File(outDir + fileName).exists()) {
      outFile = helper.getOutputDirectory() + fileName;
    } else {
      outFile = outDir + fileName;
    }

    // plotting data
    scatterPlot(outData, outFile);
  }
Ejemplo n.º 7
0
  @Override
  public Mobsim get() {
    QSimConfigGroup configGroup = scenario.getConfig().qsim();
    if (configGroup == null) {
      throw new NullPointerException(
          "There is no configuration set for the QSim. Please add the module 'qsim' to your config file.");
    }

    /* Set up the QSim. */
    QSim qSim = new QSim(scenario, eventsManager);
    /* Add the activity engine. */
    ActivityEngine activityEngine = new ActivityEngine(eventsManager, qSim.getAgentCounter());
    qSim.addMobsimEngine(activityEngine);
    qSim.addActivityHandler(activityEngine);

    /* This is the crucial part for changing the queue type. */
    NetsimNetworkFactory netsimNetworkFactory =
        new NetsimNetworkFactory() {
          @Override
          public QLinkImpl createNetsimLink(
              final Link link, final QNetwork network, final QNode toQueueNode) {
            VehicleQ<QVehicle> vehicleQ = null;
            switch (queueType) {
              case FIFO:
              case GFIP_FIFO:
                vehicleQ = new FIFOVehicleQ();
                break;
              case BASIC_PASSING:
              case GFIP_PASSING:
                vehicleQ = new PassingVehicleQ();
                break;
              default:
                throw new RuntimeException(
                    "Do not know what VehicleQ to use with queue type " + queueType.toString());
            }
            return new QLinkImpl(link, network, toQueueNode, vehicleQ);
          }

          @Override
          public QNode createNetsimNode(final Node node, QNetwork network) {
            return new QNode(node, network);
          }
        };
    QNetsimEngine netsimEngine = new QNetsimEngine(qSim, netsimNetworkFactory);

    /* Add the custom GFIP link speed calculator, but only when required. */
    if (queueType == QueueType.FIFO) {
      log.info("------------------------------------------------------------------------------");
      log.info("  Using basic FIFO link speed calculator. ");
      log.info("------------------------------------------------------------------------------");
      netsimEngine.setLinkSpeedCalculator(new DefaultLinkSpeedCalculator());
    } else if (queueType == QueueType.BASIC_PASSING) {
      log.info("------------------------------------------------------------------------------");
      log.info("  Using basic passing link speed calculator. ");
      log.info("------------------------------------------------------------------------------");
      netsimEngine.setLinkSpeedCalculator(
          new GfipLinkSpeedCalculator(scenario.getVehicles(), qSim, queueType));
    } else if (queueType == QueueType.GFIP_PASSING) {
      log.info("------------------------------------------------------------------------------");
      log.info("  Using custom GFIP-link-density-based link speed calculator with passing. ");
      log.info("------------------------------------------------------------------------------");
      netsimEngine.setLinkSpeedCalculator(
          new GfipLinkSpeedCalculator(scenario.getVehicles(), qSim, queueType));
    } else if (queueType == QueueType.GFIP_FIFO) {
      log.info("------------------------------------------------------------------------------");
      log.info("  Using custom GFIP-link-density-based link speed calculator without passing.");
      log.info("------------------------------------------------------------------------------");
      netsimEngine.setLinkSpeedCalculator(
          new GfipLinkSpeedCalculator(scenario.getVehicles(), qSim, queueType));
    }

    qSim.addMobsimEngine(netsimEngine);
    qSim.addDepartureHandler(netsimEngine.getDepartureHandler());
    TeleportationEngine teleportationEngine = new TeleportationEngine(scenario, eventsManager);
    qSim.addMobsimEngine(teleportationEngine);
    AgentFactory agentFactory = new DefaultAgentFactory(qSim);

    /* ..Update the PopulationAgentSource to ensure the correct vehicle is
     * passed to the mobsim, not some default-per-mode vehicle. */
    PopulationAgentSource agentSource =
        new PopulationAgentSource(scenario.getPopulation(), agentFactory, qSim);
    Map<String, VehicleType> modeVehicleTypes = new HashMap<String, VehicleType>();
    for (Id<VehicleType> id : scenario.getVehicles().getVehicleTypes().keySet()) {
      modeVehicleTypes.put(id.toString(), scenario.getVehicles().getVehicleTypes().get(id));
    }
    agentSource.setModeVehicleTypes(modeVehicleTypes);
    qSim.addAgentSource(agentSource);

    return qSim;
  }