diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/constraints/ConstraintSetChooser.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/constraints/ConstraintSetChooser.java index 2be22eeb9d8..124da1a39bb 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/constraints/ConstraintSetChooser.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/constraints/ConstraintSetChooser.java @@ -4,11 +4,13 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.utils.objectattributes.attributable.Attributes; +import java.util.Optional; + /** * @author nkuehnel / MOIA */ public interface ConstraintSetChooser { - DrtOptimizationConstraintsSet chooseConstraintSet(double departureTime, Link accessActLink, Link egressActLink, Person person, - Attributes tripAttributes); + Optional chooseConstraintSet(double departureTime, Link accessActLink, + Link egressActLink, Person person, Attributes tripAttributes); } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/routing/DrtRouteCreator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/routing/DrtRouteCreator.java index f002460fbaa..45dce290b0f 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/routing/DrtRouteCreator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/routing/DrtRouteCreator.java @@ -24,7 +24,6 @@ import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Route; import org.matsim.contrib.drt.optimizer.constraints.ConstraintSetChooser; -import org.matsim.contrib.drt.optimizer.constraints.DefaultDrtOptimizationConstraintsSet; import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet; import org.matsim.contrib.drt.run.DrtConfigGroup; import org.matsim.contrib.dvrp.path.VrpPathWithTravelData; @@ -72,7 +71,9 @@ public Route createRoute(double departureTime, Link accessActLink, Link egressAc double unsharedRideTime = unsharedPath.getTravelTime();//includes first & last link double unsharedDistance = VrpPaths.calcDistance(unsharedPath);//includes last link - DrtOptimizationConstraintsSet constraintsSet = constraintSetChooser.chooseConstraintSet(departureTime, accessActLink, egressActLink, person, tripAttributes); + DrtOptimizationConstraintsSet constraintsSet = + constraintSetChooser.chooseConstraintSet(departureTime, accessActLink, egressActLink, person, tripAttributes) + .orElse(drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet()); double maxTravelTime = routeConstraintsCalculator.getMaxTravelTime(constraintsSet, unsharedRideTime); double maxRideDuration = routeConstraintsCalculator.getMaxRideTime(constraintsSet, unsharedRideTime); diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/run/DrtModeRoutingModule.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/run/DrtModeRoutingModule.java index 7643ed97dab..d0b82a10f10 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/run/DrtModeRoutingModule.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/run/DrtModeRoutingModule.java @@ -58,6 +58,7 @@ import java.net.URL; import java.util.List; +import java.util.Optional; /** * This is a DRT-customised version of DvrpModeRoutingModule @@ -100,7 +101,7 @@ public void install() { DefaultDrtOptimizationConstraintsSet optimizationConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet(); bindModal(ConstraintSetChooser.class).toProvider( () -> (departureTime, accessActLink, egressActLink, person, tripAttributes) - -> optimizationConstraintsSet + -> Optional.of(optimizationConstraintsSet) ).in(Singleton.class); switch( drtCfg.operationalScheme ){ diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/config/ConfigBehaviorTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/config/ConfigBehaviorTest.java index 2e920f3cf62..0c365bbc5e7 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/config/ConfigBehaviorTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/config/ConfigBehaviorTest.java @@ -121,7 +121,7 @@ final void testMaterializeAfterReadDrtConstraints() { } @Test - final void testMaterializeAfterReadStandardParams3() { + final void testMaterializeAfterReadCustomDrtConstraints() { { // generate a test config that sets two values away from their defaults, and write it to file: Config config = ConfigUtils.createConfig(); @@ -142,13 +142,13 @@ final void testMaterializeAfterReadStandardParams3() { { // load config file without materializing the drt config group - MultiModeDrtConfigGroup multiModeDrtConfigGroup1 = + MultiModeDrtConfigGroup multiModeDrtConfigGroup = new MultiModeDrtConfigGroup(() -> new DrtConfigGroup(CustomConstraintsSet::new)); - Config config = ConfigUtils.loadConfig( new String[] { utils.getOutputDirectory() + "ad-hoc-config-custom-optimization-constraints.xml"}, multiModeDrtConfigGroup1); + Config config = ConfigUtils.loadConfig( new String[] { utils.getOutputDirectory() + "ad-hoc-config-custom-optimization-constraints.xml"}, multiModeDrtConfigGroup); // materialize the config group - MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule( config, MultiModeDrtConfigGroup.class ); + //MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule( config, MultiModeDrtConfigGroup.class ); DrtConfigGroup drtConfigGroup = multiModeDrtConfigGroup.getModalElements().iterator().next(); // check if you are getting back the values from the config file: diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/routing/DrtRoutingModuleTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/routing/DrtRoutingModuleTest.java index be99a55d320..8325834c96b 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/routing/DrtRoutingModuleTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/routing/DrtRoutingModuleTest.java @@ -19,9 +19,7 @@ package org.matsim.contrib.drt.routing; -import java.util.Arrays; -import java.util.List; - +import com.google.common.collect.ImmutableMap; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -30,15 +28,8 @@ import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.TransportMode; import org.matsim.api.core.v01.network.Link; -import org.matsim.api.core.v01.population.Activity; -import org.matsim.api.core.v01.population.Leg; -import org.matsim.api.core.v01.population.Person; -import org.matsim.api.core.v01.population.Plan; -import org.matsim.api.core.v01.population.PlanElement; -import org.matsim.api.core.v01.population.PopulationFactory; -import org.matsim.contrib.drt.optimizer.constraints.ConstraintSetChooser; +import org.matsim.api.core.v01.population.*; import org.matsim.contrib.drt.optimizer.constraints.DefaultDrtOptimizationConstraintsSet; -import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet; import org.matsim.contrib.drt.run.DrtConfigGroup; import org.matsim.contrib.drt.run.DrtControlerCreator; import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup; @@ -63,8 +54,9 @@ import org.matsim.pt.transitSchedule.api.TransitScheduleReader; import org.matsim.testcases.MatsimTestUtils; -import com.google.common.collect.ImmutableMap; -import org.matsim.utils.objectattributes.attributable.Attributes; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; /** * @author jbischoff @@ -102,7 +94,7 @@ void testCottbusClosestAccessEgressStopFinder() { DrtRouteCreator drtRouteCreator = new DrtRouteCreator(drtCfg, scenario.getNetwork(), new SpeedyDijkstraFactory(), new FreeSpeedTravelTime(), TimeAsTravelDisutility::new, new DefaultDrtRouteConstraintsCalculator(), - (departureTime, accessActLink, egressActLink, person, tripAttributes) -> defaultConstraintsSet); + (departureTime, accessActLink, egressActLink, person, tripAttributes) -> Optional.of(defaultConstraintsSet)); DefaultMainLegRouter mainRouter = new DefaultMainLegRouter(drtMode, scenario.getNetwork(), scenario.getPopulation().getFactory(), drtRouteCreator); DvrpRoutingModule dvrpRoutingModule = new DvrpRoutingModule(mainRouter, walkRouter, walkRouter, stopFinder,