flatland.envs.rail_env_shortest_paths module#
- flatland.envs.rail_env_shortest_paths.get_action_for_move(agent_position: Tuple[int, int], agent_direction: Grid4TransitionsEnum, next_agent_position: Tuple[int, int], next_agent_direction: int, rail: GridTransitionMap) RailEnvActions | None [source]#
Get the action (if any) to move from a position and direction to another.
- TODO The implementation could probably be more efficient and more elegant,
but given the few calls this has no priority now.
Parameters#
agent_position agent_direction next_agent_position next_agent_direction rail
Returns#
- Optional[RailEnvActions]
the action (if direct transition possible) or None.
- flatland.envs.rail_env_shortest_paths.get_k_shortest_paths(env, source_position: Tuple[int, int], source_direction: int, target_position=typing.Tuple[int, int], k: int = 1, debug=False) List[Tuple[Waypoint]] [source]#
Computes the k shortest paths using modified Dijkstra following pseudo-code https://en.wikipedia.org/wiki/K_shortest_path_routing In contrast to the pseudo-code in wikipedia, we do not a allow for loopy paths.
Parameters#
env : RailEnv source_position: Tuple[int,int] source_direction: int target_position: Tuple[int,int] k : int
max number of shortest paths
- debug: bool
print debug statements
Returns#
- List[Tuple[WalkingElement]]
We use tuples since we need the path elements to be hashable. We use a list of paths in order to keep the order of length.
- flatland.envs.rail_env_shortest_paths.get_new_position_for_action(agent_position: Tuple[int, int], agent_direction: Grid4TransitionsEnum, action: RailEnvActions, rail: GridTransitionMap) Tuple[int, int, int] [source]#
Get the next position for this action.
- TODO The implementation could probably be more efficient and more elegant,
but given the few calls this has no priority now.
Parameters#
agent_position agent_direction action rail
Returns#
- Tuple[int,int,int]
row, column, direction
- flatland.envs.rail_env_shortest_paths.get_shortest_paths(distance_map: DistanceMap, max_depth: int | None = None, agent_handle: int | None = None) Dict[int, List[Waypoint] | None] [source]#
Computes the shortest path for each agent to its target and the action to be taken to do so. The paths are derived from a DistanceMap.
If there is no path (rail disconnected), the path is given as None. The agent state (moving or not) and its speed are not taken into account
- example:
agent_fixed_travel_paths = get_shortest_paths(env.distance_map, None, agent.handle) path = agent_fixed_travel_paths[agent.handle]
Parameters#
distance_map : reference to the distance_map max_depth : max path length, if the shortest path is longer, it will be cutted agent_handle : if set, the shortest for agent.handle will be returned , otherwise for all agents
Returns#
Dict[int, Optional[List[WalkingElement]]]
- flatland.envs.rail_env_shortest_paths.get_valid_move_actions_(agent_direction: Grid4TransitionsEnum, agent_position: Tuple[int, int], rail: GridTransitionMap) Set[RailEnvNextAction] [source]#
Get the valid move actions (forward, left, right) for an agent.
- TODO The implementation could probably be more efficient and more elegant,
but given the few calls this has no priority now.
Parameters#
agent_direction : Grid4TransitionsEnum agent_position: Tuple[int,int] rail : GridTransitionMap
Returns#
- Set of RailEnvNextAction (tuples of (action,position,direction))
Possible move actions (forward,left,right) and the next position/direction they lead to. It is not checked that the next cell is free.
- flatland.envs.rail_env_shortest_paths.visualize_distance_map(distance_map: DistanceMap, agent_handle: int = 0)[source]#