Skip to content

Commit

Permalink
Merge branch 'master' into feat/prebooking
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Oct 11, 2023
2 parents be39de0 + ee2df4d commit 46e80a7
Show file tree
Hide file tree
Showing 88 changed files with 1,942 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.drt.optimizer.depot.DepotFinder;
import org.matsim.contrib.drt.optimizer.depot.Depots;
import org.matsim.contrib.drt.optimizer.insertion.UnplannedRequestInserter;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy.Relocation;
Expand All @@ -43,6 +42,8 @@
import org.matsim.core.mobsim.framework.MobsimTimer;
import org.matsim.core.mobsim.framework.events.MobsimBeforeSimStepEvent;

import static org.matsim.contrib.drt.schedule.DrtTaskBaseType.STAY;

/**
* @author michalm
*/
Expand Down Expand Up @@ -96,6 +97,7 @@ public void notifyMobsimBeforeSimStep(@SuppressWarnings("rawtypes") MobsimBefore
requestInserter.scheduleUnplannedRequests(unplannedRequests.getSchedulableRequests());
}

relocateVehiclesToDepot(drtCfg.returnToDepotEvaluationInterval, drtCfg.returnToDepotTimeout);
if (rebalancingInterval != null && e.getSimulationTime() % rebalancingInterval == 0) {
if (!scheduleTimingUpdated) {
for (DvrpVehicle v : fleet.getVehicles().values()) {
Expand All @@ -117,7 +119,7 @@ private void rebalanceFleet() {
for (Relocation r : relocations) {
Link currentLink = ((DrtStayTask)r.vehicle.getSchedule().getCurrentTask()).getLink();
if (currentLink != r.link) {
relocator.relocateVehicle(r.vehicle, r.link);
relocator.relocateVehicle(r.vehicle, r.link, EmptyVehicleRelocator.RELOCATE_VEHICLE_TASK_TYPE);
}
}
}
Expand All @@ -131,15 +133,29 @@ public void requestSubmitted(Request request) {
@Override
public void nextTask(DvrpVehicle vehicle) {
scheduleTimingUpdater.updateBeforeNextTask(vehicle);

vehicle.getSchedule().nextTask();
}

// if STOP->STAY then choose the best depot
if (drtCfg.idleVehiclesReturnToDepots && Depots.isSwitchingFromStopToStay(vehicle)) {
Link depotLink = depotFinder.findDepot(vehicle);
if (depotLink != null) {
relocator.relocateVehicle(vehicle, depotLink);
}
private void relocateVehiclesToDepot(double evaluationInterval, double timeout) {
if (drtCfg.idleVehiclesReturnToDepots && mobsimTimer.getTimeOfDay() % evaluationInterval == 0) {
fleet.getVehicles().values().stream()
.filter(scheduleInquiry::isIdle)
.filter(v -> stayTimeoutExceeded(v, timeout))
.forEach(v -> {
Link depotLink = depotFinder.findDepot(v);
if (depotLink != null) {
relocator.relocateVehicle(v, depotLink, EmptyVehicleRelocator.RELOCATE_VEHICLE_TO_DEPOT_TASK_TYPE);
}
});
}
}

boolean stayTimeoutExceeded(DvrpVehicle vehicle, double timeout) {
if (STAY.isBaseTypeOf(vehicle.getSchedule().getCurrentTask())) {
double now = mobsimTimer.getTimeOfDay();
double taskStart = vehicle.getSchedule().getCurrentTask().getBeginTime();
return (now - taskStart) > timeout;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */

package org.matsim.contrib.drt.optimizer.rebalancing;

import org.matsim.core.config.ReflectiveConfigGroup;

/**
* Custom rebalancing strategy parameters. User is responsible for installing rebalancing module and parameters.
*/
public final class CustomRebalancingStrategyParams extends ReflectiveConfigGroup
implements RebalancingParams.RebalancingStrategyParams {
public static final String SET_NAME = "CustomRebalancingStrategy";

public CustomRebalancingStrategyParams() {
super(SET_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void install() {
install(new DrtModePlusOneRebalanceModule(drtCfg));
} else if (rebalancingParams.getRebalancingStrategyParams() instanceof FeedforwardRebalancingStrategyParams) {
install(new DrtModeFeedforwardRebalanceModule(drtCfg));
} else if (rebalancingParams.getRebalancingStrategyParams() instanceof CustomRebalancingStrategyParams) {
// User is responsible for installing custom module
} else {
throw new RuntimeException(
"Unsupported rebalancingStrategyParams: " + rebalancingParams.getRebalancingStrategyParams());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ private void initSingletonParameterSets() {
addDefinition(PlusOneRebalancingStrategyParams.SET_NAME, PlusOneRebalancingStrategyParams::new,
() -> (ConfigGroup)rebalancingStrategyParams,
params -> rebalancingStrategyParams = (RebalancingStrategyParams)params);
addDefinition(CustomRebalancingStrategyParams.SET_NAME, CustomRebalancingStrategyParams::new,
() -> (ConfigGroup)rebalancingStrategyParams,
params -> rebalancingStrategyParams = (RebalancingStrategyParams)params);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public static DrtConfigGroup getSingleModeDrtConfig(Config config) {
@Comment("Idle vehicles return to the nearest of all start links. See: DvrpVehicle.getStartLink()")
public boolean idleVehiclesReturnToDepots = false;

@Parameter
@Comment("Specifies the duration (seconds) a vehicle needs to be idle in order to get send back to the depot." +
"Please be aware, that returnToDepotEvaluationInterval describes the minimal time a vehicle will be idle before it gets send back to depot.")
public double returnToDepotTimeout = 60;

@Parameter
@Comment("Specifies the time interval (seconds) a vehicle gets evaluated to be send back to depot.")
public double returnToDepotEvaluationInterval = 60;

public enum OperationalScheme {
stopbased, door2door, serviceAreaBased
}
Expand Down Expand Up @@ -280,6 +289,11 @@ protected void checkConsistency(Config config) {
+ " in order to speed up the DRT route update during the replanning phase.");
}

if (this.idleVehiclesReturnToDepots && this.returnToDepotTimeout < this.returnToDepotEvaluationInterval) {
log.warn("idleVehiclesReturnToDepots is active and returnToDepotTimeout < returnToDepotEvaluationInterval. " +
"Vehicles will be send back to depot after {} seconds",returnToDepotEvaluationInterval);
}

Verify.verify(getParameterSets(MinCostFlowRebalancingStrategyParams.SET_NAME).size() <= 1,
"More than one rebalancing parameter sets is specified");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
*/
public class EmptyVehicleRelocator {
public static final DrtTaskType RELOCATE_VEHICLE_TASK_TYPE = new DrtTaskType("RELOCATE", DRIVE);
public static final DrtTaskType RELOCATE_VEHICLE_TO_DEPOT_TASK_TYPE = new DrtTaskType("RELOCATE_TO_DEPOT", DRIVE);

private final TravelTime travelTime;
private final MobsimTimer timer;
Expand All @@ -55,28 +56,28 @@ public EmptyVehicleRelocator(Network network, TravelTime travelTime, TravelDisut
router = new SpeedyALTFactory().createPathCalculator(network, travelDisutility, travelTime);
}

public void relocateVehicle(DvrpVehicle vehicle, Link link) {
public void relocateVehicle(DvrpVehicle vehicle, Link link, DrtTaskType relocationTaskType) {
DrtStayTask currentTask = (DrtStayTask)vehicle.getSchedule().getCurrentTask();
Link currentLink = currentTask.getLink();

if (currentLink != link) {
VrpPathWithTravelData path = VrpPaths.calcAndCreatePath(currentLink, link, timer.getTimeOfDay(), router,
travelTime);
if (path.getArrivalTime() < vehicle.getServiceEndTime()) {
relocateVehicleImpl(vehicle, path);
relocateVehicleImpl(vehicle, path, relocationTaskType);
}
}
}

private void relocateVehicleImpl(DvrpVehicle vehicle, VrpPathWithTravelData vrpPath) {
private void relocateVehicleImpl(DvrpVehicle vehicle, VrpPathWithTravelData vrpPath, DrtTaskType relocationTaskType) {
Schedule schedule = vehicle.getSchedule();
DrtStayTask stayTask = (DrtStayTask)schedule.getCurrentTask();
if (stayTask.getTaskIdx() != schedule.getTaskCount() - 1) {
throw new IllegalStateException("The current STAY task is not last. Not possible without prebooking");
}

stayTask.setEndTime(vrpPath.getDepartureTime()); // finish STAY
schedule.addTask(taskFactory.createDriveTask(vehicle, vrpPath, RELOCATE_VEHICLE_TASK_TYPE)); // add RELOCATE
schedule.addTask(taskFactory.createDriveTask(vehicle, vrpPath, relocationTaskType)); // add RELOCATE
// append STAY
schedule.addTask(taskFactory.createStayTask(vehicle, vrpPath.getArrivalTime(), vehicle.getServiceEndTime(),
vrpPath.getToLink()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright (C) Schweizerische Bundesbahnen SBB, 2018.
*/

/* *********************************************************************** *
* project: org.matsim.* *
*
* *
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package org.matsim.contrib.zone.skims;

import java.util.ArrayList;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testA() {
PSimConfigGroup pSimConfigGroup = new PSimConfigGroup();
config.addModule(pSimConfigGroup);
pSimConfigGroup.setIterationsPerCycle(20);

config.plansCalcRoute().setRoutingRandomness(0.);

//identify selector strategies
Expand Down Expand Up @@ -95,12 +95,12 @@ public void testA() {

((Controler) runPSim.getMatsimControler()).addOverridingModule(new AbstractModule() {
@Override
public void install() {
public void install() {
this.bind(TransitEmulator.class).to(NoTransitEmulator.class);
}
});


runPSim.run();
double psimScore = execScoreTracker.executedScore;
logger.info("RunPSim score was " + psimScore);
Expand All @@ -109,15 +109,15 @@ public void install() {
Population popActual = PopulationUtils.createPopulation( config );
PopulationUtils.readPopulation( popActual, outDir + "/output_plans.xml.gz" );
new PopulationComparison().compare( popExpected, popActual ) ;
Assert.assertEquals("RunPsim score changed.", 138.90472630897597d, psimScore, MatsimTestUtils.EPSILON);
Assert.assertEquals("RunPsim score changed.", 138.90474624352407, psimScore, MatsimTestUtils.EPSILON);
// Assert.assertEquals("RunPsim score changed.", 134.54001491094124d, psimScore, MatsimTestUtils.EPSILON);
// Assert.assertEquals("RunPsim score changed.", 134.52369453719413d, psimScore, MatsimTestUtils.EPSILON);
// Assert.assertEquals("RunPsim score changed.", 132.73129073101293d, psimScore, MatsimTestUtils.EPSILON);
}

/**
* For comparison run 2 normal qsim iterations. Psim score should be slightly higher than default Controler score.
*
*
* Prior to implementing routing mode RunPSimTest tested only that psimScore outperformed default Controler on this
* test for executed score by a margin > 1%. In the last commit in matsim master where the test ran, the psim score
* in testA() was 134.52369453719413 and qsim score in testB was 131.84309487251033).
Expand All @@ -134,7 +134,7 @@ public void testB() {
ExecScoreTracker execScoreTracker = new ExecScoreTracker(controler);
controler.addControlerListener(execScoreTracker);
controler.run();

double qsimScore = execScoreTracker.executedScore;
logger.info("Default controler score was " + qsimScore );
// Assert.assertEquals("Default controler score changed.", 131.84309487251033d, qsimScore, MatsimTestUtils.EPSILON);
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright (C) Schweizerische Bundesbahnen SBB, 2018.
*/

/* *********************************************************************** *
* project: org.matsim.* *
*
* *
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package ch.sbb.matsim;

import ch.sbb.matsim.mobsim.qsim.SBBTransitModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright (C) Schweizerische Bundesbahnen SBB, 2018.
*/

/* *********************************************************************** *
* project: org.matsim.* *
*
* *
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package ch.sbb.matsim.analysis.skims;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright (C) Schweizerische Bundesbahnen SBB, 2018.
*/

/* *********************************************************************** *
* project: org.matsim.* *
*
* *
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package ch.sbb.matsim.analysis.skims;

import ch.sbb.matsim.analysis.skims.NetworkSkimMatrices.NetworkIndicators;
Expand Down Expand Up @@ -175,7 +190,7 @@ public static void main(String[] args) throws IOException {
"",
(line, route) -> route.getTransportMode().equals("train"));
}

skims.calculateAndWriteBeelineMatrix();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright (C) Schweizerische Bundesbahnen SBB, 2018.
*/

/* *********************************************************************** *
* project: org.matsim.* *
*
* *
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package ch.sbb.matsim.analysis.skims;

import java.util.Arrays;
Expand Down
Loading

0 comments on commit 46e80a7

Please sign in to comment.