Skip to content
AI360XpertBeta

Monte Carlo Control

Monte Carlo control finds good actions without any model by finishing full episodes, averaging the returns each action earned, and switching to the winners.

Two finished episodes move Q values to 4.6 for the safe action and 8.0 for the risky one, so the greedy policy switches to risky
Two finished episodes move Q values to 4.6 for the safe action and 8.0 for the risky one, so the greedy policy switches to risky

Why Does This Exist?

Dynamic programming finds optimal policies only when the transition probabilities P(s′∣s,a)P(s' \mid s, a) are known. In robotics, games, and trading systems they are not. Worse, improving a policy from state values V(s)V(s) alone still needs a one step lookahead through those unknown probabilities, so V(s)V(s) cannot pick actions by itself.

Monte Carlo control fixes both gaps at once. It evaluates action values Q(s,a)Q(s, a) directly from finished episodes, then makes the policy greedy in QQ, which needs no model at all:

π′(s)=arg⁡max⁡aQ(s,a)\pi'(s) = \arg\max_{a} Q(s, a)

Unlike temporal difference learning, nothing here bootstraps from a guessed successor value. Every update uses a true sampled return GtG_t, so there is no bootstrapping bias to diverge. The price is patience: learning waits for episodes to terminate. This page covers the control loop. Its building blocks are Monte Carlo action values and exploring starts.

The one idea you need first

Generalized policy iteration alternates evaluation with improvement: estimate how good the current policy is, then act greedily in those estimates. See generalized policy iteration for the full loop. Monte Carlo control is that loop with complete episode returns as the evaluator.

Think of It Like This

A golfer who only changes strategy at the clubhouse

A golfer tests a new swing by playing full 18 hole rounds with it. Mid fairway guesses do not count. Back at the clubhouse the scorecard shows the true total, and the golfer compares which club choices from which lies produced the lowest totals, then commits to the winners for the next round.

Two rounds can flip a decision: a safe layup strategy totals 4.6 over par while one bold drive totals 8.0 under the same scoring, so the bold drive becomes the new plan. Where it stops: golf lies are nearly repeatable, while stochastic environments need many rounds averaged before a total is trustworthy, plus deliberate experimentation so a mediocre first round does not lock in forever.

How It Actually Works

An episode τ=(S0,A0,R1,…,ST)\tau = (S_0, A_0, R_1, \dots, S_T) under policy π\pi gives a discounted return per step:

Gt=Rt+1+γGt+1,GT=0G_t = R_{t+1} + \gamma G_{t+1}, \qquad G_{T} = 0

Here Rt+1R_{t+1} is the reward after step tt, γ\gamma is the discount in [0,1][0, 1], and GtG_t is the true total from tt onward. First visit Monte Carlo keeps the GtG_t from each pair's first appearance per episode and averages:

Q(St,At)←Q(St,At)+1N(St,At)[Gt−Q(St,At)]Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \frac{1}{N(S_t, A_t)}[G_t - Q(S_t, A_t)]

where N(s,a)N(s, a) counts visits. Improvement then sets π(s)\pi(s) to the action with the highest QQ. By the policy improvement theorem this greedy step never hurts expected returns, so repeated evaluation plus greedification climbs toward the optimal policy. One guardrail is mandatory: every pair must keep being visited, through exploring starts or an epsilon greedy policy, or unvisited actions freeze at stale values.

Worked example

State s0s_0 offers asafea_{\text{safe}} and ariskya_{\text{risky}} with γ=0.9\gamma = 0.9. All values start at 0.

Episode 1 follows the safe route: (s0,asafe,R1=1.0)(s_0, a_{\text{safe}}, R_1 = 1.0) then (s1,aproceed,R2=4.0)(s_1, a_{\text{proceed}}, R_2 = 4.0) to termination. Working backward:

G1=4.0,G0=1.0+0.9×4.0=1.0+3.6=4.6G_1 = 4.0, \qquad G_0 = 1.0 + 0.9 \times 4.0 = 1.0 + 3.6 = 4.6

So Q(s1,aproceed)=4.00Q(s_1, a_{\text{proceed}}) = 4.00 and Q(s0,asafe)=4.60Q(s_0, a_{\text{safe}}) = 4.60. The greedy policy at s0s_0 still picks safe, since risky sits at 0.

Episode 2 explores: (s0,arisky,R1=8.0)(s_0, a_{\text{risky}}, R_1 = 8.0) straight to termination, giving G0=8.0G_0 = 8.0 and Q(s0,arisky)=8.00Q(s_0, a_{\text{risky}}) = 8.00. Now 8.008.00 beats 4.604.60, so π(s0)\pi(s_0) switches from safe to risky on sampled evidence alone, with no transition model consulted.

Code

This snippet replays both episodes with first visit updates and prints the values and the policy after each one.

def backward_returns(rewards: list[float], gamma: float = 0.9) -> list[float]:    g = 0.0    out = []    for r in reversed(rewards):        g = r + gamma * g        out.append(round(g, 2))    return list(reversed(out))

print(backward_returns([1.0, 4.0]))  # -> [4.6, 4.0]print(backward_returns([8.0]))       # -> [8.0]
q = {("s0", "safe"): 4.6, ("s0", "risky"): 8.0}print(max(["safe", "risky"], key=lambda a: q[("s0", a)]))  # -> risky

The first print shows G0=4.6G_0 = 4.6 for the safe route. The second shows 8.08.0 for the risky episode. The greedy pick over the two QQ values is risky, exactly the policy switch in the worked example.

Watch Out For

Episodes that never end stall all learning

Monte Carlo control updates only after termination, because GtG_t is defined backward from the final step. In continuing tasks or loops that rarely terminate, transitions pile up in memory while the policy never changes.

Symptom: a growing trajectory buffer with zero updates and flat performance. Fix: cap episodes with an explicit horizon and reset, or switch to a bootstrapping method such as one step TD that updates every transition.

Greedy improvement locking onto a mediocre action

After one lucky episode a mediocre action can lead Q(s,a)Q(s, a) while better actions sit at 0, and a fully greedy policy then never samples the alternatives, freezing their values forever.

Symptom: fast early convergence to a policy that is clearly improvable yet never changes. Fix: keep exploration alive with exploring starts where simulators allow resets, or an epsilon greedy schedule that decays but never hits exactly zero during learning.

The Quick Version

  • Monte Carlo control learns Q(s,a)Q(s, a) from finished episodes, so greedy action choice needs no transition model.
  • Each update averages true sampled returns GtG_t, which removes bootstrapping bias but forces waiting for termination.
  • In the worked example, safe scores 1.0+0.9×4.0=4.61.0 + 0.9 \times 4.0 = 4.6 while risky scores 8.08.0, so the policy switches to risky.
  • Evaluation plus greedification repeats as generalized policy iteration and climbs toward the optimal policy.
  • Exploring starts or epsilon greedy exploration is mandatory, or unvisited actions never get corrected.