Skip to content
AI360Xpert
Beta

Monte Carlo Exploring Starts

Monte Carlo with Exploring Starts guarantees comprehensive state-action exploration by beginning episodes at every state-action pair with non-zero probability.

Monte Carlo with Exploring Starts selects an initial state-action pair uniformly at random and executes a deterministic greedy policy to the terminal state.
Monte Carlo with Exploring Starts selects an initial state-action pair uniformly at random and executes a deterministic greedy policy to the terminal state.

Why Does This Exist?

In model-free reinforcement learning, finding an optimal policy requires learning action values Q(s,a)Q(s, a). However, if an agent acts deterministically according to its current greedy policy, it will only ever experience the actions it already believes are best. Suboptimal early estimates can permanently lock the agent into choosing poor actions, leaving genuinely optimal paths unvisited.

Without an environment transition model, you cannot compute expectations across untried branches. The classic solution is the Exploring Starts assumption: guaranteeing that every possible state-action pair (s,a)(s, a) has a strictly positive probability of being chosen as the initial step of an episode. This ensures infinite exploration across all choices in the limit, allowing the agent to follow a purely greedy deterministic policy for all subsequent steps.

Think of It Like This

The Flight Simulator Scenario Selector

Imagine a flight simulator designed to train pilots for every emergency scenario. If the simulation always started on the runway on a calm sunny day, a pilot following standard procedure would never experience engine failure at 30,000 feet or crosswind landings at night.

To ensure complete competence across the entire state-action space, the flight instructor uses an exploring starts protocol: the simulator initializes the airplane at an arbitrarily chosen altitude, airspeed, and weather condition, with the pilot forced to take a specific initial control input (e.g., full left rudder). Once that first step is taken, the pilot is evaluated purely on their standard emergency playbook. Over thousands of simulated flights starting from every possible altitude and rudder position, every action in every state is thoroughly evaluated without having to introduce random control wobbles during normal flight.

How It Actually Works

The Exploring Starts Protocol and Policy Iteration

