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) or1(bad).Actions (discrete) — the ints
0(go to good) and1(go to bad).Observations (discrete) — an
intequal 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
The state is the last action taken.
Action space
Action \(a\) moves the agent to state \(a\); action \(0\) is the rewarded one.
Observation space
The observation is the state itself.
Transition model. Deterministic, and independent of the current state:
Observation model
Reward function
with \(R \in [0, 1]\).
Initial belief. The start is fixed rather than drawn:
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 |
1.0 |
Action |
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?
|
Discrete |
Continuous |
|---|---|---|
State |
✔️ |
❌ |
Action |
✔️ |
❌ |
Observation |
✔️ |
❌ |
Also supports |
|
|---|---|
Native C++ backend |
❌ |
Vectorized (torch) model |
✔️ |
In the |
✔️ |
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:
DiscreteActionsEnvironmentSimple 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
- get_actions()[source]
Get all possible actions in the discrete action space.
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 thenp.array_equalsemantics used by the linear-scan fallback).
- 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:
- Return type:
- 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:
- 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.ndarrayof shape(N,)where N is the number of candidate observations. Subclasses must implement.
- reward(state, action, next_state=None)[source]
Calculate the immediate reward for a state-action(-next_state) tuple.
next_stateis the realised post-transition state when known (e.g. threaded bysample_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 whenNone.- Parameters:
- Return type:
- 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.
- 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.
See also
Mountain Car — a two-variable continuous-state control task.
Base Environment Class — the full catalog and the environment interface.