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

feat(ev): modularize charging strategies #3587

Merged
merged 6 commits into from
Dec 3, 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 @@ -59,6 +59,7 @@
import org.matsim.contrib.dvrp.schedule.ScheduleTimingUpdater;
import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic;
import org.matsim.contrib.dvrp.vrpagent.VrpLegFactory;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructure;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructureUtils;
import org.matsim.core.api.experimental.events.EventsManager;
Expand Down Expand Up @@ -116,7 +117,8 @@ protected void configureQSim() {
public EmptyVehicleChargingScheduler get() {
var taskFactory = getModalInstance(DrtTaskFactory.class);
var chargingInfrastructure = getModalInstance(ChargingInfrastructure.class);
return new EmptyVehicleChargingScheduler(timer, taskFactory, chargingInfrastructure);
ChargingStrategy.Factory chargingStrategyFactory = getModalInstance(ChargingStrategy.Factory.class);
return new EmptyVehicleChargingScheduler(timer, taskFactory, chargingInfrastructure, chargingStrategyFactory);
}
}).asEagerSingleton();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

import java.net.URL;

import org.matsim.contrib.drt.extension.edrt.optimizer.EDrtVehicleDataEntryFactory.EDrtVehicleDataEntryFactoryProvider;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.drt.extension.edrt.optimizer.EDrtVehicleDataEntryFactory.EDrtVehicleDataEntryFactoryProvider;
import org.matsim.contrib.dvrp.run.DvrpModes;
import org.matsim.contrib.ev.EvConfigGroup;
import org.matsim.contrib.ev.charging.ChargeUpToMaxSocStrategy;
import org.matsim.contrib.ev.charging.ChargingLogic;
import org.matsim.contrib.ev.charging.ChargingPower;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.charging.ChargingWithQueueingAndAssignmentLogic;
import org.matsim.contrib.ev.charging.FixedSpeedCharging;
import org.matsim.contrib.ev.temperature.TemperatureService;
Expand All @@ -40,6 +42,8 @@
import org.matsim.core.controler.Controler;
import org.matsim.vis.otfvis.OTFVisConfigGroup;

import com.google.inject.Key;

