Generation of Railway Networks#
Flatland provides the sparse_rail_generator, which generates realistic-looking railway networks.
Using this function, networks are generally generated using the following steps:
- Cities (square areas of fixed "radius") are placed randomly on the map
- Cities are vested with inner and outer connection points towards and away from its closest neighbor
- Rails are placed on (shortest) paths connecting outer connection points of closest cities
- Rails are placed connecting inner connection points with parallel rails and train stations (resp. tracks) in the middle
- Rail transitions are fixed (as their placement does not preserve transition validity)
Find more details and illustrations for the different steps below.
From a graph-theoretical perspective we have tightly connected node clusters (cities) which are then connected to their nearest neighbors. Due to the limited spatial resolution of the grid, the overlapping of paths then dynamically merges edges and generates additional nodes between the cities.
Parameters#
The following parameters are most-relevant for the basic functionality of the generation of the random networks:
Code Parameter |
Math |
Description |
|---|---|---|
|
\(n_\text{city}\) |
Number of cities |
|
\(n_\text{inter}\) |
Number of connection points at city borders |
|
\(n_\text{inner}\) |
Number of parallel tracks in the city |
The size of cities is given by their radius \(r_\text{city} = n_\text{inner} + 2\), chosen such that there is sufficient space for tracks and connection points. This results in square cities of size \((2 r_\text{city}+1) \times (2 r_\text{city}+1)\).
For the sake of this documentation we consider a generation on a grid of size \(20 \times 20\) with the following parameters:
\(n_\text{city}\) |
\(n_\text{inter}\) |
\(n_\text{inner}\) |
\(\Rightarrow\) |
\(r_\text{city}\) |
City Size |
|---|---|---|---|---|---|
3 |
2 |
1 |
3 |
\(7 \times 7\) |
We note that, for the sake of internal calculations, north and west are considered to be in positive direction. This means maps displaying the cells from \((0,0)\) at the top left are (counterintuitively) oriented such that south points up, north points down, west points right, and east points left.
City Placement#
Cities are placed randomly on the grid of given height and width. Coordinates with \(r_\text{city}+1\) distance to the borders or \(2 (r_\text{city}+1)\) to other city centers are excluded, such that cities do not overlap and have sufficient distance between each other and the borders. Generation stops when no cities are able to be placed anymore or \(n_\text{city}\) is reached.
In our example, this means that after 3 placed cities there is no valid place anymore to place another city. Hence, even if we planned more cities, the generator would not be able to place them. In fact, with a different random placement of the first cities, generation might break down even earlier.
Alternatively, if you want to place the cities evenly, setting the optional parameter grid_mode = True changes the generation to a different algorithm placing cities on a regular grid.
See below the steps of generating three cities (using the default grid_mode = False):
Inner and Outer Connection Points#
Inner and outer connection points are added at the borders of the cities on two opposing cardinal directions according to the orientation of the city, which is defined by the direction of its nearest neighbor (by Manhattan distance).
The number of pairs of inner tracks is randomly chosen between 1 and \(n_\text{inner}\) and defines the number of inner connection points per cardinal direction, which is 2 times this number (for \(n_\text{inner}=1\) it is always 2). The number of outer connections per cardinal direction is randomly chosen between 1 and \(n_\text{inter}\) (or the number of inner connection points if that’s lower). The inner connection points are offset such that parallel tracks have their inner connection points not in parallel.
See below how this plays out for our example:
Pathfinding between Cities#
For each city for each cardinal direction, the nearest city in that direction is found. If no city exists, the nearest one 90 degrees counterclockwise, then 90 degrees clockwise is chosen. In each city for each outer connection point the outer connection point with the closest (Manhattan) distance of the in this way found other city is identified. These two outer connection points of different cities are then connected via a rail path. This path is found using an A* path-finding algorithm with a Manhattan distance heuristic only allowing cells which are not part of cities and with a penalty on choosing cells which already contain rails (giving them \(+1\) distance cost in the heuristic). This process is repeated for each city and each outer connection point. As the rails placed in this way may overwrite existing rail paths, resulting in invalid transitions, some rail cells will need to be fixed later adding switches to allow for all necessary transitions.
See below how this plays out for our example:
Inner Cities & Train Stations#
In each city the inner connection points are connected by straight rail paths. Train stations are placed in the middle of these paths and parallel stations/tracks per city labeled starting from 0. Parallel tracks are connected with each other and inner connection points are connected to outer connection points in straight lines (where they both exist). The way these connections are built might result in invalid transitions which are then fixed later.
See below how this plays out for our example:
Fixing Transitions#
All cells that are in cities and part of the connections between cities are checked if their transitions are valid. Invalid transitions are fixed in the process creating switches. Resulting crossings might be level-free.
See below how this plays out for our example: