From 3e245af6f20147800b64cca3b460801a27eeb755 Mon Sep 17 00:00:00 2001 From: wrashid Date: Fri, 30 Aug 2024 12:07:19 -0500 Subject: [PATCH 1/3] use correct events manager --- .../scala/beam/agentsim/agents/PersonAgent.scala | 4 ++-- .../agentsim/agents/parking/ChoosesParking.scala | 16 ++++++++-------- .../agentsim/agents/ridehail/RideHailAgent.scala | 2 +- .../agents/ridehail/RideHailDepotManager.scala | 2 +- .../infrastructure/ParkingNetworkManager.scala | 6 ++++-- .../infrastructure/ScaleUpCharging.scala | 4 ++-- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/PersonAgent.scala b/src/main/scala/beam/agentsim/agents/PersonAgent.scala index 8e0b2301c8..114b07bd18 100755 --- a/src/main/scala/beam/agentsim/agents/PersonAgent.scala +++ b/src/main/scala/beam/agentsim/agents/PersonAgent.scala @@ -1100,14 +1100,14 @@ class PersonAgent( stay using updatedData case Event(UnpluggingVehicle(tick, _, vehicle, _, energyCharged), data: BasePersonData) => log.debug(s"Vehicle ${vehicle.id} ended charging and it is not handled by the CNM at tick $tick") - handleReleasingParkingSpot(tick, vehicle, Some(energyCharged), id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, vehicle, Some(energyCharged), id, parkingManager, beamServices, eventsManager) goto(ProcessingNextLegOrStartActivity) using data case Event(UnhandledVehicle(tick, _, vehicle, _), data: BasePersonData) => log.error( s"Vehicle ${vehicle.id} is not handled by the CNM at tick $tick. Something is broken." + s"the agent will now disconnect the vehicle ${currentBeamVehicle.id} to let the simulation continue!" ) - handleReleasingParkingSpot(tick, vehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, vehicle, None, id, parkingManager, beamServices, eventsManager) goto(ProcessingNextLegOrStartActivity) using data } diff --git a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala index 01207dac47..6bef71b7b0 100755 --- a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala +++ b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala @@ -281,7 +281,7 @@ trait ChoosesParking extends { s"Vehicle ${vehicle.id} is not handled by the CNM at tick $tick. Something is broken." + s"the agent will now disconnect the vehicle ${currentBeamVehicle.id} to let the simulation continue!" ) - handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) goto(WaitingToDrive) using data case Event(UnpluggingVehicle(tick, _, vehicle, _, energy), data: BasePersonData) @@ -289,12 +289,12 @@ trait ChoosesParking extends { log.debug( s"Vehicle ${vehicle.id} [chosen for enroute] ended charging and it is not handled by the CNM at tick $tick" ) - handleReleasingParkingSpot(tick, vehicle, Some(energy), id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, vehicle, Some(energy), id, parkingManager, beamServices, eventsManager) goto(ReadyToChooseParking) using data case Event(UnpluggingVehicle(tick, _, vehicle, _, energy), data) => log.debug(s"Vehicle ${vehicle.id} ended charging and it is not handled by the CNM at tick $tick") - handleReleasingParkingSpot(tick, vehicle, Some(energy), id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, vehicle, Some(energy), id, parkingManager, beamServices, eventsManager) releaseTickAndTriggerId() goto(WaitingToDrive) using data } @@ -326,7 +326,7 @@ trait ChoosesParking extends { if (data.enrouteData.isInEnrouteState) ReadyToChooseParking else { - handleReleasingParkingSpot(tick, vehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, vehicle, None, id, parkingManager, beamServices, eventsManager) releaseTickAndTriggerId() WaitingToDrive } @@ -335,7 +335,7 @@ trait ChoosesParking extends { } case Event(StateTimeout, data) => - handleReleasingParkingSpot(getCurrentTick.get, currentBeamVehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(getCurrentTick.get, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) releaseTickAndTriggerId() goto(WaitingToDrive) using data } @@ -380,7 +380,7 @@ trait ChoosesParking extends { triggerId, Vector(ScheduleTrigger(StartLegTrigger(nextLeg.startTime, nextLeg), self)) ) - handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) goto(WaitingToDrive) using data.copy(enrouteData = EnrouteData()) case _ => // Else the stall requires a diversion in travel, calc the new routes (in-vehicle to the stall and walking to the destination) @@ -504,7 +504,7 @@ trait ChoosesParking extends { ) ) - handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) goto(WaitingToDrive) using data.copy( currentTrip = Some(EmbodiedBeamTrip(newCurrentTripLegs)), @@ -583,7 +583,7 @@ trait ChoosesParking extends { ) ) - handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) goto(WaitingToDrive) using data.copy( currentTrip = Some(EmbodiedBeamTrip(newCurrentTripLegs)), diff --git a/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala b/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala index 814c582706..97cfc295d7 100755 --- a/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala +++ b/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala @@ -1099,7 +1099,7 @@ class RideHailAgent( vehicle.getState ) } - handleReleasingParkingSpot(tick, currentBeamVehicle, Some(energyCharged), id, parkingManager, beamServices) + handleReleasingParkingSpot(tick, currentBeamVehicle, Some(energyCharged), id, parkingManager, beamServices, eventsManager) } private def parkAndStartRefueling(stall: ParkingStall, data: RideHailAgentData): Unit = { diff --git a/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala b/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala index 4e92ab1a3e..ead9fd6f9b 100644 --- a/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala +++ b/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala @@ -152,7 +152,7 @@ trait RideHailDepotManager extends { */ private def releaseStall(parkingStall: ParkingStall, tick: Int, beamVehicle: BeamVehicle, triggerId: Long): Unit = { if (parkingStall.chargingPointType.isEmpty) { - ParkingNetworkManager.handleReleasingParkingSpot(tick, beamVehicle, None, this.id, parkingManager, beamServices) + ParkingNetworkManager.handleReleasingParkingSpot(tick, beamVehicle, None, this.id, parkingManager, beamServices, eventsManager) } else { (chargingNetworkManager ? ChargingUnplugRequest(tick, this.id, beamVehicle, triggerId)) .pipeTo(beamVehicle.getDriver.get) diff --git a/src/main/scala/beam/agentsim/infrastructure/ParkingNetworkManager.scala b/src/main/scala/beam/agentsim/infrastructure/ParkingNetworkManager.scala index e1c50a00bd..0faa8a4eda 100644 --- a/src/main/scala/beam/agentsim/infrastructure/ParkingNetworkManager.scala +++ b/src/main/scala/beam/agentsim/infrastructure/ParkingNetworkManager.scala @@ -11,6 +11,7 @@ import beam.utils.logging.LoggingMessageActor import beam.utils.metrics.SimpleCounter import com.typesafe.scalalogging.LazyLogging import org.matsim.api.core.v01.Id +import org.matsim.core.api.experimental.events.EventsManager import org.matsim.core.network.NetworkUtils import scala.concurrent.duration._ @@ -67,7 +68,8 @@ object ParkingNetworkManager extends LazyLogging { energyChargedMaybe: Option[Double], driver: Id[_], parkingManager: ActorRef, - beamServices: BeamServices + beamServices: BeamServices, + eventsManager: EventsManager ): Unit = { val stallForLeavingParkingEventMaybe = currentBeamVehicle.stall match { case Some(stall) => @@ -93,7 +95,7 @@ object ParkingNetworkManager extends LazyLogging { currentBeamVehicle.emitEmissions(vehicleActivityData, classOf[LeavingParkingEvent], beamServices) val energyCharge: Double = energyChargedMaybe.getOrElse(0.0) val score = calculateScore(stall.costInDollars, energyCharge) - beamServices.matsimServices.getEvents.processEvent( + eventsManager.processEvent( LeavingParkingEvent( tick, stall, diff --git a/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala b/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala index f96185b4d4..5c65b74cc2 100644 --- a/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala +++ b/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala @@ -114,10 +114,10 @@ trait ScaleUpCharging extends { log.debug(s"Received EndingRefuelSession: $reply") case reply @ UnhandledVehicle(tick, personId, vehicle, _) => log.error(s"Received UnhandledVehicle: $reply") - handleReleasingParkingSpot(tick, vehicle, None, personId, getParkingManager, getBeamServices) + handleReleasingParkingSpot(tick, vehicle, None, personId, getParkingManager, getBeamServices, getBeamServices.matsimServices.getEvents) case reply @ UnpluggingVehicle(tick, personId, vehicle, _, energyCharged) => log.debug(s"Received UnpluggingVehicle: $reply") - handleReleasingParkingSpot(tick, vehicle, Some(energyCharged), personId, getParkingManager, getBeamServices) + handleReleasingParkingSpot(tick, vehicle, Some(energyCharged), personId, getParkingManager, getBeamServices, getBeamServices.matsimServices.getEvents) } /** From da319d77786f3864799ad5ee9d2a0e1a37de179d Mon Sep 17 00:00:00 2001 From: wrashid Date: Sat, 31 Aug 2024 11:36:31 -0500 Subject: [PATCH 2/3] scala fmt --- .../agents/parking/ChoosesParking.scala | 10 +++++++++- .../agents/ridehail/RideHailAgent.scala | 10 +++++++++- .../ridehail/RideHailDepotManager.scala | 10 +++++++++- .../infrastructure/ScaleUpCharging.scala | 20 +++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala index 6bef71b7b0..b4ea1214a5 100755 --- a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala +++ b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala @@ -335,7 +335,15 @@ trait ChoosesParking extends { } case Event(StateTimeout, data) => - handleReleasingParkingSpot(getCurrentTick.get, currentBeamVehicle, None, id, parkingManager, beamServices, eventsManager) + handleReleasingParkingSpot( + getCurrentTick.get, + currentBeamVehicle, + None, + id, + parkingManager, + beamServices, + eventsManager + ) releaseTickAndTriggerId() goto(WaitingToDrive) using data } diff --git a/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala b/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala index 97cfc295d7..5468a08908 100755 --- a/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala +++ b/src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala @@ -1099,7 +1099,15 @@ class RideHailAgent( vehicle.getState ) } - handleReleasingParkingSpot(tick, currentBeamVehicle, Some(energyCharged), id, parkingManager, beamServices, eventsManager) + handleReleasingParkingSpot( + tick, + currentBeamVehicle, + Some(energyCharged), + id, + parkingManager, + beamServices, + eventsManager + ) } private def parkAndStartRefueling(stall: ParkingStall, data: RideHailAgentData): Unit = { diff --git a/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala b/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala index ead9fd6f9b..e4ba4a0e6e 100644 --- a/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala +++ b/src/main/scala/beam/agentsim/agents/ridehail/RideHailDepotManager.scala @@ -152,7 +152,15 @@ trait RideHailDepotManager extends { */ private def releaseStall(parkingStall: ParkingStall, tick: Int, beamVehicle: BeamVehicle, triggerId: Long): Unit = { if (parkingStall.chargingPointType.isEmpty) { - ParkingNetworkManager.handleReleasingParkingSpot(tick, beamVehicle, None, this.id, parkingManager, beamServices, eventsManager) + ParkingNetworkManager.handleReleasingParkingSpot( + tick, + beamVehicle, + None, + this.id, + parkingManager, + beamServices, + eventsManager + ) } else { (chargingNetworkManager ? ChargingUnplugRequest(tick, this.id, beamVehicle, triggerId)) .pipeTo(beamVehicle.getDriver.get) diff --git a/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala b/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala index 5c65b74cc2..122c443ec2 100644 --- a/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala +++ b/src/main/scala/beam/agentsim/infrastructure/ScaleUpCharging.scala @@ -114,10 +114,26 @@ trait ScaleUpCharging extends { log.debug(s"Received EndingRefuelSession: $reply") case reply @ UnhandledVehicle(tick, personId, vehicle, _) => log.error(s"Received UnhandledVehicle: $reply") - handleReleasingParkingSpot(tick, vehicle, None, personId, getParkingManager, getBeamServices, getBeamServices.matsimServices.getEvents) + handleReleasingParkingSpot( + tick, + vehicle, + None, + personId, + getParkingManager, + getBeamServices, + getBeamServices.matsimServices.getEvents + ) case reply @ UnpluggingVehicle(tick, personId, vehicle, _, energyCharged) => log.debug(s"Received UnpluggingVehicle: $reply") - handleReleasingParkingSpot(tick, vehicle, Some(energyCharged), personId, getParkingManager, getBeamServices, getBeamServices.matsimServices.getEvents) + handleReleasingParkingSpot( + tick, + vehicle, + Some(energyCharged), + personId, + getParkingManager, + getBeamServices, + getBeamServices.matsimServices.getEvents + ) } /** From 3288efb42e46235b95e90d4e6823707affc07f7a Mon Sep 17 00:00:00 2001 From: wrashid Date: Mon, 2 Sep 2024 07:16:46 -0500 Subject: [PATCH 3/3] test fixes --- .../beam/agentsim/agents/vehicles/BeamVehicleType.scala | 8 ++++++-- .../scala/beam/router/skim/ODVehicleTypeSkimmerSpec.scala | 2 +- src/test/scala/beam/sim/BeamWarmStartRunSpec.scala | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/vehicles/BeamVehicleType.scala b/src/main/scala/beam/agentsim/agents/vehicles/BeamVehicleType.scala index 57964cbad2..959683ea05 100755 --- a/src/main/scala/beam/agentsim/agents/vehicles/BeamVehicleType.scala +++ b/src/main/scala/beam/agentsim/agents/vehicles/BeamVehicleType.scala @@ -1,7 +1,7 @@ package beam.agentsim.agents.vehicles import beam.agentsim.agents.vehicles.FuelType._ -import beam.agentsim.agents.vehicles.VehicleCategory.{Class78Tractor, _} +import beam.agentsim.agents.vehicles.VehicleCategory._ import beam.agentsim.infrastructure.charging.ChargingPointType import org.matsim.api.core.v01.Id @@ -85,7 +85,11 @@ object VehicleCategory { case object Class78Vocational extends VehicleCategory // CLass 7&8 (GVWR 26001-33,000 lbs.) case object Class78Tractor extends VehicleCategory // Class 7&8 Tractor (GVWR >33,000 lbs.) - def fromString(value: String): VehicleCategory = fromStringOptional(value).get + def fromString(value: String): VehicleCategory = + try { fromStringOptional(value).get } + catch { + case exception: Exception => throw new RuntimeException(f"Can not parse vehicle category: '$value'.", exception) + } private def fromStringOptional(value: String): Option[VehicleCategory] = { Vector( diff --git a/src/test/scala/beam/router/skim/ODVehicleTypeSkimmerSpec.scala b/src/test/scala/beam/router/skim/ODVehicleTypeSkimmerSpec.scala index 822e8322e4..660b4a8ad9 100644 --- a/src/test/scala/beam/router/skim/ODVehicleTypeSkimmerSpec.scala +++ b/src/test/scala/beam/router/skim/ODVehicleTypeSkimmerSpec.scala @@ -24,7 +24,7 @@ class ODVehicleTypeSkimmerSpec extends AnyWordSpecLike with Matchers with BeamHe .parseString(""" beam.agentsim.lastIteration = 1 beam.agentsim.agents.freight.replanning.disableAfterIteration = 0 - beam.router.skim.origin-destination-vehicle-type-skimmer.vehicleCategories = "LightDutyTruck, HeavyDutyTruck" + beam.router.skim.origin-destination-vehicle-type-skimmer.vehicleCategories = "Class456Vocational, Class78Vocational" """) .withFallback(testConfig("test/input/beamville/beam-freight.conf")) .resolve() diff --git a/src/test/scala/beam/sim/BeamWarmStartRunSpec.scala b/src/test/scala/beam/sim/BeamWarmStartRunSpec.scala index d00e455d27..a49f1358be 100644 --- a/src/test/scala/beam/sim/BeamWarmStartRunSpec.scala +++ b/src/test/scala/beam/sim/BeamWarmStartRunSpec.scala @@ -54,6 +54,7 @@ class BeamWarmStartRunSpec "ITERS/it.2/2.skimsFreight_Aggregated.csv.gz", "ITERS/it.2/2.skimsParking_Aggregated.csv.gz", "ITERS/it.2/2.skimsTransitCrowding_Aggregated.csv.gz", + "ITERS/it.2/2.skimsEmissions_Aggregated.csv.gz", "ITERS/it.2/2.linkstats.csv.gz", "ITERS/it.2/2.plans.csv.gz", "ITERS/it.2/2.plans.xml.gz",