Sanity

Two states, two actions, perfect observations, no terminal state. Action 0 moves to the “good” state and pays 1.0; action 1 moves to “bad” and pays 0.0.

There is no partial observability and nothing to plan around. It exists to expose planner bugs: if a planner does not converge to always taking action 0, the bug is in the planner, not in the problem. Run it first when a planner produces nonsense somewhere else.

What the agent sees and does

  • State — an int: 0 (good) or 1 (bad).

  • Actions (discrete) — the ints 0 (go to good) and 1 (go to bad).

  • Observations (discrete) — an int equal to the new state, always.

Formal definition

The environment is the POMDP \(\langle S, A, Z, T, O, R, b_0, \gamma \rangle\).

State space

\[S = \{0, 1\}\]

The state is the last action taken.

Action space

\[A = \{0, 1\}\]

Action \(a\) moves the agent to state \(a\); action \(0\) is the rewarded one.

Observation space

\[Z = \{0, 1\}\]

The observation is the state itself.

Transition model. Deterministic, and independent of the current state:

\[T(s' \mid s, a) = \mathbb{1}[s' = a]\]

Observation model

\[O(o \mid s', a) = \mathbb{1}[o = s']\]

Reward function

\[R(s, a) = \mathbb{1}[a = 0]\]

with \(R \in [0, 1]\).

Initial belief. The start is fixed rather than drawn:

\[b_0(0) = 1, \qquad o_0 = 0\]

Discount. \(\gamma\) defaults to \(0.95\).

Terminal set. \(S_T = \emptyset\).

Because \(O\) is the identity, the belief collapses to a point mass after one step — this is an MDP behind the POMDP interface, so a failure on it points at the planner, not at belief tracking.

Rewards

Event

Reward

Action 0

1.0

Action 1

0.0

reward_range is (0.0, 1.0).

Key settings

discount_factor (default 0.95) is the only constructor argument that changes the problem. An episode never ends on its own: is_terminal always returns False, so episode length comes from the horizon the caller sets.

Can I use?

SanityPOMDP

Discrete

Continuous

State

✔️

❌

Action

✔️

❌

Observation

✔️

❌

Also supports

Native C++ backend

❌

Vectorized (torch) model

✔️ SanityVectorizedModel

In the get_environment registry

✔️

Optional dependencies

None

Example

from POMDPPlanners.environments.sanity_pomdp import SanityPOMDP

env = SanityPOMDP(discount_factor=0.95)
state = env.initial_state_dist().sample(1)[0]
print(env.reward(state, 0), env.reward(state, 1))

Parameters

class POMDPPlanners.environments.sanity_pomdp.SanityPOMDP(discount_factor=0.95, output_dir=None, debug=False, use_queue_logger=False)[source]

Bases: DiscreteActionsEnvironment

Simple sanity check POMDP environment for testing and debugging.

This environment provides the simplest possible POMDP formulation with deterministic dynamics and perfect observability. It serves as a baseline for testing POMDP algorithms and ensuring correctness.

Problem Structure: - States: 0 (good), 1 (bad) - Actions: 0 (choose good), 1 (choose bad) - Observations: Same as states (perfect observability) - Rewards: 1.0 for good state, 0.0 for bad state - Dynamics: Deterministic state transitions based on action

The optimal policy is trivial: always choose action 0 to stay in the good state.

Example

>>> import numpy as np
>>> np.random.seed(42)  # For reproducible results
>>>
>>> # Initialize environment
>>> env = SanityPOMDP(discount_factor=0.95)
>>>
>>> # Get initial state and actions
>>> initial_state = env.initial_state_dist().sample()[0]
>>> actions = env.get_actions()
>>>
>>> # Sample complete step using convenience method
>>> action = actions[0]
>>> next_state, observation, reward = env.sample_next_step(initial_state, action)
>>>
>>> # Check terminal condition
>>> env.is_terminal(initial_state)
False
Parameters:
  • discount_factor (float)

  • output_dir (Path | None)

  • debug (bool)

  • use_queue_logger (bool)

