Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inm / added visualizations scripts into BEAM to MATSim scripts #2277

Merged
merged 5 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package beam.utils.beam_to_matsim.visualization.via

import beam.utils.beam_to_matsim.events.BeamPathTraversal
import beam.utils.beam_to_matsim.events_filter.{MutableSamplingFilter, MutableVehiclesFilter, VehicleSample}
import beam.utils.beam_to_matsim.io.{HashSetReader, Reader, Writer}
import beam.utils.beam_to_matsim.via_event.ViaEvent

import scala.collection.mutable

object visualization_11 extends App {

val beamEventsFilePath = "D:/Work/BEAM/visualizations/v2.it20.events.bridge_cap_5000.third.csv"
val vehiclesInCircleFilePath = "D:/Work/BEAM/visualizations/v2.it20.events.bridge_cap_5000.half_in_SF.vehicles.txt"

val idPrefix = ""
val sampleSize = 1

val viaOutputBaseFilePath = "D:/Work/BEAM/visualizations/v11.it20.events.bridge_cap_5000.third.popSize" + sampleSize
val viaEventsFile = viaOutputBaseFilePath + ".via.xml"
val viaIdsFile = viaOutputBaseFilePath + ".ids.txt"

// vehicle1
val necessaryVehicles = mutable.HashSet("rideHailVehicle-1860433")
val vehiclesInCircle = HashSetReader.fromFile(vehiclesInCircleFilePath)

val filter: MutableSamplingFilter = MutableVehiclesFilter.withListOfIncludeAndNecessary(
vehiclesInCircle,
necessaryVehicles,
Seq(
VehicleSample("RH_PHEV-Car_L1", sampleSize),
VehicleSample("RH_HEV-Car_L1", sampleSize),
VehicleSample("RH_Conventional-Truck_L1", sampleSize),
VehicleSample("RH_BEV-Car_L1", sampleSize),
VehicleSample("PHEV-Car_L1", sampleSize),
VehicleSample("HEV-Car_L1", sampleSize),
VehicleSample("Conventional-Truck_L1", sampleSize),
VehicleSample("Conventional-Car_L1", sampleSize),
VehicleSample("CAR", sampleSize),
VehicleSample("BUS-WH", sampleSize),
VehicleSample("BUS-WC", sampleSize),
VehicleSample("BUS-VTA", sampleSize),
VehicleSample("BUS-VN", sampleSize),
VehicleSample("BUS-VC", sampleSize),
VehicleSample("BUS-SR", sampleSize),
VehicleSample("BUS-RV", sampleSize),
VehicleSample("BUS-PE", sampleSize),
VehicleSample("BUS-MA", sampleSize),
VehicleSample("BUS-GG", sampleSize),
VehicleSample("BUS-DEFAULT", sampleSize),
VehicleSample("BUS-DE", sampleSize),
VehicleSample("BUS-CT", sampleSize),
VehicleSample("BUS-CM", sampleSize),
VehicleSample("BUS-CC", sampleSize),
VehicleSample("BUS-AC", sampleSize),
VehicleSample("BEV-Car_L1", sampleSize)
),
0
)

def vehicleType(pte: BeamPathTraversal): String =
pte.mode + "_" + pte.vehicleType + "_P%03d".format(pte.numberOfPassengers)

def vehicleId(pte: BeamPathTraversal): String =
idPrefix + vehicleType(pte) + "__" + pte.vehicleId

val (vehiclesEvents, _) = Reader.readWithFilter(beamEventsFilePath, filter)
val (events, typeToId) = Reader.transformPathTraversals(vehiclesEvents, vehicleId, vehicleType)

Writer.writeViaEventsQueue[ViaEvent](events, _.toXml.toString, viaEventsFile)
Writer.writeViaIdFile(typeToId, viaIdsFile)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package beam.utils.beam_to_matsim.visualization.via

import beam.utils.beam_to_matsim.events_filter.{MutablePopulationFilter, MutableSamplingFilter, PopulationSample}
import beam.utils.beam_to_matsim.io.{HashSetReader, Reader, Writer}
import beam.utils.beam_to_matsim.via_event.ViaEvent

import scala.collection.mutable

object visualization_12 extends App {
val personsInCircleFilePath = "D:/Work/BEAM/visualizations/v2.it20.events.bridge_cap_5000.half_in_SF.persons.txt"
val personsInCircle = HashSetReader.fromFile(personsInCircleFilePath)

val beamEventsFilePath = "D:/Work/BEAM/visualizations/v1.it20.events.bridge_cap_5000.csv"
val sampleSize = 0.5

val viaOutputBaseFilePath = "D:/Work/BEAM/visualizations/v12.it20.events.bridge_cap_5000.popSize" + sampleSize
val viaEventsFile = viaOutputBaseFilePath + ".via.xml"
val viaIdsFile = viaOutputBaseFilePath + ".ids.txt"
val viaModesFile = viaOutputBaseFilePath + ".activity.txt"

val idPrefix = ""

val filter: MutableSamplingFilter = MutablePopulationFilter(Seq(PopulationSample(0.3, personsInCircle.contains)))

val (vehiclesEvents, personsEvents) = Reader.readWithFilter(beamEventsFilePath, filter)

val events = mutable.PriorityQueue.empty[ViaEvent]((e1, e2) => e2.time.compare(e1.time))
val (activities, activityToCnt) = Reader.transformActivities(personsEvents)
activities.foreach(events.enqueue(_))

Writer.writeViaEventsQueue[ViaEvent](events, _.toXml.toString, viaEventsFile)
Writer.writeViaActivities(activityToCnt, viaModesFile)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package beam.utils.beam_to_matsim.visualization.via

import beam.utils.beam_to_matsim.events.BeamPathTraversal
import beam.utils.beam_to_matsim.events_filter.{MutablePopulationFilter, MutableSamplingFilter, PopulationSample}
import beam.utils.beam_to_matsim.io.{Reader, Writer}
import beam.utils.beam_to_matsim.via_event.ViaEvent

object visualization_20_24 extends App {
val beamEventsFilePath = "D:/Work/BEAM/visualizations/v2.it20.events.bridge_cap_5000.half.csv"
val sampleSize = 0.1

val viaOutputBaseFilePath = "D:/Work/BEAM/visualizations/v20_24.it20.events.bridge_cap_5000.popSize" + sampleSize
val viaEventsFile = viaOutputBaseFilePath + ".via.xml"
val viaIdsFile = viaOutputBaseFilePath + ".ids.txt"
val viaActivitiesFile = viaOutputBaseFilePath + ".act.txt"
val viaModesFile = viaOutputBaseFilePath + ".mode.txt"

def notADriver(id: String) = !id.contains("Agent")
val filter: MutableSamplingFilter = MutablePopulationFilter(Seq(PopulationSample(sampleSize, notADriver)))

def vehicleType(pte: BeamPathTraversal): String = pte.mode + "_" + pte.vehicleType
def vehicleId(pte: BeamPathTraversal): String = vehicleType(pte) + "__" + pte.vehicleId

val (vehiclesEvents, personsEvents) = Reader.readWithFilter(beamEventsFilePath, filter)
val (events, typeToId) = Reader.transformPathTraversals(vehiclesEvents, vehicleId, vehicleType)

val (modeChoiceEvents, modeToCnt) = Reader.transformModeChoices(personsEvents, 20)
modeChoiceEvents.foreach(events.enqueue(_))

val (activities, activityToCnt) = Reader.transformActivities(personsEvents)
activities.foreach(events.enqueue(_))

Writer.writeViaEventsQueue[ViaEvent](events, _.toXml.toString, viaEventsFile)

Writer.writeViaActivities(activityToCnt, viaActivitiesFile)
Writer.writeViaModes(modeToCnt, viaModesFile)
Writer.writeViaIdFile(typeToId, viaIdsFile)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package beam.utils.beam_to_matsim.visualization.via

import beam.utils.beam_to_matsim.events.BeamPathTraversal
import beam.utils.beam_to_matsim.events_filter.MutableVehiclesFilter
import beam.utils.beam_to_matsim.io.{HashSetReader, Reader, Writer}
import beam.utils.beam_to_matsim.via_event.ViaEvent

object visualization_33_34 extends App {
val dirPath = "D:/Work/BEAM/visualizations/"

val beamEventsFilePath = dirPath + "v33.0.events.third.csv"
val vehiclesInCircleFilePath = dirPath + "v33.0.events.third.csv.in_SF.vehicles.txt"

// val beamEventsFilePath = dirPath + "v34.it0.events.third.csv"
// val vehiclesInCircleFilePath = dirPath + "v34.it0.events.third.csv.in_SF.vehicles.txt"

val sampleSize = 0.3

val viaOutputBaseFilePath = beamEventsFilePath + ".popSize" + sampleSize
val viaEventsFile = viaOutputBaseFilePath + ".via.xml"
val viaIdsFile = viaOutputBaseFilePath + ".ids.txt"

val vehiclesInCircle = HashSetReader.fromFile(vehiclesInCircleFilePath)

object Selector extends MutableVehiclesFilter.SelectNewVehicle {
override def select(vehicleMode: String, vehicleType: String, vehicleId: String): Boolean = {
if (!vehiclesInCircle.contains(vehicleId)) false
else {
vehicleMode match {
case "CAR" | "BUS" => fitIn(sampleSize)
case _ => false
}
}
}
}

def vehicleType(pte: BeamPathTraversal): String = {
if (pte.vehicleId.contains("rideHail")) pte.mode + "_RH_P%03d".format(pte.numberOfPassengers)
else pte.mode + "_P%03d".format(pte.numberOfPassengers)
}

def vehicleId(pte: BeamPathTraversal): String =
vehicleType(pte) + "__" + pte.vehicleId

val (vehiclesEvents, _) = Reader.readWithFilter(beamEventsFilePath, MutableVehiclesFilter(Selector))
val (events, typeToId) = Reader.transformPathTraversals(vehiclesEvents, vehicleId, vehicleType)

Writer.writeViaEventsQueue[ViaEvent](events, _.toXml.toString, viaEventsFile)
Writer.writeViaIdFile(typeToId, viaIdsFile)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package beam.utils.beam_to_matsim.visualization.via

import beam.utils.beam_to_matsim.events.BeamPathTraversal
import beam.utils.beam_to_matsim.events_filter.{MutableVehiclesFilter, VehicleTrip}
import beam.utils.beam_to_matsim.io.{Reader, Writer}
import beam.utils.beam_to_matsim.via_event.ViaEvent

import scala.collection.mutable

object visualization_35 extends App {
val dirPath = "D:/Work/BEAM/visualizations/vis35/"
val fileName = "2.events.csv"

val beamEventsFilePath = dirPath + fileName
val viaEventsFile = beamEventsFilePath + ".all.via.xml"
val viaIdFile = beamEventsFilePath + ".all.ids.txt"

object Selector extends MutableVehiclesFilter.SelectNewVehicle {
override def select(vehicleMode: String, vehicleType: String, vehicleId: String): Boolean = {
vehicleMode match {
case "CAR" | "BUS" => true
case _ => false
}
}
}

val eventsWithAlternatives = {
val (vehiclesEvents, _) = Reader.readWithFilter(beamEventsFilePath, MutableVehiclesFilter(Selector))

def alternativePathToVehiclesTrips(path: Seq[BeamPathTraversal], idPrefix: String): Iterable[VehicleTrip] = {
val trips = mutable.Map.empty[String, VehicleTrip]
path.foreach(
pte => {
val vId = idPrefix + pte.vehicleId
val pteWithCorrectVehicleId = new BeamPathTraversal(
pte.time,
vId,
pte.driverId,
pte.vehicleType,
pte.mode,
pte.numberOfPassengers,
pte.arrivalTime,
pte.linkIds,
pte.linkTravelTime
)

trips.get(vId) match {
case Some(trip) => trip.trip += pteWithCorrectVehicleId
case None => trips(vId) = VehicleTrip(vId, pteWithCorrectVehicleId)
}
}
)

trips.values
}

val alt0 = alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative0_ride_hail, "_alt0_")
val alt1 =
alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative1_ride_hail_pooled, "_alt1_")
val alt2 = alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative2_bike, "_alt2_")
val alt3 =
alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative3_walk_transit_bike_bus, "_alt3_")
val alt4 = alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative4_walk, "_alt4_")
val alt5 =
alternativePathToVehiclesTrips(visualization_35_person1_alternatives.alternative5_walk_transit_bus, "_alt5_")

vehiclesEvents // ++ alt0 ++ alt1 ++ alt2 ++ alt3 ++ alt4 ++ alt5
}

def vehicleType(pte: BeamPathTraversal): String = pte.mode + "_P%03d".format(pte.numberOfPassengers)
def vehicleId(pte: BeamPathTraversal): String = vehicleType(pte) + "__" + pte.vehicleId

val (events, typeToId) = Reader.transformPathTraversals(eventsWithAlternatives, vehicleId, vehicleType)

Writer.writeViaEventsQueue[ViaEvent](events, _.toXml.toString, viaEventsFile)
Writer.writeViaIdFile(typeToId, viaIdFile)

}
Loading