본문으로 건너뛰기

RL 006

· 약 12분

Known MDP vs Unknown MDP

AspectKnown MDPUnknown MDP
Environment Knowledge (P,RP, R)Full knowledge of state transition dynamics PP and reward function RRPP and RR are unknown; agent must explore the environment
Problem ApproachWell-defined optimization problem \rightarrow Pure planning via computationTrial-and-error interaction \rightarrow Must learn and optimize simultaneously
Primary GoalDirectly compute the optimal policy (π\pi_*)Learn and optimize policies without prior model knowledge
Representative MethodsValue Iteration, Policy Iteration (Dynamic Programming)Monte Carlo, Q-Learning, SARSA (Reinforcement Learning)
ParadigmModel-based (Planning)Model-free (Reinforcement Learning)

Model-based vs Model-free

AspectModel-Based LearningModel-Free Learning
Model Access (P,RP, R)Has full access to transition dynamics PP and reward function RRNo access to environment dynamics (PP and RR are unknown)
Optimization ApproachDirect mathematical computation and planning using the known modelDirect learning and trial-and-error optimization through interaction
Estimation SourceComputes values and optimal policy π\pi_* via dynamic programming equationsEstimates vπ,qπv_\pi, q_\pi, and π\pi_* directly from sampled experience
Computation vs. SamplingPure computation / Planning (requires no real-time environment sampling)Sample-based learning (requires trajectories / episodic experience)
Typical MethodsPolicy Iteration, Value Iteration (DP)Monte Carlo Methods, TD Learning, Q-Learning, SARSA

Monte Carlo Methods

  • A Computational technique that uses random sampling and statistical methods to solve complex problems and estimate numerical results.
    • Named after the Monte Carlo Casino in Monaco.
    • It means Estimate an unknown quantity by sampling, and averaging what you observe.
  • Used fo any estimation method whose operation involves a significant random component.
  • Used in mathematics, physics, engineering, finance, computer science, etc.
  • It is particularly valuable when an analytical solution is difficult or impossible to obtain.

Estimating π\pi by Sampling (Monte Carlo Simulation)

  • Mechanism:
    • Generate uniform random points (x,y)[1,1]×[1,1](x, y) \in [-1, 1] \times [-1, 1].
    • Count points falling inside the unit circle (x2+y21x^2 + y^2 \le 1).
    • Estimate π\pi via area proportions:
AreacircleAreasquare=π4π4×#points inside circle#total points\begin{aligned} \frac{\text{Area}_{circle}}{\text{Area}_{square}} = \frac{\pi}{4} \\ & \pi \approx 4 \times \frac{\text{\#points inside circle}}{\text{\#total points}} \end{aligned}
  • Empirical Validation:
    • Total samples (nn): 30003000 points
    • Estimated value: π=3.1386666666666665\pi = 3.1386666666666665
    • Relative error: 0.0931%-0.0931\% from true value (3.14159265\approx 3.14159265)

Monte Carlo

Monte Carlo Prediction

