Skip to content

Commit

Permalink
don't force subtourmodechoice
Browse files Browse the repository at this point in the history
remove the overly complex method to use subtourmodechoice together with drs. the permissiblemodescalculator alone is enough!
  • Loading branch information
markusstraub committed Jan 14, 2025
1 parent 6de65f7 commit 2be0b74
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 200 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ In more detail, DRS is integrated into the MATSim loop as follows:

### Replanning

First the slightly adjusted innovation strategy `SubtourModeChoiceForDrs` assigns the new modes `drsDriver` and `drsRider` to agents' subtours.
This assignment can be restricted with the person attribute `drsAffinity`.
In case of using `SubtourModeChoice` replanning:
The drs modes must be added via the regular `SubtourModeChoice` config group.
Then `PermissibleModesCalculatorForDrs` takes care to remove drs for agents that don't want it using their `drsAffinity`.

Then requests are collected and matched based on origin, destination, departure time and detour time.
The riders' acceptance of deviations to their desired departure time can be controlled with the DRS config parameter `riderDepartureTimeAdjustmentSeconds`.
Expand Down Expand Up @@ -156,10 +157,6 @@ List of all parameters:
During the matching process, the arrival of driver to pick-up point is checked
whether it is within the rider departure time +- the riderDepartureTimeAdjustment.

**Plan Innovation**

- `subtourModeChoiceChainBasedModes`: Defines the chain-based modes for the `SubtourModeChoiceForDrs` strategy, separated by commas.
- `subtourModeChoiceModes`: Defines all modes available for the `SubtourModeChoiceForDrs` strategy, including chain-based modes, separated by commas.

### Run Example

Expand All @@ -174,11 +171,12 @@ This should assure, that at the beginning of the simulation many drivers are pre
(MATSim guarantees to try out / score all un-scored plans of an agent - see `RandomUnscoredPlanSelector` -
before a different plan is selected e.g. via `SelectPlanExpBeta`).

### Mode Innovation
### Replanning / Mode Innovation

#### SubtourModeChoice

Mode innovation relies on an adapted version of the innovation strategy `SubtourModeChoice` named `SubtourModeChoiceForDrs`.
`SubtourModeChoiceForDrs` will by default add the DRS driver and rider mode to the mix
and can also be configured via the relevant parameters in the `drs` config group.
The drs modes must be added via the regular `SubtourModeChoice` config group.
Then the `PermissibleModesCalculatorForDrs` takes care to remove drs for agents that don't want it using their `drsAffinity`.

### Output

Expand Down
10 changes: 8 additions & 2 deletions data/floridsdorf/config_drs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,22 @@
<param name="weight" value="0.1"/>
</parameterset>
<parameterset type="strategysettings">
<!-- DRS NOTE: for drs this strategy must be used instead of regular SubtourModeChoice -->
<param name="strategyName" value="SubtourModeChoiceForDrs"/>
<param name="strategyName" value="SubtourModeChoice"/>
<param name="weight" value="0.3"/>
</parameterset>
</module>

<!-- DRS NOTE: when using SubtourModeChoice the drs modes must be added -->
<module name="subtourModeChoice">
<param name="chainBasedModes" value="car,drsDriver,bike" />
<param name="modes" value="car,drsDriver,drsRider,pt,bike,walk" />
</module>

<!-- DRS NOTE: main drs config, leave empty to use defaults -->
<module name="drs">
<param name="driverProfitPerKm" value="1" />
<param name="maxMatchingDistanceMeters" value="500" />
<param name="pickupWaitingSeconds" value="300" />
</module>

</config>
11 changes: 9 additions & 2 deletions data/vienna/config_drs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,17 @@
<param name="weight" value="0.1"/>
</parameterset>
<parameterset type="strategysettings">
<!-- DRS NOTE: for drs this strategy must be used instead of regular SubtourModeChoice -->
<param name="strategyName" value="SubtourModeChoiceForDrs"/>
<param name="strategyName" value="SubtourModeChoice"/>
<param name="weight" value="0.3"/>
</parameterset>
</module>

<!-- DRS NOTE: when using SubtourModeChoice the drs modes must be added -->
<module name="subtourModeChoice">
<param name="chainBasedModes" value="car,drsDriver,bike" />
<param name="modes" value="car,drsDriver,drsRider,pt,bike,walk" />
</module>

<module name="transit">
<param name="routingAlgorithmType" value="SwissRailRaptor"/>
<param name="transitModes" value="pt,bus,tram,subway,rail,ferry"/>
Expand All @@ -186,6 +191,8 @@
</parameterset>
</module>



<!-- DRS NOTE: main drs config, leave empty to use defaults -->
<module name="drs">
<param name="driverProfitPerKm" value="0.8" />
Expand Down
14 changes: 3 additions & 11 deletions prepare_config_for_drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,10 @@ def adjust_replanning(tree: etree.ElementTree) -> None:
"module[@name='replanning']/"
"parameterset[@type='strategysettings' and param[@name='strategyName'] and param[@value='SubtourModeChoice']]"
)
logger.info(
f"replanning: changing {len(smc)} SubtourModeChoice strategies to SubtourModeChoiceForDrs"
)
for p in smc:
p.find("param[@name='strategyName']").set("value", "SubtourModeChoiceForDrs")

