Skip to content

Commit

Permalink
Merge pull request #112 from moia-oss/master
Browse files Browse the repository at this point in the history
Update MATSim CW35
  • Loading branch information
mfrawley-moia authored Aug 29, 2024
2 parents aea1f51 + 42cff92 commit 8b61506
Show file tree
Hide file tree
Showing 46 changed files with 2,598 additions and 1,768 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private enum TripType {
@CommandLine.Option(names = "--tripType", description = "Set the tripType: OUTGOING, INCOMING, TRANSIT, INTERNAL, ALL", defaultValue = "ALL")
private TripType tripType;

@CommandLine.Option(names = "--LegMode", description = "Set leg mode for long distance freight legs.", defaultValue = "freight")
private String legMode;

private final SplittableRandom rnd = new SplittableRandom(4711);

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

Activity act0 = populationFactory.createActivityFromCoord("freight_start", null);
Leg leg = populationFactory.createLeg("freight");
Leg leg = populationFactory.createLeg(legMode);
Activity act1 = populationFactory.createActivityFromCoord("freight_end", null);

switch (tripType) {
Expand Down Expand Up @@ -312,7 +315,7 @@ private void createActivitiesForIncomingTrips(boolean originIsInside, boolean de
timeSpent += Math.floor(link.getLength() / link.getFreespeed()) + 1;
}
if (!isCoordSet) {
Coord originalCoord = route.links.get(0).getCoord();
Coord originalCoord = route.links.getFirst().getCoord();
act0.setCoord(ct.transform(originalCoord));
act0.setEndTime(departureTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilities;
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilityFinder;
import org.matsim.contrib.drt.extension.operations.shifts.config.ShiftsParams;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DefaultAssignShiftToVehicleLogic;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DefaultShiftStartLogic;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcher;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcherImpl;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.*;
import org.matsim.contrib.drt.extension.operations.shifts.optimizer.ShiftVehicleDataEntryFactory;
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtActionCreator;
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtTaskFactory;
import org.matsim.contrib.drt.extension.operations.shifts.scheduler.ShiftTaskScheduler;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShifts;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
Expand Down Expand Up @@ -67,19 +64,22 @@ protected void configureQSim() {
).asEagerSingleton();

bindModal(DrtShiftDispatcher.class).toProvider(modalProvider(
getter -> new EDrtShiftDispatcherImpl(((EShiftTaskScheduler) getter.getModal(ShiftTaskScheduler.class)), getter.getModal(ChargingInfrastructure.class),
drtShiftParams, getter.getModal(OperationFacilities.class), new DrtShiftDispatcherImpl(getMode(),
getter.getModal(DrtShifts.class), getter.getModal(Fleet.class),
getter.get(MobsimTimer.class), getter.getModal(OperationFacilities.class), getter.getModal(OperationFacilityFinder.class),
getter.getModal(ShiftTaskScheduler.class), getter.getModal(Network.class), getter.get(EventsManager.class),
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams)),
getter.getModal(Fleet.class)))).asEagerSingleton();
getter -> new EDrtShiftDispatcherImpl(((EShiftTaskScheduler) getter.getModal(ShiftTaskScheduler.class)),
getter.getModal(ChargingInfrastructure.class), drtShiftParams, getter.getModal(OperationFacilities.class),
new DrtShiftDispatcherImpl(getMode(), getter.getModal(Fleet.class), getter.get(MobsimTimer.class),
getter.getModal(OperationFacilities.class), getter.getModal(OperationFacilityFinder.class),
getter.getModal(ShiftTaskScheduler.class), getter.getModal(Network.class), getter.get(EventsManager.class),
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams),
getter.getModal(ShiftScheduler.class)),
getter.getModal(Fleet.class)))
).asEagerSingleton();

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


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilitiesSpecification;
import org.matsim.contrib.drt.extension.operations.shifts.io.DrtShiftsWriter;
import org.matsim.contrib.drt.extension.operations.shifts.io.OperationFacilitiesWriter;
import org.matsim.contrib.drt.extension.operations.operationFacilities.OperationFacilitiesSpecification;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.controler.events.ShutdownEvent;
import org.matsim.core.controler.listener.ShutdownListener;

import jakarta.inject.Provider;

