Skip to content
Physical AI
All experiments

Grid-world Q-learning

How a tabular agent with no prior knowledge learns action values Q(s, a) from rewards alone, how its greedy policy emerges from those values, and how α, γ, ε, slip and the reward design change what it learns.

Loading simulation…

What it demonstrates

How a tabular agent with no prior knowledge learns action values Q(s, a) from rewards alone, how its greedy policy emerges from those values, and how α, γ, ε, slip and the reward design change what it learns.

How it works

The environment is an 8 × 6 grid: a small Markov decision process. The state is the agent's cell, the four actions move up, right, down or left, walls block movement, and every move earns the step reward. Entering the goal or a pit adds the goal reward or pit penalty and ends the episode. Episodes are also cut off after 100 steps; that cut-off is a truncation, not a terminal state, so the update still bootstraps from the next state. With slip > 0, the chosen action is replaced by a uniformly random one with that probability.

The agent stores one number per (state, action) pair in a table, initialised to zero. Each step it chooses an action ε-greedily, observes the reward and next state, and applies one temporal-difference update. Q-learning bootstraps from the best next action (off-policy). SARSA bootstraps from the action it will actually take next (on-policy). The value colour of each cell is V(s) = max_a Q(s, a), and the arrow is the greedy action.

The readouts compare the current greedy policy (evaluated from the Q-table without exploration) with a uniformly random policy evaluated over 300 episodes. The training curve plots the real return of every training episode, exploratory actions included.

Adjustable parameters

  • Algorithm: Q-learning or SARSA
  • Learning on/off (off = frozen greedy policy, i.e. inference)
  • Speed (0.1–2500 environment steps per frame)
  • Learning rate α (0.01–1)
  • Discount γ (0–0.99)
  • Exploration ε (0–1)
  • Slip probability (0–0.5)
  • Step, pit and goal rewards
  • Click cells to add or remove walls and pits

Known limitations

  • A table needs one entry per state–action pair. A walking robot's state is continuous and high-dimensional, so real locomotion uses neural-network function approximation instead.
  • The state is fully observed (the agent knows its cell). Robots see only partial, noisy observations.
  • Editing the grid keeps the old Q-table, so the agent must unlearn stale values; this is deliberate, to show adaptation.

Equations

Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)](Q-learning)Q(s,a) \leftarrow Q(s,a) + \alpha\big[r + \gamma \max_{a'} Q(s',a') - Q(s,a)\big] \quad \text{(Q-learning)}
Q(s,a)←Q(s,a)+α[r+γ Q(s′,a′)−Q(s,a)],  a′∼ε-greedy(SARSA)Q(s,a) \leftarrow Q(s,a) + \alpha\big[r + \gamma\, Q(s',a') - Q(s,a)\big],\; a' \sim \varepsilon\text{-greedy} \quad \text{(SARSA)}
π(s)=arg⁡max⁡aQ(s,a),V(s)=max⁡aQ(s,a)\pi(s) = \arg\max_a Q(s,a), \qquad V(s) = \max_a Q(s,a)

Core update rule

// One environment step + one TD update (lib/sim/ch07-gridworld.ts)
const a = ag.a;                                       // chosen ε-greedily
const { next, reward, terminated } = envStep(grid, s, a, rewards, slip, rng);
let bootstrap = 0;
if (!terminated) {
  nextA = epsilonGreedy(Q, next, epsilon, rng);
  bootstrap = algorithm === "sarsa"
    ? Q[next * 4 + nextA]                              // on-policy
    : maxQ(Q, next);                                   // off-policy
}
const td = reward + gamma * bootstrap - Q[s * 4 + a];
Q[s * 4 + a] += alpha * td;

Explained in Chapter 7: RL Fundamentals