Maze

DiscreteMazePOMDP — a generated maze, one-cell moves.

One recorded episode planned by PFT-DPW, replayed in 3D. Drag to orbit, scroll to zoom, and use the bar to play, scrub and switch camera.

ContinuousMazePOMDP — the same maze, with bounded displacements.

One recorded episode planned by PFT-DPW, replayed in 3D. Drag to orbit, scroll to zoom, and use the bar to play, scrub and switch camera.

TMazePOMDP — a T-shaped corridor.

One recorded episode planned by PFT-DPW, replayed in 3D. Drag to orbit, scroll to zoom, and use the bar to play, scrub and switch camera.

These three environments test memory of a noisy, single-use cue. The map is known and movement is deterministic; only the rewarding goal side is hidden. Entering either goal ends the episode, but only the correct goal counts as success. The cue names that side with probability cue_accuracy; later observations are empty.

What the agent sees and does

  • State — a float64 array [x, y, goal, cue_phase]: the position, the goal side (0.0 left, 1.0 right) and the cue phase (0.0 unseen, 1.0 emitting, 2.0 consumed). x and y are integer cells in the discrete variants and real numbers in the continuous one.

  • Actions — discrete "up", "down", "left", "right" for DiscreteMazePOMDP and TMazePOMDP; continuous [dx, dy], at most max_step_size long, for ContinuousMazePOMDP.

  • Observations (discrete) — "left_cue", "right_cue", "empty". Only the step that crosses the cue cell can return a cue.

Formal definition

The environment is the POMDP \(\langle S, A, Z, T, O, R, b_0, \gamma \rangle\). All three variants share one model and differ only in \(A\) and how a move is resolved.

State space. Let \(G\) be the walkable cells. Position, the hidden goal side, and the cue’s delivery phase:

\[s = (x,\; y,\; \mathrm{side},\; \mathrm{phase})\]
\[S = \mathcal{P} \times \{\textsf{L}, \textsf{R}\} \times \{\textsf{UNSEEN}, \textsf{EMITTING}, \textsf{CONSUMED}\}\]

The symbols are:

  • \((x, y)\) — the agent’s position.

  • \(\mathcal{P} = G\) for the discrete variants, and \(\mathcal{P} \subseteq \mathbb{R}^2\) the walkable region for the continuous one (a point belongs to the cell it rounds to).

  • \(\mathrm{side}\) — the goal that pays, left (\(\textsf{L}\)) or right (\(\textsf{R}\)).

  • \(\mathrm{phase}\) — whether the cue is not yet seen (\(\textsf{UNSEEN}\)), being read this step (\(\textsf{EMITTING}\)), or used up (\(\textsf{CONSUMED}\)).

Action space.

\[\begin{split}A = \begin{cases} \{\textsf{up}, \textsf{down}, \textsf{left}, \textsf{right}\} & \texttt{DiscreteMazePOMDP},\ \texttt{TMazePOMDP} \\ \{d \in \mathbb{R}^2 : \lVert d \rVert_2 \leq \texttt{max\_step\_size}\} & \texttt{ContinuousMazePOMDP} \end{cases}\end{split}\]

A discrete action moves one cell:

  • up: \(y + 1\)

  • down: \(y - 1\)

  • left: \(x - 1\)

  • right: \(x + 1\)

A continuous action is the displacement \(d\) itself; a longer one is rescaled to the cap rather than rejected.

Observation space

\[Z = \{\textsf{left\_cue},\; \textsf{right\_cue},\; \textsf{empty}\}\]

Transition model. Deterministic, and a goal is absorbing:

\[T(s' \mid s, a) = \mathbb{1}[s' = f(s, a)], \qquad f(s, a) = s \ \text{ for } s \in S_T\]

The position update refuses illegal moves without moving the agent. The offsets for the discrete variants are:

  • \(\Delta_{\textsf{up}} = (0, 1)\)

  • \(\Delta_{\textsf{down}} = (0, -1)\)

  • \(\Delta_{\textsf{left}} = (-1, 0)\)

  • \(\Delta_{\textsf{right}} = (1, 0)\)

For the continuous one, \(\Delta_d = d\). The update is:

\[\begin{split}(x', y') = \begin{cases} (x, y) + \Delta_a & \text{the move is legal} \\ (x, y) & \text{otherwise (wall collision)} \end{cases}\end{split}\]

A discrete step is legal when the target cell is walkable. A continuous step is legal only when the whole swept segment stays inside the walkable region. The goal side never changes. Let \(c \in G\) be the cue cell. The cue phase advances on every action, including one a wall refused:

