Skip to content

Commit

Permalink
Merge pull request #3486 from LBNL-UCB-STI/gd/feature-3485_add_tripId…
Browse files Browse the repository at this point in the history
…_to_the_events-csv_output

feature-3485 add tripId to the events-csv output
  • Loading branch information
g-derzhavets authored Mar 11, 2022
2 parents ef65131 + e6cc27b commit f5f7397
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/main/java/beam/agentsim/events/ModeChoiceEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> personId;
Expand All @@ -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<Person> 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;
Expand All @@ -59,6 +61,7 @@ public ModeChoiceEvent(double time, Id<Person> personId, String chosenMode, Stri
this.chosenTrip = chosenTrip;
this.currentActivity = currentActivity;
this.nextActivity = nextActivity;
this.tripId = tripId;
}

public static ModeChoiceEvent apply(Event event) {
Expand All @@ -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;
}
Expand All @@ -100,6 +103,7 @@ public Map<String, String> getAttributes() {
}
attr.put(ATTRIBUTE_CURRENT_ACTIVITY, currentActivity);
attr.put(ATTRIBUTE_NEXT_ACTIVITY, nextActivity);
attr.put(ATTRIBUTE_TRIP_ID, tripId);
return attr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/beam/agentsim/agents/PersonAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ class PersonAgent(
case Some(fixedDuration) => tick + fixedDuration
case _ => activityEndTime
}

if (lastTickOfSimulation >= tick) {
Math.min(lastTickOfSimulation, endTime)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1484,23 +1484,25 @@ 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,
_experiencedBeamPlan.tourIndexOfElement(nextActivity(data.personData).get),
chosenTrip,
_experiencedBeamPlan.activities(data.personData.currentActivityIndex).getType,
nextActivity(data.personData).get.getType
nextActivity(data.personData).get.getType,
tripId
)
eventsManager.processEvent(modeChoiceEvent)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/utils/csv/writers/PlansCsvWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
""
},
Expand Down
10 changes: 6 additions & 4 deletions src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PlanMerger(modeMap: Map[String, String]) extends Merger[InputPlanElement,

private def transform(inputPlanElement: InputPlanElement): PlanElement = {
PlanElement(
inputPlanElement.tripId.toString,
inputPlanElement.tripId.getOrElse(""),
PersonId(inputPlanElement.personId),
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ActivitySimFilterEventSpec extends AnyWordSpecLike with Matchers {
tourIndex,
EmbodiedBeamTrip(IndexedSeq.empty[EmbodiedBeamLeg]),
Random.nextString(10),
Random.nextString(10),
Random.nextString(10)
)
}
Expand Down

0 comments on commit f5f7397

Please sign in to comment.