Skip to content
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 @@ -128,6 +128,9 @@ private void overrideDefaultLoggerSetup(String eventsToWrite) {
case "LeavingParkingEvent":
eventClass = LeavingParkingEvent.class;
break;
case "OvernightParkingEvent":
eventClass = OvernightParkingEvent.class;
break;
case "LinkEnterEvent":
eventClass = LinkEnterEvent.class;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,16 @@ 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 maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(
currentLeg.startTime,
currentBeamVehicle,
beamServices
)
currentBeamVehicle.setLastVehicleTimeLink(
beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
currentBeamVehicle,
Some(currentLeg.endTime),
currentLeg.travelPath.linkIds.lastOption
)
Expand All @@ -374,8 +377,8 @@ trait DrivesVehicle[T <: DrivingData] extends BeamAgent[T] with Stash with Expon
beamServices
)
val emissionsProfile = EmissionsProfile.join(emissionsProfilePTE, emissionsProfileIDLE)

val numberOfPassengers: Int = calculateNumberOfPassengersBasedOnCurrentTripMode(data, currentLeg, riders)
val currentTourMode: Option[String] = getCurrentTripMode(data)
val pte = PathTraversalEvent(
tick,
currentVehicleUnderControl,
Expand Down Expand Up @@ -602,12 +605,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 +632,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 +767,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
89 changes: 68 additions & 21 deletions src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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.utils.misc.Time
Expand Down Expand Up @@ -353,8 +353,23 @@ class RideHailAgent(
val isTimeForShift =
shifts.isEmpty || shifts.get.exists(shift => shift.range.lowerBound <= tick && shift.range.upperBound >= tick)
if (isTimeForShift) {
vehicle.setLastVehicleTime(Some(tick))
eventsManager.processEvent(new ShiftEvent(tick, StartShift, id.toString, vehicle))
val maybeVehicleLink: Option[Int] =
try {
Some(GeoUtils.GeoUtilsWgs.getNearestR5Edge(transportNetwork.streetLayer, geo.utm2Wgs(vehicle.spaceTime.loc)))
} catch {
case exception: Exception =>
logger.error(s"Could not get nearest link for vehicle ${vehicle.id}:$exception")
None
}
beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
vehicle,
Some(tick),
maybeVehicleLink
)

eventsManager.processEvent(
new ShiftEvent(tick, StartShift, id.toString, vehicle)
)
rideHailManager ! NotifyVehicleIdle(
vehicle.id,
this.id,
Expand Down Expand Up @@ -453,15 +468,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, tick)

currentBeamVehicle.resetLastVehicleLinkTime()
isCurrentlyOnShift = false
needsToEndShift = false
if (data.remainingShifts.size < 1) {
Expand All @@ -484,31 +500,60 @@ class RideHailAgent(
stash()
stay()
} else {
if (needsToEndShift) {
val maybeIDLEVehicleActivity = BeamVehicle.getIDLEActivityForEmissions(tick, currentBeamVehicle, beamServices)
val newLocation: SpaceTime = 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)
}
val vehicleLink: Int =
GeoUtils.GeoUtilsWgs.getNearestR5Edge(transportNetwork.streetLayer, geo.utm2Wgs(newLocation.loc))

val shiftWasEnded = if (needsToEndShift) {
val maybeIDLEVehicleActivityWithEngine =
BeamVehicle.getIDLEActivitiesWithRunningEngineForEmissions(tick, currentBeamVehicle, beamServices)
val emissionsProfileIDLE = currentBeamVehicle.emitEmissions(
maybeIDLEVehicleActivity,
maybeIDLEVehicleActivityWithEngine,
classOf[PathTraversalEvent],
beamServices
)
currentBeamVehicle.resetLastVehicleLinkTime()
eventsManager.processEvent(new ShiftEvent(tick, EndShift, id.toString, vehicle, emissionsProfileIDLE))
needsToEndShift = false
isCurrentlyOnShift = false
}

true
} else { false }

updateLatestObservedTick(tick)
currentBeamVehicle.setLastVehicleTime(Some(tick))
eventsManager.processEvent(new ShiftEvent(tick, StartShift, id.toString, vehicle))

val emissionsProfileIDLE = if (shiftWasEnded) {
val maybeIDLEVehicleActivityWithoutEngine = BeamVehicle.getIDLEActivitiesWithStoppedEngineForEmissions(
currentBeamVehicle,
beamServices,
tick
)
currentBeamVehicle.emitEmissions(
maybeIDLEVehicleActivityWithoutEngine,
classOf[VehicleEntersTrafficEvent],
beamServices
)
} else { None }

beamServices.beamScenario.vehicleEmissions.rememberLastVehiclePosition(
vehicle,
Some(tick),
Some(vehicleLink)
)

eventsManager.processEvent(
new ShiftEvent(tick, StartShift, id.toString, vehicle, emissionsProfile = emissionsProfileIDLE)
)

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)
}

if (debugEnabled) outgoingMessages += ev
if (debugEnabled)
outgoingMessages += NotifyVehicleIdle(
Expand Down Expand Up @@ -621,7 +666,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 +676,10 @@ class RideHailAgent(
classOf[PathTraversalEvent],
beamServices
)
currentBeamVehicle.resetLastVehicleLinkTime()

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

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