Maze¶
This module provides a base class for mazes. It defines the basic structure and methods for a maze, which is a specific type of graph. In a maze, each vertex is placed on a grid and can only be connected along the cardinal directions.
- class Maze.Maze(width=None, height=None, *args, **kwargs)[source]¶
(This class inherits from
Graph
).A maze is a particular type of graph. Each vertex is a cell, indexed by a number from 0 to
width*height-1
. There are edges between adjacent cells. Weights indicate the number of actions required to go from one cell to an adjacent one. In this implementation, cells are placed on a grid and can only be connected along the cardinal directions.- __init__(width=None, height=None, *args, **kwargs)[source]¶
(This class is abstract and meant to be subclassed, not instantiated directly).
Initializes a new instance of the class.
- Parameters:
width (
int
|None
) – Width of the maze, can beNone
if determined later.height (
int
|None
) – Height of the maze, can beNone
if determined later.*args (
object
) – Additional arguments to pass to the parent constructor.**kwargs (
object
) – Additional keyword arguments to pass to the parent constructor.
- add_edge(vertex_1, vertex_2, weight=1)[source]¶
(This method redefines the method of the parent class with the same name).
Adds an edge between two vertices in the maze. Here, we want edges to link only cells that are above or below. Also, weights should be positive integers. Edges are symmetric by default.
- Parameters:
vertex_1 (
int
) – First vertex.vertex_2 (
int
) – Second vertex.weight (
int
) – Weight of the edge.
- Return type:
None
- add_vertex(vertex)[source]¶
(This method redefines the method of the parent class with the same name).
Adds a vertex to the maze. Only integer vertices are allowed in a maze.
- Parameters:
vertex (
int
) – Vertex to add.- Return type:
None
- as_numpy_ndarray()[source]¶
(This method redefines the method of the parent class with the same name).
Returns a
numpy.ndarray
representing the graph. Entries are given in order of the indices of the vertices.- Return type:
object
- Returns:
A
numpy.ndarray
representing the adjacency matrix (return type isobject
to allownumpy
to be optional).
- as_torch_tensor()[source]¶
(This method redefines the method of the parent class with the same name).
Returns a
torch.tensor
representing the graph. Entries are given in order of the indices of the vertices.- Return type:
object
- Returns:
A
torch.tensor
representing the adjacency matrix (return type isAny
to allowtorch
to be optional).
- coords_difference(vertex_1, vertex_2)[source]¶
Computes the difference between the coordinates of two cells.
- Parameters:
vertex_1 (
int
) – First cell.vertex_2 (
int
) – Second cell.
- Return type:
tuple
[int
,int
]- Returns:
Tuple containing
(row_diff, col_diff)
difference between the rows and columns of the cells.
- get_height()[source]¶
Returns the height of the maze. This is the number of cells in a column.
- Return type:
int
- Returns:
Height of the maze, in number of cells.
- get_width()[source]¶
Returns the width of the maze. This is the number of cells in a row.
- Return type:
int
- Returns:
Width of the maze, in number of cells.
- i_exists(index)[source]¶
Checks if a given index is a valid cell in the maze.
- Parameters:
index (
int
) – Index of the cell.- Return type:
bool
- Returns:
True
if the cell exists,False
otherwise.
- i_to_rc(index)[source]¶
Transforms a maze index into a
(row, col)
pair. Does not check if the cell exists.- Parameters:
index (
int
) – Index of the cell.- Return type:
tuple
[int
,int
]- Returns:
Tuple containing
(row, col)
of the cell.
- rc_exists(row, col)[source]¶
Checks if a given
(row, col)
pair corresponds to a valid cell in the maze.- Parameters:
row (
int
) – Row of the cell.col (
int
) – Column of the cell.
- Return type:
bool
- Returns:
True
if the cell exists,False
otherwise.
- rc_to_i(row, col)[source]¶
Transforms a
(row, col)
pair of maze coordinates (lexicographic order) into a maze index. Does not check if the cell exists.- Parameters:
row (
int
) – Row of the cell.col (
int
) – Column of the cell.
- Return type:
int
- Returns:
Corresponding cell index in the maze.
- locations_to_action(source, target)[source]¶
Transforms two locations into the action required to reach the target from the source.
- Parameters:
source (
int
) – Vertex where the player is.target (
int
) – Vertex where the character wants to go.
- Return type:
Action
|None
- Returns:
Value of the
Action
enumeration to go from the source to the target, orNone
if the move is impossible.
- locations_to_actions(locations)[source]¶
Transforms a list of locations into a list of actions to go from one location to the next.
- Parameters:
locations (
list
[int
]) – List of vertices to go through.- Return type:
list
[Action
|None
]- Returns:
Series of values from the
Action
enumeration to go from one location to the next.