From 871240c608677d8ca2fb6a8ca7e3e6b816377340 Mon Sep 17 00:00:00 2001 From: Grigory D Date: Wed, 9 Mar 2022 10:43:01 +0400 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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,