flatland.core.transition_map module#
TransitionMap and derived classes.
- class flatland.core.transition_map.GridTransitionMap(width, height, transitions: ~flatland.core.transitions.Transitions = <flatland.core.grid.grid4.Grid4Transitions object>, grid: ~numpy.ndarray | None = None)[source]#
Bases:
TransitionMap
[Tuple
[Tuple
[int
,int
],int
],Grid4Transitions
,Tuple
[bool
],Any
],Generic
[ActionsType
]Implements a TransitionMap over a 2D grid.
- cell_neighbours_valid(rcPos: List[Tuple[int, int]], check_this_cell=False)[source]#
Check validity of cell at rcPos = tuple(row, column) Checks that: - surrounding cells have inbound transitions for all the outbound transitions of this cell.
These are NOT checked - see transition.is_valid: - all transitions have the mirror transitions (N->E <=> W->S) - Reverse transitions (N -> S) only exist for a dead-end - a cell contains either no dead-ends or exactly one
Returns: True (valid) or False (invalid)
- check_path_exists(start: List[Tuple[int, int]], direction: int, end: List[Tuple[int, int]])[source]#
Breath first search for a possible path from one node with a certain orientation to a target node. :param start: Start cell rom where we want to check the path :param direction: Start direction for the path we are testing :param end: Cell that we try to reach from the start cell :return: True if a path exists, False otherwise
- fix_neighbours(rcPos: List[Tuple[int, int]], check_this_cell=False)[source]#
Check validity of cell at rcPos = tuple(row, column) Checks that: - surrounding cells have inbound transitions for all the outbound transitions of this cell.
These are NOT checked - see transition.is_valid: - all transitions have the mirror transitions (N->E <=> W->S) - Reverse transitions (N -> S) only exist for a dead-end - a cell contains either no dead-ends or exactly one
Returns: True (valid) or False (invalid)
- get_full_transitions(row, column) UnderlyingTransitionsType [source]#
Returns the full transitions for the cell at (row, column) in the format transition_map’s transitions.
Parameters#
row: int column: int
(row,column) specifies the cell in this transition map.
Returns#
- self.transitions.get_type()
The cell content int the format of this map’s Transitions.
- get_transition(configuration: Tuple[Tuple[int, int], int], transition_index)[source]#
Return the status of whether an agent in cell configuration can perform a movement along transition transition_index (e.g., the NESW direction of movement, for agents on a grid).
Parameters#
- configuration[cell identifier]
The configuration object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.
- transition_indexint
Index of the transition to probe, as index in the tuple returned by get_transitions(). e.g., the NESW direction of movement, for agents on a grid.
Returns#
- int or float (depending on Transitions used)
Validity of the requested transition (e.g., 0/1 allowed/not allowed, a probability in [0,1], etc…)
- get_transitions(configuration: Tuple[Tuple[int, int], int]) Tuple[Grid4Transitions] [source]#
Return a tuple of transitions available in a cell specified by configuration (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#
- configurationtuple
The configuration indices a cell as ((column, row), orientation), where orientation is the direction an agent is facing within a cell. Alternatively, it can be accessed as (column, row) to return the full cell content.
Returns#
- tuple
List of the validity of transitions in the cell as given by the maps transitions.
- is_dead_end(rcPos: List[Tuple[int, int]])[source]#
Check if the cell is a dead-end.
Parameters#
- rcPos: Tuple[int,int]
tuple(row, column) with grid coordinate
Returns#
- boolean
True if and only if the cell is a dead-end.
- is_simple_turn(rcPos: List[Tuple[int, int]])[source]#
Check if the cell is a left/right simple turn
Parameters#
- rcPos: Tuple[int,int]
tuple(row, column) with grid coordinate
Returns#
- boolean
True if and only if the cell is a left/right simple turn.
- load_transition_map(package, resource)[source]#
Load the transitions grid from filename (npy format). The load function only updates the transitions grid, and possibly width and height, but the object has to be initialized with the correct transitions object anyway.
Parameters#
- packagestring
Name of the package from which to load the transitions grid.
- resourcestring
Name of the file from which to load the transitions grid within the package.
- override_gridsizebool
If override_gridsize=True, the width and height of the GridTransitionMap object are replaced with the size of the map loaded from filename. If override_gridsize=False, the transitions grid is either cropped (if the grid size is larger than (height,width) ) or padded with zeros (if the grid size is smaller than (height,width) )
- save_transition_map(filename)[source]#
Save the transitions grid as filename, in npy format.
Parameters#
- filenamestring
Name of the file to which to save the transitions grid.
- set_transition(configuration: Tuple[Tuple[int, int], int], transition_index, new_transition, remove_deadends=False)[source]#
Replaces the validity of transition to transition_index in cell configuration’ with the new `new_transition.
Parameters#
- configurationtuple
The configuration indices a cell as (column, row, orientation), where orientation is the direction an agent is facing within a cell.
- transition_indexint
Index of the transition to probe, as index in the tuple returned by get_transitions(). e.g., the NESW direction of movement, for agents on a grid.
- new_transitionint or float (depending on Transitions used in the map.)
Validity of the requested transition (e.g., 0/1 allowed/not allowed, a probability in [0,1], etc…)
- set_transitions(configuration: Tuple[int, int], new_transitions: Transitions)[source]#
Replaces the available transitions in cell configuration with the tuple new_transitions’. `new_transitions must have one element for each possible transition.
Parameters#
- configurationtuple
The configuration indices a cell as (column, row, orientation), where orientation is the direction an agent is facing within a cell. Alternatively, it can be accessed as (column, row) to replace the full cell content.
- new_transitionstuple
Tuple of new transitions validitiy for the cell.
- validate_new_transition(prev_pos: Tuple[int, int], current_pos: Tuple[int, int], new_pos: Tuple[int, int], end_pos: Tuple[int, int])[source]#
Utility function to test that a path drawn by a-start algorithm uses valid transition objects. We us this to quide a-star as there are many transition elements that are not allowed in RailEnv
Parameters#
- prev_posIntVector2D
The previous position we were checking
- current_posIntVector2D
The current position we are checking
- new_posIntVector2D
Possible child position we move into
- end_posIntVector2D
End cell of path we are drawing
Returns#
True if the transition is valid, False if transition element is illegal
- class flatland.core.transition_map.TransitionMap[source]#
Bases:
Generic
[ConfigurationType
,UnderlyingTransitionsType
,UnderlyingTransitionsValidityType
,ActionsType
]Base TransitionMap class.
Generic class that implements a collection of transitions over a set of cells.
- check_action_on_agent(action: ActionsType, configuration: ConfigurationType) Tuple[bool, ConfigurationType, bool, ActionsType] [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[ActionsType]
Action to execute
- configurationConfigurationType
position and orientation
Returns#
- new_cell_valid: bool
is the new position and direction valid (i.e. is it within bounds and does it have > 0 outgoing transitions)
- new_position: [ConfigurationType]
New position after applying the action
- transition_valid: bool
Whether the transition from old and direction is defined in the grid.
- preprocessed_action: [ActionType]
Corrected action if not transition_valid.
- get_transition(configuration: ConfigurationType, transition_index: int) UnderlyingTransitionsValidityType [source]#
Return the status of whether an agent in cell configuration can perform a movement along transition transition_index (e.g., the NESW direction of movement, for agents on a grid).
Parameters#
- configuration[cell identifier]
The configuration object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.
- transition_indexint
Index of the transition to probe, as index in the tuple returned by get_transitions(). e.g., the NESW direction of movement, for agents on a grid.
Returns#
- int or float (depending on Transitions used)
Validity of the requested transition (e.g., 0/1 allowed/not allowed, a probability in [0,1], etc…)
- get_transitions(configuration: ConfigurationType) Tuple[UnderlyingTransitionsValidityType] [source]#
Return a tuple of transitions available in a cell specified by configuration (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#
configuration
Returns#
- tuple
List of the validity of transitions in the cell.
- set_transition(configuration: ConfigurationType, transition_index, new_transition)[source]#
Replaces the validity of transition to transition_index in cell configuration’ with the new `new_transition.
Parameters#
- configuration[cell identifier]
The configuration object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.
- transition_indexint
Index of the transition to probe, as index in the tuple returned by get_transitions(). e.g., the NESW direction of movement, for agents on a grid.
- new_transitionint or float (depending on Transitions used)
Validity of the requested transition (e.g., 0/1 allowed/not allowed, a probability in [0,1], etc…)
- set_transitions(configuration: ConfigurationType, new_transitions: UnderlyingTransitionsType)[source]#
Replaces the available transitions in cell configuration with the tuple new_transitions’. `new_transitions must have one element for each possible transition.
Parameters#
- configuration[ConfigurationType]
The configuration object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.
- new_transitions[TransitionsType]
Tuple of new transitions validitiy for the cell.