flatland.envs.graph.rail_graph_transition_map module#

A Flatland (microscopic) topology can be represented by different kinds of graphs. The topology must reflect the possible paths through the rail network - it must not be possible to traverse a switch in the acute angle. With the help of the graph it is very easy to calculate the shortest connection from node A to node B. The API makes it possible to solve such tasks very efficiently. Moreover, the graph can be simplified so that only decision-relevant nodes remain in the graph and all other nodes are merged. A decision node is a node or flatland cell (track) that reasonably allows the agent to stop, go, or branch off. For straight track edges within a route, it makes little sense to wait in many situations. This is because the agent would block many resources, i.e., if an agent does not drive to the decision point: a cell before a crossing, the agent blocks the area in between. This makes little sense from an optimization point of view.

Two (dual, equivalent) approaches are possible: - agents are positioned on the nodes - agents are positioned on the edges. The second approach makes it easier to visualize agents moving forward on edges. Hence, we choose the second approach.

Our directed graph consists of nodes and edges: * A node in the graph is defined by position and direction. The position corresponds to the position of the underlying cell in the original flatland topology, and the direction corresponds to the direction in which an agent reaches the cell. Thus, the node is defined by (r, c, d), where c (column) is the index of the horizontal cell grid position, r (row) is the index of the vertical cell grid position, and d (direction) is the direction of cell entry. In the Flatland (2d grid), not every of the eight neighbor cells can be reached from every direction. Therefore, the entry direction information is key. * An edge is defined by “from-node” u and “to-node” v such that for the edge e = (u, v). Edges reflect feasible transition from node u to node v. We can think of the suggestive notation \([u,v)\) in terms of resource occupation of the underlying cell, as the “from-node” refers to the underlying cell entered, and the “to-node” refers to the neighbor cell entered when the edge is left.

The implementation uses networkX, so there are also many graph functions available.

References: - Egli, Adrian. FlatlandGraphBuilder. aiAdrian/flatland_railway_extension - Nygren, E., Eichenberger, Ch., Frejinger, E. Scope Restriction for Scalable Real-Time Railway Rescheduling: An Exploratory Study. https://arxiv.org/abs/2305.03574

class flatland.envs.graph.rail_graph_transition_map.GraphTransitionMap(g: DiGraph)[source]#

Bases: TransitionMap[str, Tuple[str, str], bool, RailEnvActions]

Flatland 3 Transition map represented by a directed graph.

The grid transition map contains for all cells a set of pairs (heading at cell entry, heading at cell exit).

E.g. horizontal straight is {(E,E), (W,W)}.

The directed graph’s nodes are entry pins (cell + plus heading at entry). Edges always go from entry pin at one cell to entry pin of a neighboring cell. The outgoing heading for the grid transition map is the incoming heading at a neighboring cell.

Incoming heading:

S ⌄ |

E >—+—< W

^ N

Outgoing heading (=incoming at neighbor cell):

N (of cell-to-the-north) ^ |

E <—+—> E (of cell-to-the-east)

(of cell-to- |
the-east) ⌄

S (of cell-to-the-south)

Attributes#

g: nx.DiGraph

The optional length edge attributes are read when constructed (see edge_length()) - like the lru_cache’d topology methods, they must not change on g afterwards (see GridTransitionMap.edge_lengths for the intended workflow).

apply_action_independent(action: RailEnvActions, entry_point: str) → str | None[source]#

Apply the action on the train regardless of locations of other agents. Checks for valid cells to move and valid rail transitions.

Parameters#

action[ActionsT]

Action to execute

entry_point[EntryPointT]

position and orientation

Returns#

entry_pointOptional[EntryPointT]

the next entry_point (cell + entry direction), or None if the action has no valid transition

edge_length(u: str, v: str) → int | Fraction[source]#

The length attribute of the edge (u, v) (read when constructed), DEFAULT_EDGE_LENGTH if undefined.

static from_rail_env(env: RailEnv) → GraphTransitionMap[source]#

Factory method to create a graph transition map from a rail env.

Parameters#

env: RailEnv

Returns#

GraphTransitionMap

The graph transition map.

get_predecessor_entry_points(entry_point: str) → Set[str][source]#
get_successor_entry_points(entry_point: str) → Set[str][source]#
get_transitions(entry_point: str) → Tuple[bool][source]#

Return a tuple of transitions available in a cell specified by entry_point (e.g., a tuple of size of the maximum number of transitions, with values 0 or 1, or potentially in between, for stochastic transitions).

Parameters#

entry_point

Returns#

tuple

List of the validity of transitions in the cell.

static graph_entry_point_to_grid_entry_point(s: str) → Tuple[Tuple[int, int], int] | None[source]#
static grid_entry_point_to_graph_entry_point(r: int, c: int, d: int) → str[source]#
static grid_to_digraph(transition_map: GridTransitionMap) → DiGraph[source]#

The graph representation of a grid transition map.

Edge data#

actionsSet[str]

set of actions to go for this edge

lengthEdgeLength, optional

length of this edge (int or Fraction, at least MIN_EDGE_LENGTH), see edge_length(). Design decision: the attribute is not enforced on every edge. An edge without it has DEFAULT_EDGE_LENGTH (1), as an edge missing from GridTransitionMap.edge_lengths. So graphs without lengths stay valid unchanged, and only edges that differ from the default need the attribute. grid_to_digraph copies GridTransitionMap.edge_lengths onto it; edges without a length there get none.

Node data#

prohibited_actionsSet[str]

set of actions that must not be chosen when passing this node

Prohibited actions and all edge actions must be pairwise disjoint and cover all 5 Flatland actions.

Parameters#

transition_map

Returns#

has_edge_lengths() → bool[source]#

Whether any edge may have a non-default length - if not, consumers like the distance map can use plain integer unit lengths (faster than Fraction arithmetic).

is_valid_entry_point(entry_point: str) → bool[source]#

Whether the map contains the entry_point (a cell plus entry direction - a position+direction tuple for grid, a node id encoding the same for graph).

Parameters#

entry_pointEntryPointT

the entry_point to check

Returns#

bool

whether the entry_point is valid