Skip to content

Commit 8b61506

Browse files
Merge pull request #112 from moia-oss/master
Update MATSim CW35
2 parents aea1f51 + 42cff92 commit 8b61506

File tree

46 files changed

+2598
-1768
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2598
-1768
lines changed

contribs/application/src/main/java/org/matsim/application/prepare/freight/tripExtraction/ExtractRelevantFreightTrips.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private enum TripType {
7474
@CommandLine.Option(names = "--tripType", description = "Set the tripType: OUTGOING, INCOMING, TRANSIT, INTERNAL, ALL", defaultValue = "ALL")
7575
private TripType tripType;
7676

77+
@CommandLine.Option(names = "--LegMode", description = "Set leg mode for long distance freight legs.", defaultValue = "freight")
78+
private String legMode;
79+
7780
private final SplittableRandom rnd = new SplittableRandom(4711);
7881

7982
private final List<Coord> fromCoords = new ArrayList<>();
@@ -161,7 +164,7 @@ public Integer call() throws Exception {
161164
boolean destinationIsInside = relevantArea.contains(MGC.coord2Point(sct.transform(endCoord)));
162165

163166
Activity act0 = populationFactory.createActivityFromCoord("freight_start", null);
164-
Leg leg = populationFactory.createLeg("freight");
167+
Leg leg = populationFactory.createLeg(legMode);
165168
Activity act1 = populationFactory.createActivityFromCoord("freight_end", null);
166169

167170
switch (tripType) {
@@ -312,7 +315,7 @@ private void createActivitiesForIncomingTrips(boolean originIsInside, boolean de
312315
timeSpent += Math.floor(link.getLength() / link.getFreespeed()) + 1;
313316
}
314317
if (!isCoordSet) {
315-
Coord originalCoord = route.links.get(0).getCoord();
318+
Coord originalCoord = route.links.getFirst().getCoord();
316319
act0.setCoord(ct.transform(originalCoord));
317320
act0.setEndTime(departureTime);
318321
}

contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/operations/eshifts/run/ShiftEDrtModeOptimizerQSimModule.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilities;
1818
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilityFinder;
1919
import org.matsim.contrib.drt.extension.operations.shifts.config.ShiftsParams;
20-
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DefaultAssignShiftToVehicleLogic;
21-
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DefaultShiftStartLogic;
22-
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcher;
23-
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcherImpl;
20+
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.*;
2421
import org.matsim.contrib.drt.extension.operations.shifts.optimizer.ShiftVehicleDataEntryFactory;
2522
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtActionCreator;
2623
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtTaskFactory;
2724
import org.matsim.contrib.drt.extension.operations.shifts.scheduler.ShiftTaskScheduler;
28-
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShifts;
25+
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
2926
import org.matsim.contrib.drt.optimizer.VehicleEntry;
3027
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
3128
import org.matsim.contrib.drt.run.DrtConfigGroup;
@@ -67,19 +64,22 @@ protected void configureQSim() {
6764
).asEagerSingleton();
6865

6966
bindModal(DrtShiftDispatcher.class).toProvider(modalProvider(
70-
getter -> new EDrtShiftDispatcherImpl(((EShiftTaskScheduler) getter.getModal(ShiftTaskScheduler.class)), getter.getModal(ChargingInfrastructure.class),
71-
drtShiftParams, getter.getModal(OperationFacilities.class), new DrtShiftDispatcherImpl(getMode(),
72-
getter.getModal(DrtShifts.class), getter.getModal(Fleet.class),
73-
getter.get(MobsimTimer.class), getter.getModal(OperationFacilities.class), getter.getModal(OperationFacilityFinder.class),
74-
getter.getModal(ShiftTaskScheduler.class), getter.getModal(Network.class), getter.get(EventsManager.class),
75-
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
76-
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams)),
77-
getter.getModal(Fleet.class)))).asEagerSingleton();
67+
getter -> new EDrtShiftDispatcherImpl(((EShiftTaskScheduler) getter.getModal(ShiftTaskScheduler.class)),
68+
getter.getModal(ChargingInfrastructure.class), drtShiftParams, getter.getModal(OperationFacilities.class),
69+
new DrtShiftDispatcherImpl(getMode(), getter.getModal(Fleet.class), getter.get(MobsimTimer.class),
70+
getter.getModal(OperationFacilities.class), getter.getModal(OperationFacilityFinder.class),
71+
getter.getModal(ShiftTaskScheduler.class), getter.getModal(Network.class), getter.get(EventsManager.class),
72+
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
73+
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams),
74+
getter.getModal(ShiftScheduler.class)),
75+
getter.getModal(Fleet.class)))
76+
).asEagerSingleton();
7877

