From 5113e9b2b6f2332ef579a99c54b8be8febaac6c4 Mon Sep 17 00:00:00 2001 From: Nikolay Ilyin Date: Fri, 25 Feb 2022 17:35:17 +0300 Subject: [PATCH 1/7] fix for getting correct tazId for UTM location --- .../beam/agentsim/agents/parking/ChoosesParking.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala index da97f1f24cf..77a1b5e072e 100755 --- a/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala +++ b/src/main/scala/beam/agentsim/agents/parking/ChoosesParking.scala @@ -71,8 +71,8 @@ object ChoosesParking { ) eventsManager.processEvent(parkEvent) // nextLeg.endTime -> to fix repeated path traversal restOfTrip.foreach { legs => - if (legs.size >= 2 && legs(0).beamLeg.mode == BeamMode.CAR && legs(1).beamLeg.mode == BeamMode.WALK) { - val parkingSkimmerEvent = createParkingSkimmerEvent(tick, tazTreeMap, nextActivity, stall, legs) + if (legs.size >= 2 && legs.head.beamLeg.mode == BeamMode.CAR && legs(1).beamLeg.mode == BeamMode.WALK) { + val parkingSkimmerEvent = createParkingSkimmerEvent(tick, geo, tazTreeMap, nextActivity, stall, legs) eventsManager.processEvent(parkingSkimmerEvent) val freightRequestType = nextActivity.flatMap(activity => @@ -121,6 +121,7 @@ object ChoosesParking { private def createParkingSkimmerEvent( tick: Int, + geo: GeoUtils, tazTreeMap: TAZTreeMap, nextActivity: Option[Activity], stall: ParkingStall, @@ -128,7 +129,8 @@ object ChoosesParking { ): ParkingSkimmerEvent = { require(restOfTrip.size >= 2, "Rest of trip must consist of two legs at least: current car leg, walk leg") val walkLeg = restOfTrip(1) - val tazId = tazTreeMap.getTAZ(walkLeg.beamLeg.travelPath.endPoint.loc).tazId + val tripEndPointUTMLocation = geo.wgs2Utm(walkLeg.beamLeg.travelPath.endPoint.loc) + val tazId = tazTreeMap.getTAZ(tripEndPointUTMLocation).tazId val chargerType = stall.chargingPointType match { case Some(chargingType) if ChargingPointType.getChargingPointCurrent(chargingType) == ElectricCurrentType.DC => ChargerType.DCFastCharger From 6ee9bc101e45c13eb8b2f2ef85360908becb546e Mon Sep 17 00:00:00 2001 From: Zach Needell Date: Thu, 3 Mar 2022 14:44:24 -0800 Subject: [PATCH 2/7] Force vehicle creation when plans require it --- .../agents/modalbehaviors/ChoosesMode.scala | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala index 46d369da40a..0068f1f20cd 100755 --- a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala +++ b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala @@ -233,7 +233,7 @@ trait ChoosesMode { _, _, _, - None | Some(CAR | BIKE | DRIVE_TRANSIT | BIKE_TRANSIT), + plansModeOption @ (None | Some(CAR | BIKE | DRIVE_TRANSIT | BIKE_TRANSIT)), _, _, _, @@ -267,11 +267,29 @@ trait ChoosesMode { _ ) => implicit val executionContext: ExecutionContext = context.system.dispatcher - requestAvailableVehicles( - vehicleFleets, - currentLocation, - _experiencedBeamPlan.activities(currentActivityIndex) - ) pipeTo self + plansModeOption match { + case Some(CAR | DRIVE_TRANSIT) => + requestAvailableVehicles( + vehicleFleets, + currentLocation, + _experiencedBeamPlan.activities(currentActivityIndex), + Some(VehicleCategory.Car) + ) pipeTo self + case Some(BIKE | BIKE_TRANSIT) => + requestAvailableVehicles( + vehicleFleets, + currentLocation, + _experiencedBeamPlan.activities(currentActivityIndex), + Some(VehicleCategory.Bike) + ) pipeTo self + case _ => + requestAvailableVehicles( + vehicleFleets, + currentLocation, + _experiencedBeamPlan.activities(currentActivityIndex) + ) pipeTo self + } + // Otherwise, send empty list to self case _ => self ! MobilityStatusResponse(Vector(), getCurrentTriggerIdOrGenerate) From 871240c608677d8ca2fb6a8ca7e3e6b816377340 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Wed, 9 Mar 2022 10:43:01 +0400 Subject: [PATCH 3/7] feature-3485 add tripId to the events-csv output --- .../java/beam/agentsim/events/ModeChoiceEvent.java | 10 +++++++--- .../events/handling/BeamEventsWriterCSV.java | 2 +- .../scala/beam/agentsim/agents/PersonAgent.scala | 3 +-- .../agents/modalbehaviors/ChoosesMode.scala | 3 ++- .../beam/utils/csv/writers/PlansCsvWriter.scala | 2 +- .../utils/scenario/UrbanSimScenarioLoader.scala | 10 ++++++---- .../generic/readers/PlanElementReader.scala | 14 +++++++++++--- .../urbansim/censusblock/merger/PlanMerger.scala | 2 +- 8 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main/java/beam/agentsim/events/ModeChoiceEvent.java b/src/main/java/beam/agentsim/events/ModeChoiceEvent.java index 87edc1c34a5..41d81f4eca6 100755 --- a/src/main/java/beam/agentsim/events/ModeChoiceEvent.java +++ b/src/main/java/beam/agentsim/events/ModeChoiceEvent.java @@ -28,6 +28,7 @@ public class ModeChoiceEvent extends Event implements HasPersonId { public final static String ATTRIBUTE_LEG_VEHICLE_IDS = "legVehicleIds"; public final static String ATTRIBUTE_CURRENT_ACTIVITY = "currentActivity"; public final static String ATTRIBUTE_NEXT_ACTIVITY = "nextActivity"; + public final static String ATTRIBUTE_TRIP_ID = "tripId"; public final EmbodiedBeamTrip chosenTrip; public final Id personId; @@ -41,10 +42,11 @@ public class ModeChoiceEvent extends Event implements HasPersonId { public final Integer tourIndex; public final String currentActivity; public final String nextActivity; + public final String tripId; public ModeChoiceEvent(double time, Id personId, String chosenMode, String currentTourMode, Double expectedMaxUtility, String linkId, String availableAlternatives, Boolean vehAvailable, Double length, - Integer tourIndex, EmbodiedBeamTrip chosenTrip, String currentActivity, String nextActivity) { + Integer tourIndex, EmbodiedBeamTrip chosenTrip, String currentActivity, String nextActivity, String tripId) { super(time); this.personId = personId; @@ -59,6 +61,7 @@ public ModeChoiceEvent(double time, Id personId, String chosenMode, Stri this.chosenTrip = chosenTrip; this.currentActivity = currentActivity; this.nextActivity = nextActivity; + this.tripId = tripId; } public static ModeChoiceEvent apply(Event event) { @@ -76,8 +79,8 @@ public static ModeChoiceEvent apply(Event event) { Integer.parseInt(attr.get(ATTRIBUTE_TOUR_INDEX)), null, attr.get(ATTRIBUTE_CURRENT_ACTIVITY), - attr.get(ATTRIBUTE_NEXT_ACTIVITY) - ); + attr.get(ATTRIBUTE_NEXT_ACTIVITY), + attr.get(ATTRIBUTE_TRIP_ID)); } return (ModeChoiceEvent) event; } @@ -100,6 +103,7 @@ public Map getAttributes() { } attr.put(ATTRIBUTE_CURRENT_ACTIVITY, currentActivity); attr.put(ATTRIBUTE_NEXT_ACTIVITY, nextActivity); + attr.put(ATTRIBUTE_TRIP_ID, tripId); return attr; } diff --git a/src/main/java/beam/agentsim/events/handling/BeamEventsWriterCSV.java b/src/main/java/beam/agentsim/events/handling/BeamEventsWriterCSV.java index 8ea6880b37c..00dba60f0b2 100755 --- a/src/main/java/beam/agentsim/events/handling/BeamEventsWriterCSV.java +++ b/src/main/java/beam/agentsim/events/handling/BeamEventsWriterCSV.java @@ -78,7 +78,7 @@ public void writeEvent(Event event) { for (String attribute : attributeKeys) { if (!attributeToColumnIndexMapping.containsKey(attribute)) { if (this.eventTypeToLog == null || !attribute.equals(Event.ATTRIBUTE_TYPE)) { - DebugLib.stopSystemAndReportInconsistency("unkown attribute:" + attribute + ";class:" + event.getClass()); + DebugLib.stopSystemAndReportInconsistency("unknown attribute:" + attribute + ";class:" + event.getClass()); } } if (this.eventTypeToLog == null || !attribute.equals(Event.ATTRIBUTE_TYPE)) { diff --git a/src/main/scala/beam/agentsim/agents/PersonAgent.scala b/src/main/scala/beam/agentsim/agents/PersonAgent.scala index 2170f710f79..c2907cbf1b3 100755 --- a/src/main/scala/beam/agentsim/agents/PersonAgent.scala +++ b/src/main/scala/beam/agentsim/agents/PersonAgent.scala @@ -515,9 +515,8 @@ class PersonAgent( } val endTime = beamServices.beamScenario.fixedActivitiesDurations.get(activity.getType) match { case Some(fixedDuration) => tick + fixedDuration - case _ => activityEndTime + case None => activityEndTime } - if (lastTickOfSimulation >= tick) { Math.min(lastTickOfSimulation, endTime) } else { diff --git a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala index 0068f1f20cd..2f629cbfa80 100755 --- a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala +++ b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala @@ -1500,7 +1500,8 @@ trait ChoosesMode { _experiencedBeamPlan.tourIndexOfElement(nextActivity(data.personData).get), chosenTrip, _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getType, - nextActivity(data.personData).get.getType + nextActivity(data.personData).get.getType, + _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getAttributes.getAttribute("trip_id").toString ) eventsManager.processEvent(modeChoiceEvent) diff --git a/src/main/scala/beam/utils/csv/writers/PlansCsvWriter.scala b/src/main/scala/beam/utils/csv/writers/PlansCsvWriter.scala index 48fd24310e1..f2bbb27611f 100755 --- a/src/main/scala/beam/utils/csv/writers/PlansCsvWriter.scala +++ b/src/main/scala/beam/utils/csv/writers/PlansCsvWriter.scala @@ -76,7 +76,7 @@ object PlansCsvWriter extends ScenarioCsvWriter { val route = Option(leg.getRoute) PlanElement( tripId = if (leg.getAttributes.getAttribute("trip_id") != null) { - leg.getAttributes.getAttribute("trip_id").toString.filter(x => (x.isDigit || x.equals('.'))) + leg.getAttributes.getAttribute("trip_id").toString.filter(x => x.isDigit || x.equals('.')) } else { "" }, diff --git a/src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala b/src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala index b418a1bf2bb..725bf5e1fda 100644 --- a/src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala +++ b/src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala @@ -574,17 +574,18 @@ class UrbanSimScenarioLoader( person.setSelectedPlan(plan) } val planElement = planInfo.planElementType + val tripId = Option(planInfo.tripId).getOrElse("") if (planElement == PlanElement.Leg) { planInfo.legMode match { case Some(mode) => val leg = PopulationUtils.createLeg(mode) - leg.getAttributes.putAttribute("trip_id", Option(planInfo.tripId).getOrElse("")) + leg.getAttributes.putAttribute("trip_id", tripId) plan.addLeg(leg) - plan.getAttributes.putAttribute("trip_id", Option(planInfo.tripId).getOrElse("")) + plan.getAttributes.putAttribute("trip_id", tripId) case None => val leg = PopulationUtils.createLeg("") - leg.getAttributes.putAttribute("trip_id", Option(planInfo.tripId).getOrElse("")) - plan.getAttributes.putAttribute("trip_id", Option(planInfo.tripId).getOrElse("")) + leg.getAttributes.putAttribute("trip_id", tripId) + plan.getAttributes.putAttribute("trip_id", tripId) } } else if (planElement == PlanElement.Activity) { assert( @@ -606,6 +607,7 @@ class UrbanSimScenarioLoader( ) ) val act = PopulationUtils.createAndAddActivityFromCoord(plan, activityType, coord) + plan.getPlanElements.asScala.last.getAttributes.putAttribute("trip_id", tripId) planInfo.activityEndTime.foreach { endTime => act.setEndTime(endTime * 60 * 60) } diff --git a/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala b/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala index 9eb93bf6329..ad5e0024a57 100644 --- a/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala +++ b/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala @@ -71,7 +71,6 @@ object CsvPlanElementReader extends PlanElementReader { } object XmlPlanElementReader extends PlanElementReader { - import beam.utils.csv.GenericCsvReader._ override def read(path: String): Array[PlanElement] = { val scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()) @@ -105,7 +104,11 @@ object XmlPlanElementReader extends PlanElementReader { planElementIdx: Int ): PlanElement = PlanElement( - tripId = "", + tripId = if (activity.getAttributes.getAttribute("trip_id") != null) { + activity.getAttributes.getAttribute("trip_id").toString.filter(x => (x.isDigit || x.equals('.'))) + } else { + "" + }, personId = PersonId(person.getId.toString), planIndex = planIdx, planScore = plan.getScore, @@ -128,7 +131,12 @@ object XmlPlanElementReader extends PlanElementReader { geoId = None ) - private def toPlanElement(leg: Leg, plan: Plan, planIdx: Int, person: Person, planElementIdx: Int): PlanElement = + private def toPlanElement( + leg: Leg, + plan: Plan, + planIdx: Int, + person: Person, + planElementIdx: Int): PlanElement = PlanElement( tripId = if (leg.getAttributes.getAttribute("trip_id") != null) { leg.getAttributes.getAttribute("trip_id").toString.filter(x => (x.isDigit || x.equals('.'))) diff --git a/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala b/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala index 327a8128c33..05c83e4149a 100644 --- a/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala +++ b/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala @@ -9,7 +9,7 @@ class PlanMerger(modeMap: Map[String, String]) extends Merger[InputPlanElement, private def transform(inputPlanElement: InputPlanElement): PlanElement = { PlanElement( - inputPlanElement.tripId.toString, + if (inputPlanElement.tripId.isEmpty) "" else inputPlanElement.tripId.get, PersonId(inputPlanElement.personId), 0, 0, From 75bc162b869d72efbcd142c1d106eb4302865692 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Wed, 9 Mar 2022 10:59:06 +0400 Subject: [PATCH 4/7] scalafmt --- .../beam/agentsim/agents/modalbehaviors/ChoosesMode.scala | 6 +++++- .../utils/scenario/generic/readers/PlanElementReader.scala | 7 +------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala index 2f629cbfa80..5fd6b46e077 100755 --- a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala +++ b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala @@ -1501,7 +1501,11 @@ trait ChoosesMode { chosenTrip, _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getType, nextActivity(data.personData).get.getType, - _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getAttributes.getAttribute("trip_id").toString + _experiencedBeamPlan + .activities(data.personData.currentActivityIndex) + .getAttributes + .getAttribute("trip_id") + .toString ) eventsManager.processEvent(modeChoiceEvent) diff --git a/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala b/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala index ad5e0024a57..e13e3b77ac0 100644 --- a/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala +++ b/src/main/scala/beam/utils/scenario/generic/readers/PlanElementReader.scala @@ -131,12 +131,7 @@ object XmlPlanElementReader extends PlanElementReader { geoId = None ) - private def toPlanElement( - leg: Leg, - plan: Plan, - planIdx: Int, - person: Person, - planElementIdx: Int): PlanElement = + private def toPlanElement(leg: Leg, plan: Plan, planIdx: Int, person: Person, planElementIdx: Int): PlanElement = PlanElement( tripId = if (leg.getAttributes.getAttribute("trip_id") != null) { leg.getAttributes.getAttribute("trip_id").toString.filter(x => (x.isDigit || x.equals('.'))) From 8887853ea431c8d541b8d48617f36910972c44f2 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Wed, 9 Mar 2022 11:24:59 +0400 Subject: [PATCH 5/7] fix test compilation --- .../analysis/plots/filterevent/ActivitySimFilterEventSpec.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/scala/beam/analysis/plots/filterevent/ActivitySimFilterEventSpec.scala b/src/test/scala/beam/analysis/plots/filterevent/ActivitySimFilterEventSpec.scala index 83e905467ab..d99dc784cf0 100644 --- a/src/test/scala/beam/analysis/plots/filterevent/ActivitySimFilterEventSpec.scala +++ b/src/test/scala/beam/analysis/plots/filterevent/ActivitySimFilterEventSpec.scala @@ -106,6 +106,7 @@ class ActivitySimFilterEventSpec extends AnyWordSpecLike with Matchers { tourIndex, EmbodiedBeamTrip(IndexedSeq.empty[EmbodiedBeamLeg]), Random.nextString(10), + Random.nextString(10), Random.nextString(10) ) } From e8fe4e37f5532e3cb9670533034de0f94a36a7c4 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Wed, 9 Mar 2022 12:20:59 +0400 Subject: [PATCH 6/7] fix test --- .../agents/modalbehaviors/ChoosesMode.scala | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala index 5fd6b46e077..56a885ad8cd 100755 --- a/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala +++ b/src/main/scala/beam/agentsim/agents/modalbehaviors/ChoosesMode.scala @@ -1484,16 +1484,17 @@ trait ChoosesMode { ) } + val tripId = Option( + _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getAttributes.getAttribute("trip_id") + ).getOrElse("").toString + val modeChoiceEvent = new ModeChoiceEvent( tick, id, chosenTrip.tripClassifier.value, data.personData.currentTourMode.map(_.value).getOrElse(""), data.expectedMaxUtilityOfLatestChoice.getOrElse[Double](Double.NaN), - _experiencedBeamPlan - .activities(data.personData.currentActivityIndex) - .getLinkId - .toString, + _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getLinkId.toString, data.availableAlternatives.get, data.availablePersonalStreetVehicles.nonEmpty, chosenTrip.legs.view.map(_.beamLeg.travelPath.distanceInM).sum, @@ -1501,11 +1502,7 @@ trait ChoosesMode { chosenTrip, _experiencedBeamPlan.activities(data.personData.currentActivityIndex).getType, nextActivity(data.personData).get.getType, - _experiencedBeamPlan - .activities(data.personData.currentActivityIndex) - .getAttributes - .getAttribute("trip_id") - .toString + tripId ) eventsManager.processEvent(modeChoiceEvent) From e6cc27bbf7c99d8bce1f05c1121b1a7e59995c38 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Fri, 11 Mar 2022 14:49:28 +0400 Subject: [PATCH 7/7] fix review comment --- src/main/scala/beam/agentsim/agents/PersonAgent.scala | 2 +- .../utils/scenario/urbansim/censusblock/merger/PlanMerger.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/scala/beam/agentsim/agents/PersonAgent.scala b/src/main/scala/beam/agentsim/agents/PersonAgent.scala index c2907cbf1b3..48546da05f7 100755 --- a/src/main/scala/beam/agentsim/agents/PersonAgent.scala +++ b/src/main/scala/beam/agentsim/agents/PersonAgent.scala @@ -515,7 +515,7 @@ class PersonAgent( } val endTime = beamServices.beamScenario.fixedActivitiesDurations.get(activity.getType) match { case Some(fixedDuration) => tick + fixedDuration - case None => activityEndTime + case _ => activityEndTime } if (lastTickOfSimulation >= tick) { Math.min(lastTickOfSimulation, endTime) diff --git a/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala b/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala index 05c83e4149a..1519d57bbb5 100644 --- a/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala +++ b/src/main/scala/beam/utils/scenario/urbansim/censusblock/merger/PlanMerger.scala @@ -9,7 +9,7 @@ class PlanMerger(modeMap: Map[String, String]) extends Merger[InputPlanElement, private def transform(inputPlanElement: InputPlanElement): PlanElement = { PlanElement( - if (inputPlanElement.tripId.isEmpty) "" else inputPlanElement.tripId.get, + inputPlanElement.tripId.getOrElse(""), PersonId(inputPlanElement.personId), 0, 0,