flatland.core.grid.grid4 module¶
-
class
flatland.core.grid.grid4.
Grid4Transitions
(transitions)[source]¶ Bases:
flatland.core.transitions.Transitions
Grid4Transitions class derived from Transitions.
Special case of Transitions over a 2D-grid (FlatLand). Transitions are possible to neighboring cells on the grid if allowed. GridTransitions keeps track of valid transitions supplied as transitions list, each represented as a bitmap of 16 bits.
Whether a transition is allowed or not depends on which direction an agent inside the cell is facing (0=North, 1=East, 2=South, 3=West) and which direction the agent wants to move to (North, East, South, West, relative to the cell). Each transition (orientation, direction) can be allowed (1) or forbidden (0).
For example, in case of no diagonal transitions on the grid, the 16 bits of the transition bitmaps are organized in 4 blocks of 4 bits each, the direction that the agent is facing. E.g., the most-significant 4-bits represent the possible movements (NESW) if the agent is facing North, etc…
agent’s direction: North East South West agent’s allowed movements: [nesw] [nesw] [nesw] [nesw] example: 1000 0000 0010 0000
In the example, the agent can move from North to South and viceversa.
-
get_transition
(self, cell_transition, orientation, direction)[source]¶ Get the transition bit (1 value) that determines whether an agent oriented in direction orientation and inside a cell with transitions cell_transition’ can move to the cell in direction `direction relative to the current cell.
Parameters: - cell_transition : int
16 bits used to encode the valid transitions for a cell.
- orientation : int
Orientation of the agent inside the cell.
- direction : int
Direction of movement whose validity is to be tested.
Returns: - int
Validity of the requested transition: 0/1 allowed/not allowed.
-
get_transitions
(self, cell_transition, orientation)[source]¶ Get the 4 possible transitions ((N,E,S,W), 4 elements tuple if no diagonal transitions allowed) available for an agent oriented in direction orientation and inside a cell with transitions cell_transition.
Parameters: - cell_transition : int
16 bits used to encode the valid transitions for a cell.
- orientation : int
Orientation of the agent inside the cell.
Returns: - tuple
List of the validity of transitions in the cell.
-
has_deadend
(self, cell_transition)[source]¶ Checks if one entry can only by exited by a turn-around.
-
rotate_transition
(self, cell_transition, rotation=0)[source]¶ Clockwise-rotate a 16-bit transition bitmap by rotation={0, 90, 180, 270} degrees.
Parameters: - cell_transition : int
16 bits used to encode the valid transitions for a cell.
- rotation : int
Angle by which to clock-wise rotate the transition bits in cell_transition by. I.e., rotation={0, 90, 180, 270} degrees.
Returns: - int
An updated bitmap that replaces the original transitions bits with the equivalent bitmap after rotation.
-
set_transition
(self, cell_transition, orientation, direction, new_transition, remove_deadends=False)[source]¶ Set the transition bit (1 value) that determines whether an agent oriented in direction orientation and inside a cell with transitions cell_transition’ can move to the cell in direction `direction relative to the current cell.
Parameters: - cell_transition : int
16 bits used to encode the valid transitions for a cell.
- orientation : int
Orientation of the agent inside the cell.
- direction : int
Direction of movement whose validity is to be tested.
- new_transition : int
Validity of the requested transition: 0/1 allowed/not allowed.
- remove_deadends – boolean, default False
remove all deadend transitions.
- Returns
- ——-
- int
An updated bitmap that replaces the original transitions validity of cell_transition’ with `new_transitions, for the appropriate orientation.
-
set_transitions
(self, cell_transition, orientation, new_transitions)[source]¶ Set the possible transitions (e.g., (N,E,S,W), 4 elements tuple if no diagonal transitions allowed) available for an agent oriented in direction orientation and inside a cell with transitions cell_transition’. A new `cell_transition is returned with the specified bits replaced by new_transitions.
Parameters: - cell_transition : int
16 bits used to encode the valid transitions for a cell.
- orientation : int
Orientation of the agent inside the cell.
- new_transitions : tuple
Tuple of new transitions validitiy for the cell.
Returns: - int
An updated bitmap that replaces the original transitions validity of cell_transition’ with `new_transitions, for the appropriate orientation.
-