matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
75 stars 173 forks source link

integrating roadPricing and multiModeDrt with drtServiceArea #1123

Open FNaqavi opened 1 month ago

FNaqavi commented 1 month ago

Hello!

I have a few questions about integrating toll (roadPricing module) and drt (multiModeDrt module). I want to have a congestion pricing scheme for drt vehicles.

Question about drt:

I have a large network, but I want drt to operate in a specified area. On the main drt page (here: https://github.com/matsim-org/matsim-maas/blob/master/drt.md) I see

    <param name="drtServiceAreaShapeFile" value="shape.xml"/>

which I suspect is a subnetwork xml file. In another example (here: https://github.com/matsim-vsp/vsp-playgrounds/blob/master/ikaddoura/src/main/java/playground/ikaddoura/savPricing/RunBerlinDrtScenarioA.java) I see this example where drtServiceAreaShapeFile is actually a polygon shapefile (according to this: https://github.com/matsim-org/matsim-code-examples/issues/249) . I tried both, but my "drt_wait_Stats_drt_zonal.shp" in the output is a gird over my full network.

Things I tried before asking the question: I checked MATSim-MaaS project for the service area but there was no Coordinates Reference System defined, so I could not use a smaller shapefile compared to the network and check if and how the serviceArea works with that example. I also tried to look into the second example with the actual shapeFile but I didn't find the config file for that example either.

Questions:

1- I wonder how to make MATSim pick up my subnetwork, or specified service area? 2- Is there any other way of verifying that the subnetwork is picked up other than looking at the "drt_wait_Stats_drt_zonal.shp"?

Question about roadPricing:

To clarify that drt vehicles are subjected to toll, I use the mode definition example (here: https://github.com/matsim-org/matsim-libs/blob/master/contribs/roadpricing/src/main/java/org/matsim/contrib/roadpricing/run/RunRoadPricingUsingTollFactorExample.java). The problem I face is that:

    scenario.getVehicles().getVehicles().get(vehicleId) == null. 

This is my TollFactor:

    TollFactor tollFactor = (personId, vehicleId, linkId, time) -> {
        if (scenario.getVehicles().getVehicles().get(vehicleId) == null) {
            if (vehicleId.toString().contains("drt")) {
                return 1;
            } else {
                return 0;
            }
        } else if (scenario.getVehicles().getVehicles().get(vehicleId).getType().getNetworkMode().equals("car")) {
            return 0;
        } else {
            return 0;
        }
    };

Note that if I write

scenario.getVehicles().getVehicles().get(vehicleId).getType().getNetworkMode().equals("drt") 

and set return to 1, MATSim returns "The sum of all paid tolls: 0.0 monetary units."

Things I tried before asking the question: This structure can return paid-toll for drt, and car, separately and together (if I set return = 1 for car too).

Question:

3- Is there a general problem with how drt modules are picked up or does my mode definition structure for toll make sense?

My input plan file does not contain any drt and agents can switch to drt as the simulation iterates.

Here is part of my config file that I think is relevant:

<module name="counts">
    <param name="analyzedModes" value="car,drt"/>
</module>

<module name="travelTimeCalculator">
    <param name="analyzedModes" value="car,drt"/>
</module>

<module name="scoring"> 
    <parameterset type="modeParams">
        <param name="mode" value="drt"/>
        <param name="dailyMonetaryConstant" value="0.0"/>
        <param name="dailyUtilityConstant" value="0.0"/>
        <param name="constant" value="0.0"/>
        <param name="marginalUtilityOfDistance_util_m" value="0.0"/>
        <param name="marginalUtilityOfTraveling_util_hr" value="-0.100"/>
        <param name="monetaryDistanceRate" value="-0.0002"/>
    </parameterset>
    <parameterset type="modeParams">
        <param name="mode" value="drt_walk"/>
        <param name="dailyMonetaryConstant" value="0.0"/>
        <param name="dailyUtilityConstant" value="-0.01"/>   <!-- 0.0 -->
        <param name="constant" value="-2.0"/>
        <param name="marginalUtilityOfDistance_util_m" value="0.0"/>
        <param name="marginalUtilityOfTraveling_util_hr" value="-0.073"/>  <!-- -0.073 -->
        <param name="monetaryDistanceRate" value="-0.0002"/>
    </parameterset>
</module>

<module name="changeMode">
    <param name="modeSwitchBehavior" value="fromSpecifiedModesToSpecifiedModes"/>
    <param name="modes" value="car,pt,drt"/>
</module>

<module name="subtourModeChoice">
    <param name="chainBasedModes" value="car,bike"/>
    <param name="modes" value="car,pt,bike,walk,drt" />
</module>

<module name="multiModeDrt">
    <parameterset type="drt">
        <parameterset type="ExtensiveInsertionSearch"/>
        <param name="changeStartLinkToLastLinkInSchedule" value="true"/>
        <param name="drtServiceAreaShapeFile" value="subnetwork.xml"/>
        <!-- param name="drtServiceAreaShapeFile" value="center.shp"/ -->
        <param name="maxTravelTimeAlpha" value="1.5"/>
        <param name="maxTravelTimeBeta" value="1200.0"/>
        <param name="maxWaitTime" value="1200.0"/>
        <param name="operationalScheme" value="door2door"/>
        <param name="stopDuration" value="60.0"/>
        <param name="vehiclesFile" value="./drtvehicles.xml"/>
        <param name="writeDetailedCustomerStats" value="true"/>

        <parameterset type="zonalSystem">
            <param name="zonesGeneration" value="GridFromNetwork"/>
            <param name="cellSize" value="2000"/>
        </parameterset>

        <parameterset type="rebalancing">
            <parameterset type="minCostFlowRebalancingStrategy">
                <param name="targetAlpha" value="0.5"/>
                <param name="targetBeta" value="0.5"/>
            </parameterset>
        </parameterset>

        <parameterset type="drtfare">
            <param name="basefare" value="0.5"/>
            <param name="dailySubscriptionFee" value="0.0"/>
            <param name="distanceFare_m" value="0.0002"/>
            <param name="timeFare_h" value="3.6"/>
            <param name="minFarePerTrip" value="2.0"/>
        </parameterset>
    </parameterset>
</module>

<module name="dvrp">
    <parameterset type="travelTimeMatrix">
        <param name="cellSize" value="1000"/>
        <param name="maxNeighborDistance" value="10000"/>
    </parameterset>

      <param name="networkModes" value="car,drt"/>
      <param name="mobsimMode" value="car"/>
      <param name="travelTimeEstimationAlpha" value="0.05"/>
</module>

This is my RunFile:

package org.matsim;
import java.net.URL;
import org.matsim.api.core.v01.Scenario;
import org.matsim.contrib.drt.run.*;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpModule;
import org.matsim.contrib.dvrp.run.DvrpQSimComponents;
import org.matsim.contrib.roadpricing.*;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.network.algorithms.NetworkSegmentDoubleLinks;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import static org.matsim.contrib.drt.run.DrtControlerCreator.createScenarioWithDrtRouteFactory;

public class RunMatsim {

  public static void main(String[] args) {

      Config config;
      if (args == null || args.length == 0 || args[0] == null) {
          config = ConfigUtils.loadConfig("./matsim-config.xml");
      } else {
          config = ConfigUtils.loadConfig(args);
      }

      config.controller().setOverwriteFileSetting((OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists));
      RoadPricingConfigGroup rpConfig = ConfigUtils.addOrGetModule(config, RoadPricingConfigGroup.class);

      MultiModeDrtConfigGroup multiModeDrtConfig = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
      DvrpConfigGroup dvrpConfigGroup = ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class);
      DrtConfigs.adjustMultiModeDrtConfig(multiModeDrtConfig, config.scoring(), config.routing());
      Scenario scenario = createScenarioWithDrtRouteFactory(config);
      ScenarioUtils.loadScenario(scenario);

      NetworkSegmentDoubleLinks algorithm = new NetworkSegmentDoubleLinks();
      algorithm.run(scenario.getNetwork());

      TollFactor tollFactor = (personId, vehicleId, linkId, time) -> {
          if (scenario.getVehicles().getVehicles().get(vehicleId) == null) {
              if (vehicleId.toString().contains("drt")) {
                  return 0;
              } else {
                  return 0;
              }
          } else if (scenario.getVehicles().getVehicles().get(vehicleId).getType().getNetworkMode().equals("car")) {
              return 1;
          } else {
              return 0;
          }
      };

      URL roadpricingUrl;
      roadpricingUrl = IOUtils.extendUrl(config.getContext(), rpConfig.getTollLinksFile());
      RoadPricingSchemeUsingTollFactor rp = RoadPricingSchemeUsingTollFactor.createAndRegisterRoadPricingSchemeUsingTollFactor(roadpricingUrl, tollFactor, scenario);

      config.checkConsistency();

      Controler controler = new Controler(scenario);
      controler.addOverridingModule(new MultiModeDrtModule());
      controler.addOverridingModule(new DvrpModule());
      controler.configureQSimComponents(DvrpQSimComponents.activateAllModes(multiModeDrtConfig));
      controler.addOverridingModule(new RoadPricingModule( rp ));

      controler.run();

     }
 }