Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make drt work with custom constraints only #3336

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ private static void loadConfigGroups(Config config) {
MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);

DrtConfigGroup drtCfg = new DrtConfigGroup();
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtCfg.addOrGetDrtOptimizationConstraintsParams()
.addOrGetDefaultDrtOptimizationConstraintsSet();
defaultConstraintsSet.maxWaitTime = 2 * 3600;
defaultConstraintsSet.maxTravelTimeAlpha = 5;
defaultConstraintsSet.maxTravelTimeBeta = 15 * 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.matsim.contrib.drt.extension.edrt.scheduler.EmptyVehicleChargingScheduler;
import org.matsim.contrib.drt.optimizer.*;
import org.matsim.contrib.drt.optimizer.constraints.DefaultDrtOptimizationConstraintsSet;
import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet;
import org.matsim.contrib.drt.optimizer.depot.DepotFinder;
import org.matsim.contrib.drt.optimizer.insertion.CostCalculationStrategy;
import org.matsim.contrib.drt.optimizer.insertion.DefaultInsertionCostCalculator;
Expand Down Expand Up @@ -143,7 +144,7 @@ public EmptyVehicleChargingScheduler get() {
bindModal(VehicleEntry.EntryFactory.class).toProvider(
EDrtVehicleDataEntryFactory.EDrtVehicleDataEntryFactoryProvider.class).asEagerSingleton();

DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DrtOptimizationConstraintsSet defaultConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
bindModal(CostCalculationStrategy.class)
.to(defaultConstraintsSet.rejectRequestIfMaxWaitOrTravelTimeViolated
?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void test() {
DrtConfigGroup drtConfigGroup = drtWithShiftsConfigGroup;
drtConfigGroup.mode = TransportMode.drt;
drtConfigGroup.stopDuration = 30.;
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtConfigGroup.addOrGetDrtOptimizationConstraintsParams()
.addOrGetDefaultDrtOptimizationConstraintsSet();
defaultConstraintsSet.maxTravelTimeAlpha = 1.5;
defaultConstraintsSet.maxTravelTimeBeta = 10. * 60.;
defaultConstraintsSet.maxWaitTime = 600.;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ void test() {

DrtConfigGroup drtConfigGroup = drtWithShiftsConfigGroup;
drtConfigGroup.mode = TransportMode.drt;
DefaultDrtOptimizationConstraintsSet constraintsSet = drtConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DefaultDrtOptimizationConstraintsSet constraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtConfigGroup.addOrGetDrtOptimizationConstraintsParams()
.addOrGetDefaultDrtOptimizationConstraintsSet();
constraintsSet.maxTravelTimeAlpha = 1.5;
constraintsSet.maxTravelTimeBeta = 10. * 60.;
drtConfigGroup.stopDuration = 30.;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void test() {

DrtConfigGroup drtConfigGroup = drtWithShiftsConfigGroup;
drtConfigGroup.mode = TransportMode.drt;
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtConfigGroup.addOrGetDrtOptimizationConstraintsParams()
.addOrGetDefaultDrtOptimizationConstraintsSet();
drtConfigGroup.stopDuration = 30.;
defaultConstraintsSet.maxTravelTimeAlpha = 1.5;
defaultConstraintsSet.maxTravelTimeBeta = 10. * 60.;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,122 +33,124 @@

public class RunShiftDrtScenarioIT {

@Test
void test() {

MultiModeDrtConfigGroup multiModeDrtConfigGroup = new MultiModeDrtConfigGroup(DrtWithOperationsConfigGroup::new);

String fleetFile = "holzkirchenFleet.xml";
String plansFile = "holzkirchenPlans.xml.gz";
String networkFile = "holzkirchenNetwork.xml.gz";
String opFacilitiesFile = "holzkirchenOperationFacilities.xml";
String shiftsFile = "holzkirchenShifts.xml";

DrtWithOperationsConfigGroup drtWithShiftsConfigGroup = (DrtWithOperationsConfigGroup) multiModeDrtConfigGroup.createParameterSet("drt");

drtWithShiftsConfigGroup.mode = TransportMode.drt;
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtWithShiftsConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
drtWithShiftsConfigGroup.stopDuration = 30.;
defaultConstraintsSet.maxTravelTimeAlpha = 1.5;
defaultConstraintsSet.maxTravelTimeBeta = 10. * 60.;
defaultConstraintsSet.maxWaitTime = 600.;
defaultConstraintsSet.rejectRequestIfMaxWaitOrTravelTimeViolated = true;
defaultConstraintsSet.maxWalkDistance = 1000.;
drtWithShiftsConfigGroup.useModeFilteredSubnetwork = false;
drtWithShiftsConfigGroup.vehiclesFile = fleetFile;
drtWithShiftsConfigGroup.operationalScheme = DrtConfigGroup.OperationalScheme.door2door;
drtWithShiftsConfigGroup.plotDetailedCustomerStats = true;
drtWithShiftsConfigGroup.idleVehiclesReturnToDepots = false;

drtWithShiftsConfigGroup.addParameterSet(new ExtensiveInsertionSearchParams());

ConfigGroup rebalancing = drtWithShiftsConfigGroup.createParameterSet("rebalancing");
drtWithShiftsConfigGroup.addParameterSet(rebalancing);
((RebalancingParams) rebalancing).interval = 600;

MinCostFlowRebalancingStrategyParams strategyParams = new MinCostFlowRebalancingStrategyParams();
strategyParams.targetAlpha = 0.3;
strategyParams.targetBeta = 0.3;

drtWithShiftsConfigGroup.getRebalancingParams().get().addParameterSet(strategyParams);

DrtZoneSystemParams drtZoneSystemParams = new DrtZoneSystemParams();
SquareGridZoneSystemParams zoneParams = (SquareGridZoneSystemParams) drtZoneSystemParams.createParameterSet(SquareGridZoneSystemParams.SET_NAME);
zoneParams.cellSize = 500.;
drtZoneSystemParams.addParameterSet(zoneParams);
drtZoneSystemParams.targetLinkSelection = DrtZoneSystemParams.TargetLinkSelection.mostCentral;
drtWithShiftsConfigGroup.addParameterSet(drtZoneSystemParams);

multiModeDrtConfigGroup.addParameterSet(drtWithShiftsConfigGroup);

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

final Config config = ConfigUtils.createConfig(multiModeDrtConfigGroup,
dvrpConfigGroup);
config.setContext(ExamplesUtils.getTestScenarioURL("holzkirchen"));

Set<String> modes = new HashSet<>();
modes.add("drt");
config.travelTimeCalculator().setAnalyzedModes(modes);

ScoringConfigGroup.ModeParams scoreParams = new ScoringConfigGroup.ModeParams("drt");
config.scoring().addModeParams(scoreParams);
ScoringConfigGroup.ModeParams scoreParams2 = new ScoringConfigGroup.ModeParams("walk");
config.scoring().addModeParams(scoreParams2);

config.plans().setInputFile(plansFile);
config.network().setInputFile(networkFile);

config.qsim().setSimStarttimeInterpretation(QSimConfigGroup.StarttimeInterpretation.onlyUseStarttime);
config.qsim().setSimEndtimeInterpretation(QSimConfigGroup.EndtimeInterpretation.minOfEndtimeAndMobsimFinished);

final ScoringConfigGroup.ActivityParams home = new ScoringConfigGroup.ActivityParams("home");
home.setTypicalDuration(8 * 3600);
final ScoringConfigGroup.ActivityParams other = new ScoringConfigGroup.ActivityParams("other");
other.setTypicalDuration(4 * 3600);
final ScoringConfigGroup.ActivityParams education = new ScoringConfigGroup.ActivityParams("education");
education.setTypicalDuration(6 * 3600);
final ScoringConfigGroup.ActivityParams shopping = new ScoringConfigGroup.ActivityParams("shopping");
shopping.setTypicalDuration(2 * 3600);
final ScoringConfigGroup.ActivityParams work = new ScoringConfigGroup.ActivityParams("work");
work.setTypicalDuration(2 * 3600);

config.scoring().addActivityParams(home);
config.scoring().addActivityParams(other);
config.scoring().addActivityParams(education);
config.scoring().addActivityParams(shopping);
config.scoring().addActivityParams(work);

final ReplanningConfigGroup.StrategySettings stratSets = new ReplanningConfigGroup.StrategySettings();
stratSets.setWeight(1);
stratSets.setStrategyName("ChangeExpBeta");
config.replanning().addStrategySettings(stratSets);

config.controller().setLastIteration(1);
config.controller().setWriteEventsInterval(1);

config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setOutputDirectory("test/output/holzkirchen_shifts");

DrtOperationsParams operationsParams = (DrtOperationsParams) drtWithShiftsConfigGroup.createParameterSet(DrtOperationsParams.SET_NAME);
ShiftsParams shiftsParams = (ShiftsParams) operationsParams.createParameterSet(ShiftsParams.SET_NAME);
OperationFacilitiesParams operationFacilitiesParams = (OperationFacilitiesParams) operationsParams.createParameterSet(OperationFacilitiesParams.SET_NAME);
operationsParams.addParameterSet(shiftsParams);
operationsParams.addParameterSet(operationFacilitiesParams);

operationFacilitiesParams.operationFacilityInputFile = opFacilitiesFile;
shiftsParams.shiftInputFile = shiftsFile;
shiftsParams.allowInFieldChangeover = true;
drtWithShiftsConfigGroup.addParameterSet(operationsParams);

DrtFareParams drtFareParams = new DrtFareParams();
drtFareParams.baseFare = 1.;
drtFareParams.distanceFare_m = 1. / 1000;
drtWithShiftsConfigGroup.addParameterSet(drtFareParams);

final Controler run = DrtOperationsControlerCreator.createControler(config, false);
run.run();
}
@Test
void test() {

MultiModeDrtConfigGroup multiModeDrtConfigGroup = new MultiModeDrtConfigGroup(DrtWithOperationsConfigGroup::new);

String fleetFile = "holzkirchenFleet.xml";
String plansFile = "holzkirchenPlans.xml.gz";
String networkFile = "holzkirchenNetwork.xml.gz";
String opFacilitiesFile = "holzkirchenOperationFacilities.xml";
String shiftsFile = "holzkirchenShifts.xml";

DrtWithOperationsConfigGroup drtWithShiftsConfigGroup = (DrtWithOperationsConfigGroup) multiModeDrtConfigGroup.createParameterSet("drt");

drtWithShiftsConfigGroup.mode = TransportMode.drt;
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtWithShiftsConfigGroup.addOrGetDrtOptimizationConstraintsParams()
.addOrGetDefaultDrtOptimizationConstraintsSet();
drtWithShiftsConfigGroup.stopDuration = 30.;
defaultConstraintsSet.maxTravelTimeAlpha = 1.5;
defaultConstraintsSet.maxTravelTimeBeta = 10. * 60.;
defaultConstraintsSet.maxWaitTime = 600.;
defaultConstraintsSet.rejectRequestIfMaxWaitOrTravelTimeViolated = true;
defaultConstraintsSet.maxWalkDistance = 1000.;
drtWithShiftsConfigGroup.useModeFilteredSubnetwork = false;
drtWithShiftsConfigGroup.vehiclesFile = fleetFile;
drtWithShiftsConfigGroup.operationalScheme = DrtConfigGroup.OperationalScheme.door2door;
drtWithShiftsConfigGroup.plotDetailedCustomerStats = true;
drtWithShiftsConfigGroup.idleVehiclesReturnToDepots = false;

drtWithShiftsConfigGroup.addParameterSet(new ExtensiveInsertionSearchParams());

ConfigGroup rebalancing = drtWithShiftsConfigGroup.createParameterSet("rebalancing");
drtWithShiftsConfigGroup.addParameterSet(rebalancing);
((RebalancingParams) rebalancing).interval = 600;

MinCostFlowRebalancingStrategyParams strategyParams = new MinCostFlowRebalancingStrategyParams();
strategyParams.targetAlpha = 0.3;
strategyParams.targetBeta = 0.3;

drtWithShiftsConfigGroup.getRebalancingParams().get().addParameterSet(strategyParams);

DrtZoneSystemParams drtZoneSystemParams = new DrtZoneSystemParams();
SquareGridZoneSystemParams zoneParams = (SquareGridZoneSystemParams) drtZoneSystemParams.createParameterSet(SquareGridZoneSystemParams.SET_NAME);
zoneParams.cellSize = 500.;
drtZoneSystemParams.addParameterSet(zoneParams);
drtZoneSystemParams.targetLinkSelection = DrtZoneSystemParams.TargetLinkSelection.mostCentral;
drtWithShiftsConfigGroup.addParameterSet(drtZoneSystemParams);

multiModeDrtConfigGroup.addParameterSet(drtWithShiftsConfigGroup);

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

final Config config = ConfigUtils.createConfig(multiModeDrtConfigGroup,
dvrpConfigGroup);
config.setContext(ExamplesUtils.getTestScenarioURL("holzkirchen"));

Set<String> modes = new HashSet<>();
modes.add("drt");
config.travelTimeCalculator().setAnalyzedModes(modes);

ScoringConfigGroup.ModeParams scoreParams = new ScoringConfigGroup.ModeParams("drt");
config.scoring().addModeParams(scoreParams);
ScoringConfigGroup.ModeParams scoreParams2 = new ScoringConfigGroup.ModeParams("walk");
config.scoring().addModeParams(scoreParams2);

config.plans().setInputFile(plansFile);
config.network().setInputFile(networkFile);

config.qsim().setSimStarttimeInterpretation(QSimConfigGroup.StarttimeInterpretation.onlyUseStarttime);
config.qsim().setSimEndtimeInterpretation(QSimConfigGroup.EndtimeInterpretation.minOfEndtimeAndMobsimFinished);

final ScoringConfigGroup.ActivityParams home = new ScoringConfigGroup.ActivityParams("home");
home.setTypicalDuration(8 * 3600);
final ScoringConfigGroup.ActivityParams other = new ScoringConfigGroup.ActivityParams("other");
other.setTypicalDuration(4 * 3600);
final ScoringConfigGroup.ActivityParams education = new ScoringConfigGroup.ActivityParams("education");
education.setTypicalDuration(6 * 3600);
final ScoringConfigGroup.ActivityParams shopping = new ScoringConfigGroup.ActivityParams("shopping");
shopping.setTypicalDuration(2 * 3600);
final ScoringConfigGroup.ActivityParams work = new ScoringConfigGroup.ActivityParams("work");
work.setTypicalDuration(2 * 3600);

config.scoring().addActivityParams(home);
config.scoring().addActivityParams(other);
config.scoring().addActivityParams(education);
config.scoring().addActivityParams(shopping);
config.scoring().addActivityParams(work);

final ReplanningConfigGroup.StrategySettings stratSets = new ReplanningConfigGroup.StrategySettings();
stratSets.setWeight(1);
stratSets.setStrategyName("ChangeExpBeta");
config.replanning().addStrategySettings(stratSets);

config.controller().setLastIteration(1);
config.controller().setWriteEventsInterval(1);

config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setOutputDirectory("test/output/holzkirchen_shifts");

DrtOperationsParams operationsParams = (DrtOperationsParams) drtWithShiftsConfigGroup.createParameterSet(DrtOperationsParams.SET_NAME);
ShiftsParams shiftsParams = (ShiftsParams) operationsParams.createParameterSet(ShiftsParams.SET_NAME);
OperationFacilitiesParams operationFacilitiesParams = (OperationFacilitiesParams) operationsParams.createParameterSet(OperationFacilitiesParams.SET_NAME);
operationsParams.addParameterSet(shiftsParams);
operationsParams.addParameterSet(operationFacilitiesParams);

operationFacilitiesParams.operationFacilityInputFile = opFacilitiesFile;
shiftsParams.shiftInputFile = shiftsFile;
shiftsParams.allowInFieldChangeover = true;
drtWithShiftsConfigGroup.addParameterSet(operationsParams);

DrtFareParams drtFareParams = new DrtFareParams();
drtFareParams.baseFare = 1.;
drtFareParams.distanceFare_m = 1. / 1000;
drtWithShiftsConfigGroup.addParameterSet(drtFareParams);

final Controler run = DrtOperationsControlerCreator.createControler(config, false);
run.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.matsim.contrib.common.util.ChartSaveUtils;
import org.matsim.contrib.drt.analysis.DrtEventSequenceCollector.EventSequence;
import org.matsim.contrib.drt.optimizer.constraints.DefaultDrtOptimizationConstraintsSet;
import org.matsim.contrib.drt.optimizer.constraints.DrtOptimizationConstraintsSet;
import org.matsim.contrib.drt.passenger.events.DrtRequestSubmittedEvent;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtStayTask;
Expand Down Expand Up @@ -664,15 +665,18 @@ private static void analyseDetours(Network network, List<DrtLeg> legs, Map<Id<Re
distances);
ChartSaveUtils.saveAsPNG(chart, fileName + "_distancePlot", 1500, 1500);

DefaultDrtOptimizationConstraintsSet defaultConstraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
DrtOptimizationConstraintsSet constraintsSet = drtCfg.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet();
Pair<Double, Double> lineCoeffs = null;
if(constraintsSet instanceof DefaultDrtOptimizationConstraintsSet defaultConstraintsSet) {
lineCoeffs = Pair.of(defaultConstraintsSet.maxTravelTimeAlpha,
defaultConstraintsSet.maxTravelTimeBeta);
}
final JFreeChart chart2 = DensityScatterPlots.createPlot("Travel Times", "travel time [s]", "unshared ride time [s]", travelTimes,
Pair.of(defaultConstraintsSet.maxTravelTimeAlpha,
defaultConstraintsSet.maxTravelTimeBeta));
lineCoeffs);
ChartSaveUtils.saveAsPNG(chart2, fileName + "_travelTimePlot", 1500, 1500);

final JFreeChart chart3 = DensityScatterPlots.createPlot("Ride Times", "ride time [s]", "unshared ride time [s]", rideTimes,
Pair.of(defaultConstraintsSet.maxTravelTimeAlpha,
defaultConstraintsSet.maxTravelTimeBeta));
lineCoeffs);
ChartSaveUtils.saveAsPNG(chart3, fileName + "_rideTimePlot", 1500, 1500);
}
}
Expand Down
Loading
Loading