mixle.task.rl module¶
Reinforcement learning: tabular Q-learning over a discrete MDP with a KNOWN reward.
This is the classical value-based counterpart to sequence-level optimization routines such as
outcome_decomposer and probe_policy: those optimize a whole action sequence’s terminal,
oracle-verified score by propose/filter/refit, with no per-step value estimate. This module is the
other end of the design space – a per-step Bellman backup,
Q(s, a) <- Q(s, a) + alpha * [r + gamma * max_a' Q(s', a') - Q(s, a)] – for problems that ARE
naturally modeled as a finite MDP with a known step reward. It composes with mixle.task.irl
(the complementary direction: reward FROM demonstrations, not policy from a known reward) via the
shared GridWorld environment.
world = GridWorld(size=5, goal=(4, 4)) result = tabular_q_learning(world, episodes=500, seed=0) policy = result.greedy_policy(world) # {state: best action}, the recovered optimal policy
- class GridWorld(size, goal, obstacles=<factory>, step_cost=-1.0, goal_reward=10.0, max_steps=100)[source]
Bases:
objectA deterministic
sizexsizegrid MDP: a goal cell worthgoal_reward, a per-step cost ofstep_cost, and optional impassableobstacles(moving into a wall or obstacle leaves the agent in place, still paying the step cost). The optimal policy is the shortest obstacle-free path to the goal – computable independently viaoptimal_path_length()(BFS), which is what makes this a closed-form-known-optimum test environment.- Parameters:
- property n_states: int
Return the number of states in the square grid.
- state_index(state)[source]
Map a
(row, column)state to its row-major integer index.
- index_state(index)[source]
Map a row-major integer state index back to
(row, column).
- transition(state, action)[source]
The deterministic next state for
actionatstate(walls/obstacles are a no-op).
- reset(start=(0, 0))[source]
Reset the environment to
startand return the initial state.
- step(action)[source]
Apply one action and return
(next_state, reward, done).
- class QLearningResult(q_table, rewards_per_episode)[source]
Bases:
objectThe fitted Q-table plus the per-episode return trace (the learning curve).
- greedy_action_index(state_index)[source]
Return the index of the highest-valued action for
state_index.
- tabular_q_learning(env, *, episodes=500, alpha=0.3, gamma=0.95, epsilon=0.2, seed=None)[source]
Epsilon-greedy tabular Q-learning:
episodesfull rollouts fromenv.reset(), each step updatingQ(s, a)toward the observed one-step Bellman target.
- rollout(env, policy, *, start=(0, 0))[source]
Roll out a deterministic state -> action
policyfromstart; the(state, action)trace (stops at the goal orenv.max_steps, whichever first).