\[\begin{split}\mathrm{phase}' = \begin{cases} \textsf{EMITTING} & \mathrm{phase} = \textsf{UNSEEN} \text{ and the step crosses } c \\ \textsf{CONSUMED} & \mathrm{phase} = \textsf{EMITTING} \\ \mathrm{phase} & \text{otherwise} \end{cases}\end{split}\]

So the cue is read once: it is consumed by whatever action follows it.

Observation model

\[\begin{split}O(\textsf{left\_cue} \mid s', \cdot) &= \begin{cases} p_{\text{cue}} & \mathrm{phase}' = \textsf{EMITTING},\ \mathrm{side} = \textsf{L} \\ 1 - p_{\text{cue}} & \mathrm{phase}' = \textsf{EMITTING},\ \mathrm{side} = \textsf{R} \\ 0 & \mathrm{phase}' \neq \textsf{EMITTING} \end{cases} \\ O(\textsf{empty} \mid s', \cdot) &= \mathbb{1}[\mathrm{phase}' \neq \textsf{EMITTING}]\end{split}\]

with \(p_{\text{cue}}\) = cue_accuracy \(\in [0.5, 1]\), and \(\textsf{right\_cue}\) mirrored. The action does not enter.

Reward function. The terminal payout replaces the step cost rather than stacking with it. \(g_L, g_R \in G\) are the left and right goal cells:

\[\begin{split}R(s, a) = \begin{cases} 0 & s \in S_T \\ +\texttt{goal\_reward} & \text{the step enters } g_{\mathrm{side}} \\ -\texttt{wrong\_goal\_penalty} & \text{the step enters the other goal} \\ -\texttt{step\_penalty} & \text{otherwise, wall collisions included} \end{cases}\end{split}\]

Initial belief. Position and cue phase known, goal side a coin flip:

\[b_0\big((x_0, y_0, \textsf{L}, \textsf{UNSEEN})\big) = b_0\big((x_0, y_0, \textsf{R}, \textsf{UNSEEN})\big) = \tfrac{1}{2}\]

where \((x_0, y_0)\) is the fixed start cell. The opening observation is fixed at \(\textsf{empty}\).

Discount. \(\gamma\) = discount_factor, default \(0.95\).

Terminal set. Either goal, correct or not:

\[S_T = \{s : (x, y) \in \{g_L, g_R\}\}\]

with \(g_L, g_R\) the left and right goal cells.

Rewards

Event

Reward

Step (including a wall collision)

-1.0

Enter the correct goal

+10.0

Enter the wrong goal

-10.0

These are the defaults of step_penalty, goal_reward and wrong_goal_penalty, the same in all three variants. reward_range is built from them, (-10.0, 10.0) by default.

Key settings

All three classes take:

  • discount_factor (default 0.95)

  • cue_accuracy (default 0.9)

  • goal_reward (default 10.0)

  • wrong_goal_penalty (default 10.0)

  • step_penalty (default 1.0)

Every argument has a default, so each class builds with no arguments. The two penalties are passed as positive numbers and subtracted.

The layout arguments differ by class:

  • DiscreteMazePOMDP and ContinuousMazePOMDP take maze_width (7), maze_height (9), maze_seed (0) and loop_fraction (0.15).

  • ContinuousMazePOMDP also takes max_step_size (1.0).

  • TMazePOMDP takes stem_length (4) and arm_length (1).

DiscreteMazePOMDP

DiscreteMazePOMDP uses integer cell positions in a generated maze. The four actions, up, down, left and right, move one cell; a wall blocks the move. maze_width, maze_height, maze_seed and loop_fraction control the layout.

ContinuousMazePOMDP

ContinuousMazePOMDP uses real-valued positions and displacement actions [dx, dy], capped in length by max_step_size. Collision checks cover the whole movement path. With the same layout settings as DiscreteMazePOMDP, it uses the same maze geometry.

TMazePOMDP

TMazePOMDP uses a T-shaped corridor and the same four one-cell actions as the discrete maze. stem_length sets the distance to the junction and arm_length sets the distance from the junction to each endpoint. The agent must remember the cue while walking up the stem, then choose an arm.

Vectorized model

VOPP plans on a batched torch copy of the environment. DiscreteMazeVectorizedModel and TMazeVectorizedModel support every configuration of their environment. Their constructors step the scalar environment once from every (cell, goal side, cue phase) state under every action and store the results as lookup tables, so the scalar class decides every transition, terminal flag and observation probability. ContinuousMazePOMDP has no torch model: its actions are real 2-vectors, VOPP needs a finite action set, and no discrete-action variant of it exists.

Can I use?

DiscreteMazePOMDP

Discrete

Continuous

State

✔️

❌

Action

✔️

❌

Observation

✔️

❌

ContinuousMazePOMDP

Discrete

Continuous

State

❌

✔️

Action

❌

✔️

Observation

✔️

❌

TMazePOMDP

Discrete

Continuous

State

✔️

❌

Action

✔️

❌

Observation

✔️

❌

Also supports

DiscreteMazePOMDP

ContinuousMazePOMDP

TMazePOMDP

Native C++ backend

❌

❌

❌

Vectorized (torch) model

✔️ DiscreteMazeVectorizedModel

❌ (continuous actions; VOPP needs a finite action set)

✔️ TMazeVectorizedModel

In the get_environment registry

✔️

✔️

✔️

Optional dependencies

None

None

None

Example

import numpy as np

from POMDPPlanners.environments.maze_pomdp import ContinuousMazePOMDP, DiscreteMazePOMDP

env = DiscreteMazePOMDP(discount_factor=0.95, maze_seed=0)

state = env.initial_state_dist().sample(1)[0]
# With n_samples=1 these return the value itself, not a list of one.
next_state = env.sample_next_state(state, "up")
observation = env.sample_observation(next_state, "up")
print(state, "->", next_state, observation, env.reward(state, "up"))

# The continuous maze takes a displacement [dx, dy] instead.
continuous_env = ContinuousMazePOMDP(discount_factor=0.95, max_step_size=1.0)
state = continuous_env.initial_state_dist().sample(1)[0]
print(continuous_env.sample_next_state(state, np.array([0.0, 0.5])))

Parameters

class POMDPPlanners.environments.maze_pomdp.DiscreteMazePOMDP(discount_factor=0.95, maze_width=7, maze_height=9, maze_seed=0, loop_fraction=0.15, cue_accuracy=0.9, goal_reward=10.0, wrong_goal_penalty=10.0, step_penalty=1.0, name='DiscreteMazePOMDP', output_dir=None, debug=False, use_queue_logger=False)[source]

Bases: BaseMazePOMDP, DiscreteActionsEnvironment

Maze POMDP with cell positions and four one-cell moves.

Example

>>> env = DiscreteMazePOMDP(discount_factor=0.95, maze_width=11, maze_height=13)
>>> env.start_cell
(5, 1)
>>> env.left_goal_cell, env.right_goal_cell
((1, 11), (9, 11))
Parameters:
get_actions()[source]

The four movement actions, in a fixed order.

Return type:

List[str]

hash_action(action)[source]

The action label itself is already a hashable key.

Return type:

Hashable

Parameters:

action (Any)

class POMDPPlanners.environments.maze_pomdp.ContinuousMazePOMDP(discount_factor=0.95, maze_width=7, maze_height=9, maze_seed=0, loop_fraction=0.15, max_step_size=1.0, cue_accuracy=0.9, goal_reward=10.0, wrong_goal_penalty=10.0, step_penalty=1.0, name='ContinuousMazePOMDP', output_dir=None, debug=False, use_queue_logger=False)[source]

Bases: BaseMazePOMDP

Maze POMDP with real positions and a bounded displacement vector per step.

The map, the cue, the goals, the reward and the metrics are the discrete variant’s. Only how the agent moves differs: a step is a straight segment, and it is legal only if the whole segment stays inside the walkable region.

Parameters:
max_step_size

The longest displacement one action may cover.

Example

>>> import numpy as np
>>> env = ContinuousMazePOMDP(discount_factor=0.95)
>>> env.start_cell
(3, 1)
>>> state = env.initial_state_dist().sample()[0]
>>> float(env.sample_next_state(state, np.array([0.0, 0.4]))[1])
1.4
clip_action(action)[source]

The displacement action actually applies, at most max_step_size long.

Scaling rather than rejecting keeps the action space closed under any sampler: a planner drawing from a disc of the wrong radius still produces legal moves, and the direction it chose is preserved.

Parameters:

action (Any) – A 2-vector displacement.

Return type:

ndarray

Returns:

The same direction, with magnitude capped at max_step_size.

Raises:

ValueError – If action is not a 2-vector.

property draws_cell_guides: bool

positions are real here, and a grid would suggest a quantization.

Type:

False

hash_action(action)[source]

Bytes of the action vector, which match np.array_equal for one shape.

Return type:

Hashable

Parameters:

action (Any)

class POMDPPlanners.environments.maze_pomdp.TMazePOMDP(discount_factor=0.95, stem_length=4, arm_length=1, cue_accuracy=0.9, goal_reward=10.0, wrong_goal_penalty=10.0, step_penalty=1.0, name='TMazePOMDP', output_dir=None, debug=False, use_queue_logger=False)[source]

Bases: NativeMazeMixin, DiscreteActionsEnvironment

T-Maze POMDP with a single-use noisy cue and a delayed, side-dependent reward.

The environment API calls a planner makes per step are answered by the C++ model through NativeMazeMixin. The Python methods below that implement the same model (_successor, _reward_from_successor, _observation_probs, _py_is_terminal) are the reference the native-equivalence tests compare it with.

See the module docstring for the full model. The environment is a proper generative model: transitions and observations can be resampled from any state, and both carry densities, so it can back a particle filter and a tree search.

Parameters:
stem_length

Moves from the start cell up to the junction.

arm_length

Moves from the junction out to an arm endpoint.

cue_accuracy

Probability the cue names the true goal side.

goal_reward

Paid for entering the correct endpoint.

wrong_goal_penalty

Magnitude of the penalty for entering the wrong endpoint.

step_penalty

Magnitude of the per-action cost on non-terminal actions.

Example

>>> env = TMazePOMDP(discount_factor=0.95, stem_length=4, arm_length=1)
>>> env.junction
(0, 4)
>>> env.left_endpoint, env.right_endpoint
((-1, 4), (1, 4))
property cue_cell: Tuple[int, int]

The cell whose entry emits the one cue reading of the episode.

episode_visualizer()[source]

Return the visualizer that writes this environment’s traces.

Return type:

TMazeVisualizer

get_actions()[source]

The four movement actions, in a fixed order.

Return type:

List[str]

get_metric_specs()[source]

Declare the episode metrics derived from the per-step channels.

Completion reduces with ANY because the task is to reach something: once the correct endpoint is entered the episode is over, and no later step can undo it.

Reaching the wrong endpoint is reported separately from timing out because the two call for opposite fixes — a planner that guesses at the junction needs a better belief, one that times out needs a longer horizon — and a completion rate alone cannot tell them apart.

There is no severity metric here. The only hazard is entering the wrong endpoint, which happens at most once per episode and always costs the same; a “worst moment” number would restate the count.

Return type:

List[StepInfoMetric]

Returns:

One spec per metric, in the order compute_metrics reports them.

goal_endpoint(goal_side)[source]

The endpoint that pays the goal reward for goal_side.

Return type:

Tuple[int, int]

Parameters:

goal_side (float)

hash_action(action)[source]

The action label itself is already a hashable key.

Return type:

Hashable

Parameters:

action (Any)

hash_observation(observation)[source]

The label itself is already a hashable key.

Return type:

Hashable

Parameters:

observation (Any)

initial_observation_dist()[source]

Always "empty".

The cue is never in the initial reading: it is emitted by entering the cue cell, and the runner only observes after an action. Putting it here would hand the agent the answer before it had moved.

Return type:

Distribution

initial_state_dist()[source]

Uniform over the two goal sides, at the start cell with the cue unseen.

Return type:

Distribution

is_equal_observation(observation1, observation2)[source]

Observations are labels, so equality is string equality.

Return type:

bool

Parameters:
  • observation1 (Any)

  • observation2 (Any)

is_valid_cell(position)[source]

Whether position is inside the T-shaped corridor.

Return type:

bool

Parameters:

position (Tuple[int, int])

property junction: Tuple[int, int]

The cell where the stem meets the two arms.

property left_endpoint: Tuple[int, int]

The terminal cell at the end of the left arm.

property right_endpoint: Tuple[int, int]

The terminal cell at the end of the right arm.

property start_cell: Tuple[int, int]

The fixed cell every episode starts from.

step_info(state, action, next_state)[source]

Report the per-step channels for one transition.

Every channel is a pure function of the arguments — no draw, no state kept on self — as Environment.step_info() requires.

The episode-end channels are read off the outcome of the step: the realised successor when there is one, and state itself on the terminal bookkeeping call, where action and next_state are both None. Reading them off state alone would report a timeout on the very step that entered an endpoint, which is the last recorded step whenever a runner stops without appending the terminal record.

Parameters:
  • state (Any) – The state the step was taken from, or the final state.

  • action (Any) – The action taken, or None on the terminal step.

  • next_state (Any) – The realised successor, or None on the terminal step.

Return type:

Dict[str, float]

Returns:

One entry per TMazeStepChannel.

property valid_cells: frozenset

Every cell of the T, as (x, y) pairs.

See also