7978
bindModal(VehicleEntry.EntryFactory.class).toProvider(modalProvider(getter ->
8079
new ShiftVehicleDataEntryFactory(new EDrtVehicleDataEntryFactory(0),
8180
drtShiftParams.considerUpcomingShiftsForInsertion))).asEagerSingleton();
8281

82+
8383
bindModal(DrtTaskFactory.class).toProvider(modalProvider(getter -> new ShiftEDrtTaskFactoryImpl(new EDrtTaskFactoryImpl(), getter.getModal(OperationFacilities.class)))).in(Singleton.class);
8484
bindModal(ShiftDrtTaskFactory.class).toProvider(modalProvider(getter -> ((ShiftDrtTaskFactory) getter.getModal(DrtTaskFactory.class))));
8585

contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/operations/shifts/analysis/DumpShiftDataAtEndImpl.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import org.apache.logging.log4j.LogManager;
44
import org.apache.logging.log4j.Logger;
5+
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilitiesSpecification;
56
import org.matsim.contrib.drt.extension.operations.shifts.io.DrtShiftsWriter;
67
import org.matsim.contrib.drt.extension.operations.shifts.io.OperationFacilitiesWriter;
7-
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilitiesSpecification;
88
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
99
import org.matsim.core.controler.OutputDirectoryHierarchy;
1010
import org.matsim.core.controler.events.ShutdownEvent;
1111
import org.matsim.core.controler.listener.ShutdownListener;
1212

13-
import jakarta.inject.Provider;
14-
1513
/**
1614
* Dumps DRT shift related data at end. Based on {@link org.matsim.core.controler.corelisteners.DumpDataAtEndImpl}
1715
*
@@ -20,13 +18,13 @@
2018
final public class DumpShiftDataAtEndImpl implements ShutdownListener {
2119
private static final Logger log = LogManager.getLogger( DumpShiftDataAtEndImpl.class );
2220

23-
private final Provider<DrtShiftsSpecification> shifts;
21+
private final DrtShiftsSpecification shifts;
2422

2523
private final OperationFacilitiesSpecification operationFacilities;
2624

2725
private final OutputDirectoryHierarchy controlerIO;
2826

29-
public DumpShiftDataAtEndImpl(Provider<DrtShiftsSpecification> shifts, OperationFacilitiesSpecification operationFacilities, OutputDirectoryHierarchy controlerIO) {
27+
public DumpShiftDataAtEndImpl(DrtShiftsSpecification shifts, OperationFacilitiesSpecification operationFacilities, OutputDirectoryHierarchy controlerIO) {
3028
this.shifts = shifts;
3129
this.operationFacilities = operationFacilities;
3230
this.controlerIO = controlerIO;
@@ -44,7 +42,7 @@ public void notifyShutdown(ShutdownEvent event) {
4442
private void dumpShiftPans() {
4543
try {
4644
if ( this.shifts!=null){
47-
new DrtShiftsWriter(shifts.get()).writeFile(this.controlerIO.getOutputFilename("output_shifts.xml.gz"));
45+
new DrtShiftsWriter(shifts).writeFile(this.controlerIO.getOutputFilename("output_shifts.xml.gz"));
4846
}
4947
} catch ( Exception ee ) {
5048
log.error("Exception writing shifts.", ee);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.matsim.contrib.drt.extension.operations.shifts.analysis;
2+
3+
import com.google.inject.Provider;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.matsim.contrib.drt.extension.operations.shifts.io.DrtShiftsWriter;
7+
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
8+
import org.matsim.core.controler.OutputDirectoryHierarchy;
9+
import org.matsim.core.controler.events.IterationEndsEvent;
10+
import org.matsim.core.controler.listener.IterationEndsListener;
11+
12+
13+
/**
14+
*
15+
* @author nkuehnel / MOIA
16+
*/
17+
final public class RegularShiftDump implements IterationEndsListener {
18+
private static final Logger log = LogManager.getLogger( RegularShiftDump.class );
19+
20+
private final Provider<DrtShiftsSpecification> shifts;
21+
22+
private final OutputDirectoryHierarchy controlerIO;
23+
24+
public RegularShiftDump(Provider<DrtShiftsSpecification> shifts, OutputDirectoryHierarchy controlerIO) {
25+
this.shifts = shifts;
26+
this.controlerIO = controlerIO;
27+
}
28+
29+
private void dumpShiftPans(int iteration) {
30+
try {
31+
if ( this.shifts!=null){
32+
new DrtShiftsWriter(shifts.get()).writeFile(this.controlerIO.getIterationFilename(iteration, "output_shifts.xml.gz"));
33+
}
34+
} catch ( Exception ee ) {
35+
log.error("Exception writing shifts.", ee);
36+
}
37+
}
38+
39+
40+
@Override
41+
public void notifyIterationEnds(IterationEndsEvent event) {
42+
dumpShiftPans(event.getIteration());
43+
}
44+
}

contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/operations/shifts/analysis/ShiftDurationXY.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,28 @@ public void handleEvent(DrtShiftBreakStartedEvent event) {
5858
@Override
5959
public void handleEvent(final DrtShiftEndedEvent event) {
6060
if (event.getMode().equals(mode)) {
61-
final Double start = shift2StartTime.get(event.getShiftId());
62-
double duration = event.getTime() - start;
63-
final DrtShiftSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId());
64-
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
65-
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
61+
Map<Id<DrtShift>, DrtShiftSpecification> shiftSpecifications = shifts.get().getShiftSpecifications();
62+
if(shiftSpecifications.containsKey(event.getShiftId())) {
63+
final DrtShiftSpecification drtShift = shiftSpecifications.get(event.getShiftId());
64+
final Double start = shift2StartTime.get(event.getShiftId());
65+
double duration = event.getTime() - start;
66+
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
67+
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
68+
}
6669
}
6770
}
6871

6972
@Override
7073
public void handleEvent(DrtShiftBreakEndedEvent event) {
7174
if (event.getMode().equals(mode)) {
72-
final Double start = shift2BreakStartTime.get(event.getShiftId());
73-
double duration = event.getTime() - start;
74-
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
75-
double plannedDuration = drtShift.getDuration();
76-
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
75+
Map<Id<DrtShift>, DrtShiftSpecification> shiftSpecifications = shifts.get().getShiftSpecifications();
76+
if(shiftSpecifications.containsKey(event.getShiftId())) {
77+
final Double start = shift2BreakStartTime.get(event.getShiftId());
78+
double duration = event.getTime() - start;
79+
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
80+
double plannedDuration = drtShift.getDuration();
81+
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
82+
}
7783
}
7884
}
7985

contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/operations/shifts/analysis/ShiftHistogram.java

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.matsim.contrib.drt.extension.operations.shifts.analysis;
22

3-
import org.matsim.api.core.v01.Id;
4-
import org.matsim.api.core.v01.population.Population;
53
import org.matsim.contrib.drt.extension.operations.shifts.events.*;
6-
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShift;
74
import org.matsim.core.config.Config;
85
import org.matsim.core.utils.io.IOUtils;
96
import org.matsim.core.utils.misc.Time;
@@ -12,35 +9,30 @@
129
import java.io.OutputStream;
1310
import java.io.PrintStream;
1411
import java.io.UncheckedIOException;
15-
import java.util.Set;
1612