get_actions()[source]

Get all possible actions in the discrete action space.

Return type:

List[int]

Returns:

List containing all valid actions that can be executed

Note

Subclasses must implement this method to enumerate all possible actions. This is used by planning algorithms that need to iterate over actions.

hash_action(action)[source]

Return a hashable key consistent with action equality.

Used by tree-search planners to index action children of a belief node in O(1). The returned key MUST satisfy:

action_a == action_b   (per env's notion of equality)
==> hash_action(action_a) == hash_action(action_b)

Subclasses with non-hashable actions (e.g. np.ndarray) must override to return a hashable surrogate (tobytes() is the standard choice for ndarray actions, which mirrors the np.array_equal semantics used by the linear-scan fallback).

Parameters:

action (Any) – Action to hash.

Return type:

Hashable

Returns:

A hashable key derived from action.

initial_observation_dist()[source]

Get the initial observation distribution.

Return type:

SanityInitialObservationDist

Returns:

Distribution over initial observations

Note

Subclasses must implement this method to define initial observations.

initial_state_dist()[source]

Get the initial state distribution.

Return type:

SanityInitialStateDist

Returns:

Distribution over initial states

Note

Subclasses must implement this method to define the starting distribution.

is_equal_observation(observation1, observation2)[source]

Check if two observations are equal.

Parameters:
  • observation1 (int) – First observation to compare

  • observation2 (int) – Second observation to compare

Return type:

bool

Returns:

True if observations are considered equal, False otherwise

Note

Subclasses must implement this method to define observation equality. This is particularly important for discrete observation spaces.

is_terminal(state)[source]

Check if a state is terminal.

Parameters:

state (int) – State to check for terminal condition

Return type:

bool

Returns:

True if the state is terminal, False otherwise

Note

Subclasses must implement this method to define terminal conditions.

observation_log_probability(next_state, action, observations)[source]

Log-probability of each candidate observation under (next_state, action).

Returns np.ndarray of shape (N,) where N is the number of candidate observations. Subclasses must implement.

Return type:

ndarray

Parameters:
reward(state, action, next_state=None)[source]

Calculate the immediate reward for a state-action(-next_state) tuple.

next_state is the realised post-transition state when known (e.g. threaded by sample_next_step()), allowing rewards that depend on stochastic transition outcomes to use the same draw as the trajectory instead of resampling. Subclasses whose reward is a pure function of (state, action) may ignore it; subclasses whose reward depends on the realised next state (collision penalties, win bonuses) should consume it when provided and fall back to drawing/computing one when None.

Parameters:
  • state (int) – Current state.

  • action (int) – Action executed from state.

  • next_state (Any) – Realised next state, or None if the caller did not pre-sample one. Defaults to None.

Return type:

float

Returns:

Immediate reward value.

Note

Subclasses must implement this method to define reward structure.

sample_next_state(state, action, n_samples=1)[source]

Sample one or more next states for (state, action).

Hot-path entry point used by MCTS planners and particle filters. Subclasses must implement.

Returns:

a single next state of the env’s native type. When n_samples > 1: an array-like of length n_samples (numeric envs return np.ndarray of shape (n_samples, *dim); structured envs return List[T]).

Return type:

Union[int, ndarray]

Parameters:
sample_observation(next_state, action, n_samples=1)[source]

Sample one or more observations for (next_state, action).

Hot-path entry point used by MCTS planners and particle filters. Subclasses must implement.

Returns:

a single observation. When n_samples > 1: an array-like of length n_samples.

Return type:

Union[int, ndarray]

Parameters:
  • next_state (int)

  • action (int)

  • n_samples (int)

transition_log_probability(state, action, next_states)[source]

Log-probability of each candidate next state under (state, action).

Returns np.ndarray of shape (N,) where N is the number of candidate next states. Subclasses must implement.

Return type:

ndarray

Parameters:

See also