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>, random_seed=None)[source]

Bases: flatland.core.transition_map.TransitionMap

Implements a TransitionMap over a 2D grid.

GridTransitionMap implements utility functions.

cell_neighbours_valid(self, 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(self, 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(self, 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)

fix_transitions(self, rcPos:List[Tuple[int, int]], direction:Tuple[int, int]=-1)[source]

Fixes broken transitions

get_full_transitions(self, row, column)[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(self, cell_id, transition_index)[source]

Return the status of whether an agent in cell cell_id can perform a movement along transition transition_index (e.g., the NESW direction of movement, for agents on a grid).

Parameters:
cell_id : tuple

The cell_id indices a cell as (column, row, orientation), where orientation is the direction an agent is facing within a cell.

transition_index : int

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 in the )

Validity of the requested transition (e.g., 0/1 allowed/not allowed, a probability in [0,1], etc…)

get_transitions(self, row, column, orientation)[source]

Return a tuple of transitions available in a cell specified by cell_id (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:
cell_id : tuple

The cell_id 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(self, 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(self, 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(self, 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:
package : string

Name of the package from which to load the transitions grid.

resource : string

Name of the file from which to load the transitions grid within the package.

override_gridsize : bool

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(self, filename)[source]

Save the transitions grid as filename, in npy format.

Parameters:
filename : string

Name of the file to which to save the transitions grid.

set_transition(self, cell_id, transition_index, new_transition, remove_deadends=False)[source]

Replaces the validity of transition to transition_index in cell cell_id’ with the new `new_transition.

Parameters:
cell_id : tuple

The cell_id indices a cell as (column, row, orientation), where orientation is the direction an agent is facing within a cell.

transition_index : int

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_transition : int 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(self, cell_id, new_transitions)[source]

Replaces the available transitions in cell cell_id with the tuple new_transitions’. `new_transitions must have one element for each possible transition.

Parameters:
cell_id : tuple

The cell_id 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_transitions : tuple

Tuple of new transitions validitiy for the cell.

validate_new_transition(self, 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_pos – The previous position we were checking
  • current_pos – The current position we are checking
  • new_pos – Possible child position we move into
  • end_pos – 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: object

Base TransitionMap class.

Generic class that implements a collection of transitions over a set of cells.

get_transition(self, cell_id, transition_index)[source]

Return the status of whether an agent in cell cell_id can perform a movement along transition transition_index (e.g., the NESW direction of movement, for agents on a grid).

Parameters:
cell_id : [cell identifier]

The cell_id object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.

transition_index : int

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(self, cell_id)[source]

Return a tuple of transitions available in a cell specified by cell_id (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:
cell_id : [cell identifier]

The cell_id object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.

Returns:
tuple

List of the validity of transitions in the cell.

set_transition(self, cell_id, transition_index, new_transition)[source]

Replaces the validity of transition to transition_index in cell cell_id’ with the new `new_transition.

Parameters:
cell_id : [cell identifier]

The cell_id object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.

transition_index : int

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_transition : 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…)

set_transitions(self, cell_id, new_transitions)[source]

Replaces the available transitions in cell cell_id with the tuple new_transitions’. `new_transitions must have one element for each possible transition.

Parameters:
cell_id : [cell identifier]

The cell_id object depends on the specific implementation. It generally is an int (e.g., an index) or a tuple of indices.

new_transitions : tuple

Tuple of new transitions validitiy for the cell.

flatland.core.transition_map.mirror(dir)[source]