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

Emissions feature: improvements for DIURN, HOTSOAK #3908

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from
Draft
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 @@ -31,7 +31,7 @@ import beam.sim.common.GeoUtils
import beam.sim.config.BeamConfig
import beam.sim.{BeamScenario, BeamServices}
import beam.utils.NetworkHelper
import beam.utils.logging.ExponentialLazyLogging
import beam.utils.logging.{ExponentialLazyLogging, LoggerWrapper}
import com.conveyal.r5.transit.TransportNetwork
import org.matsim.api.core.v01.Id
import org.matsim.api.core.v01.events.{
Expand All @@ -43,6 +43,7 @@ import org.matsim.api.core.v01.events.{
import org.matsim.api.core.v01.population.Person
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.vehicles.Vehicle
import org.slf4j.LoggerFactory

import scala.collection.{immutable, mutable}
import scala.language.postfixOps
Expand Down Expand Up @@ -352,13 +353,26 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
None,
beamServices
)
currentBeamVehicle.setLastVehicleLink(currentLeg.travelPath.linkIds.headOption)
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(
beamServices.beamScenario.vehicleEmissions
.rememberLastVehicleLink(currentBeamVehicle, currentLeg.travelPath.linkIds.headOption)

val initialIdleActivity = BeamVehicle.getIDLEActivitiesWithStoppedEngineForEmissions(
currentBeamVehicle,
beamServices
)
val maybeInitialIdleEmission = currentBeamVehicle.emitEmissions(
initialIdleActivity,
classOf[VehicleEntersTrafficEvent],
beamServices
)

val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(
currentLeg.startTime,
currentBeamVehicle,
beamServices
)
currentBeamVehicle.setLastVehicleTimeLink(
beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
currentBeamVehicle,
Some(currentLeg.endTime),
currentLeg.travelPath.linkIds.lastOption
)
Expand All @@ -373,9 +387,12 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
classOf[PathTraversalEvent],
beamServices
)
val emissionsProfile = EmissionsProfile.join(emissionsProfilePTE, emissionsProfileIDLE)
val emissionsProfile = EmissionsProfile.join(
emissionsProfilePTE,
EmissionsProfile.join(emissionsProfileIDLE, maybeInitialIdleEmission)
)

val numberOfPassengers: Int = calculateNumberOfPassengersBasedOnCurrentTripMode(data, currentLeg, riders)
val currentTourMode: Option[String] = getCurrentTripMode(data)
val pte = PathTraversalEvent(
tick,
currentVehicleUnderControl,
Expand Down Expand Up @@ -602,12 +619,13 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
None,
beamServices
)
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(
currentLeg.startTime,
currentBeamVehicle,
beamServices
)
currentBeamVehicle.setLastVehicleTimeLink(
beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
currentBeamVehicle,
Some(currentLeg.endTime),
currentLeg.travelPath.linkIds.lastOption
)
Expand All @@ -628,7 +646,6 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
tollsAccumulated += tollOnCurrentLeg
val numberOfPassengers: Int =
calculateNumberOfPassengersBasedOnCurrentTripMode(data, partiallyCompletedBeamLeg, riders)
val currentTourMode: Option[String] = getCurrentTripMode(data)
val pte = PathTraversalEvent(
updatedStopTick,
currentVehicleUnderControl,
Expand Down Expand Up @@ -764,7 +781,8 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
)
currentBeamVehicle.stall.foreach { theStall =>
parkingManager ! ReleaseParkingStall(theStall, tick)
currentBeamVehicle.setLastVehicleTimeLink(
beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
currentBeamVehicle,
Some(tick),
theStall.link.map(_.getId.toString.toInt)
)
Expand Down
35 changes: 21 additions & 14 deletions src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import beam.utils.NetworkHelper
import beam.utils.logging.LogActorState
import beam.utils.reflection.ReflectionUtils
import com.conveyal.r5.transit.TransportNetwork
import org.matsim.api.core.v01.events.PersonEntersVehicleEvent
import org.matsim.api.core.v01.events.{PersonEntersVehicleEvent, VehicleEntersTrafficEvent}
import org.matsim.api.core.v01.{Coord, Id}
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.core.network.NetworkUtils
import org.matsim.core.utils.misc.Time
import org.matsim.vehicles.Vehicle

Expand Down Expand Up @@ -353,7 +354,7 @@ class RideHailAgent(
val isTimeForShift =
shifts.isEmpty || shifts.get.exists(shift => shift.range.lowerBound <= tick && shift.range.upperBound >= tick)
if (isTimeForShift) {
vehicle.setLastVehicleTime(Some(tick))
beamServices.beamScenario.vehicleEmissions.rememberLastVehicleTime(vehicle, Some(tick))
eventsManager.processEvent(new ShiftEvent(tick, StartShift, id.toString, vehicle))
rideHailManager ! NotifyVehicleIdle(
vehicle.id,
Expand Down Expand Up @@ -453,15 +454,16 @@ class RideHailAgent(
)
}
val newShiftToSchedule = if (needsToEndShift) {
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(tick, currentBeamVehicle, beamServices)
val maybeIDLEVehicleActivity =
BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(tick, currentBeamVehicle, beamServices)
val emissionsProfileIDLE = currentBeamVehicle.emitEmissions(
maybeIDLEVehicleActivity,
classOf[PathTraversalEvent],
beamServices
)
eventsManager.processEvent(new ShiftEvent(tick, EndShift, id.toString, vehicle, emissionsProfileIDLE))
beamServices.beamScenario.vehicleEmissions.rememberLastVehicleTime(vehicle, Some(tick))

currentBeamVehicle.resetLastVehicleLinkTime()
isCurrentlyOnShift = false
needsToEndShift = false
if (data.remainingShifts.size < 1) {
Expand All @@ -485,30 +487,33 @@ class RideHailAgent(
stay()
} else {
if (needsToEndShift) {
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(tick, currentBeamVehicle, beamServices)
val maybeIDLEVehicleActivity =
BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(tick, currentBeamVehicle, beamServices)
val emissionsProfileIDLE = currentBeamVehicle.emitEmissions(
maybeIDLEVehicleActivity,
classOf[PathTraversalEvent],
beamServices
)
currentBeamVehicle.resetLastVehicleLinkTime()
eventsManager.processEvent(new ShiftEvent(tick, EndShift, id.toString, vehicle, emissionsProfileIDLE))
needsToEndShift = false
isCurrentlyOnShift = false
}
updateLatestObservedTick(tick)
currentBeamVehicle.setLastVehicleTime(Some(tick))
eventsManager.processEvent(new ShiftEvent(tick, StartShift, id.toString, vehicle))
log.debug("state(RideHailingAgent.Offline): starting shift {}", id)
holdTickAndTriggerId(tick, triggerId)
isStartingNewShift = true

val newLocation = data.remainingShifts.headOption match {
case Some(Shift(_, Some(startLocation))) =>
//TODO this is teleportation and should be fixed in favor of new protocol to make vehicles move
SpaceTime(startLocation, time = tick)
case _ =>
vehicle.spaceTime.copy(time = tick)
}

updateLatestObservedTick(tick)
beamServices.beamScenario.vehicleEmissions.rememberLastVehicleTime(currentBeamVehicle, Some(tick))
eventsManager.processEvent(new ShiftEvent(tick, StartShift, id.toString, vehicle))
log.debug("state(RideHailingAgent.Offline): starting shift {}", id)
holdTickAndTriggerId(tick, triggerId)
isStartingNewShift = true

if (debugEnabled) outgoingMessages += ev
if (debugEnabled)
outgoingMessages += NotifyVehicleIdle(
Expand Down Expand Up @@ -621,7 +626,7 @@ class RideHailAgent(
) =>
log.debug(s"state(RideHailAgent.Idle.EndShiftTrigger; Trigger ID: $triggerId; Vehicle ID: ${vehicle.id}")
updateLatestObservedTick(tick)
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(
tick,
currentBeamVehicle,
beamServices
Expand All @@ -631,8 +636,10 @@ class RideHailAgent(
classOf[PathTraversalEvent],
beamServices
)
currentBeamVehicle.resetLastVehicleLinkTime()

eventsManager.processEvent(new ShiftEvent(tick, EndShift, id.toString, vehicle, emissionsProfileIDLE))
beamServices.beamScenario.vehicleEmissions.rememberLastVehicleTime(vehicle, Some(tick))

isCurrentlyOnShift = false
val newShiftToSchedule = if (data.remainingShifts.size < 1) {
Vector()
Expand Down
Loading