Week 05 · Monte Carlo prediction and control

Estimate an action value using rewards from complete episodes.

← Week 4 · Subject contents · Week 6 →

Estimate an action value using rewards from complete episodes.

Work through the problem

Monte Carlo prediction averages returns observed after visits. For action a in state s, the incremental first-visit update is Q ← Q+(G−Q)/n after episode n for that pair. An exploring policy is needed to gather returns for multiple actions.

Consider a safe action paying 1 and a gamble paying 4 with probability 0.4, otherwise paying zero. Expected returns are 1 and 1.6. The example uses a known one-step terminal task so the average can be checked, then improves the policy greedily from its estimates.

Run the Python lab

import numpy as np
rng = np.random.default_rng(13)
q,n = np.zeros(2),np.zeros(2)
for episode in range(16000):
    a = episode if episode<2 else (rng.integers(2) if rng.random()<.2 else q.argmax())
    G = 1 if a==0 else 4*(rng.random()<.4)
    n[a] += 1
    q[a] += (G-q[a])/n[a]
print("Visits:",n.astype(int))
print("Values:",q.round(3),"greedy action",q.argmax())
assert q.argmax()==1 and abs(q[1]-1.6)<.08

Output from the lab

Visits: [ 1653 14347]
Values: [1.    1.618] greedy action 1

Practice

  1. What would change if the gamble led to a second state?

Check your reasoning

  1. Wait for the episode to end and include later rewards discounted from the action’s visit.

← Week 4 · Subject contents · Week 6 →

Further reading · Sutton and Barto · Reinforcement Learning