Ejemplo n.º 1
0
  @Override
  public void run() {
    if (scenario instanceof MutableScenario) {
      ((MutableScenario) scenario).setLocked();
      // see comment in ScenarioImpl. kai, sep'14
    }

    /*
     * Create single-mode network here and hand it over to PersonPrepareForSim. Otherwise, each instance would create its
     * own single-mode network. However, this assumes that the main mode is car - which PersonPrepareForSim also does. Should
     * be probably adapted in a way that other main modes are possible as well. cdobler, oct'15.
     */
    final Network net;
    if (NetworkUtils.isMultimodal(network)) {
      log.info(
          "Network seems to be multimodal. Create car-only network which is handed over to PersonPrepareForSim.");
      TransportModeNetworkFilter filter = new TransportModeNetworkFilter(network);
      net = NetworkUtils.createNetwork();
      HashSet<String> modes = new HashSet<>();
      modes.add(TransportMode.car);
      filter.filter(net, modes);
    } else {
      net = network;
    }

    // make sure all routes are calculated.
    ParallelPersonAlgorithmRunner.run(
        population,
        globalConfigGroup.getNumberOfThreads(),
        new ParallelPersonAlgorithmRunner.PersonAlgorithmProvider() {
          @Override
          public AbstractPersonAlgorithm getPersonAlgorithm() {
            return new MyPersonPrepareForSim(
                new PlanRouter(tripRouterProvider.get(), activityFacilities), scenario, net);
          }
        });
    if (population instanceof Lockable) {
      ((Lockable) population).setLocked();
    }
  }
  @Override
  public TripRouter instantiateAndConfigureTripRouter(RoutingContext routingContext) {

    TripRouter tripRouter = new TripRouter();

    PlansCalcRouteConfigGroup routeConfigGroup = scenario.getConfig().plansCalcRoute();

    LeastCostPathCalculator routeAlgo =
        leastCostPathCalculatorFactory.createPathCalculator(
            scenario.getNetwork(),
            routingContext.getTravelDisutility(),
            routingContext.getTravelTime());

    FreespeedTravelTimeAndDisutility ptTimeCostCalc =
        new FreespeedTravelTimeAndDisutility(-1.0, 0.0, 0.0);
    LeastCostPathCalculator routeAlgoPtFreeFlow =
        leastCostPathCalculatorFactory.createPathCalculator(
            scenario.getNetwork(), ptTimeCostCalc, ptTimeCostCalc);

    final boolean networkIsMultimodal = NetworkUtils.isMultimodal(scenario.getNetwork());
    if (networkIsMultimodal) {
      // note: LinkImpl has a default allowed mode of "car" so that all links
      // of a monomodal network are actually restricted to car, making the check
      // of multimodality unecessary from a behavioral point of view.
      // However, checking the mode restriction for each link is expensive,
      // so it is not worth doing it if it is not necessary. (td, oct. 2012)
      if (routeAlgo instanceof IntermodalLeastCostPathCalculator) {
        ((IntermodalLeastCostPathCalculator) routeAlgo)
            .setModeRestriction(Collections.singleton(TransportMode.car));
        ((IntermodalLeastCostPathCalculator) routeAlgoPtFreeFlow)
            .setModeRestriction(Collections.singleton(TransportMode.car));
      } else {
        // this is impossible to reach when using the algorithms of org.matsim.*
        // (all implement IntermodalLeastCostPathCalculator)
        log.warn(
            "network is multimodal but least cost path algorithm is not an instance of IntermodalLeastCostPathCalculator!");
      }
    }

    for (String mode : routeConfigGroup.getTeleportedModeFreespeedFactors().keySet()) {
      final RoutingModule routingModule =
          DefaultRoutingModules.createPseudoTransitRouter(
              mode,
              scenario.getPopulation().getFactory(),
              scenario.getNetwork(),
              routeAlgoPtFreeFlow,
              routeConfigGroup.getModeRoutingParams().get(mode));
      tripRouter.setRoutingModule(mode, routingModule);
    }

    for (String mode : routeConfigGroup.getTeleportedModeSpeeds().keySet()) {
      final RoutingModule routingModule =
          DefaultRoutingModules.createTeleportationRouter(
              mode,
              scenario.getPopulation().getFactory(),
              routeConfigGroup.getModeRoutingParams().get(mode));
      final RoutingModule result = tripRouter.setRoutingModule(mode, routingModule);

      if (result != null) {
        log.error("inconsistent router configuration for mode " + mode);
        log.error(
            "One situation which triggers this warning: setting both speed and speedFactor for a mode (this used to be possible).");
        throw new RuntimeException(
            "there was already a module set when trying to set teleporting module for mode "
                + mode
                + ": "
                + result);
      }
    }

    for (String mode : routeConfigGroup.getNetworkModes()) {
      final RoutingModule routingModule =
          DefaultRoutingModules.createNetworkRouter(
              mode, scenario.getPopulation().getFactory(), scenario.getNetwork(), routeAlgo);
      final RoutingModule result = tripRouter.setRoutingModule(mode, routingModule);

      if (result != null) {
        log.error("inconsistent router configuration for mode " + mode);
        throw new RuntimeException(
            "there was already a module set when trying to set network routing module for mode "
                + mode
                + ": "
                + result);
      }

      // The default router will always route on the car network.  A user may, however, have
      // prepared a network with dedicated bicycle
      // links and then expect the router to route on that.  The following test tries to catch that.
      //  If someone improves on this,
      // the test can be removed.  kai, feb'15
      if (networkIsMultimodal) {
        switch (mode) {
          case TransportMode.car:
          case TransportMode.ride:
            break;
          default:
            throw new RuntimeException(
                "you have a multi-modal network and configured "
                    + mode
                    + " to be routed as a network mode.  "
                    + "The present configuration will route this "
                    + "mode on the car network.  This may be ok (e.g. with ``truck'' or ``motorbike''), or not (e.g. with ``bicycle''). "
                    + "Throwing an exception anyways; please use a uni-modal network if you want to keep this configuration.");
        }
      }
    }

    if (scenario.getConfig().transit().isUseTransit()) {
      TransitRouterWrapper routingModule =
          new TransitRouterWrapper(
              transitRouterFactory.get(),
              scenario.getTransitSchedule(),
              scenario.getNetwork(), // use a walk router in case no PT path is found
              DefaultRoutingModules.createTeleportationRouter(
                  TransportMode.transit_walk,
                  scenario.getPopulation().getFactory(),
                  routeConfigGroup.getModeRoutingParams().get(TransportMode.walk)));
      for (String mode : scenario.getConfig().transit().getTransitModes()) {
        // XXX one can't check for inconsistent setting here...
        // because the setting is inconsistent by default (defaults
        // set a teleportation setting for pt routing, which is overriden
        // here) (td, may 2013)
        tripRouter.setRoutingModule(mode, routingModule);
      }
    }
    return tripRouter;
  }