vπ(s)=Eπ[GtSt=s]1Ni=1N(s)G(i)v_\pi(s) = \mathbb{E}_{\pi}[G_t | S_t = s] \approx \frac{1}{N} \sum_{i=1}^{N(s)} G^{(i)}

  • 1Ni=1N(s)G(i)\frac{1}{N} \sum_{i=1}^{N(s)} G^{(i)}: Mean of the returns/rewards
  • Model-Free Learning: Operates without prior knowledge of transition dynamics PP or reward function RR.
  • Learning from Complete Episodes: Learns state-value functions vπv_\pi exclusively from full trajectories under policy π\pi. Only works for episodic tasks where termination at step TT is guaranteed.
  • No Bootstrapping: Does not update estimates using other value estimates v(s)v(s'); instead, updates rely solely on actual empirical returns observed at the end of the episode.
  • Empirical Mean Returns: The value of state ss is estimated by the sample average of all observed returns starting from ss:

An Episode of Experience & Return Formulation

  • Trajectory Structure: S0A0,R1S1A1,R2S2AT1,RTSTS_0 \xrightarrow{A_0, R_1} S_1 \xrightarrow{A_1, R_2} S_2 \dots \xrightarrow{A_{T-1}, R_T} S_T
  • Discounted Return (GtG_t): Gt=Rt+1+γRt+2+γ2Rt+3++γTt1RTG_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots + \gamma^{T-t-1} R_T
  • Discount Factor (γ=1\gamma = 1): For episodic tasks with terminal-only rewards (such as Blackjack), setting γ=1\gamma = 1 is standard practice because the episode length is finite.

First-Visit Monte Carlo

  • First-Visit MC estimates the value function V(s)V(s) by averaging returns following only the first occurrence of state ss within each sampled episode.
  • Algorithmic Flow:
    • Initialize counters N(s)0N(s) \leftarrow 0 and accumulate returns: G(s)0sSG(s) \leftarrow 0 \quad \forall s \in S
    • Sample a complete episode i={Si,0,Ai,1,Ri,2,,Si,Ti}i = \{S_{i,0}, A_{i,1}, R_{i,2}, \dots, S_{i,T_i}\} generated under policy π\pi
    • Compute return Gi,t=k=t+1Tiγkt1Ri,kG_{i,t} = \sum_{k=t+1}^{T_i} \gamma^{k-t-1} R_{i,k} from step tt to termination.
    • Only for the first time step tt where state ss appears in episode ii:
      • N(s)N(s)+1N(s) \leftarrow N(s) + 1 (Increment counter for total first visits)
      • G(s)G(s)+Gi,tG(s) \leftarrow G(s) + G_{i,t} (Increment total return)
      • V(s)G(s)N(s)V(s) \leftarrow \frac{G(s)}{N(s)} (Update the value by mean return)

AR1=1BR2=2AR3=1CR4=1terminalA \xrightarrow{R_1=1} B \xrightarrow{R_2=2} A \xrightarrow{R_3 = 1} C \xrightarrow{R_4 = 1} \text{terminal}

State sN(s)N(s)G(s)G(s)V(s)=G(s)N(s)V(s) = \frac{G(s)}{N(s)}
A199.00
B188.00
C155.00

Every-Visit Monte Carlo

  • Every-Visit MC treats every single occurrence of state ss within an episode as a valid data sample, accumulating its corresponding return into the empirical mean.
  • Algorithmic Flow:
    • Initialize counters N(s)0N(s) \leftarrow 0 and accumulate returns: G(s)0sSG(s) \leftarrow 0 \quad \forall s \in S
    • Sample a complete episode i={Si,0,Ai,1,Ri,2,,Si,Ti}i = \{S_{i,0}, A_{i,1}, R_{i,2}, \dots, S_{i,T_i}\} generated under policy π\pi
    • Compute return Gi,t=k=t+1Tiγkt1Ri,kG_{i,t} = \sum_{k=t+1}^{T_i} \gamma^{k-t-1} R_{i,k} from step tt to termination.
    • For each time step tt where state ss appears in episode ii:
      • N(s)N(s)+1N(s) \leftarrow N(s) + 1 (Increment counter for total visits)
      • G(s)G(s)+Gi,tG(s) \leftarrow G(s) + G_{i,t} (Increment total return)
      • V(s)G(s)N(s)V(s) \leftarrow \frac{G(s)}{N(s)} (Update the value by mean return)

AR1=1BR2=2AR3=1CR4=1terminalA \xrightarrow{R_1=1} B \xrightarrow{R_2=2} A \xrightarrow{R_3 = 1} C \xrightarrow{R_4 = 1} \text{terminal}

State sN(s)N(s)G(s)G(s)V(s)=G(s)N(s)V(s) = \frac{G(s)}{N(s)}
A29+6=157.50
B188.00
C155.00
DimensionFirst-Visit MCEvery-Visit MC
Returns used per episodeOnly the first occurrenceEvery occurrence
Sample independenceIndependent across episodes (i.i.d.)Correlated within an episode (mitigated by sampling)
Bias (finite samples)UnbiasedBiased, but bias0\text{bias} \to 0
ConvergenceConsistent (Law of Large Numbers)Consistent (as episodes\text{episodes} \to \infty)
ImplementationRequires a "seen this episode?" checkSimpler — no check needed
Practical UseStandard theoretical baselineFrequently preferred (uses all available data)

Incremental Mean

μk=1kj=1kxj\mu_k = \frac{1}{k} \sum_{j=1}^{k} x_j

μk+1=1k+1j=1k+1xj=1k+1(j=1kxj+xk+1)=1k+1(kμk+xk+1)=μk+1k+1(xk+1μk)\begin{aligned} \mu_{k+1} &= \frac{1}{k+1} \sum_{j=1}^{k+1} x_j \\ &= \frac{1}{k+1} \left( \sum_{j=1}^{k} x_j + x_{k+1} \right) \\ &= \frac{1}{k+1} \left( k \mu_k + x_{k+1} \right) \\ &= \mu_k + \frac{1}{k+1} (x_{k+1} - \mu_k) \end{aligned}
  • New estimate = Old estimate + step-size * (new sample - old estimate).
  • Memory efficient: Only need to store the old estimate (μk\mu_k) and the number of visits (kk).

Incremental Monte Carlo

  • Updates the value function V(s)V(s) incrementally after each trajectory without requiring memory to store individual past returns.
  • Algorithmic Flow:
    • Initialize N(s)0N(s) \leftarrow 0 and V(s)0V(s) \leftarrow 0 for all sSs \in S.
    • Episode sampling: Execute episode ii under policy π\pi and compute returns Gi,t=k=t+1Tiγkt1RkG_{i,t} = \sum_{k=t+1}^{T_i} \gamma^{k-t-1} R_{k}.
    • Incremental update: For each visited state ss at time step tt:
      • N(s)N(s)+1N(s) \leftarrow N(s) + 1 (Increment counter for total visits)
      • V(s)V(s)+α(Gi,tV(s))V(s) \leftarrow V(s) + \alpha (G_{i,t} - V(s)) (Update the value by mean return)
      • Where Gi,tV(s)G_{i,t} - V(s) represents the temporal difference (TD) error between the sampled return target and the current estimate.
  • Choice step size α\alpha
    • α=1N(s)\alpha = \frac{1}{N(s)}: Replicates the exact empirical sample-average of Every-Visit MC where all observed samples receive equal weight.
    • α=constant\alpha = \text{constant}: Implements an exponential recency-weighted running average that decays outdated experience, making it suited for non-stationary environments where MDP dynamics change over time.
  • Cons:
    • Requires a significant number of episodes to converge to the true value function.
    • Requires to terminate the episode.

Model-free Control

  • The MDP model is unknown, or too large/complex to use directly.
  • But experience can be sampled.
  • Applications:
    • Robo soccer
    • Autonomous vehicles
    • Robotics
    • Game playing
    • Recommender systems
    • Finance & portfolio management

On-Policy vs Off-Policy

DimensionOn-Policy LearningOff-Policy Learning
DefinitionEvaluates and improves the exact same policy currently used to generate action decisionsEvaluates/improves a target policy (π\pi) while behaving via a different behavior policy (bb)
Data GenerationTrajectories must be generated by the current active policy π\piTrajectories can come from exploratory policies, historical logs, or human demonstrations
Exploration Trade-offMust balance exploration directly within the target policy (e.g., ϵ\epsilon-greedy)Separates exploration (behavior policy) from exploitation/optimality (target policy)
Representative AlgorithmsGLIE Monte Carlo Control, SARSAQ-Learning, Deep Q-Networks (DQN)

General Policy Iteration

  • Alternates between Policy Evaluation (VvπV \approx v_\pi or QqπQ \approx q_\pi) and Policy Improvement (πgreedy\pi \leftarrow \text{greedy}).
  • This interplay drives the system asymptotically toward the optimal pair (v,π)(v_*, \pi_*) or (q,π)(q_*, \pi_*).

π(s)=argmaxaA(R(s,a)+γsSP(ss,a)V(s))\pi'(s) = \text{argmax}_{a \in A} \big( \mathcal{R}(s, a) + \gamma \sum_{s' \in S} \mathcal{P}(s' | s, a) V(s') \big)

  • Why V(s)V(s) Fails in Model-Free Settings:
    • Finding the maximizing action over V(s)V(s) strictly requires the transition model P(ss,a)P(s' \mid s, a) and reward function R(s,a)R(s, a).
    • In Model-Free RL, these dynamics are unavailable; hence, V(s)V(s) alone cannot guide policy updates without an explicit environment model.

GPI

π(s)=argmaxaAQ(s,a)\pi'(s) = \text{argmax}_{a \in A}Q(s,a)

  • Model-Free Advantage:
    • By estimating Q(s,a)Q(s, a) directly through sample episodes, policy improvement requires no knowledge of PP or RR.
    • This fundamental shift makes Q(s,a)Q(s, a) estimation the standard paradigm for Model-Free Control (Monte Carlo Control, SARSA, Q-Learning).
V(s)V(s)Q(s,a)Q(s, a)
QuestionHow good is this state?How good is this action in this state?
Includes Action?NoYes
Policy EvaluationVery suitableSlightly more difficult
Policy ImprovementRequires a modelDirectly possible
Model-Free ControlInconvenientVery suitable

GPI with Q

  • This is why MC Control estimates qπ(s,a)q_\pi(s,a), not vπ(s)v_\pi(s).

Exploration Problem

a=argmaxaQ(s,a)a = \arg\max_a Q(s,a)
  • A greedy agent always chooses the action with the highest current estimated value.
  • But what if the current Q(s,a)Q(s,a) estimates are inaccurate?

Mystery Box Example

Initially,

Q(red)=0,Q(blue)=0Q(\text{red}) = 0, \qquad Q(\text{blue}) = 0
  1. Open the red box:
    R=0Q(red)=0R = 0 \rightarrow Q(\text{red}) = 0

  2. Open the blue box:
    R=+1Q(blue)=1R = +1 \rightarrow Q(\text{blue}) = 1

  3. Keep opening the blue box:
    R=+1,+3,+2,Q(blue)2R = +1, +3, +2, \ldots \rightarrow Q(\text{blue}) \approx 2

  4. A purely greedy agent now keeps choosing the blue box because
    Q(blue)>Q(red)Q(\text{blue}) > Q(\text{red}).

  5. However, the red box may actually be better. For example,
    E[Rred]=5\mathbb{E}[R \mid \text{red}] = 5.

    The agent does not know this because it sampled the red box only once.

Exploitation vs. Exploration

  • Exploitation: Choose the action that currently has the highest estimated value.
  • Exploration: Try less-sampled or unfamiliar actions to discover whether they may actually be better.
  • In this example, the agent should occasionally explore the red box because it has not been sampled enough to confidently conclude that it is worse.

ϵ\epsilon-Greedy Exploration

π(as)={greedy action with high probabilityrandom action with probability ϵ\pi(a|s) = \begin{cases} \text{greedy action \quad with high probability} \\ \text{random action \quad with probability $\epsilon$} \\ \end{cases}
  • if ϵ=0.1\epsilon = 0.1, then the agent will choose the greedy action with probability 0.90.9 (Exploitation)
  • And the random action with probability 0.10.1 (Exploration).
  • Policy Improvement Theorem:
    • for any ϵ\epsilon-greedy policy π\pi
    • The ϵ\epsilon-greedy policy π\pi' is as good or better than the original policy π\pi: vπ(s)vπ(s)v_{\pi'}(s) \geq v_{\pi}(s)

Monte Carlo Control

  • Traditional Policy Iteration:
    • Fix the policy π\pi.
    • Evaluate the current policy until QQ is sufficiently close to qπ(s,a)q_\pi(s,a).
    • Improve the policy using the estimated action values.
    • Repeat policy evaluation and policy improvement.
  • Monte Carlo Control:
    • Perform MC Policy Evaluation after every episode.
    • Improve the policy using ϵ\epsilon-greedy after every episode.
    • Q1π1=ϵ-greedy(Q1)Q_1 \rightarrow \pi_1 = \epsilon\text{-greedy}(Q_1)
    • Q2π2=ϵ-greedy(Q2)Q_2 \rightarrow \pi_2 = \epsilon\text{-greedy}(Q_2)
    • \cdots
    • Eventually approaches (q,π)(q_*, \pi_*) under appropriate convergence conditions.

MC Control

Evaluate a littleImproveEvaluate a littleImprove\text{Evaluate a little} \rightarrow \text{Improve} \rightarrow \text{Evaluate a little} \rightarrow \text{Improve} \rightarrow \cdots

GLIE

Greedy in the Limit with Infinite Exploration

  1. limkNk(s,a)=\lim_{k \to \infty} N_k(s,a) = \infty
  2. limkπk(as)=1(a=arg maxaAQk(s,a))\lim_{k \to \infty} \pi_k(a|s) = 1(a = \argmax_{a' \in A} Q_k(s,a'))
  • Explore enough early -> Explore less over time -> Eventually greedy.
    • Nk(s,a)N_k(s,a): How many times the state-action pair (s,a)(s,a) has been visited in the kk-th episode.
    • πkgreedy(Qk)\pi_k \rightarrow \text{greedy}(Q_k)
  • Infinite exploration first, greedy behavior in the limit.

GILE Monte Carlo Control

  • Algorithm flow:
    • Run episode k={S0,A0,R1,,ST}k = \{S_0, A_0, R_1, \dots, S_{T}\} with policy πk\pi_k.
    • For each state-action pair (s,a)(s,a) in the episode:
      • Nk(s,a)Nk(s,a)+1N_k(s,a) \leftarrow N_k(s,a) + 1
      • Qk(s,a)Qk(s,a)+1Nk(s,a)(GkQk(s,a))Q_k(s,a) \leftarrow Q_k(s,a) + \frac{1}{N_k(s,a)} (G_k - Q_k(s,a))
    • Improve the policy based on the new QQ value.
      • ϵ1k\epsilon \leftarrow \frac{1}{k}
      • πϵ-greedy(Q)\pi \leftarrow \epsilon\text{-greedy}(Q)
    • Repeat until convergence.
  • old estimate + step-size * (new sample - old estimate).
    • old estimate = Q(St,At)Q(S_t, A_t)
    • new sample = GtG_t
    • step-size = 1Nk(s,a)\frac{1}{N_k(s,a)}
  • Run Episode → Update Q → Reduce ϵ\epsilon → Improve Policy

DP vs MC

  • Dynamic Programming:
    • Model-based
    • Bootstrapping: Updating a value estimate using another estimated value instead of waiting for the final outcome.
    • Lower variance
  • Monte Carlo:
    • Model-free
    • No bootstrapping: It uses rewards that actually occurred, not an estimated value of the next state.
    • Uses complete sampled returns
    • Requires episodic tasks
    • Higher variance