Skip to content

Commit

Permalink
Merge branch 'develop' into do/#3441-current-trip-tour-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuan-1998 committed Mar 15, 2022
2 parents 4d2e375 + f5f7397 commit 056c125
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 27 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 @@ -544,7 +544,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 @@ -153,7 +153,7 @@ trait ChoosesMode {
_,
_,
_,
None | Some(CAR | BIKE | DRIVE_TRANSIT | BIKE_TRANSIT),
plansModeOption @ (None | Some(CAR | BIKE | DRIVE_TRANSIT | BIKE_TRANSIT)),
_,
_,
_,
Expand Down Expand Up @@ -188,11 +188,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)
Expand Down Expand Up @@ -1396,23 +1414,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.currentTripMode.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
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -121,14 +121,16 @@ object ChoosesParking {

private def createParkingSkimmerEvent(
tick: Int,
geo: GeoUtils,
tazTreeMap: TAZTreeMap,
nextActivity: Option[Activity],
stall: ParkingStall,
restOfTrip: List[EmbodiedBeamLeg]
): 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
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 056c125

Please sign in to comment.