/**
* Dumps DRT shift related data at end. Based on {@link org.matsim.core.controler.corelisteners.DumpDataAtEndImpl}
*
Expand All @@ -20,13 +18,13 @@
final public class DumpShiftDataAtEndImpl implements ShutdownListener {
private static final Logger log = LogManager.getLogger( DumpShiftDataAtEndImpl.class );

private final Provider<DrtShiftsSpecification> shifts;
private final DrtShiftsSpecification shifts;

private final OperationFacilitiesSpecification operationFacilities;

private final OutputDirectoryHierarchy controlerIO;

public DumpShiftDataAtEndImpl(Provider<DrtShiftsSpecification> shifts, OperationFacilitiesSpecification operationFacilities, OutputDirectoryHierarchy controlerIO) {
public DumpShiftDataAtEndImpl(DrtShiftsSpecification shifts, OperationFacilitiesSpecification operationFacilities, OutputDirectoryHierarchy controlerIO) {
this.shifts = shifts;
this.operationFacilities = operationFacilities;
this.controlerIO = controlerIO;
Expand All @@ -44,7 +42,7 @@ public void notifyShutdown(ShutdownEvent event) {
private void dumpShiftPans() {
try {
if ( this.shifts!=null){
new DrtShiftsWriter(shifts.get()).writeFile(this.controlerIO.getOutputFilename("output_shifts.xml.gz"));
new DrtShiftsWriter(shifts).writeFile(this.controlerIO.getOutputFilename("output_shifts.xml.gz"));
}
} catch ( Exception ee ) {
log.error("Exception writing shifts.", ee);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.matsim.contrib.drt.extension.operations.shifts.analysis;

import com.google.inject.Provider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.contrib.drt.extension.operations.shifts.io.DrtShiftsWriter;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShiftsSpecification;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.controler.events.IterationEndsEvent;
import org.matsim.core.controler.listener.IterationEndsListener;


/**
*
* @author nkuehnel / MOIA
*/
final public class RegularShiftDump implements IterationEndsListener {
private static final Logger log = LogManager.getLogger( RegularShiftDump.class );

private final Provider<DrtShiftsSpecification> shifts;

private final OutputDirectoryHierarchy controlerIO;

public RegularShiftDump(Provider<DrtShiftsSpecification> shifts, OutputDirectoryHierarchy controlerIO) {
this.shifts = shifts;
this.controlerIO = controlerIO;
}

private void dumpShiftPans(int iteration) {
try {
if ( this.shifts!=null){
new DrtShiftsWriter(shifts.get()).writeFile(this.controlerIO.getIterationFilename(iteration, "output_shifts.xml.gz"));
}
} catch ( Exception ee ) {
log.error("Exception writing shifts.", ee);
}
}


@Override
public void notifyIterationEnds(IterationEndsEvent event) {
dumpShiftPans(event.getIteration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,28 @@ public void handleEvent(DrtShiftBreakStartedEvent event) {
@Override
public void handleEvent(final DrtShiftEndedEvent event) {
if (event.getMode().equals(mode)) {
final Double start = shift2StartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId());
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
Map<Id<DrtShift>, DrtShiftSpecification> shiftSpecifications = shifts.get().getShiftSpecifications();
if(shiftSpecifications.containsKey(event.getShiftId())) {
final DrtShiftSpecification drtShift = shiftSpecifications.get(event.getShiftId());
final Double start = shift2StartTime.get(event.getShiftId());
double duration = event.getTime() - start;
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
}
}
}

@Override
public void handleEvent(DrtShiftBreakEndedEvent event) {
if (event.getMode().equals(mode)) {
final Double start = shift2BreakStartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
double plannedDuration = drtShift.getDuration();
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
Map<Id<DrtShift>, DrtShiftSpecification> shiftSpecifications = shifts.get().getShiftSpecifications();
if(shiftSpecifications.containsKey(event.getShiftId())) {
final Double start = shift2BreakStartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
double plannedDuration = drtShift.getDuration();
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.matsim.contrib.drt.extension.operations.shifts.analysis;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Population;
import org.matsim.contrib.drt.extension.operations.shifts.events.*;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShift;
import org.matsim.core.config.Config;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.core.utils.misc.Time;
Expand All @@ -12,35 +9,30 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.util.Set;

/**
* @author nkuehnel / MOIA
*/
public class ShiftHistogram implements DrtShiftStartedEventHandler, DrtShiftEndedEventHandler,
DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {
DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {

private final String mode;

public static final int DEFAULT_END_TIME = 30 * 3600;
public static final int DEFAULT_BIN_SIZE = 300;
public static final int DEFAULT_END_TIME = 30 * 3600;
public static final int DEFAULT_BIN_SIZE = 300;

private Set<Id<DrtShift>> shiftIds;
private int iteration = 0;
private final int binSize;
private final int nofBins;
private DataFrame data = null;


public ShiftHistogram(Population population, String mode, Config config) {
super();
public ShiftHistogram(String mode, Config config) {
super();
this.mode = mode;
this.binSize = DEFAULT_BIN_SIZE;
this.nofBins = ((int) config.qsim().getEndTime().orElse(DEFAULT_END_TIME) ) / this.binSize + 1;
reset(0);
if (population == null) {
this.shiftIds = null;
}
this.nofBins = ((int) config.qsim().getEndTime().orElse(DEFAULT_END_TIME)) / this.binSize + 1;
reset(0);
}

/**
Expand All @@ -63,21 +55,19 @@ public ShiftHistogram(String mode, final int binSize, final int nofBins) {
public void handleEvent(final DrtShiftStartedEvent event) {
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsStart[index]++;
}
DataFrame dataFrame = getData();
dataFrame.countsStart[index]++;

}
}

@Override
public void handleEvent(final DrtShiftEndedEvent event) {
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsEnd[index]++;
}
DataFrame dataFrame = getData();
dataFrame.countsEnd[index]++;

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/
package org.matsim.contrib.drt.extension.operations.shifts.config;

import com.google.common.base.Verify;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.contrib.ev.infrastructure.ChargerSpecification;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;

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

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

public ShiftsParams() {
public ShiftsParams() {
super(SET_NAME);
}

public URL getShiftInputUrl(URL context) {
return shiftInputFile == null ? null : ConfigGroup.getInputFileURL(context, shiftInputFile);
}

@Override
protected void checkConsistency(Config config) {
super.checkConsistency(config);
Verify.verify(chargeAtHubThreshold >= shiftAssignmentBatteryThreshold,
"chargeAtHubThreshold must be higher than shiftAssignmentBatteryThreshold to " +
"avoid deadlocks with undercharged vehicles in hubs.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.matsim.contrib.drt.extension.operations.shifts.dispatcher;

import com.google.common.collect.ImmutableMap;
import org.matsim.api.core.v01.Id;
import org.matsim.contrib.drt.extension.operations.shifts.shift.*;
import org.matsim.contrib.dvrp.fleet.Fleet;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

/**
* @author nkuehnel / MOIA
*/
public final class DefaultShiftScheduler implements ShiftScheduler {

private final DrtShiftsSpecification shiftsSpecification;
public static Function<DrtShiftSpecification, DrtShift> createShiftFromSpec = spec -> {
DefaultShiftBreakImpl shiftBreak = null;
DrtShiftBreakSpecification breakSpec = spec.getBreak().orElse(null);
if (breakSpec != null) {
shiftBreak = new DefaultShiftBreakImpl(
breakSpec.getEarliestBreakStartTime(),
breakSpec.getLatestBreakEndTime(),
breakSpec.getDuration());
}
return (DrtShift) new DrtShiftImpl(spec.getId(), spec.getStartTime(), spec.getEndTime(),
spec.getOperationFacilityId().orElse(null), spec.getDesignatedVehicleId().orElse(null),
shiftBreak);
};

public DefaultShiftScheduler(DrtShiftsSpecification shiftsSpecification) {
this.shiftsSpecification = shiftsSpecification;
}
@Override
public List<DrtShift> schedule(double time, Fleet fleet) {
return Collections.emptyList();
}

@Override
public ImmutableMap<Id<DrtShift>, DrtShift> initialSchedule() {
return shiftsSpecification.getShiftSpecifications().values()
.stream()
.map(createShiftFromSpec)
.collect(ImmutableMap.toImmutableMap(DrtShift::getId, s -> s));
}

@Override
public DrtShiftsSpecification get() {
return shiftsSpecification;
}
}
Loading

0 comments on commit 8b61506

Please sign in to comment.