flatland.envs.rail_env_shortest_paths module¶
-
flatland.envs.rail_env_shortest_paths.
get_action_for_move
(agent_position:Tuple[int, int], agent_direction:flatland.core.grid.grid4.Grid4TransitionsEnum, next_agent_position:Tuple[int, int], next_agent_direction:int, rail:flatland.core.transition_map.GridTransitionMap) → Union[flatland.envs.rail_env.RailEnvActions, NoneType][source]¶ Get the action (if any) to move from a position and direction to another.
TODO https://gitlab.aicrowd.com/flatland/flatland/issues/299 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:flatland.envs.rail_env.RailEnv, source_position:Tuple[int, int], source_direction:int, target_position=typing.Tuple[int, int], k:int=1, debug=False) → List[Tuple[flatland.envs.rail_trainrun_data_structures.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:flatland.core.grid.grid4.Grid4TransitionsEnum, action:flatland.envs.rail_env.RailEnvActions, rail:flatland.core.transition_map.GridTransitionMap) → Tuple[int, int, int][source]¶ Get the next position for this action.
TODO https://gitlab.aicrowd.com/flatland/flatland/issues/299 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:flatland.envs.distance_map.DistanceMap, max_depth:Union[int, NoneType]=None, agent_handle:Union[int, NoneType]=None) → Dict[int, Union[List[flatland.envs.rail_trainrun_data_structures.Waypoint], NoneType]][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:flatland.core.grid.grid4.Grid4TransitionsEnum, agent_position:Tuple[int, int], rail:flatland.core.transition_map.GridTransitionMap) → Set[flatland.envs.rail_env.RailEnvNextAction][source]¶ Get the valid move actions (forward, left, right) for an agent.
TODO https://gitlab.aicrowd.com/flatland/flatland/issues/299 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.