Monte Carlo with Exploring Starts (MCES) alternates between sample-based policy evaluation and greedy policy improvement:

  1. Episode Initialization: Select S0∈SS_0 \in \mathcal{S} and A0∈A(S0)A_0 \in \mathcal{A}(S_0) such that P(S0=s,A0=a)>0P(S_0 = s, A_0 = a) > 0 for all s,as, a.
  2. Deterministic Trajectory: For t=1,2,…,T−1t = 1, 2, \dots, T-1, sample actions strictly according to the current deterministic policy: At=π(St)A_t = \pi(S_t)
  3. Return Computation: Backward iterate from terminal time TT to compute returns: Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}
  4. Value Update (First-Visit): For each pair (s,a)(s, a) appearing in the episode, record the first occurrence time τ\tau: Q(s,a)←Q(s,a)+1N(s,a)[Gτ−Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \frac{1}{N(s, a)} \left[ G_\tau - Q(s, a) \right]
  5. Greedy Improvement: Update the policy immediately for each state visited: π(s)←arg⁡max⁡aQ(s,a)\pi(s) \leftarrow \arg\max_{a} Q(s, a)

Because exploring starts handles the exploration burden on step 00, the policy π\pi does not need an exploration parameter like ε\varepsilon. It remains purely deterministic and greedy.

Worked numerical example

Consider a simplified grid navigation task with states S1,S2S_1, S_2 and actions {Left,Right}\{\text{Left}, \text{Right}\}. The discount factor is γ=0.90\gamma = 0.90.

Suppose initial estimates are Q(S1,Left)=2.0Q(S_1, \text{Left}) = 2.0, Q(S1,Right)=1.0Q(S_1, \text{Right}) = 1.0, meaning the initial greedy policy is π(S1)=Left\pi(S_1) = \text{Left}.

  • Episode 1: Exploring starts selects S0=S1S_0 = S_1 and forces exploratory action A0=RightA_0 = \text{Right}.
    • The transition yields reward R1=+4.0R_1 = +4.0 and transitions to S2S_2.
    • From S2S_2, the agent follows π(S2)=Right\pi(S_2) = \text{Right}, receives R2=+10.0R_2 = +10.0, and reaches the terminal state.
  • Return Calculation:
    • G1=10.0G_1 = 10.0
    • G0=R1+γG1=4.0+0.90×10.0=13.0G_0 = R_1 + \gamma G_1 = 4.0 + 0.90 \times 10.0 = 13.0
  • Value Update:
    • Prior N(S1,Right)=1N(S_1, \text{Right}) = 1, prior Q=1.0Q = 1.0.
    • New count N=2N = 2.
    • Updated Q(S1,Right)=1.0+12(13.0−1.0)=7.0Q(S_1, \text{Right}) = 1.0 + \frac{1}{2}(13.0 - 1.0) = 7.0.
  • Policy Improvement:
    • Compare arg⁡max⁡a{Q(S1,Left)=2.0,Q(S1,Right)=7.0}\arg\max_a \{ Q(S_1, \text{Left})=2.0, Q(S_1, \text{Right})=7.0 \}.
    • The greedy policy updates: π(S1)←Right\pi(S_1) \leftarrow \text{Right}.

The forced initial exploration discovered a path worth 13.013.0 that the initial greedy policy would have ignored forever.

Code

from collections import defaultdictimport random
class MonteCarloExploringStarts:    def __init__(self, states: list[str], actions: list[str], gamma: float = 0.9):        self.states = states        self.actions = actions        self.gamma = gamma        self.Q = defaultdict(float)        self.returns_count = defaultdict(int)        self.policy = {s: random.choice(actions) for s in states}
    def generate_episode(self, env_step_fn) -> list[tuple[str, str, float]]:        # Exploring starts: pick S0 and A0 uniformly at random        s0 = random.choice(self.states)        a0 = random.choice(self.actions)        trajectory = []
        s = s0        a = a0        while True:            next_s, reward, done = env_step_fn(s, a)            trajectory.append((s, a, reward))            if done:                break            s = next_s            a = self.policy[s]  # Strictly follow deterministic policy thereafter        return trajectory
    def update_policy(self, trajectory: list[tuple[str, str, float]]) -> None:        G = 0.0        visited = set()        # Backward return accumulation        for s, a, r in reversed(trajectory):            G = self.gamma * G + r
        # First-visit MC update for the starting pair        s0, a0, _ = trajectory[0]        self.returns_count[(s0, a0)] += 1        n = self.returns_count[(s0, a0)]        self.Q[(s0, a0)] += (G - self.Q[(s0, a0)]) / n
        # Greedy policy improvement        best_action = max(self.actions, key=lambda act: self.Q[(s0, act)])        self.policy[s0] = best_action
# Simulated 2-state environment stepdef dummy_env(s: str, a: str) -> tuple[str, float, bool]:    if s == "S1" and a == "Right":        return "S2", 4.0, False    return "Terminal", 10.0, True
agent = MonteCarloExploringStarts(states=["S1", "S2"], actions=["Left", "Right"])sample_traj = [("S1", "Right", 4.0), ("S2", "Right", 10.0)]agent.update_policy(sample_traj)print("Updated Q(S1, Right):", agent.Q[("S1", "Right")])print("Updated Policy for S1:", agent.policy["S1"])# -> Updated Q(S1, Right): 13.0# -> Updated Policy for S1: Right

Watch Out For

Physical Realizability in Real-World Systems

The Exploring Starts assumption is mathematically elegant in simulated environments like games or board scenarios, but it is physically impossible in most real-world applications. A real autonomous vehicle cannot be randomly initialized at 100 km/h100\text{ km/h} perpendicular to oncoming highway traffic, and an industrial chemical reactor cannot be safely initialized at explosive core temperatures.

When exploring starts cannot be physically guaranteed, relying on this algorithm leads to complete policy stagnation. In these scenarios, use ε\varepsilon-soft on-policy control or off-policy importance sampling to explore safely from natural start states.

The Quick Version

  • Exploring Starts guarantees that every state-action pair (s,a)(s, a) has non-zero probability of being the initial step of an episode.
  • By guaranteeing exploration on step t=0t=0, the policy π\pi can remain strictly deterministic and greedy on all subsequent steps.
  • Monte Carlo updates wait for episode termination and compute unbiased returns by backward accumulation.
  • The assumption is viable in simulators and games but physically impractical in real-world robotics and industrial control.