Concepts / First-visit Monte Carlo methods

First-visit Monte Carlo methods

On-policy first-visit MC control alternates between episode generation and policy improvement.

  • Programming

The Repeating Learning Cycle

On-policy first-visit Monte Carlo control learns by repeating two activities: it generates an episode using the current policy, then uses returns observed in that episode to update its action values and policy. The updated policy is used to generate later episodes, so a policy change influences the data collected in the future.

generatesprovidesare averaged intodeterminesguides a laterCurrent policyε-softEpisodegeneratedReturnsfirst visitsQ valuesaveraged returnsUpdated policyε-soft
What happens next as the algorithm generates an episode, evaluates returns, updates Q, and improves the policy?

The order matters: generate an episode, identify first visits, use the associated returns, update Q, and then improve the policy. When tracing an unexpected result, compare the actual process with this sequence.

A First-Visit Trace

Consider an illustrative episode in which the same state-action pair appears more than once. The first occurrence of a pair is the one that matters for the first-visit update. A later occurrence of that same pair in the same episode does not supply another return for that first-visit update.

selects associatednot another first-visit updateState-action pairfirst occurrenceReturnused for first-visit updateOther transitionState-action pairlater occurrence
How does the algorithm identify the first occurrence of a state-action pair and connect it to the return used for updating its value?

One Episode with a Repeated Pair

Trace how a repeated state-action pair is handled in one illustrative episode.

Locate occurrences: Scan the episode and find every occurrence of the state-action pair being considered.

Keep the first occurrence: Select the return following the first occurrence. This is the return used for the first-visit update.

Ignore the later occurrence for this update: The later occurrence does not provide another return for that same first-visit update within the episode.

For a first-visit update, one state-action pair in the episode contributes the return associated with its first occurrence.

What do you think happens?

A state-action pair appears twice in one episode. Which occurrence determines the return used for its first-visit update?

  • The first occurrence
  • The last occurrence
  • Both occurrences automatically
Reveal answer

Answer: The first occurrence

First-visit Monte Carlo control uses only the return after the first occurrence of a state-action pair in each episode for that first-visit update.

From Returns to Q Values

The algorithm stores returns associated with state-action pairs. To estimate Q for a pair, it averages the stored returns for that pair. Each new first-visit observation can therefore contribute another stored return, while the Q estimate reflects the average of the returns collected so far.

averaged intoaveraged intoState-action pair Astored returns: G1, G2Q(A)average of G1 and G2State-action pair Bstored returns: G3Q(B)average of G3
How do the returns stored for each state-action pair combine to produce its estimated Q value?

Updating an Estimate from Stored Returns

Suppose an illustrative state-action pair has two stored returns, G1 and G2.

Collect: The returns are stored under the state-action pair when the pair is eligible for first-visit updates.

Combine: The stored returns are averaged rather than treated as separate competing Q values.

Interpret: The resulting average is the current Q estimate for that state-action pair.

Q for the pair is estimated from the average of its stored returns.

Turning Q into an ε-Soft Policy

After Q values have been updated, the algorithm identifies A*, the action with the maximum Q value, for every state appearing in the episode. It then updates the probabilities of all available actions according to the ε-soft rule. The best action is favored, but the policy remains ε-soft, meaning every action retains a minimum probability of being selected.

compare Q valuesreceives favored selection probabilityretains minimum probabilityAction Alower QA*favored probabilityAction A*maximum QOther actionsminimum probability
How do updated Q values determine the greedy action, and how are action-selection probabilities assigned under an ε-soft policy?

The policy update has two parts. First, compare the available actions through their Q values and identify the maximum-Q action A*. Second, assign action probabilities using the ε-soft rule. Do not interpret this as removing all probability from the other actions: the source method keeps every action selectable with at least a minimum probability.

Decision stageQuestionResult
Q comparisonWhich available action has the maximum Q value?That action is labeled A*.
Policy assignmentHow should actions be selected?Use the ε-soft rule.
Exploration conditionCan another action still be selected?Yes. Every action keeps a minimum probability.
Later episode generationWhich policy supplies future episode actions?The updated ε-soft policy.

Finding the First Divergence

When a traced result is unexpected, inspect the algorithm in its execution order. Check the generated episode first. Then check whether the first occurrence of each state-action pair was identified correctly. Next check the return attached to that occurrence, the stored-return average used for Q, and finally the policy improvement step. The first stage that differs from the intended process is the most useful place to investigate.

thenthenthenthenEpisode generationcheck firstFirst-visit checkcheck nextReturncheck associationQ updatecheck averagePolicy improvementcheck last
At which stage of episode generation, first-visit checking, return calculation, Q updating, or policy improvement does an unexpected result first appear?
  • Using the return after every occurrence of a repeated state-action pair in the same episode.

    First-visit control uses only the return after the first occurrence for that first-visit update.

    Fix: Locate the earliest occurrence of the pair before selecting the return.

  • Treating a Q update as the end of the process.

    The method alternates between episode generation and policy improvement, and the updated policy generates later episodes.

    Fix: After updating Q, identify the maximum-Q action and update the ε-soft policy.

  • Making the policy completely deterministic after finding A*.

    The resulting policy remains ε-soft, so every action has a minimum probability of being selected.

    Fix: Favor A* while retaining the minimum probability required by the ε-soft rule for every action.

Trace It Yourself

MEDIUM

You are tracing one episode and find that a state-action pair appears at two different points. Write down the checks you would perform, in order, before deciding whether the Q estimate or policy update is wrong.

Hints
  • Begin with the episode produced by the current policy.
  • Find the first occurrence before examining the later occurrence.
  • Check the return, the stored-return average, and the policy update in that order.
  • Confirm which episode was generated by the current ε-soft policy.
  • Identify the first occurrence of each state-action pair being updated.
  • Confirm that the return after that first occurrence is the one stored.
  • Confirm that stored returns are averaged to estimate Q.
  • Confirm that the maximum-Q action is identified as A*.
  • Confirm that all available actions receive probabilities according to the ε-soft rule.

Key Takeaways

  1. On-policy first-visit Monte Carlo control alternates between generating episodes and improving the policy.
  2. Only the return after the first occurrence of a state-action pair is used for its first-visit update in an episode.
  3. Q values are estimated by averaging stored returns for each state-action pair.
  4. The maximum-Q action becomes A*, but the policy remains ε-soft and keeps every action selectable with a minimum probability.
  5. To debug an unexpected result, trace episode generation, first-visit selection, return handling, Q averaging, and policy improvement in order.

Key Takeaways

  • First-visit Monte Carlo control learns through a repeating episode-generation and policy-improvement cycle.
  • The first occurrence of a state-action pair determines which return is used for its first-visit update.
  • Stored returns are averaged to estimate Q values.
  • The maximum-Q action is A*, while the complete policy remains ε-soft.
  • A reliable trace finds the first divergence by checking each stage in execution order.