Exemplo n.º 1
0
  /**
   * Setting up the simulation {@link Scenario}, using a given {@link Config}. In the scenario setup
   * each network link is given both {@code car} and {@code commercial} as mode. Also, each person
   * is assigned a vehicle, which is based on the Gauteng Freeway Improvement Project (GFIP) vehicle
   * toll classes. the following vehicle types are available.
   *
   * <ul>
   *   <li>{@code A2}: Light delivery vehicle with length 7.0m, maximum velocity of 100km/h, and
   *       accounting for 80% of all commercial vehicles;
   *   <li>{@code B}: Short heavy vehicles with length 12.0m, maximum velocity of 90km/h, and
   *       accounting for 15% of all commercial vehicles; and
   *   <li>{@code C}: Long heavy vehicles with length 20.0m, maximum velocity of 90km/h, and
   *       accounting for the remaining 5% of all commercial vehicles.
   * </ul>
   *
   * These vehicle classes are randomly sampled and assigned to the individuals. No cognisance is
   * given to intra- and inter-area traffic (based on the activity chain structure). This
   * <i><b>should</b></i> be refined.
   *
   * @param config typically the {@link Config} resulting from calling the method {@link
   *     #setupConfig(String, Machine)}.
   * @return a scenario in which the network has the necessary {@code commercial} mode; and each
   *     agent has a vehicle with a particular commercial vehicle class.
   */
  public static Scenario setupScenario(Config config) {
    Scenario sc = ScenarioUtils.loadScenario(config);

    /* Ensure that all links take 'commercial' as mode. */
    LOG.warn("Ensuring all links take 'commercial' as mode...");
    Collection<String> modesCollection = Arrays.asList("car", "commercial");
    Set<String> modesSet = new HashSet<>(modesCollection);
    for (Link link : sc.getNetwork().getLinks().values()) {
      link.setAllowedModes(modesSet);
    }
    LOG.warn("Done adding 'commercial' modes.");

    /* Add all VehicleTypes. */
    Vehicles vehicles = sc.getVehicles();
    for (VehicleTypeSA vt : VehicleTypeSA.values()) {
      vehicles.addVehicleType(vt.getVehicleType());
    }

    MatsimRandom.reset(2015093001l);
    for (Person person : sc.getPopulation().getPersons().values()) {
      /* Randomly sample a vehicle type for each person. */
      VehicleType vehicleType = null;
      double r = MatsimRandom.getRandom().nextDouble();
      if (r <= 0.8) {
        vehicleType = VehicleTypeSA.A2.getVehicleType();
      } else if (r <= 0.95) {
        vehicleType = VehicleTypeSA.B.getVehicleType();
      } else {
        vehicleType = VehicleTypeSA.C.getVehicleType();
      }

      Vehicle truck =
          VehicleUtils.getFactory()
              .createVehicle(Id.create(person.getId(), Vehicle.class), vehicleType);
      vehicles.addVehicle(truck);

      /* Link the vehicle type to the person's attributes. */
      sc.getPopulation()
          .getPersonAttributes()
          .putAttribute(person.getId().toString(), "vehicleType", vehicleType.getId().toString());
    }

    return sc;
  }