Skip to content

feat(ev): EV contrib compatible with DSim#4834

Merged
Janekdererste merged 58 commits into
mainfrom
ev-contrib-dsim-ready
Apr 21, 2026
Merged

feat(ev): EV contrib compatible with DSim#4834
Janekdererste merged 58 commits into
mainfrom
ev-contrib-dsim-ready

Conversation

@Janekdererste

@Janekdererste Janekdererste commented Mar 27, 2026

Copy link
Copy Markdown
Member

This PR makes the infrastructure in ev contrib compatible with DSim. It only changes the parts related to the simulation. Statistics are not touched yet and do not work correctly with DSim. This will be fixed in a separate PR.

Mostly it makes the used engines and Handlers aware of partitions. Things that had to be changed:

  • ChargerInfrastructure is created per simulation process: Only chargers located on the partition are added to the infrastructure collection
  • ElectricFleet interface better schields the underlying vehicle collections and has two implementations:
    1. GlobalElectricFleet creates all EVs at the beginning of an iteration. This variant can be used for a QSim.
    2. DistributedElectricFleet creates EVs lazily when queried for it. It also listenes for vehicles leaving partition and transfer the charging state of the corresponding EV to the next partition. Callers are responsible for only quering EVs that are actually present on the partition. Otherwise the distributed fleet will create duplicate vehicles.
  • DistributedDriveDischargeHandler is partition aware and sends drive state to other partitions when vehicles cross partition boundaries. The handler dispatches DischargeEvents immediately instead of delaying them for one second, as there are no race conditions between mobsim and event handling in DSim
  • VehicleChargeHandler is now ChargingActivityEngine. This handler queried the mobsim for agents, which is not supported by DSim. Agents are added to this engine if they want to perform a charging activity. This way it is not necessary to query the global agent state of the mobsim. Also, the engine has ownership of agents peforming a charging activity, which is a cleaner desing, since the Engine is modifying the agent plans if they are queuing at a charger
  • DistributedWithinDayEvEngine mimics the behavior of the WithinDayEvEngine. Since the distributed simulation cannot give global access to agents the new engine acts as an activity engine. When agents start an activity we can be sure to have a local reference to that agent and that altering an agentś plan is save at that moment. It also is a departure handler for the same reason. Agents can find charging alternatives on departure. The engine always returns falsefor departures, signalling, that another engine should actually handle the agent`s leg.
  • DistributedChargingReservationManager provides functionality to reserve chargers across domain boundaries. Agents can request a reservation for a charger and the manager requests the reservation from the on the partition where the charger is located. The new manager uses a callback to inform callees about the result of the reservation request, as the described roundtrip takes at least one simulation time step.

Breaking changes

(dis)charging events are generated one timestep earlier compared to the original QSim code. Event handlers can be called directly from the simulation process so that it is not necessary to mitigate for race conditions between mobsim and event handling. The original code did that by delaying calculations and event dispatch by one second. I think not waiting one second is closer to the expected outcome and justifies a breaking change.

Plug- and Unplug events have different timestamps for the same reason.

Reservations may take +1 time step, as making a reservation might require a messaging roundtrip to another partion. This is also not deterministic at the moment and depends on where chargers are located.

Signed-off-by: Janek Laudan <laudan@simunto.com>
…e local to the SimProcess and should be called by that process.

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…module work with DSim and QSim

Signed-off-by: Janek Laudan <laudan@simunto.com>
…ingHandler.

* Before nothing would be processed, as the handler would wait for a later timestep
* Add unit test to cover this edge case
* Adjust existing test to new slightly higher energy consumption

Signed-off-by: Janek Laudan <laudan@simunto.com>
* Compares events relevant for DriveDischarging between QSim and DSim
* Verifies overall Discharge to fixed amount

Signed-off-by: Janek Laudan <laudan@simunto.com>
* The current implementation modifies the state of agents queueing for a charger. Since SimProcess doesn't keep track of agents it cannot provide the MobsimAgents to the Handler. By acting as an ActivityEngine we have direct access to the agents we want to modify the plans of. I think this is a better design in terms of ownership
* Bind ActivityEngineDefaultImpl as ActivityEngine, so that it becomnes injectable. We need this as the ChargingHandler delegates activity handling to the Default activity engine.
* Extend ChargingListener to include QuitQueueAtCharger

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…ation is set

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…ault activity engine as singleton qsim component

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
# Conflicts:
#	contribs/ev/src/test/java/org/matsim/contrib/ev/discharging/DriveDischargingHandlerTest.java
Signed-off-by: Janek Laudan <laudan@simunto.com>
…ferences.

* EVs are sent as messages and circular references are hard to serialize. Therore remove the backpointer and pass the battery into the BatteryCharging, as this is the only thing used from the EV.

Signed-off-by: Janek Laudan <laudan@simunto.com>
…ferences.

* EVs are sent as messages and circular references are hard to serialize. Therore remove the backpointer and pass the battery into the BatteryCharging, as this is the only thing used from the EV.

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
* Create concrete ElectricFleet implementation for QSim instead of provider/lambda construct. This is more straight forward.
* Clear usages of getElectricVehicles in favor of hasVehicle/getVehicle which keeps the internal collection private

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…ding engine type.

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…ler.java

* Cleanup module code and rename references to constants
* add new expected output, as all events are shifted one second (The engine does not have a delay of one second, which is actually correct)

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Janekdererste and others added 24 commits April 10, 2026 16:38
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…entation for other mobsims (QSim)

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
# Conflicts:
#	contribs/distributed-simulation/src/test/java/org/matsim/dsim/DistributedEventsManagerTest.java
#	contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/insertion/extensive/MultiInsertionDetourPathCalculatorManager.java
#	contribs/ev/src/test/java/org/matsim/contrib/ev/extensions/battery_chargers/TestScenarioBuilder.java
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
…dule

Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
Signed-off-by: Janek Laudan <laudan@simunto.com>
@Janekdererste Janekdererste enabled auto-merge April 21, 2026 12:01
@Janekdererste Janekdererste added this pull request to the merge queue Apr 21, 2026
Merged via the queue into main with commit 6133790 Apr 21, 2026
53 checks passed
@Janekdererste Janekdererste deleted the ev-contrib-dsim-ready branch April 21, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant