# Railway Features

Flatland is a deliberately simple railway model: it keeps what matters for planning and dispatching trains on a shared network and leaves out the
details that railway simulation tools such as [OpenTrack](https://www.opentrack.ch) model at full depth, e.g. track geometry, signalling and train
physics. The table lists which railway features Flatland models, by area, and what it does not cover.

Flatland represents the network in one of two ways: as a grid of cells (`RailEnv`) or as a graph of entry points and edges (`GraphRailEnv`). Both
share the same simulation model and step; only the representation of the topology differs. Every grid can be converted into a graph rail env
(`GraphRailEnv.from_rail_env()`), but not every graph into a grid.

| Area           | Feature                     | Grid                                                                                                                                                                                                                                                                                                                                                                                                                                                               | Graph                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | Not covered                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---------------|:----------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Topology       | Track layout                | The network is a grid of equally long track sections (cells). Each cell holds one of 9 track layouts, up to rotation and mirroring: empty, straight, curve, simple switch, symmetric switch, diamond crossing, single slip, double slip and dead end. Generators build random networks of stations with several parallel tracks, linked by lines. At dead ends, trains turn around.                                                                                | The network is a double vertex graph (Montigel 1992, see the notes below): each point of the track has one node, called entry point, per direction of travel, and each edge is a move a train can make, so it cannot reverse at a switch or pass it in the acute angle. A train is on a transition (see the notes below): the edge from its current to its next entry point. The graph can be converted from a grid or imported from a real network, e.g. from [OpenStreetMap](https://www.openstreetmap.org) (see [flatland-scenarios PR #36](https://github.com/flatland-association/flatland-scenarios/pull/36)). Each transition still counts as equally long. Trains turn around only where the graph has an edge for it, e.g. a reversing loop. | Real track lengths: every cell or transition is equally long. Real lengths in the grid and the graph are coming with flatland-rl 5.0.0 ([PR #542](https://github.com/flatland-association/flatland-rl/pull/542), open). Track geometry: geometry, e.g. coordinates, can be attached to the edges of the graph, but the discrete simulation step uses only their length (PR #542). There are no curve radii or gradients. |
| Topology       | Switches and crossings      | The switch and crossing layouts of the 9: simple switches, single and double slips, diamond crossings and symmetric switches (a fork without a straight route). Crossings can be level-free (bridges). At a switch, the train chooses its route itself.                                                                                                                                                                                                            | A switch is an entry point with several outgoing edges; the train chooses among them with its action (left, straight, right). A crossing is two transitions that share one resource; if they do not, the crossing is level-free.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | Setting and locking routes in an interlocking, and the time it takes.                                                                                                                                                                                                                                                                                                                                                    |
| Topology       | Track occupation            | Resource model: each cell is one resource that holds one train at a time; all moves through the cell map to it (n:1). A level-free crossing is the exception: its two tracks map to two resources, one per axis (north-south and east-west), so two trains can cross it at the same time. A train can enter a cell as soon as the train ahead leaves it, so trains can follow each other closely. Two trains facing each other on a single track block each other. | Resource model: a resource map assigns each transition, by its start entry point, one resource that holds one train at a time. The mapping is n:1 and free, e.g. a crossing or a block of several transitions sharing one resource. A crossing is level-free if its transitions map to different resources. A graph converted from a grid keeps the grid's resources: one per cell, two at a level-free crossing. Following and facing trains as in the grid.                                                                                                                                                                                                                                                                                         | Signals and safety distances. Train length: a train occupies one cell or transition only.                                                                                                                                                                                                                                                                                                                                |
| Timetable      | Train runs                  | Each train runs once, from its origin to its destination. The origin is one entry point; the destination is passed as a set of entry points, e.g. several tracks or directions at the destination, and the train arrives at whichever of them it reaches first. It appears on its first track when it departs and leaves the network when it arrives.                                                                                                              | Same as grid.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | Train compositions (coupling, splitting), rolling stock and crew rotations, i.e. a train continuing as another run, and periodic timetables.                                                                                                                                                                                                                                                                             |
| Timetable      | Departure and arrival times | Each train has an earliest departure and a latest arrival, also at its intermediate stops. Time runs in steps. Arriving late, departing early and not starting at all are penalised.                                                                                                                                                                                                                                                                               | Same as grid. The timetable is given or taken over from a grid; without one, all trains may depart at once.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | Exact scheduled times with minimum running and dwell times, connections between trains, and train priorities.                                                                                                                                                                                                                                                                                                            |
| Timetable      | Intermediate stops          | A train can have stops on its way, each with alternative tracks, e.g. any platform of a station. A stop counts as served only if the train stops there; running through does not count. Stops are assumed to be disjoint, i.e. no two stops of a train share an entry point, and each is visited once; this is not checked ([flatland-rl issue #557](https://github.com/flatland-association/flatland-rl/issues/557), open, planned for 5.0.0).                    | Same as grid.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | Minimum dwell times, platform lengths and passenger flows.                                                                                                                                                                                                                                                                                                                                                               |
| Speed dynamics | Maximum speed               | Each train has its own maximum speed, e.g. fast passenger trains and slow freight trains. Speed is measured in cells (or transitions) per step.                                                                                                                                                                                                                                                                                                                    | Same as grid.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | Speed limits of the line, e.g. in curves, at switches or in stations.                                                                                                                                                                                                                                                                                                                                                    |
| Speed dynamics | Acceleration and braking    | Trains accelerate and brake by fixed amounts per step, the same for all trains. A change of speed takes effect from the next step on, as a train cannot change its speed at once.                                                                                                                                                                                                                                                                                  | Same as grid.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | Train physics: mass, tractive effort, running resistance, gradients and braking curves.                                                                                                                                                                                                                                                                                                                                  |
| Disturbances   | Malfunctions                | Trains can break down and stand still for a number of steps, also before departure. The stopped train blocks its cell or transition, and the trains behind it are delayed. Malfunctions follow different distributions (at random with a rate and a range of durations, a single malfunction at a given step, or from a file), and can be conditional, e.g. only when stopped at an intermediate stop or on given cells within a time range.                       | Same as grid; the predefined conditions refer to grid cells and stops.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Varying running and dwell times, and failures of the infrastructure, such as a broken switch. Construction works, i.e. closing tracks or restricting the speed for a period. Effects generators could change the network during an episode, but none does, and the consequences are unclear: e.g. the distance map is computed only in `reset()`, and the transitions of the graph are cached.                           |

Notes:

* Simulation: grid and graph share the simulation step, see [Agent-Environment Interaction](../key-concepts/key_concepts) in the key
  concepts. The rail environment manages the agent state of each train: its current and next entry point, offset, speed, malfunction counter and
  whether it is done. The timetable is
  used in the rewards; the simulation itself only enforces the earliest departure at the origin (a train does not enter the network before it),
  which is to move to the controller in flatland-rl 5.0.0
  ([flatland-rl issue #559](https://github.com/flatland-association/flatland-rl/issues/559)). Malfunctions are generated by effects generators, which can modify the environment
  at the start and end of every step.
* Double vertex graph: M. Montigel: Formal Representation of Track Topologies by Double Vertex Graphs. In: Computers in Railways III, WIT
  Press, 1992, pp. 359-370. [OpenTrack](https://www.opentrack.ch) describes networks with double vertex graphs, too.
* Transition: a pair of entry points, i.e. a directed edge of the graph (`TransitionMap`). The agent state holds `current_entry_point` and
  `next_entry_point` ([flatland-rl issue #549](https://github.com/flatland-association/flatland-rl/issues/549)); together they span the transition the
  train is on. Up to flatland-rl 4.3.0, "configuration" meant the entry point `((r, c), d)`; the term is no longer used for the rail.

See [Key concepts](../key-concepts/key_concepts) for how Flatland models these features, and [Speed Profiles](../environment/environment/speed_profiles)
for the speed dynamics step by step.
