r"""
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 :math:`[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. https://github.com/aiAdrian/flatland_railway_extension/blob/e2b15bdd851ad32fb26c1a53f04621a3ca38fc00/flatland_railway_extension/FlatlandGraphBuilder.py
- Nygren, E., Eichenberger, Ch., Frejinger, E. Scope Restriction for Scalable Real-Time Railway Rescheduling: An Exploratory Study. https://arxiv.org/abs/2305.03574
"""
import ast
import math
from collections import defaultdict
from functools import lru_cache
from typing import Dict, Tuple, Optional, Set
import networkx as nx
from flatland.core.grid.grid4_utils import get_new_position
from flatland.core.transition_map import GridTransitionMap, TransitionMap, DEFAULT_EDGE_LENGTH, EdgeLength, validated_edge_length
from flatland.envs.rail_env import RailEnv
from flatland.envs.rail_env_action import RailEnvActions
GraphNode = str
GraphEdge = Tuple[GraphNode, GraphNode]
[docs]
class GraphTransitionMap(TransitionMap[GraphNode, GraphEdge, 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).
"""
def __init__(self, g: nx.DiGraph):
self.g = g
self.cell_in_pins = defaultdict(lambda: set())
# validated once, not per edge_length() call (called several times per agent and step)
self._edge_lengths: Dict[GraphEdge, EdgeLength] = {
(u, v): validated_edge_length(d["length"]) for u, v, d in g.edges(data=True) if "length" in d}
[docs]
@staticmethod
def grid_to_digraph(transition_map: GridTransitionMap) -> nx.DiGraph:
"""
The graph representation of a grid transition map.
Edge data
---------
actions : Set[str]
set of actions to go for this edge
length : EdgeLength, 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_actions : Set[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
-------
"""
g = nx.DiGraph()
for r in range(transition_map.height):
for c in range(transition_map.width):
for d in range(4):
u = GraphTransitionMap.grid_entry_point_to_graph_entry_point(r, c, d)
possible_transitions = transition_map.get_transitions(((r, c), d))
for new_direction in range(4):
if possible_transitions[new_direction]:
new_position = get_new_position((r, c), new_direction)
r2, c2 = new_position
d2 = new_direction
g.add_edge(u, GraphTransitionMap.grid_entry_point_to_graph_entry_point(r2, c2, d2))
if u not in g.nodes:
continue
g.nodes[u].setdefault("prohibited_actions", set())
for a in RailEnvActions:
t = transition_map.apply_action_independent(a, ((r, c), d))
if t is None:
g.nodes[u].setdefault("prohibited_actions", set()).add(a)
else:
(r2, c2), d2 = t
v = GraphTransitionMap.grid_entry_point_to_graph_entry_point(r2, c2, d2)
g[u][v].setdefault("actions", set()).add(a)
# Copy the grid's edge lengths here, so every conversion from the grid keeps them (GraphTransitionMap.from_rail_env
# and GraphRailEnv.from_rail_env). Only edges with a given length get the attribute. All other edges read as
# DEFAULT_EDGE_LENGTH (1) in edge_length(), as on the grid. A grid without edge lengths gives a graph without the
# attribute. Writing length 1 on every edge would make has_edge_lengths() True and switch the distance map from
# plain ints to the slower weighted path.
# Every key is an edge of g, as GridTransitionMap.edge_lengths only accepts edges of the grid.
gctgc = GraphTransitionMap.grid_entry_point_to_graph_entry_point
for (((r, c), d), ((r2, c2), d2)), length in (transition_map.edge_lengths or {}).items():
u = gctgc(r, c, d)
v = gctgc(r2, c2, d2)
assert g.has_edge(u, v), (u, v)
g.edges[u, v]["length"] = length
return g
[docs]
@staticmethod
def from_rail_env(env: RailEnv) -> "GraphTransitionMap":
"""
Factory method to create a graph transition map from a rail env.
Parameters
----------
env: RailEnv
Returns
-------
GraphTransitionMap
The graph transition map.
"""
return GraphTransitionMap(GraphTransitionMap.grid_to_digraph(env.rail))
[docs]
@lru_cache(maxsize=1_000_000)
def apply_action_independent(self, action: RailEnvActions, entry_point: GraphNode) -> Optional[GraphNode]:
if action in self.g.nodes[entry_point].get("prohibited_actions", set()):
return None
succs = list(self.g.successors(entry_point))
for v in succs:
edge_data = self.g.get_edge_data(entry_point, v)
if action in edge_data["actions"]:
return v
[docs]
def get_transitions(self, entry_point: GraphNode) -> Tuple[bool]:
return True,
[docs]
@staticmethod
@lru_cache
def grid_entry_point_to_graph_entry_point(r: int, c: int, d: int) -> str:
return f"{int(r), int(c), int(d)}"
[docs]
@staticmethod
@lru_cache
def graph_entry_point_to_grid_entry_point(s: str) -> Optional[Tuple[Tuple[int, int], int]]:
# pandas 3's default string dtype surfaces a missing entry to Series.map() as a raw float NaN
# (not None, which object-dtype columns passed) - see trajectories.py's normalize_position_read
# for the same guard against the same pandas behavior.
if s is None or (isinstance(s, float) and math.isnan(s)):
return None
r, c, d = ast.literal_eval(s)
return ((r, c), d)
[docs]
@lru_cache
def get_successor_entry_points(self, entry_point: GraphNode) -> Set[GraphNode]:
return set(self.g.successors(entry_point))
[docs]
@lru_cache
def get_predecessor_entry_points(self, entry_point: GraphNode) -> Set[GraphNode]:
return set(self.g.predecessors(entry_point))
[docs]
@lru_cache
def is_valid_entry_point(self, entry_point: GraphNode) -> bool:
return entry_point in self.g.nodes
[docs]
def edge_length(self, u: GraphNode, v: GraphNode) -> EdgeLength:
"""
The `length` attribute of the edge `(u, v)` (read when constructed), `DEFAULT_EDGE_LENGTH` if undefined.
"""
return self._edge_lengths.get((u, v), DEFAULT_EDGE_LENGTH)
[docs]
def has_edge_lengths(self) -> bool:
return bool(self._edge_lengths)
def _assert_actions_partitioned(self):
"""
For each node, every rail env action is either in the node's `prohibited_actions` or in the `actions` of exactly one
of its outgoing edges.
"""
for u in self.g.nodes:
for a in RailEnvActions:
prohibited = a in self.g.nodes[u].get("prohibited_actions", set())
edges = [v for v in self.g.successors(u) if a in self.g.edges[u, v].get("actions", set())]
assert prohibited + len(edges) == 1, f"{u}: action {a} prohibited={prohibited}, in actions of edges to {edges}"