/**
* @author Michal Maciejewski (michalm)
*/
Expand Down Expand Up @@ -69,10 +73,13 @@ public void install() {
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
bind(ChargingLogic.Factory.class).toProvider(new ChargingWithQueueingAndAssignmentLogic.FactoryProvider(
charger -> new ChargeUpToMaxSocStrategy(charger, MAX_RELATIVE_SOC)));
bind(ChargingLogic.Factory.class).to(ChargingWithQueueingAndAssignmentLogic.Factory.class);
bind(ChargingPower.Factory.class).toInstance(ev -> new FixedSpeedCharging(ev, CHARGING_SPEED_FACTOR));
bind(TemperatureService.class).toInstance(linkId -> TEMPERATURE);

for (DrtConfigGroup drtCfg : MultiModeDrtConfigGroup.get(config).getModalElements()) {
bind(Key.get(ChargingStrategy.Factory.class, DvrpModes.mode(drtCfg.mode))).toInstance(new ChargeUpToMaxSocStrategy.Factory(MAX_RELATIVE_SOC));
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
package org.matsim.contrib.drt.extension.edrt.schedule;

import org.matsim.contrib.drt.schedule.DrtTaskType;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.fleet.ElectricVehicle;
import org.matsim.contrib.ev.infrastructure.Charger;
import org.matsim.contrib.evrp.ChargingTaskImpl;

public class EDrtChargingTask extends ChargingTaskImpl {
public static final DrtTaskType TYPE = new DrtTaskType("CHARGING");

public EDrtChargingTask(double beginTime, double endTime, Charger charger, ElectricVehicle ev, double totalEnergy) {
super(TYPE, beginTime, endTime, charger, ev, totalEnergy);
public EDrtChargingTask(double beginTime, double endTime, Charger charger, ElectricVehicle ev, double totalEnergy, ChargingStrategy chargingStrategy) {
super(TYPE, beginTime, endTime, charger, ev, totalEnergy, chargingStrategy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.matsim.contrib.dvrp.schedule.DefaultStayTask;
import org.matsim.contrib.evrp.EvDvrpVehicle;
import org.matsim.contrib.evrp.VrpPathEnergyConsumptions;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.fleet.ElectricVehicle;
import org.matsim.contrib.ev.infrastructure.Charger;

Expand Down Expand Up @@ -59,8 +60,8 @@ public DefaultStayTask createInitialTask(DvrpVehicle vehicle, double beginTime,
}

public EDrtChargingTask createChargingTask(DvrpVehicle vehicle, double beginTime, double endTime, Charger charger,
double totalEnergy) {
double totalEnergy, ChargingStrategy chargingStrategy) {
return new EDrtChargingTask(beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(),
totalEnergy);
totalEnergy, chargingStrategy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public class EmptyVehicleChargingScheduler {
private final MobsimTimer timer;
private final EDrtTaskFactoryImpl taskFactory;
private final Map<Id<Link>, List<Charger>> linkToChargersMap;
private final ChargingStrategy.Factory chargingStrategyFactory;

public EmptyVehicleChargingScheduler(MobsimTimer timer, DrtTaskFactory taskFactory,
ChargingInfrastructure chargingInfrastructure) {
ChargingInfrastructure chargingInfrastructure, ChargingStrategy.Factory chargingStrategyFactory) {
this.timer = timer;
this.chargingStrategyFactory = chargingStrategyFactory;
this.taskFactory = (EDrtTaskFactoryImpl)taskFactory;
linkToChargersMap = chargingInfrastructure.getChargers()
.values()
Expand All @@ -68,15 +70,16 @@ public void chargeVehicle(DvrpVehicle vehicle) {
// Empty charger or at least smallest queue charger
Charger charger = freeCharger.orElseGet(() -> chargers.stream().min(Comparator.comparingInt(e -> e.getLogic().getQueuedVehicles().size())).orElseThrow());
ElectricVehicle ev = ((EvDvrpVehicle)vehicle).getElectricVehicle();
if (!charger.getLogic().getChargingStrategy().isChargingCompleted(ev)) {
chargeVehicleImpl(vehicle, charger);
ChargingStrategy strategy = chargingStrategyFactory.createStrategy(charger.getSpecification(), ev);
if (!strategy.isChargingCompleted()) {
chargeVehicleImpl(vehicle, ev, charger, strategy);
}
}
}



private void chargeVehicleImpl(DvrpVehicle vehicle, Charger charger) {
private void chargeVehicleImpl(DvrpVehicle vehicle, ElectricVehicle ev, Charger charger, ChargingStrategy strategy) {
Schedule schedule = vehicle.getSchedule();
DrtStayTask stayTask = (DrtStayTask)schedule.getCurrentTask();
if (stayTask.getTaskIdx() != schedule.getTaskCount() - 1) {
Expand All @@ -86,19 +89,17 @@ private void chargeVehicleImpl(DvrpVehicle vehicle, Charger charger) {

// add CHARGING TASK
double beginTime = stayTask.getEndTime();
ChargingStrategy strategy = charger.getLogic().getChargingStrategy();
ElectricVehicle ev = ((EvDvrpVehicle)vehicle).getElectricVehicle();
double totalEnergy = -strategy.calcRemainingEnergyToCharge(ev);
double totalEnergy = -strategy.calcRemainingEnergyToCharge();

double chargingDuration = Math.min(strategy.calcRemainingTimeToCharge(ev),
double chargingDuration = Math.min(strategy.calcRemainingTimeToCharge(),
vehicle.getServiceEndTime() - beginTime);
if (chargingDuration <= 0) {
return;// no charging
}
double endTime = beginTime + chargingDuration;

schedule.addTask(taskFactory.createChargingTask(vehicle, beginTime, endTime, charger, totalEnergy));
((ChargingWithAssignmentLogic)charger.getLogic()).assignVehicle(ev);
schedule.addTask(taskFactory.createChargingTask(vehicle, beginTime, endTime, charger, totalEnergy, strategy));
((ChargingWithAssignmentLogic)charger.getLogic()).assignVehicle(ev, strategy);

// append STAY
schedule.addTask(taskFactory.createStayTask(vehicle, endTime, vehicle.getServiceEndTime(), charger.getLink()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ public class EDrtShiftDispatcherImpl implements DrtShiftDispatcher {

private final Fleet fleet;

private final ChargingStrategy.Factory chargingStrategyFactory;

public EDrtShiftDispatcherImpl(EShiftTaskScheduler shiftTaskScheduler, ChargingInfrastructure chargingInfrastructure,
ShiftsParams drtShiftParams, OperationFacilities operationFacilities,
DrtShiftDispatcher delegate, Fleet fleet) {
DrtShiftDispatcher delegate, Fleet fleet, ChargingStrategy.Factory chargingStrategyFactory) {
this.shiftTaskScheduler = shiftTaskScheduler;
this.chargingInfrastructure = chargingInfrastructure;
this.drtShiftParams = drtShiftParams;
this.operationFacilities = operationFacilities;
this.delegate = delegate;
this.fleet = fleet;
this.chargingStrategyFactory = chargingStrategyFactory;
}

@Override
Expand Down Expand Up @@ -112,18 +115,18 @@ private void checkChargingAtHub(double timeStep) {

if (selectedCharger.isPresent()) {
Charger selectedChargerImpl = selectedCharger.get();
ChargingStrategy chargingStrategy = selectedChargerImpl.getLogic().getChargingStrategy();
if (!chargingStrategy.isChargingCompleted(electricVehicle)) {
ChargingStrategy chargingStrategy = chargingStrategyFactory.createStrategy(selectedChargerImpl.getSpecification(), electricVehicle);
if (!chargingStrategy.isChargingCompleted()) {
final double waitTime = ChargingEstimations
.estimateMaxWaitTimeForNextVehicle(selectedChargerImpl);
final double chargingTime = chargingStrategy
.calcRemainingTimeToCharge(electricVehicle);
.calcRemainingTimeToCharge();
double energy = -chargingStrategy
.calcRemainingEnergyToCharge(electricVehicle);
.calcRemainingEnergyToCharge();
final double endTime = timeStep + waitTime + chargingTime;
if (endTime < currentTask.getEndTime()) {
shiftTaskScheduler.chargeAtHub((WaitForShiftTask) currentTask, eShiftVehicle,
electricVehicle, selectedChargerImpl, timeStep, endTime, energy);
electricVehicle, selectedChargerImpl, timeStep, endTime, energy, chargingStrategy);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic;
import org.matsim.contrib.dvrp.vrpagent.VrpLegFactory;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructure;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.mobsim.framework.MobsimTimer;
Expand Down Expand Up @@ -72,7 +73,7 @@ protected void configureQSim() {
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams),
getter.getModal(ShiftScheduler.class)),
getter.getModal(Fleet.class)))
getter.getModal(Fleet.class), getter.getModal(ChargingStrategy.Factory.class)))
).asEagerSingleton();

bindModal(VehicleEntry.EntryFactory.class).toProvider(modalProvider(getter ->
Expand All @@ -87,7 +88,8 @@ drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
getter -> new EShiftTaskScheduler(getter.getModal(Network.class), getter.getModal(TravelTime.class),
getter.getModal(TravelDisutilityFactory.class).createTravelDisutility(getter.getModal(TravelTime.class)),
getter.get(MobsimTimer.class), getter.getModal(ShiftDrtTaskFactory.class), drtShiftParams, getter.getModal(ChargingInfrastructure.class),
getter.getModal(OperationFacilities.class), getter.getModal(Fleet.class))
getter.getModal(OperationFacilities.class), getter.getModal(Fleet.class),
getter.getModal(ChargingStrategy.Factory.class))
)).asEagerSingleton();

// See EDrtModeOptimizerQSimModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.path.VrpPathWithTravelData;
import org.matsim.contrib.dvrp.schedule.DefaultStayTask;
import org.matsim.contrib.ev.charging.ChargingStrategy;
import org.matsim.contrib.ev.infrastructure.Charger;
import org.matsim.contrib.evrp.ChargingTask;
import org.matsim.contrib.evrp.ChargingTaskImpl;
Expand Down Expand Up @@ -99,21 +100,21 @@ public WaitForShiftTask createWaitForShiftStayTask(DvrpVehicle vehicle, double b

public WaitForShiftTask createChargingWaitForShiftStayTask(DvrpVehicle vehicle, double beginTime,
double endTime, Link link, OperationFacility facility,
double totalEnergy, Charger charger) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy);
double totalEnergy, Charger charger, ChargingStrategy strategy) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy, strategy);
return new EDrtWaitForShiftTask(beginTime, endTime, link, totalEnergy, facility, chargingTask);
}

public EDrtShiftBreakTaskImpl createChargingShiftBreakTask(DvrpVehicle vehicle, double beginTime, double endTime, Link link,
DrtShiftBreak shiftBreak, Charger charger, double totalEnergy, OperationFacility facility) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy);
DrtShiftBreak shiftBreak, Charger charger, double totalEnergy, OperationFacility facility, ChargingStrategy strategy) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy, strategy);
return new EDrtShiftBreakTaskImpl(beginTime, endTime, link, shiftBreak, totalEnergy, chargingTask, facility);
}

public ShiftChangeOverTask createChargingShiftChangeoverTask(DvrpVehicle vehicle, double beginTime, double endTime,
Link link, Charger charger, double totalEnergy,
DrtShift shift, OperationFacility facility) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy);
DrtShift shift, OperationFacility facility, ChargingStrategy strategy) {
ChargingTask chargingTask = new ChargingTaskImpl(EDrtChargingTask.TYPE, beginTime, endTime, charger, ((EvDvrpVehicle)vehicle).getElectricVehicle(), totalEnergy, strategy);
return new EDrtShiftChangeoverTaskImpl(beginTime, endTime, link, shift, totalEnergy, chargingTask, facility);
}
}
Loading
Loading