Extra environments — rl_mind.envs, rl_mind.mazemdp_env¶
Both modules register their environment in gymnasium when imported, which is
why they are never imported by rl_mind itself:
import rl_mind.envs # CartPoleContinuous-v1
import rl_mind.mazemdp_env # MazeMDP-v0
Extra environments.
CartPoleContinuous-v1 is the classic cart-pole environment with a
continuous action in \([-1, 1]\) (the force applied to the cart), used
by the DDPG/TD3/SAC notebooks. Adapted from bbrl_gymnasium (MIT license,
Sorbonne Université).
- class rl_mind.envs.ContinuousCartPoleEnv(**kwargs)[source]¶
Bases:
CartPoleEnvContinuous-action version of the CartPole-v1 environment
Same observation space, reward (+1 per step) and termination condition as
CartPole-v1, but the action is the signed fraction of the maximum force applied to the cart, aBox(-1, 1, shape=(1,)). Registered asCartPoleContinuous-v1(500 steps at most) whenrl_mind.envsis imported.- step(action)[source]¶
Apply a force to the cart and advance the simulation by one step
- Parameters:
action – The force, as a fraction of
force_magin \([-1, 1]\) (clipped); a scalar or a one-element array- Returns:
The gymnasium 5-tuple
(obs, reward, terminated, truncated, info), with a reward of 1.0 at every step
The Simple Maze MDP environment (MazeMDP-v0).
A small tabular Markov Decision Process on a grid, used by the
dynamic-programming and tabular-RL notebooks. Wraps the mazemdp package
and exposes the transition model (P, r, terminal_states, gamma)
for planning, plus gymnasium-style reset / step for learning.
Importing this module registers MazeMDP-v0 in gymnasium. Adapted from
bbrl_gymnasium (MIT license, Sorbonne Université).
- class rl_mind.mazemdp_env.MazeMDPEnv(render_mode=None, **kwargs)[source]¶
Bases:
EnvA tabular grid-world MDP, with its transition model exposed
Observations are state indices (a
Discretespace) and there are four actions (north, south, east, west). Unlike a usual gymnasium environment, the whole model is available as attributes (P,r,terminal_states,gamma), which is what the dynamic-programming notebooks need; thedraw_*methods render a value function and/or a policy on the grid.Created through gymnasium, either random or from an explicit wall list:
import rl_mind.mazemdp_env # registers MazeMDP-v0 env = gym.make("MazeMDP-v0", kwargs={"width": 5, "height": 5, "ratio": 0.2, "seed": 0})
- Parameters:
render_mode –
"rgb_array","human", orNonekwargs – A single
kwargs=dictionary describing the maze:width,height,hit(reward for hitting a wall), then eitherratio(fraction of walls, with an optionalseedfor a reproducible random maze) orwalls(explicit wall indices, with optionalterminal_states), plus optionalstart_states. With no argument at all, a random 10x10 maze with 20% walls is built.
- nb_states¶
The number of states (walls excluded)
- coord_x¶
The grid column of each state
- coord_y¶
The grid row of each state
- observation_space¶
The observation space: a state index in
[0, nb_states)
- action_space¶
The action space: 0 = north, 1 = south, 2 = east, 3 = west
- terminal_states¶
The indices of the terminal states
- P¶
The transition model \(P(s' \mid s, a)\), shape
[nb_states, 4, nb_states]
- gamma¶
The discount factor \(\gamma\) of the MDP
- r¶
The reward model \(r(s, a)\), shape
[nb_states, 4]
- state¶
The current state index. Reading it is cheating: a learning agent only knows the states
reset/stepreturn.
- set_render_func(render_func: Callable, *args, **kwargs)[source]¶
Set the function
rendercalls- Parameters:
render_func – The drawing function (the
mazemdprenderer by default); the currentrender_modeis passed asmode=args – Positional arguments bound to
render_funckwargs – Keyword arguments bound to
render_func
- seed(seed=None)[source]¶
Seed the environment’s random generator
- Parameters:
seed – The seed, or
Noneto draw one- Returns:
A one-element list holding the seed actually used
- step(action)[source]¶
Perform one action in the maze
- Parameters:
action – The action index (0 = north, 1 = south, 2 = east, 3 = west)
- Returns:
The gymnasium 5-tuple
(state, reward, terminated, truncated, info), wherestateis a state index
- reset(*, seed: int | None = None, options: dict[str, Any] | None = None) tuple[ObsType, dict[str, Any]][source]¶
Start a new episode
- Parameters:
seed – Unused (the maze itself is fixed at construction time; seed it there for a reproducible random maze)
options – Passed to
mazemdp’sreset, e.g.{"uniform": True}to start from a state drawn uniformly
- Returns:
The initial state index and an (empty) info dictionary
- sample_transition(**kwargs)[source]¶
Draw a transition from the model, without stepping the environment
- Parameters:
kwargs – Passed to
mazemdp’ssample_transition- Returns:
What
mazemdpreturns, a sampled(state, action, reward, next state)transition
- draw_v_pi_a(v, policy, agent_pos, title='MDP studies', recorder=None)[source]¶
Draw the value function, the policy and the agent’s position
- Parameters:
v – The value of each state, shape
[nb_states]policy – The action chosen in each state, shape
[nb_states]agent_pos – The state the agent stands on
title – The title drawn above the maze
recorder – A video recorder to capture the frame into, instead of displaying it
- Returns:
What the renderer returns (an
[H, W, 3]array inrgb_arraymode), orNonewhen recording
- draw_v_pi(v, policy, title='MDP studies', recorder=None)[source]¶
Draw the value function (colors) and the policy (arrows)
- Parameters:
v – The value of each state, shape
[nb_states]policy – The action chosen in each state, shape
[nb_states]title – The title drawn above the maze
recorder – A video recorder to capture the frame into, instead of displaying it
- Returns:
What the renderer returns, or
Nonewhen recording
- draw_v(v, title='MDP studies', recorder=None)[source]¶
Draw a value function (or the Q-values of each action)
- Parameters:
v – The value of each state (shape
[nb_states]), or of each state-action pair (shape[nb_states, 4], drawn as four triangles per cell)title – The title drawn above the maze
recorder – A video recorder to capture the frame into, instead of displaying it
- Returns:
What the renderer returns, or
Nonewhen recording
- draw_pi(policy, title='MDP studies', recorder=None)[source]¶
Draw a policy (one arrow per state)
- Parameters:
policy – The action chosen in each state, shape
[nb_states]title – The title drawn above the maze
recorder – A video recorder to capture the frame into, instead of displaying it
- Returns:
What the renderer returns, or
Nonewhen recording
- init_draw(title, recorder=None)[source]¶
Open (or reset) the figure the maze is drawn in
- Parameters:
title – The title drawn above the maze
recorder – A video recorder to capture the frame into
- Returns:
What the renderer returns, or
Nonewhen recording
- render()[source]¶
Render the maze with the current render function
- Returns:
An
[H, W, 3]array inrgb_arraymode,Noneinhumanmode
- render_mdp(func, *args, **kwargs)[source]¶
Render with a custom drawing function (legacy; prefer
set_render_funcfollowed byrender)Note that this replaces the
set_render_funcmethod with a partial offunc, so later calls to it fail; nothing in the practicals uses this method.- Parameters:
func – The drawing function
args – Positional arguments bound to
funckwargs – Keyword arguments bound to
func