-
Notifications
You must be signed in to change notification settings - Fork 2
3.1 Entry for Timetable Simulation
Timetable entries enable users invoke timetable simulation from their customized Python script. An example of a timetable simulation in Jupyter Notebook can be seen here.
Timetable entries are defined in a java class TimetableSimulationEntry. From Python code, users can through a JavaGateway and call the method createTimetableSimulationEntry of the gateway object to create a TimetableSimulationEntry to communnicate with PULSim.
In PULSim, the "Py4J" communication should be activated.
With the createTimetableSimulationEntry method, a TimetableSimulationEntry will be created and initialized with the given path of railsys data. The TimetableSimulationEntry can be used for setting, running and querying of timetable simulation.
/**
* @param infraPath: The path of infrastructure data.
* @param rsPath: The path of rolling stocks data.
* @param ttPath: The path of timetable data.
* @return: A TimetableSimulationEntry.
*/
public TimetableSimulationEntry createTimetableSimulationEntry(String infraPath, String rsPath, String ttPath)
With getAllInfrastructureElements method, all infrastructure elements will be returned as a collection. An infrastructure element can be a track, a turnout, a crossing, a single slip, or a double slip.
/**
* @return: The collection of infrastructure elements.
*/
public Collection<InfrastructureElement> getAllInfrastructureElements()
With getAllTrains method, all trains running in PULSim will be returned as a collection.
/**
* @return: The collection of trains running in PULSim.
*/
public Collection<AbstractTrainSimulator> getAllTrains(SingleSimulationManager simulator)
With getOccupancyMap method, the occupancy situation of an infrastructure element will be returned.
/**
* @param simulator: An initialized timetable simulator.
* @param element: The infrastructure element for querying occupancy situation.
* @return: A map of occupancy:
* key: the reference of the distance on the track (in meter)
* value: the duration of occupancy to the last entry of the map.
*/
public TreeMap<Double, Duration> getOccupancyMap(SingleSimulationManager simulator, InfrastructureElement element)
With getArriveDepatures method, the simulated departure time and arrive time of a given train will be returned.
/**
* @param simulator: An initialized timetable simulator.
* @param index: The index of the train in the return collection called by getAllTrains().
* @return: A map of arrive and departure time:
* key: the train number.
* value: an array of strings with two elements. The first element is the arrive time, and the second element is the daparture time.
*/
public TreeMap<String, String[]> getArriveDepatures(SingleSimulationManager simulator, int index)
With getCourse method, the running dynamic information of a train will be returned.
/**
* @param simulator: An initialized timetable simulator.
* @param index: The index of the train in the return collection called by getAllTrains().
* @return: A list of array with running dynamic informations. Each array has three elements, which represents
* - the distance [meter],
* - the velocity [km/hour],
* - the time duration to the last dicrete point [second]
* at each discrete point respectively.
*/
public List<Double[]> getCourse(SingleSimulationManager simulator, int index)
In the following Python script, a timetable simulator will be initialized and simulated. The results of simulation will be output afterwards. The script can be downloaded here.
from py4j.java_gateway import JavaGateway
import matplotlib.pyplot as plt
gateway = JavaGateway()
timetable_entry = gateway.createTimetableSimulationEntry(
"c:\\temp\\raildata\\infrastructure",
"c:\\temp\\raildata\\rollingstocks",
"c:\\temp\\raildata\\timetable")
simulator = timetable_entry.getSimulator()
timetable_entry.setSimulationTime(simulator, 7, 8)
print("run simulation ...")
simulator.run()
def output_occupancy():
print ("Occupancy time of tracks:")
elements = timetable_entry.getAllInfrastructureElements()
for element in elements:
oMap = timetable_entry.getOccupancyMap(simulator, element)
if oMap is not None:
print ("Track Id: ", element, ":")
lastKey = 0;
for key in oMap:
if key != 0:
print (" ", lastKey, "-", key, "m:", oMap[key])
lastKey = key
print()