smc_for_drs = tree.xpath(
"module[@name='replanning']/"
"parameterset[@type='strategysettings' and param[@name='strategyName'] and param[@value='SubtourModeChoiceForDrs']]"
logger.info(f"replanning: using {len(smc)} SubtourModeChoice strategies")
logger.warning(
"TODO implement checking if drsDriver and drsRider are present to the subtourmodechoice config group"
)
if len(smc_for_drs) == 0:
logger.warning("replanning: no SubtourModeChoiceForDrs found")


def adjust_routing(tree: etree.ElementTree) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
import at.ac.ait.matsim.drs.run.Drs;
import at.ac.ait.matsim.drs.util.DrsUtil;

/**
* Adds drs modes where applicable (see drsAffinity) in addition to the default
* modes.
* Mostly relevant for replanning with SubtourModeChoice.
*/
public class PermissibleModesCalculatorForDrs implements PermissibleModesCalculator {

private final Set<String> availableModes;

@Inject
public PermissibleModesCalculatorForDrs(Config config) {
this.availableModes = ImmutableSet.copyOf(Drs.addOrGetConfigGroup(config).getSubtourModeChoiceModes());
this.availableModes = ImmutableSet.copyOf(config.subtourModeChoice().getModes());
}

@Override
Expand Down

This file was deleted.

75 changes: 0 additions & 75 deletions src/main/java/at/ac/ait/matsim/drs/run/DrsConfigGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import java.util.Map;

import org.matsim.api.core.v01.TransportMode;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.core.utils.misc.StringUtils;

import at.ac.ait.matsim.drs.engine.SubtourModeChoiceForDrs;

public class DrsConfigGroup extends ReflectiveConfigGroupWithConfigurableParameterSets {

Expand All @@ -33,14 +29,6 @@ public class DrsConfigGroup extends ReflectiveConfigGroupWithConfigurableParamet
public static final String CAR_AND_DRS_DAILY_MONETARY_CONSTANT = "carAndDrsDailyMonetaryConstant";
private double carAndDrsDailyMonetaryConstant = 0;

public static final String SUBTOUR_MODE_CHOICE_MODES = "subtourModeChoiceModes";
private String[] subtourModeChoiceModes = { TransportMode.car, Drs.DRIVER_MODE, Drs.RIDER_MODE,
TransportMode.pt, TransportMode.bike, TransportMode.walk };

public static final String SUBTOUR_MODE_CHOICE_CHAIN_BASED_MODES = "subtourModeChoiceChainBasedModes";
private String[] subtourModeChoiceChainBasedModes = new String[] { TransportMode.car, Drs.DRIVER_MODE,
TransportMode.bike };

public static final String MIN_DRIVER_LEG_METERS = "minDriverLegMeters";
private int minDriverLegMeters = 10;

Expand Down Expand Up @@ -80,12 +68,6 @@ public Map<String, String> getComments() {
"Daily price for car usage including when using the private car as drsDriver. "
+ "If specified here do not additionaly specify it in planCalcScore.scoringParameters.modeParams.dailyMonetaryConstant - "
+ "otherwise it will be counted twice (typically negative)");
map.put(SUBTOUR_MODE_CHOICE_MODES,
"Defines all modes available for the '" + SubtourModeChoiceForDrs.STRATEGY_NAME
+ "' strategy, including chain-based modes, separated by commas");
map.put(SUBTOUR_MODE_CHOICE_CHAIN_BASED_MODES,
"Defines the chain-based modes for the '" + SubtourModeChoiceForDrs.STRATEGY_NAME
+ "' strategy, separated by commas");
map.put(MIN_DRIVER_LEG_METERS,
"minimum length of legs (routed with the drsDriver mode) to be considered for the drs driver mode. 0 means no minimum.");
map.put(MIN_RIDER_LEG_METERS,
Expand Down Expand Up @@ -163,42 +145,6 @@ public void setCarAndDrsDailyMonetaryConstant(double carAndDrsDailyMonetaryConst
this.carAndDrsDailyMonetaryConstant = carAndDrsDailyMonetaryConstant;
}

public String[] getSubtourModeChoiceModes() {
return subtourModeChoiceModes;
}

@StringGetter(SUBTOUR_MODE_CHOICE_MODES)
public String getSubtourModeChoiceModesString() {
return toString(subtourModeChoiceModes);
}

public void setSubtourModeChoiceModes(String[] subtourModeChoiceModes) {
this.subtourModeChoiceModes = subtourModeChoiceModes;
}

@StringSetter(SUBTOUR_MODE_CHOICE_MODES)
public void setSubtourModeChoiceModesString(String subtourModeChoiceModes) {
this.subtourModeChoiceModes = toArray(subtourModeChoiceModes);
}

public String[] getSubtourModeChoiceChainBasedModes() {
return subtourModeChoiceChainBasedModes;
}

@StringGetter(SUBTOUR_MODE_CHOICE_CHAIN_BASED_MODES)
public String getSubtourModeChoiceChainBasedModesString() {
return toString(subtourModeChoiceChainBasedModes);
}

public void setSubtourModeChoiceChainBasedModes(String[] subtourModeChoiceChainBasedModes) {
this.subtourModeChoiceChainBasedModes = subtourModeChoiceChainBasedModes;
}

@StringSetter(SUBTOUR_MODE_CHOICE_CHAIN_BASED_MODES)
public void setSubtourModeChoiceChainBasedModesString(String subtourModeChoiceChainBasedModes) {
this.subtourModeChoiceChainBasedModes = toArray(subtourModeChoiceChainBasedModes);
}

@StringGetter(MIN_DRIVER_LEG_METERS)
public int getMinDriverLegMeters() {
return minDriverLegMeters;
Expand All @@ -219,25 +165,4 @@ public void setMinRiderLegMeters(int minRiderLegMeters) {
this.minRiderLegMeters = minRiderLegMeters;
}

/** copied from SubtourModeChoiceConfigGroup */
private static String toString(final String[] modes) {
StringBuilder b = new StringBuilder();
if (modes.length > 0)
b.append(modes[0]);
for (int i = 1; i < modes.length; i++) {
b.append(',');
b.append(modes[i]);
}
return b.toString();
}

/** copied from SubtourModeChoiceConfigGroup */
private static String[] toArray(final String modes) {
String[] parts = StringUtils.explode(modes, ',');
for (int i = 0, n = parts.length; i < n; i++) {
parts[i] = parts[i].trim().intern();
}
return parts;
}

}
3 changes: 0 additions & 3 deletions src/main/java/at/ac/ait/matsim/drs/run/DrsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import at.ac.ait.matsim.drs.engine.DrsReplanningListener;
import at.ac.ait.matsim.drs.engine.PermissibleModesCalculatorForDrs;
import at.ac.ait.matsim.drs.engine.PlanModificationUndoer;
import at.ac.ait.matsim.drs.engine.SubtourModeChoiceForDrs;
import at.ac.ait.matsim.drs.engine.UnmatchedRiderConflictIdentifier;

public final class DrsModule extends AbstractModule {
Expand All @@ -34,8 +33,6 @@ public void install() {
bind(DrsData.class).asEagerSingleton();

bind(PermissibleModesCalculator.class).to(PermissibleModesCalculatorForDrs.class);
addPlanStrategyBinding(SubtourModeChoiceForDrs.STRATEGY_NAME)
.toProvider(SubtourModeChoiceForDrs.Provider.class);

installQSimModule(new DrsEngineQSimModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public void adjustConfig(Config config) {
// (so that we can demonstrate the match)
config.replanning().clearStrategySettings();
config.replanning()
.addStrategySettings(new StrategySettings().setStrategyName("SubtourModeChoiceForDrs").setWeight(1));
.addStrategySettings(new StrategySettings().setStrategyName("SubtourModeChoice").setWeight(1));

DrsConfigGroup drs = (DrsConfigGroup) config.getModules().get("drs");
drs.setSubtourModeChoiceModes(new String[] { Drs.DRIVER_MODE, Drs.RIDER_MODE, TransportMode.bike });
drs.setSubtourModeChoiceChainBasedModes(new String[] { Drs.DRIVER_MODE, TransportMode.bike });
config.subtourModeChoice().setModes(new String[] { Drs.DRIVER_MODE, Drs.RIDER_MODE, TransportMode.bike });
config.subtourModeChoice().setChainBasedModes(new String[] { Drs.DRIVER_MODE, TransportMode.bike });
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void adjustConfig(Config config) {
// (so that we can demonstrate the unmatched rider problem)
config.replanning().clearStrategySettings();
config.replanning()
.addStrategySettings(new StrategySettings().setStrategyName("SubtourModeChoiceForDrs").setWeight(1));
.addStrategySettings(new StrategySettings().setStrategyName("SubtourModeChoice").setWeight(1));

DrsConfigGroup drs = (DrsConfigGroup) config.getModules().get("drs");
drs.setSubtourModeChoiceModes(new String[] { Drs.RIDER_MODE, TransportMode.bike });
drs.setSubtourModeChoiceChainBasedModes(new String[] { TransportMode.bike });
config.subtourModeChoice().setModes(new String[] { Drs.RIDER_MODE, TransportMode.bike });
config.subtourModeChoice().setChainBasedModes(new String[] { TransportMode.bike });
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public void adjustConfig(Config config) {
config.controller().setLastIteration(100);
config.controller().setOutputDirectory("output-floridsdorf-perfectMatch");
config.plans().setInputFile("population_drs_perfectMatch.xml");
// just check if multi-threading breaks
config.qsim().setNumberOfThreads(10);
config.global().setNumberOfThreads(10);

// DrsConfigGroup drs = (DrsConfigGroup) config.getModules().get("drs");
}

}
Loading

0 comments on commit 2be0b74

Please sign in to comment.