1713
/**
1814
* @author nkuehnel / MOIA
1915
*/
2016
public class ShiftHistogram implements DrtShiftStartedEventHandler, DrtShiftEndedEventHandler,
21-
DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {
17+
DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {
2218

2319
private final String mode;
2420

25-
public static final int DEFAULT_END_TIME = 30 * 3600;
26-
public static final int DEFAULT_BIN_SIZE = 300;
21+
public static final int DEFAULT_END_TIME = 30 * 3600;
22+
public static final int DEFAULT_BIN_SIZE = 300;
2723

28-
private Set<Id<DrtShift>> shiftIds;
2924
private int iteration = 0;
3025
private final int binSize;
3126
private final int nofBins;
3227
private DataFrame data = null;
3328

3429

35-
public ShiftHistogram(Population population, String mode, Config config) {
36-
super();
30+
public ShiftHistogram(String mode, Config config) {
31+
super();
3732
this.mode = mode;
3833
this.binSize = DEFAULT_BIN_SIZE;
39-
this.nofBins = ((int) config.qsim().getEndTime().orElse(DEFAULT_END_TIME) ) / this.binSize + 1;
40-
reset(0);
41-
if (population == null) {
42-
this.shiftIds = null;
43-
}
34+
this.nofBins = ((int) config.qsim().getEndTime().orElse(DEFAULT_END_TIME)) / this.binSize + 1;
35+
reset(0);
4436
}
4537

4638
/**
@@ -63,21 +55,19 @@ public ShiftHistogram(String mode, final int binSize, final int nofBins) {
6355
public void handleEvent(final DrtShiftStartedEvent event) {
6456
if (event.getMode().equals(mode)) {
6557
int index = getBinIndex(event.getTime());
66-
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
67-
DataFrame dataFrame = getData();
68-
dataFrame.countsStart[index]++;
69-
}
58+
DataFrame dataFrame = getData();
59+
dataFrame.countsStart[index]++;
60+
7061
}
7162
}
7263

7364
@Override
7465
public void handleEvent(final DrtShiftEndedEvent event) {
7566
if (event.getMode().equals(mode)) {
7667
int index = getBinIndex(event.getTime());
77-
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
78-
DataFrame dataFrame = getData();
79-
dataFrame.countsEnd[index]++;
80-
}
68+
DataFrame dataFrame = getData();
69+
dataFrame.countsEnd[index]++;
70+
8171
}
8272
}
8373

contribs/drt-extensions/src/main/java/org/matsim/contrib/drt/extension/operations/shifts/config/ShiftsParams.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99
package org.matsim.contrib.drt.extension.operations.shifts.config;
1010

11+
import com.google.common.base.Verify;
1112
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
1213
import org.matsim.contrib.ev.infrastructure.ChargerSpecification;
14+
import org.matsim.core.config.Config;
1315
import org.matsim.core.config.ConfigGroup;
1416

1517
import java.net.URL;
@@ -60,7 +62,7 @@ public class ShiftsParams extends ReflectiveConfigGroupWithConfigurableParameter
6062
@Parameter
6163
@Comment("defines the battery state of charge threshold at which vehicles will start charging" +
6264
" at hubs when not in an active shift. values between [0,1)")
63-
public double chargeAtHubThreshold = 0.5;
65+
public double chargeAtHubThreshold = 0.6;
6466

6567
@Parameter
6668
@Comment("defines the battery state of charge threshold at which vehicles will start charging" +
@@ -97,11 +99,19 @@ public class ShiftsParams extends ReflectiveConfigGroupWithConfigurableParameter
9799
"In this case, make sure that 'shiftScheduleLookAhead' is larger than the prebboking slack.")
98100
public boolean considerUpcomingShiftsForInsertion = false;
99101

100-
public ShiftsParams() {
102+
public ShiftsParams() {
101103
super(SET_NAME);
102104
}
103105

104106
public URL getShiftInputUrl(URL context) {
105107
return shiftInputFile == null ? null : ConfigGroup.getInputFileURL(context, shiftInputFile);
106108
}
109+
110+
@Override
111+
protected void checkConsistency(Config config) {
112+
super.checkConsistency(config);
113+
Verify.verify(chargeAtHubThreshold >= shiftAssignmentBatteryThreshold,
114+
"chargeAtHubThreshold must be higher than shiftAssignmentBatteryThreshold to " +
115+
"avoid deadlocks with undercharged vehicles in hubs.");
116+
}
107117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.matsim.contrib.drt.extension.operations.shifts.dispatcher;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import org.matsim.api.core.v01.Id;
5+
import org.matsim.contrib.drt.extension.operations.shifts.shift.*;
6+
import org.matsim.contrib.dvrp.fleet.Fleet;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.function.Function;
11+
12+
/**
13+
* @author nkuehnel / MOIA
14+
*/
15+
public final class DefaultShiftScheduler implements ShiftScheduler {
16+
17+
private final DrtShiftsSpecification shiftsSpecification;
18+
public static Function<DrtShiftSpecification, DrtShift> createShiftFromSpec = spec -> {
19+
DefaultShiftBreakImpl shiftBreak = null;
20+
DrtShiftBreakSpecification breakSpec = spec.getBreak().orElse(null);
21+
if (breakSpec != null) {
22+
shiftBreak = new DefaultShiftBreakImpl(
23+
breakSpec.getEarliestBreakStartTime(),
24+
breakSpec.getLatestBreakEndTime(),
25+
breakSpec.getDuration());
26+
}
27+
return (DrtShift) new DrtShiftImpl(spec.getId(), spec.getStartTime(), spec.getEndTime(),
28+
spec.getOperationFacilityId().orElse(null), spec.getDesignatedVehicleId().orElse(null),
29+
shiftBreak);
30+
};
31+
32+
public DefaultShiftScheduler(DrtShiftsSpecification shiftsSpecification) {
33+
this.shiftsSpecification = shiftsSpecification;
34+
}
35+
@Override
36+
public List<DrtShift> schedule(double time, Fleet fleet) {
37+
return Collections.emptyList();
38+
}
39+
40+
@Override
41+
public ImmutableMap<Id<DrtShift>, DrtShift> initialSchedule() {
42+
return shiftsSpecification.getShiftSpecifications().values()
43+
.stream()
44+
.map(createShiftFromSpec)
45+
.collect(ImmutableMap.toImmutableMap(DrtShift::getId, s -> s));
46+
}
47+
48+
@Override
49+
public DrtShiftsSpecification get() {
50+
return shiftsSpecification;
51+
}
52+
}

0 commit comments

Comments
 (0)