Skip to content

Commit

Permalink
Merge branch 'master' into freespeed-model
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Mar 18, 2024
2 parents dd3305d + a507598 commit 0175396
Show file tree
Hide file tree
Showing 54 changed files with 1,147 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.matsim.utils.eventsfilecomparison.EventsFileComparator.Result.FILES_ARE_EQUAL;
import static org.matsim.utils.eventsfilecomparison.ComparisonResult.FILES_ARE_EQUAL;

/**
* @author dziemke
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.matsim.contribs.discrete_mode_choice.components.utils;

import java.util.List;

import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.pt.routes.TransitPassengerRoute;

/**
Expand All @@ -9,8 +12,15 @@
* @author sebhoerl
*/
public class NullWaitingTimeEstimator implements PTWaitingTimeEstimator {

@Override
public double estimateWaitingTime(double agentDepartureTime, TransitPassengerRoute route) {
return 0.0;
}

@Override
public double estimateWaitingTime(List<? extends PlanElement> elements) {
return 0.0;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.matsim.contribs.discrete_mode_choice.components.utils;

import java.util.List;

import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.pt.routes.TransitPassengerRoute;

/**
Expand All @@ -11,5 +14,9 @@
* @author sebhoerl
*/
public interface PTWaitingTimeEstimator {

double estimateWaitingTime(double departureTime, TransitPassengerRoute route);

double estimateWaitingTime(List<? extends PlanElement> elements);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.utils.collections.Tuple;
Expand Down Expand Up @@ -54,17 +55,14 @@ private Tuple<Id<TransitLine>, Id<TransitRoute>> createId(TransitLine transitLin
return new Tuple<>(transitLine.getId(), transitRoute.getId());
}

@Override
public double estimateWaitingTime(List<? extends PlanElement> elements) {
double totalWaitingTime = 0.0;

for (PlanElement element : elements) {
if (element instanceof Leg) {
Leg leg = (Leg) element;

if (leg.getMode().equals("pt")) {
TransitPassengerRoute route = (TransitPassengerRoute) leg.getRoute();
totalWaitingTime += estimateWaitingTime(leg.getDepartureTime().seconds(), route);
}
if (element instanceof Leg leg && leg.getMode().equals(TransportMode.pt)) {
TransitPassengerRoute route = (TransitPassengerRoute) leg.getRoute();
totalWaitingTime += this.estimateWaitingTime(leg.getDepartureTime().seconds(), route);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.matsim.contrib.drt.optimizer.insertion.UnplannedRequestInserter;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtTaskFactory;
Expand Down Expand Up @@ -178,7 +179,8 @@ public EmptyVehicleRelocator get() {
getter.getModal(StopTimeCalculator.class), scheduleWaitBeforeDrive)))
.asEagerSingleton();

bindModal(DrtOfferAcceptor.class).toInstance(DrtOfferAcceptor.DEFAULT_ACCEPTOR);
bindModal(DefaultOfferAcceptor.class).toProvider(modalProvider(getter -> new DefaultOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
bindModal(DrtOfferAcceptor.class).to(modalKey(DefaultOfferAcceptor.class));

bindModal(ScheduleTimingUpdater.class).toProvider(modalProvider(
getter -> new ScheduleTimingUpdater(getter.get(MobsimTimer.class),
Expand All @@ -187,11 +189,11 @@ public EmptyVehicleRelocator get() {
bindModal(VrpLegFactory.class).toProvider(modalProvider(getter -> {
DvrpConfigGroup dvrpCfg = getter.get(DvrpConfigGroup.class);
MobsimTimer timer = getter.get(MobsimTimer.class);

// Makes basic DrtActionCreator create legs with consumption tracker
return v -> EDrtActionCreator.createLeg(dvrpCfg.mobsimMode, v, timer);
})).in(Singleton.class);

bindModal(EDrtActionCreator.class).toProvider(modalProvider(getter -> {
VrpAgentLogic.DynActionCreator delegate = drtCfg.getPrebookingParams().isPresent()
? getter.getModal(PrebookingActionCreator.class)
Expand All @@ -201,9 +203,9 @@ public EmptyVehicleRelocator get() {
// + adds ChargingActivity
return new EDrtActionCreator(delegate, getter.get(MobsimTimer.class));
})).asEagerSingleton();

bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(EDrtActionCreator.class));

bindModal(VrpOptimizer.class).to(modalKey(DrtOptimizer.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,53 +53,55 @@ public DrtModeZonalSystemModule(DrtConfigGroup drtCfg) {

@Override
public void install() {
DrtZonalSystemParams params = drtCfg.getZonalSystemParams().orElseThrow();

bindModal(DrtZonalSystem.class).toProvider(modalProvider(getter -> {
Network network = getter.getModal(Network.class);
switch (params.zonesGeneration) {
case ShapeFile:
final List<PreparedGeometry> preparedGeometries = loadPreparedGeometries(
if (drtCfg.getZonalSystemParams().isPresent()) {
DrtZonalSystemParams params = drtCfg.getZonalSystemParams().get();

bindModal(DrtZonalSystem.class).toProvider(modalProvider(getter -> {
Network network = getter.getModal(Network.class);
switch (params.zonesGeneration) {
case ShapeFile:
final List<PreparedGeometry> preparedGeometries = loadPreparedGeometries(
ConfigGroup.getInputFileURL(getConfig().getContext(), params.zonesShapeFile));
return DrtZonalSystem.createFromPreparedGeometries(network,
return DrtZonalSystem.createFromPreparedGeometries(network,
EntryStream.of(preparedGeometries).mapKeys(i -> (i + 1) + "").toMap());

case GridFromNetwork:
Preconditions.checkNotNull(params.cellSize);
var gridZones = drtCfg.operationalScheme == OperationalScheme.serviceAreaBased ?
case GridFromNetwork:
Preconditions.checkNotNull(params.cellSize);
var gridZones = drtCfg.operationalScheme == OperationalScheme.serviceAreaBased ?
createGridFromNetworkWithinServiceArea(network, params.cellSize,
loadPreparedGeometries(ConfigGroup.getInputFileURL(getConfig().getContext(),
drtCfg.drtServiceAreaShapeFile))) :
loadPreparedGeometries(ConfigGroup.getInputFileURL(getConfig().getContext(),
drtCfg.drtServiceAreaShapeFile))) :
createGridFromNetwork(network, params.cellSize);
return DrtZonalSystem.createFromPreparedGeometries(network, gridZones);

default:
throw new RuntimeException("Unsupported zone generation");
}
})).asEagerSingleton();

bindModal(DrtZoneTargetLinkSelector.class).toProvider(modalProvider(getter -> {
switch (params.targetLinkSelection) {
case mostCentral:
return new MostCentralDrtZoneTargetLinkSelector(getter.getModal(DrtZonalSystem.class));
case random:
return new RandomDrtZoneTargetLinkSelector();
default:
throw new RuntimeException(
return DrtZonalSystem.createFromPreparedGeometries(network, gridZones);

default:
throw new RuntimeException("Unsupported zone generation");
}
})).asEagerSingleton();

bindModal(DrtZoneTargetLinkSelector.class).toProvider(modalProvider(getter -> {
switch (params.targetLinkSelection) {
case mostCentral:
return new MostCentralDrtZoneTargetLinkSelector(getter.getModal(DrtZonalSystem.class));
case random:
return new RandomDrtZoneTargetLinkSelector();
default:
throw new RuntimeException(
"Unsupported target link selection = " + params.targetLinkSelection);
}
})).asEagerSingleton();
}
})).asEagerSingleton();

//zonal analysis
bindModal(ZonalIdleVehicleXYVisualiser.class).toProvider(modalProvider(
//zonal analysis
bindModal(ZonalIdleVehicleXYVisualiser.class).toProvider(modalProvider(
getter -> new ZonalIdleVehicleXYVisualiser(getter.get(MatsimServices.class), drtCfg.getMode(),
getter.getModal(DrtZonalSystem.class)))).asEagerSingleton();
addControlerListenerBinding().to(modalKey(ZonalIdleVehicleXYVisualiser.class));
addEventHandlerBinding().to(modalKey(ZonalIdleVehicleXYVisualiser.class));
getter.getModal(DrtZonalSystem.class)))).asEagerSingleton();
addControlerListenerBinding().to(modalKey(ZonalIdleVehicleXYVisualiser.class));
addEventHandlerBinding().to(modalKey(ZonalIdleVehicleXYVisualiser.class));

bindModal(DrtZonalWaitTimesAnalyzer.class).toProvider(modalProvider(
bindModal(DrtZonalWaitTimesAnalyzer.class).toProvider(modalProvider(
getter -> new DrtZonalWaitTimesAnalyzer(drtCfg, getter.getModal(DrtEventSequenceCollector.class),
getter.getModal(DrtZonalSystem.class)))).asEagerSingleton();
addControlerListenerBinding().to(modalKey(DrtZonalWaitTimesAnalyzer.class));
getter.getModal(DrtZonalSystem.class)))).asEagerSingleton();
addControlerListenerBinding().to(modalKey(DrtZonalWaitTimesAnalyzer.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.matsim.contrib.drt.optimizer.insertion.selective.SelectiveInsertionSearchQSimModule;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtStayTaskEndTimeCalculator;
Expand Down Expand Up @@ -138,7 +139,7 @@ public EmptyVehicleRelocator get() {
}).asEagerSingleton();

bindModal(DrtScheduleInquiry.class).to(DrtScheduleInquiry.class).asEagerSingleton();

boolean scheduleWaitBeforeDrive = drtCfg.getPrebookingParams().map(p -> p.scheduleWaitBeforeDrive).orElse(false);
bindModal(RequestInsertionScheduler.class).toProvider(modalProvider(
getter -> new DefaultRequestInsertionScheduler(getter.getModal(Fleet.class),
Expand All @@ -147,7 +148,8 @@ public EmptyVehicleRelocator get() {
getter.getModal(StopTimeCalculator.class), scheduleWaitBeforeDrive)))
.asEagerSingleton();

bindModal(DrtOfferAcceptor.class).toInstance(DrtOfferAcceptor.DEFAULT_ACCEPTOR);
bindModal(DefaultOfferAcceptor.class).toProvider(modalProvider(getter -> new DefaultOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
bindModal(DrtOfferAcceptor.class).to(modalKey(DefaultOfferAcceptor.class));

bindModal(ScheduleTimingUpdater.class).toProvider(modalProvider(
getter -> new ScheduleTimingUpdater(getter.get(MobsimTimer.class),
Expand All @@ -160,7 +162,7 @@ public EmptyVehicleRelocator get() {
return v -> VrpLegFactory.createWithOnlineTracker(dvrpCfg.mobsimMode, v, OnlineTrackerListener.NO_LISTENER,
timer);
})).in(Singleton.class);

if (drtCfg.getPrebookingParams().isEmpty()) {
bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(DrtActionCreator.class));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public RebalancingModule(DrtConfigGroup drtCfg) {
public void install() {
if (drtCfg.getRebalancingParams().isPresent()) {
RebalancingParams rebalancingParams = drtCfg.getRebalancingParams().get();
install(new DrtModeZonalSystemModule(drtCfg));

if (rebalancingParams.getRebalancingStrategyParams() instanceof MinCostFlowRebalancingStrategyParams) {
install(new DrtModeMinCostFlowRebalancingModule(drtCfg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

import java.util.Optional;

public class MaxDetourOfferAcceptor implements DrtOfferAcceptor{
public class DefaultOfferAcceptor implements DrtOfferAcceptor{
private final double maxAllowedPickupDelay;

public MaxDetourOfferAcceptor(double maxAllowedPickupDelay) {
/**
* Generate Default offer acceptor with max allowed pickup delay.
* @param maxAllowedPickupDelay: maximum allowed delay since the initially assigned pickup time.
*/
public DefaultOfferAcceptor(double maxAllowedPickupDelay) {
this.maxAllowedPickupDelay = maxAllowedPickupDelay;
}

/**
* Generate Default offer acceptor.
*/
public DefaultOfferAcceptor() {
this.maxAllowedPickupDelay = Double.POSITIVE_INFINITY;
}

@Override
public Optional<AcceptedDrtRequest> acceptDrtOffer(DrtRequest request, double departureTime, double arrivalTime) {
double updatedLatestStartTime = Math.min(departureTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
* @author Michal Maciejewski (michalm)
*/
public interface DrtOfferAcceptor {
DrtOfferAcceptor DEFAULT_ACCEPTOR = (request, departureTime, arrivalTime) -> Optional.of(
AcceptedDrtRequest.createFromOriginalRequest(request));

/**
* @param request drt request
* @param departureTime offered departure time for the new request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static DrtConfigGroup getSingleModeDrtConfig(Config config) {
@Comment(
"Defines the maximum delay allowed from the initial scheduled pick up time. Once the initial pickup time is offered, the latest promised"
+ "pickup time is calculated based on initial scheduled pickup time + maxAllowedPickupDelay. "
+ "By default, this limit is disabled. If enabled, a value between 120 and 240 is a good choice.")
+ "By default, this limit is disabled. If enabled, a value between 0 and 240 is a good choice.")
@PositiveOrZero
public double maxAllowedPickupDelay = Double.POSITIVE_INFINITY;// [s]

Expand Down Expand Up @@ -323,6 +323,12 @@ protected void checkConsistency(Config config) {
if (useModeFilteredSubnetwork) {
DvrpModeRoutingNetworkModule.checkUseModeFilteredSubnetworkAllowed(config, mode);
}

if ((maxDetourAlpha != Double.POSITIVE_INFINITY && maxDetourBeta != Double.POSITIVE_INFINITY) || maxAbsoluteDetour != Double.POSITIVE_INFINITY) {
Verify.verify(maxAllowedPickupDelay != Double.POSITIVE_INFINITY, "Detour constraints are activated, " +
"maxAllowedPickupDelay must be specified! A value between 0 and 240 seconds can be a good choice for maxAllowedPickupDelay.");
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.inject.name.Names;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.analysis.DrtEventSequenceCollector;
import org.matsim.contrib.drt.analysis.zonal.DrtModeZonalSystemModule;
import org.matsim.contrib.drt.fare.DrtFareHandler;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingModule;
import org.matsim.contrib.drt.prebooking.analysis.PrebookingModeAnalysisModule;
Expand Down Expand Up @@ -65,6 +66,7 @@ public void install() {
null :
ConfigGroup.getInputFileURL(getConfig().getContext(), drtCfg.vehiclesFile),
drtCfg.changeStartLinkToLastLinkInSchedule));
install(new DrtModeZonalSystemModule(drtCfg));
install(new RebalancingModule(drtCfg));
install(new DrtModeRoutingModule(drtCfg));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.MaxDetourOfferAcceptor;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
Expand All @@ -29,7 +26,6 @@ public void testMaxDetourConstraint() {
URL configUrl = IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("mielec"), "mielec_drt_config.xml");
Config config = ConfigUtils.loadConfig(configUrl, new MultiModeDrtConfigGroup(), new DvrpConfigGroup(),
new OTFVisConfigGroup());
MultiModeDrtConfigGroup multiModeDrtConfigGroup = MultiModeDrtConfigGroup.get(config);
DrtConfigGroup drtConfigGroup = DrtConfigGroup.getSingleModeDrtConfig(config);

// Max wait time
Expand All @@ -50,15 +46,6 @@ public void testMaxDetourConstraint() {

Controler controler = DrtControlerCreator.createControler(config, false);

for (DrtConfigGroup drtCfg : multiModeDrtConfigGroup.getModalElements()) {
controler.addOverridingQSimModule(new AbstractDvrpModeQSimModule(drtCfg.mode) {
@Override
protected void configureQSim() {
bindModal(DrtOfferAcceptor.class).toProvider(modalProvider(getter -> new MaxDetourOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
}
});
}

controler.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.matsim.contrib.drt.optimizer.DrtRequestInsertionRetryQueue;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.passenger.AcceptedDrtRequest;
import org.matsim.contrib.drt.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.schedule.DefaultDrtStopTask;
Expand Down Expand Up @@ -285,7 +286,7 @@ private DefaultUnplannedRequestInserter newInserter(Fleet fleet, double now,
VehicleEntry.EntryFactory vehicleEntryFactory, DrtRequestInsertionRetryQueue insertionRetryQueue,
DrtInsertionSearch insertionSearch, RequestInsertionScheduler insertionScheduler) {
return new DefaultUnplannedRequestInserter(mode, fleet, () -> now, eventsManager, insertionScheduler,
vehicleEntryFactory, insertionRetryQueue, insertionSearch, DrtOfferAcceptor.DEFAULT_ACCEPTOR,
vehicleEntryFactory, insertionRetryQueue, insertionSearch, new DefaultOfferAcceptor(),
forkJoinPoolExtension.forkJoinPool, StaticPassengerStopDurationProvider.of(10.0, 0.0));
}

Expand Down
Loading

0 comments on commit 0175396

Please sign in to comment.