Skip to content

Commit

Permalink
Provide personal vehicle only when agent is at home
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaopen committed Feb 25, 2022
1 parent c40970f commit 4d2e375
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
26 changes: 6 additions & 20 deletions src/main/scala/beam/agentsim/agents/PersonAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ object PersonAgent {
case basePersonData: BasePersonData => Some(basePersonData)
case _ => None
}

def atHome(activity: Activity): Boolean = activity.getType.equalsIgnoreCase("home")
}

class PersonAgent(
Expand Down Expand Up @@ -407,7 +409,7 @@ class PersonAgent(
// which is used in place of our real remaining tour distance of 0.0
// which should help encourage residential end-of-day charging
val tomorrowFirstLegDistance =
if (nextAct.getType.toLowerCase == "home") {
if (atHome(nextAct)) {
findFirstCarLegOfTrip(personData) match {
case Some(carLeg) =>
carLeg.beamLeg.travelPath.distanceInM
Expand Down Expand Up @@ -497,22 +499,6 @@ class PersonAgent(
val tripIndexOfElement = tour
.tripIndexOfElement(nextAct)
.getOrElse(throw new IllegalArgumentException(s"Element [$nextAct] not found"))
if (tripIndexOfElement == 0 && currentActivity(personData).getType != "Home")
logger.warn(
"~Activity {}, idx {}, id {}, next {}",
currentActivity(personData).getType,
tripIndexOfElement,
id,
nextAct.getType
)
if (tripIndexOfElement == lastTripIndex && nextAct.getType != "Home")
logger.warn(
"Cur ~Activity {}, idx {}, id {}, next {}",
currentActivity(personData).getType,
tripIndexOfElement,
id,
nextAct.getType
)
(tripIndexOfElement, lastTripIndex)
}

Expand Down Expand Up @@ -1353,7 +1339,7 @@ class PersonAgent(
currentTrip = None,
restOfCurrentTrip = List(),
currentTourPersonalVehicle = None,
currentTourMode = if (activity.getType.equals("Home")) None else data.currentTourMode,
currentTourMode = if (atHome(activity)) None else data.currentTourMode,
currentTripMode = None,
hasDeparted = false
)
Expand Down Expand Up @@ -1443,7 +1429,7 @@ class PersonAgent(
currentTourPersonalVehicle = currentTourPersonalVehicle match {
case Some(personalVehId) =>
val personalVeh = beamVehicles(personalVehId).asInstanceOf[ActualVehicle].vehicle
if (activity.getType.equals("Home")) {
if (atHome(activity)) {
potentiallyChargingBeamVehicles.put(personalVeh.id, beamVehicles(personalVeh.id))
beamVehicles -= personalVeh.id
personalVeh.getManager.get ! ReleaseVehicle(personalVeh, triggerId)
Expand All @@ -1454,7 +1440,7 @@ class PersonAgent(
case None =>
None
},
currentTourMode = if (activity.getType.equals("Home")) None else currentTourMode,
currentTourMode = if (atHome(activity)) None else currentTourMode,
currentTripMode = None,
hasDeparted = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ object HouseholdActor {
val homeCoordFromPlans = household.members
.flatMap(person =>
person.getSelectedPlan.getPlanElements.asScala.flatMap {
case act: Activity if act.getType == "Home" => Some(act.getCoord)
case _ => None
case act: Activity if PersonAgent.atHome(act) => Some(act.getCoord)
case _ => None
}
)
.headOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import akka.util.Timeout
import beam.agentsim.Resource.NotifyVehicleIdle
import beam.agentsim.agents.BeamAgent.Finish
import beam.agentsim.agents.InitializeTrigger
import beam.agentsim.agents.PersonAgent.atHome
import beam.agentsim.agents.household.HouseholdActor._
import beam.agentsim.agents.household.HouseholdFleetManager.ResolvedParkingResponses
import beam.agentsim.agents.modalbehaviors.DrivesVehicle.ActualVehicle
Expand Down Expand Up @@ -104,7 +105,7 @@ class HouseholdFleetManager(
case GetVehicleTypes(triggerId) =>
sender() ! VehicleTypesResponse(vehicles.values.map(_.beamVehicleType).toSet, triggerId)

case MobilityStatusInquiry(personId, whenWhere, _, requireVehicleCategoryAvailable, triggerId) =>
case MobilityStatusInquiry(personId, whenWhere, originActivity, requireVehicleCategoryAvailable, triggerId) =>
{
for {
neededVehicleCategory <- requireVehicleCategoryAvailable
Expand Down Expand Up @@ -157,13 +158,15 @@ class HouseholdFleetManager(
}
}.getOrElse {
availableVehicles = availableVehicles match {
case firstVehicle :: rest =>
//in case of replanning because of TRANSIT failure WALK_TRANSIT is used
//but we may want to introduce maxWalkingDistance and check that the agent is close enough to the vehicle
case firstVehicle :: rest if atHome(originActivity) =>
logger.debug("Vehicle {} is now taken", firstVehicle.id)
firstVehicle.becomeDriver(sender)
sender() ! MobilityStatusResponse(Vector(ActualVehicle(firstVehicle)), triggerId)
rest
case Nil =>
logger.debug(s"Not returning vehicle because no default is defined")
case _ =>
logger.debug(s"Not returning vehicle because no default is defined or agent is not at home")
sender() ! MobilityStatusResponse(Vector(), triggerId)
Nil
}
Expand Down

0 comments on commit 4d2e375

Please sign in to comment.