Skip to content

Commit

Permalink
ensure backwards compatibility with previous drt config design
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed May 10, 2024
1 parent 52fdd8c commit 83a29ad
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@

package org.matsim.contrib.drt.run;

import static org.matsim.core.config.groups.QSimConfigGroup.EndtimeInterpretation;

import java.util.Collection;
import java.util.Optional;

import javax.annotation.Nullable;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.units.qual.C;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.contrib.drt.analysis.zonal.DrtZoneSystemParams;
import org.matsim.contrib.drt.estimator.DrtEstimatorParams;
import org.matsim.contrib.drt.fare.DrtFareParams;
Expand All @@ -44,17 +42,15 @@
import org.matsim.contrib.drt.speedup.DrtSpeedUpParams;
import org.matsim.contrib.dvrp.router.DvrpModeRoutingNetworkModule;
import org.matsim.contrib.dvrp.run.Modal;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.core.config.Config;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.config.groups.RoutingConfigGroup;
import org.matsim.core.config.groups.ScoringConfigGroup;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Optional;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import static org.matsim.core.config.groups.QSimConfigGroup.EndtimeInterpretation;

public class DrtConfigGroup extends ReflectiveConfigGroupWithConfigurableParameterSets implements Modal {
private static final Logger log = LogManager.getLogger(DrtConfigGroup.class);
Expand Down Expand Up @@ -238,6 +234,27 @@ private void initSingletonParameterSets() {

}

/**
* for backwards compatibility with old drt config groups
*/
public void handleAddUnknownParam(final String paramName, final String value) {
switch (paramName) {
case "maxWaitTime":
case "maxTravelTimeAlpha":
case "maxTravelTimeBeta":
case "maxAbsoluteDetour":
case "maxDetourAlpha":
case "maxDetourBeta":
case "maxAllowedPickupDelay":
case "rejectRequestIfMaxWaitOrTravelTimeViolated":
case "maxWalkDistance":
getDrtOptimizationConstraintsParam().addParam(paramName, value);
break;
default:
super.handleAddUnknownParam(paramName, value);
}
}

@Override
protected void checkConsistency(Config config) {
super.checkConsistency(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void testRunDrtWithPrebooking() {
* rejectionRate, rejections, waitAverage, inVehicleTravelTimeMean, & totalTravelTimeMean
*/

private void verifyDrtCustomerStatsCloseToExpectedStats(String outputDirectory, Stats expectedStats) {
static void verifyDrtCustomerStatsCloseToExpectedStats(String outputDirectory, Stats expectedStats) {

String filename = outputDirectory + "/drt_customer_stats_drt.csv";

Expand Down Expand Up @@ -424,7 +424,7 @@ private void verifyDrtCustomerStatsCloseToExpectedStats(String outputDirectory,
assertThat(actualStats).usingRecursiveComparison().isEqualTo(expectedStats);
}

private static class Stats {
static class Stats {
private final double rejectionRate;
private final double rejections;
private final double waitAverage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.matsim.contrib.drt.run.examples;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.Id;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.zone.skims.DvrpTravelTimeMatrixParams;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.vis.otfvis.OTFVisConfigGroup;

import java.net.URL;

/**
*
* Tests the older drt config version where optimization constraints where not stored as separate parameters
* @author nkuehnel / MOIA
*/
public class RunOldDrtConfigCompatibilityIT {

@RegisterExtension
private MatsimTestUtils utils = new MatsimTestUtils();

@Test
void testRunDrtExampleWithNoRejections_ExtensiveSearch() {
Id.resetCaches();

DvrpConfigGroup dvrpConfigGroup = new DvrpConfigGroup();
DvrpTravelTimeMatrixParams matrixParams = dvrpConfigGroup.getTravelTimeMatrixParams();
matrixParams.addParameterSet(matrixParams.createParameterSet(SquareGridZoneSystemParams.SET_NAME));

URL configUrl = IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("mielec"), "mielec_drt_config_v1.xml");
Config config = ConfigUtils.loadConfig(configUrl, new MultiModeDrtConfigGroup(), dvrpConfigGroup,
new OTFVisConfigGroup());

for (var drtCfg : MultiModeDrtConfigGroup.get(config).getModalElements()) {
//disable rejections
drtCfg.getDrtOptimizationConstraintsParam().rejectRequestIfMaxWaitOrTravelTimeViolated = false;
}

config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setOutputDirectory(utils.getOutputDirectory());
RunDrtExample.run(config, false);

var expectedStats = RunDrtExampleIT.Stats.newBuilder()
.rejectionRate(0.0)
.rejections(0)
.waitAverage(297.19)
.inVehicleTravelTimeMean(386.78)
.totalTravelTimeMean(683.97)
.build();

RunDrtExampleIT.verifyDrtCustomerStatsCloseToExpectedStats(utils.getOutputDirectory(), expectedStats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.matsim.contrib.drt.fare.DrtFareParams;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.DrtOptimizationConstraintsParams;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.core.config.Config;
Expand Down Expand Up @@ -45,9 +46,10 @@ void testTeleportationEngine() throws IOException {

Controler controler = DrtControlerCreator.createControler(config, false);
DrtConfigGroup drtConfigGroup = DrtConfigGroup.getSingleModeDrtConfig(config);
drtConfigGroup.maxTravelTimeAlpha = 1.2;
drtConfigGroup.maxTravelTimeBeta = 600;
drtConfigGroup.maxWaitTime = 300;
DrtOptimizationConstraintsParams constraintsParam = drtConfigGroup.getDrtOptimizationConstraintsParam();
constraintsParam.maxTravelTimeAlpha = 1.2;
constraintsParam.maxTravelTimeBeta = 600;
constraintsParam.maxWaitTime = 300;
DrtFareParams fareParams = new DrtFareParams();
fareParams.baseFare = 1.0;
fareParams.distanceFare_m = 0.001;
Expand Down Expand Up @@ -75,12 +77,12 @@ public void install() {

double waitAvg = Double.parseDouble(row.get("wait_average"));

assertThat(waitAvg).isEqualTo(drtConfigGroup.maxWaitTime);
assertThat(waitAvg).isEqualTo(constraintsParam.maxWaitTime);

double distMean = Double.parseDouble(row.get("distance_m_mean"));
double directDistMean = Double.parseDouble(row.get("directDistance_m_mean"));

assertThat(distMean / directDistMean).isCloseTo(drtConfigGroup.maxTravelTimeAlpha, Offset.offset(0.0001));
assertThat(distMean / directDistMean).isCloseTo(constraintsParam.maxTravelTimeAlpha, Offset.offset(0.0001));

}

Expand Down
72 changes: 72 additions & 0 deletions examples/scenarios/mielec/mielec_drt_config_v1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "http://www.matsim.org/files/dtd/config_v2.dtd">
<config>
<module name="dvrp">
</module>

<module name="multiModeDrt">
<parameterset type="drt">
<parameterset type="ExtensiveInsertionSearch"/>
<param name="stopDuration" value="60"/>
<param name="maxWaitTime" value="600"/>
<param name="maxTravelTimeAlpha" value="1.3"/>
<param name="maxTravelTimeBeta" value="400"/>

<param name="vehiclesFile" value="vehicles-10-cap-4.xml"/>
<!-- param name="vehiclesFile" value="vehicles-20-cap-2.xml" / -->

<parameterset type="zonalSystem">
<parameterset type="SquareGridZoneSystem" >
<param name="cellSize" value="500"/>
</parameterset>
</parameterset>

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

<module name="network">
<param name="inputNetworkFile" value="network.xml"/>
</module>

<module name="plans">
<param name="inputPlansFile" value="plans_only_drt_1.0.xml.gz"/>
</module>

<module name="qsim">
<param name="simStarttimeInterpretation" value="onlyUseStarttime"/>
<param name="endTime" value="30:00:00"/>
<param name="insertingWaitingVehiclesBeforeDrivingVehicles" value="true"/>
<param name="snapshotStyle" value="queue"/>
</module>

<module name="otfvis">
<param name="coloringScheme" value="taxicab"/>
<param name="drawNonMovingItems" value="true"/>
</module>

<module name="controller">
<param name="outputDirectory" value="test/output/mielec_drt"/>
<param name="overwriteFiles" value="deleteDirectoryIfExists"/>
<param name="firstIteration" value="0"/>
<param name="lastIteration" value="0"/>
</module>

<module name="scoring">
<param name="activityType_0" value="dummy"/>
<param name="activityTypicalDuration_0" value="24:00:00"/>
<param name="traveling_drt" value="-6"/>
</module>

<module name="replanning">
<!-- We focus only on DynAgents. "Standard" agents do not change their behaviour -->
<param name="maxAgentPlanMemorySize" value="1"/>
<param name="ModuleProbability_1" value="1.0"/>
<param name="Module_1" value="KeepLastSelected"/>
</module>
</config>

0 comments on commit 83a29ad

Please sign in to comment.