flatland.graphs.graph_utils module#
- class flatland.graphs.graph_utils.PathInfo(nStart, nTarget, length, tPath, Gpath)#
Bases:
tuple
- Gpath#
Alias for field number 4
- length#
Alias for field number 2
- nStart#
Alias for field number 0
- nTarget#
Alias for field number 1
- tPath#
Alias for field number 3
- class flatland.graphs.graph_utils.RailEnvGraph(env)[source]#
Bases:
object
Represent a RailEnv with a NetworkX DiGraph:
Node types: - “grid” nodes, rows x cols, connected in a lattice / grid. eg (2,3)=row2, col3 - “rail” nodes attached to grid nodes, one for each direction.
Edge types: - “grid” edges between grid nodes, to give the grid structure - “hold” edges to hold a rail to a grid point,
and to represent the resource occupied by an agent moving in any direction
“dir” edges (directional) between rail nodes
So a RailEnv cell has a grid node showing its location, and two or more rail nodes representing the direction of entry, eg (2,3,0) means row 2, col 3, entry direction north (ie from the south)
An agent moves along “rail” edges between rail nodes, but occupies the whole grid node ie the whole complex of {grid node - hold edges - rail nodes}
- graph_rail_grid()[source]#
returns a NX graph of rails only; includes: - grid nodes with rails - grid edges between grid nodes along rails (but not between adjacent rails) - rail nodes - hold edges - dir edges Excludes: - Grid nodes associated with empty rails - grid edges with empty grid nodes
- reduce_simple_paths()[source]#
Reduce linear paths, ie unbranched chains, into a single node, preserving length in the dir edges “l” property, and also in an “l” property of (contracted) grid nodes. This function seems unnecessarily complicated! Possibly a result of the data/graph model, or maybe naive coding.
After this, rail nodes may end up connected to different grid nodes via their hold edge. (Not sure how)
- flatland.graphs.graph_utils.calcConflict_unused(G, lPI, nSteps=500, twostep=False)[source]#
Create two reservation tables: - dResource - dict[grid node] -> resource usage at step t (0,1,…) - dg2Dirs - dict[grid node] -> NESW x bool usage at step t (0 or 1)
- flatland.graphs.graph_utils.genStartTargetDirs(G, env, shortest=True)[source]#
find the possible directions for each agent in an env. Flatland does not currently define an initial direction for an agent - the agent gets to choose. Nor does it define a final direction. However some combinations of initial and final direction may not be possible. This function finds the possible directions.
- flatland.graphs.graph_utils.getHammockFromPath(G, oPI: PathInfo, endSkip=0, ratioMax=10.0) List [source]#
Get the hammock of paths from a single PathInfo
- flatland.graphs.graph_utils.getPathsForArrivalDirs(G, rcdStart, rcTarg)[source]#
(recent addition) return the oPI for the shortest path(s) from the start to target. Returns a list of paths, one for each feasible arrival direction at the target (usually returns a list of two paths).
- flatland.graphs.graph_utils.get_simple_path(G, u)[source]#
Follow a linear path in G starting at u, terminating as soon as there is more than (or less than) 1 successor, ie when the linear section ends. The branching node is not included.
- flatland.graphs.graph_utils.grid_node_for_rails(G, lnRails)[source]#
Return the grid nodes for a bunch of rail nodes (eg a path of rail nodes)
- flatland.graphs.graph_utils.hammockPaths(G, nStart, nTarget, endSkip=0, preamble=True, ratioMax=10)[source]#
Return the “diversion” paths generated by taking the diversion at each junction along the shortest path, and following the shortest path from there. preamble: include preamble leading up to the decision point
- flatland.graphs.graph_utils.neighbors(G, nbunch, edge_types=None, outer_edge_types=None)[source]#
From a graph G, and nodes nbunch, return the list of nodes who are neighbors of nbunch via edges of types edge_types. Include those edges, and edges between the neighbors of type types outer_edge_types, in the second return value.
*Unfinished*
- flatland.graphs.graph_utils.plotGraphEnv(G, env: RailEnv, aImg, space=0.3, figsize=(8, 8), show_labels=(), show_edges='dir', show_edge_weights=False, show_nodes='all', node_colors=None, edge_colors=None, alpha_img=0.2, node_size=300, lvHighlight=None, arrowsize=10)[source]#
- flatland.graphs.graph_utils.plotPath(G: DiGraph, env: RailEnv, aImg, tPath, lvHighlight=None)[source]#
- flatland.graphs.graph_utils.plotResourceUsage(G, llnPaths, llnAltPaths=None, nSteps=500, nStepsShow=200, contradir=False, nResources=50, figsize=(20, 8), twostep=False, node_ticks=False, agent_increment=False, vmax=3, grid=True, cmap=None)[source]#
Create two reservation tables: - dResource - dict[grid node] -> resource usage at step t (0,1,…) - dg2Dirs - dict[grid node] -> NESW x bool usage at step t (0 or 1)
Plots an Ibry (or Ibry-Serjev?) diagram of resource usage through time.
- flatland.graphs.graph_utils.trans_int_to_4x4(intTrans)[source]#
Turn a transition into a 4x4 array of 0s and 1s