Skip to content

Commit

Permalink
Merge branch 'master' into feature/fixPseudoNetworkWithLoopLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Sep 16, 2024
2 parents 38c5fa8 + 22d8b7e commit f8b3e9d
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public PreplannedDrtOptimizer(DrtConfigGroup drtCfg, PreplannedSchedules preplan
TravelTime travelTime, TravelDisutility travelDisutility, MobsimTimer timer, DrtTaskFactory taskFactory,
EventsManager eventsManager, Fleet fleet, ScheduleTimingUpdater scheduleTimingUpdater) {
Preconditions.checkArgument(
fleet.getVehicles().keySet().equals(preplannedSchedules.vehicleToPreplannedStops.keySet()),
fleet.getVehicles().keySet().containsAll(preplannedSchedules.vehicleToPreplannedStops.keySet()),
"Some schedules are preplanned for vehicles outside the fleet");

this.mode = drtCfg.getMode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.matsim.contrib.drt.optimizer.constraints;

/**
* @author Sebastian Hörl, IRT SystemX
*/
public record DrtRouteConstraints( //
double maxTravelTime, //
double maxRideTime, //
double maxWaitTime//
) {

}
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
package org.matsim.contrib.drt.routing;

import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Person;
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.optimizer.constraints.DrtRouteConstraints;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.utils.objectattributes.attributable.Attributes;

/**
* @author nkuehnel / MOIA
*/
public class DefaultDrtRouteConstraintsCalculator implements DrtRouteConstraintsCalculator {

/**
* Calculates the maximum travel time defined as: drtCfg.getMaxTravelTimeAlpha() * unsharedRideTime + drtCfg.getMaxTravelTimeBeta()
*
* @param constraintsSet
* @param unsharedRideTime ride time of the direct (shortest-time) route
* @return maximum travel time
*/
@Override
public double getMaxTravelTime(DrtOptimizationConstraintsSet constraintsSet, double unsharedRideTime) {
if(constraintsSet instanceof DefaultDrtOptimizationConstraintsSet defaultSet) {
return defaultSet.maxTravelTimeAlpha * unsharedRideTime
+ defaultSet.maxTravelTimeBeta;
} else {
throw new IllegalArgumentException("Constraint set is not a default set");
}
}

/**
* Calculates the maximum ride time defined as: drtCfg.maxDetourAlpha * unsharedRideTime + drtCfg.maxDetourBeta
*
* @param constraintsSet
* @param unsharedRideTime ride time of the direct (shortest-time) route
* @return maximum ride time
*/
@Override
public double getMaxRideTime(DrtOptimizationConstraintsSet constraintsSet, double unsharedRideTime) {
if(constraintsSet instanceof DefaultDrtOptimizationConstraintsSet defaultSet) {
return Math.min(unsharedRideTime + defaultSet.maxAbsoluteDetour,
defaultSet.maxDetourAlpha * unsharedRideTime
+ defaultSet.maxDetourBeta);
} else {
throw new IllegalArgumentException("Constraint set is not a default set");
}
}
private final DrtConfigGroup drtCfg;
private final ConstraintSetChooser constraintSetChooser;

public DefaultDrtRouteConstraintsCalculator(DrtConfigGroup drtCfg, ConstraintSetChooser constraintSetChooser) {
this.drtCfg = drtCfg;
this.constraintSetChooser = constraintSetChooser;
}

/**
* Calculates the maximum travel time defined as: drtCfg.getMaxTravelTimeAlpha()
* unsharedRideTime + drtCfg.getMaxTravelTimeBeta()
*
* Calculates the maximum ride time defined as: drtCfg.maxDetourAlpha *
* unsharedRideTime + drtCfg.maxDetourBeta
*
* @return DrtRouteConstraints constraints
*/
@Override
public DrtRouteConstraints calculateRouteConstraints(double departureTime, Link accessActLink, Link egressActLink,
Person person, Attributes tripAttributes, double unsharedRideTime, double unsharedDistance) {
DrtOptimizationConstraintsSet constraintsSet = constraintSetChooser
.chooseConstraintSet(departureTime, accessActLink, egressActLink, person, tripAttributes).orElse(drtCfg
.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet());

if (constraintsSet instanceof DefaultDrtOptimizationConstraintsSet defaultSet) {
double maxTravelTime = defaultSet.maxTravelTimeAlpha * unsharedRideTime + defaultSet.maxTravelTimeBeta;
double maxRideTime = Math.min(unsharedRideTime + defaultSet.maxAbsoluteDetour,
defaultSet.maxDetourAlpha * unsharedRideTime + defaultSet.maxDetourBeta);
double maxWaitTime = constraintsSet.maxWaitTime;

return new DrtRouteConstraints(maxTravelTime, maxRideTime, maxWaitTime);
} else {
throw new IllegalArgumentException("Constraint set is not a default set");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package org.matsim.contrib.drt.routing;

import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Person;
import org.matsim.contrib.drt.optimizer.constraints.DrtRouteConstraints;
import org.matsim.utils.objectattributes.attributable.Attributes;

/**
* @author nkuehnel / MOIA
* @author Sebastian Hörl, IRT SystemX
*/
public interface DrtRouteConstraintsCalculator {

double getMaxTravelTime(DrtOptimizationConstraintsSet constraintsSet, double unsharedRideTime);
DrtRouteConstraints calculateRouteConstraints( //
double departureTime, //
Link accessActLink, //
Link egressActLink, //
Person person, //
Attributes tripAttributes, //
double unsharedRideTime, //
double unsharedDistance //
);

double getMaxRideTime(DrtOptimizationConstraintsSet constraintsSet, double unsharedRideTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.matsim.api.core.v01.population.Route;
import org.matsim.contrib.drt.optimizer.constraints.ConstraintSetChooser;
import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet;
import org.matsim.contrib.drt.optimizer.constraints.DrtRouteConstraints;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.dvrp.path.VrpPathWithTravelData;
import org.matsim.contrib.dvrp.path.VrpPaths;
Expand All @@ -40,6 +41,7 @@
* @author jbischoff
* @author michalm (Michal Maciejewski)
* @author Kai Nagel
* @author Sebastian Hörl, IRT SystemX
*/
public class DrtRouteCreator implements DefaultMainLegRouter.RouteCreator {
private final DrtConfigGroup drtCfg;
Expand All @@ -48,16 +50,13 @@ public class DrtRouteCreator implements DefaultMainLegRouter.RouteCreator {

private final DrtRouteConstraintsCalculator routeConstraintsCalculator;

private final ConstraintSetChooser constraintSetChooser;

public DrtRouteCreator(DrtConfigGroup drtCfg, Network modalNetwork,
LeastCostPathCalculatorFactory leastCostPathCalculatorFactory, TravelTime travelTime,
TravelDisutilityFactory travelDisutilityFactory,
DrtRouteConstraintsCalculator routeConstraintsCalculator, ConstraintSetChooser constraintSetChooser) {
DrtRouteConstraintsCalculator routeConstraintsCalculator) {
this.drtCfg = drtCfg;
this.travelTime = travelTime;
this.routeConstraintsCalculator = routeConstraintsCalculator;
this.constraintSetChooser = constraintSetChooser;
router = leastCostPathCalculatorFactory.createPathCalculator(modalNetwork,
travelDisutilityFactory.createTravelDisutility(travelTime), travelTime);
}
Expand All @@ -71,18 +70,15 @@ 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)
.orElse(drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet());
double maxTravelTime = routeConstraintsCalculator.getMaxTravelTime(constraintsSet, unsharedRideTime);
double maxRideDuration = routeConstraintsCalculator.getMaxRideTime(constraintsSet, unsharedRideTime);
DrtRouteConstraints constraints = routeConstraintsCalculator.calculateRouteConstraints(departureTime, accessActLink, egressActLink, person,
tripAttributes, unsharedRideTime, unsharedDistance);

DrtRoute route = routeFactories.createRoute(DrtRoute.class, accessActLink.getId(), egressActLink.getId());
route.setDistance(unsharedDistance);
route.setTravelTime(maxTravelTime);
route.setMaxRideTime(maxRideDuration);
route.setTravelTime(constraints.maxTravelTime());
route.setMaxRideTime(constraints.maxRideTime());
route.setDirectRideTime(unsharedRideTime);
route.setMaxWaitTime(constraintsSet.maxWaitTime);
route.setMaxWaitTime(constraints.maxWaitTime());

if (this.drtCfg.storeUnsharedPath) {
route.setUnsharedPath(unsharedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void install() {
// yyyy possibly not used for door2door; try to move inside the corresponding switch statement below. kai, feb'24


bindModal(DrtRouteConstraintsCalculator.class).toProvider(modalProvider(getter -> new DefaultDrtRouteConstraintsCalculator())).in(Singleton.class);
bindModal(DrtRouteConstraintsCalculator.class).toProvider(modalProvider(getter -> new DefaultDrtRouteConstraintsCalculator(
drtCfg, getter.getModal(ConstraintSetChooser.class)))).in(Singleton.class);
DrtOptimizationConstraintsSet optimizationConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
bindModal(ConstraintSetChooser.class).toProvider(
() -> (departureTime, accessActLink, egressActLink, person, tripAttributes)
Expand Down Expand Up @@ -159,8 +160,7 @@ public DrtRouteCreator get() {
var travelTime = getModalInstance(TravelTime.class);
return new DrtRouteCreator(drtCfg, getModalInstance(Network.class), leastCostPathCalculatorFactory,
travelTime, getModalInstance(TravelDisutilityFactory.class),
getModalInstance(DrtRouteConstraintsCalculator.class),
getModalInstance(ConstraintSetChooser.class));
getModalInstance(DrtRouteConstraintsCalculator.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ void testCottbusClosestAccessEgressStopFinder() {
scenario.getNetwork(), QuadTrees.createQuadTree(drtStops.values()));
DrtRouteCreator drtRouteCreator = new DrtRouteCreator(drtCfg, scenario.getNetwork(),
new SpeedyDijkstraFactory(), new FreeSpeedTravelTime(), TimeAsTravelDisutility::new,
new DefaultDrtRouteConstraintsCalculator(),
(departureTime, accessActLink, egressActLink, person, tripAttributes) -> Optional.of(defaultConstraintsSet));
new DefaultDrtRouteConstraintsCalculator(drtCfg,
(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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.matsim.contrib.zone.skims;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.contrib.common.zones.ZoneSystemParams;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class DvrpTravelTimeMatrixParams extends ReflectiveConfigGroupWithConfigu
+ " The unit is seconds. Default value is 0 s (for backward compatibility).")
@PositiveOrZero
public double maxNeighborTravelTime = 0; //[s]
@NotNull
private ZoneSystemParams zoneSystemParams;


Expand Down Expand Up @@ -101,6 +103,11 @@ public void handleAddUnknownParam(String paramName, String value) {
}

public ZoneSystemParams getZoneSystemParams() {
if(this.zoneSystemParams == null) {
SquareGridZoneSystemParams squareGridZoneSystemParams = new SquareGridZoneSystemParams();
squareGridZoneSystemParams.cellSize = 200;
this.zoneSystemParams = squareGridZoneSystemParams;
}
return zoneSystemParams;
}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.29.2</version>
<version>2.31.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -321,7 +321,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<version>5.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -420,7 +420,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<version>3.5.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down

0 comments on commit f8b3e9d

Please sign in to comment.