Skip to content

Guest. Introduction to Reinforcement Learning

Given by: Asst. Prof. Gokhan Alcan

  1. Define cost-to-go Vk(x)V_k(x) (min cost from time k to N)
  2. Split the problem ( Current step + Future)
  3. The Result (Recursive Optimality)

Pro:

  1. Decoupling: dont solve the full trajectory at once, solve one step at a time, working backwards from N to 0.
  2. The RL connection:
    • in optimal control, we calculate V using the model f.
    • in RL, we learn Q directly so we can pick u without needing f.
最优控制 (Optimal Control)强化学习 (Reinforcement Learning)
状态 (State) xkx_k状态 (State) sts_t
控制输入 (Control Input) uku_k动作 (Action) ata_t
阶段代价 (Stage Cost) (x,u)\ell(x, u)回报 (Reward) r(s,a)r(s, a) \approx -\ell
终端代价 (Terminal Cost) N(x)\ell_N(x)终端回报 (Terminal Reward)
动力学 (Dynamics) f(x,u)f(x, u)环境 (Env.) P(ss,a)P(s' \mid s, a)
时界 (Horizon) NN折扣因子 (Discount) γ\gamma
控制器 (Controller)策略 (Policy) π(as)\pi(a \mid s)
graph LR
    Agent --->|Action a_t| Environment
    Environment --->|State s_t+1 <br> Reward r_t| Agent

Maximize the expected cumulative discounted reward:

J(π)=Eτπ[t=0γtr(st,at)]J(\pi) = \mathbb{E}_{\tau \sim \pi} \left[ \sum_{t=0}^{\infty} \gamma^t r(s_t, a_t) \right]
  • γ[0,1)\gamma \in [0, 1):折扣因子 (Discount factor),在无限时界问题中替代了有限时界 NN
  • E\mathbb{E}:期望值 (Expectation),用于处理环境或策略中的随机性 (stochasticity)。
核心概念 (Concept)数学符号 (Notation)倒立摆示例 (Cart-Pole Example)
策略 (Policy)
(The Controller)
  • πθ(as)\pi_{\theta}(a \mid s) (随机性策略 Stochastic)
  • μθ(s)\mu_{\theta}(s) (确定性策略 Deterministic)
“如果杆子向右倾斜 (ss),以 0.9 的概率向右推 (aa)。”
价值函数 (Value Functions)
(The Critic)
  • Vπ(s)=E[γtrt]V^{\pi}(s) = \mathbb{E}\left[\sum \gamma^t r_t\right]
  • Qπ(s,a)Q^{\pi}(s, a)
  • VπV^{\pi}:“当前这个状态有多好?”
  • QπQ^{\pi}:“现在立刻向左推有多好?”
优势函数 (Advantage)
(Relative Value)
Aπ(s,a)=Qπ(s,a)Vπ(s)A^{\pi}(s, a) = Q^{\pi}(s, a) - V^{\pi}(s)“相比于当前策略的平均表现,做这个动作能好多少?”
轨迹 (Trajectory)
(Rollout)
τ=(s0,a0,r0,s1,)\tau = (s_0, a_0, r_0, s_1, \dots)从开始(杆子竖直)到失败(杆子倒下)的单次完整模拟运行。
  1. Environment Assumptions (The Physics)
    • Markov Property: the state sts_t is sufficient. History doesnt matter (ONLY the current state matter, its enough to transit to next state)
    • Stationarity: The environment dynamics P(ss,a)P(s'|s,a) do not change over time. (mass or something stays constant)
    • Ergodicity & Coverage: The agent can reach all relevant states with non-zero probability. (Implication: If we stop exploring (greedy), we violate this and stop learning)
  2. Structural Assumptions
    • Reward Boundedness: Rewards are finite. We use a discount factor γ<1\gamma < 1 to ensure the infinite sum converges:
    t=0γtrt<\sum_{t=0}^{\infty} \gamma^t r_t < \infty
    • Function Approximation Class
      • Linear: Convex optimization, convergence guarantees, able to find global optima.
      • Neural Networks: Non-convex, no strong guarantees, but scales to high dimensions (images, complex dynamics)
    • Deep Q-Networks
      • Qθ(s,a)Q_{\theta}(s,a): Parameterized by weights θ\theta.
      • Qϕ(s,a)Q_{\phi}(s,a): Parameterized by weight ϕ\phi (a frozen copy of θ\theta).
  1. recursive optimality(OC)
Vk(x)=minu[(x,u)+Vk+1(f(x,u))]V_k(x) = \min_{u} \Big[ \ell(x, u) + V_{k+1}(f(x, u)) \Big]
  1. Action value QkQ_k current state cost + remaining states’ within the bracket as the action value, consequently, the state value function can be simplified to minimising QkQ_k:
Qk(x,u):=(x,u)+Vk+1(f(x,u))Q_k(x, u) := \ell(x, u) + V_{k+1}(f(x, u)) Vk(x)=minuQk(x,u)V_k(x) = \min_{u} Q_k(x, u)
  1. subsitute Vk+1=minuQk+1V_{k+1} = \min_{u'} Q_{k+1} into Q. Recursive form, purely in QQ
Qk(x,u)=(x,u)+minuQk+1(f(x,u),u)Q_k(x, u) = \ell(x, u) + \min_{u'} Q_{k+1}\big(f(x, u), u'\big)
  1. Bellman optimality equation in RL
Q(s,a)=r(s,a)+γmaxaQ(s,a)Q^*(s, a) = r(s, a) + \gamma \max_{a'} Q^*(s', a')

Exploration policy: ϵ\epsilon - greedy schedule. Experience Replay: Buffer D of past transitions     \implies decorrelate samples. TD target & loss:: 在训练时,我们通过当前步的奖励 rr 和下一步的极大动作价值来构建 TD 目标值 (TD Target) yy

y=r+γmaxaQϕ(s,a)y = r + \gamma \max_{a'} Q_{\phi}(s', a')

随后,利用均方误差 (MSE) 构建 损失函数 (Loss Function),通过梯度下降来更新当前网络参数 θ\theta

L(θ)=(Qθ(s,a)y)2\mathcal{L}(\theta) = \big( Q_{\theta}(s, a) - y \big)^2

Target update rule

为了解决训练中的非平稳目标问题,DQN 引入了目标网络 (Target Network)

  • QθQ_{\theta}:当前网络 (Online Network),其参数 θ\theta 在每一步都在通过梯度下降进行实时更新。
  • QϕQ_{\phi}:目标网络 (Target Network),其参数 ϕ\phi 保持冻结,仅进行周期性同步 (Periodic copy)
ϕθ(every C steps)\phi \leftarrow \theta \quad (\text{every } C \text{ steps})

Explorations schedule The exploration rate ϵt\epsilon_t linearly decays from an initial value to a minimum floor over a fixed duration TdecayT_{decay}, then remains constant.

ϵt=max(ϵend,ϵstartϵstartϵendTdecayt)\epsilon_t = \max \left( \epsilon_{\text{end}}, \, \epsilon_{\text{start}} - \frac{\epsilon_{\text{start}} - \epsilon_{\text{end}}}{T_{\text{decay}}} \cdot t \right)

"Exploration"


  1. 动作选择 (Action Selection: ϵ\epsilon-Greedy)

在当前时间步,智能体以 ϵt\epsilon_t 的概率进行随机探索

atUniform(A)a_t \sim \text{Uniform}(\mathcal{A})

否则(以 1ϵt1 - \epsilon_t 的概率),根据当前策略网络选择能够带来最大估计价值的贪婪动作

at=argmaxaAQθ(st,a)a_t = \arg\max_{a \in \mathcal{A}} Q_{\theta}(s_t, a)
  1. 环境模拟步 (Simulation Step)

在环境中执行选择的动作 ata_t,并从环境反馈中获取以下信息:

  • 下一状态 st+1s_{t+1} (Next state)
  • 奖励 rtr_t (Reward)
  • 终止标志 dtd_t (Termination flag):若达到终止状态则 dt=1d_t = 1,否则 dt=0d_t = 0

  1. 数据存储与状态更新 (Storage & Update)

存储经验:将这一步产生的五元组(转移数据 Transition)存入经验回放缓冲区 (Replay Buffer) D\mathcal{D} 中:

DD{(st,at,rt,st+1,dt)}\mathcal{D} \leftarrow \mathcal{D} \cup \big\{ (s_t, a_t, r_t, s_{t+1}, d_t) \big\}

更新当前状态:向前推进时间步,将当前状态设为下一状态,进入新的循环:

stst+1s_t \leftarrow s_{t+1}

It carries everything needed to evaluate the TD target later: "Anatomy of a Replay Tuple" TD Target with Termination

yt=rt+(1dt)γmaxaQϕ(st+1,a)y_t = r_t + (1 - d_t) \gamma \max_{a'} Q_{\phi}(s_{t+1}, a')

Loss Function

L(θ)=(Qθ(st,at)yt)2\mathcal{L}(\theta) = \big( Q_{\theta}(s_t, a_t) - y_t \big)^2

For every train step:

从经验回放缓冲区 D\mathcal{D} 中随机抽取一个大小为 B=128B = 128 的小批次(Minibatch)数据:

{(sj,aj,rj,sj,dj)}j=1BD\{(s_j, a_j, r_j, s'_j, d_j)\}_{j=1}^B \sim \mathcal{D}

Compute Temporal Difference TD using Targe Network目标网络 QϕQ_\phi 计算离策(Off-Policy)的时序差分目标值 yjy_j此步骤不需要计算梯度

yj=rj+γ(1dj)maxaAQϕ(sj,a)y_j = r_j + \gamma \cdot (1 - d_j) \cdot \max_{a' \in \mathcal{A}} Q_\phi(s'_j, a')

计算目标网络输出的 yjy_j 与当前网络 QθQ_\theta 预测值之间的均方误差(MSE):

L(θ)=1Bj=1B(yjQθ(sj,aj))2\mathcal{L}(\theta) = \frac{1}{B} \sum_{j=1}^B \big( y_j - Q_\theta(s_j, a_j) \big)^2

对当前网络参数 θ\theta 执行梯度下降步以完成参数更新:

θθαθL(θ)\theta \leftarrow \theta - \alpha \cdot \nabla_\theta \mathcal{L}(\theta)

为了保证训练的稳定性,每隔固定的步数(满足 target_frequency 步数要求时),更新目标网络权重 ϕ\phi

  • Hard Update, τ=1.0\tau = 1.0:直接将当前网络的权重完全复制给目标网络: ϕθ\phi \leftarrow \theta
  • Soft Update, τ<1.0\tau < 1.0:让目标网络向当前网络平滑逼近: ϕτθ+(1τ)ϕ\phi \leftarrow \tau\theta + (1 - \tau)\phi

Hyper ParameteresMathClassical valueUsuage
经验回放区容量Capacity NN10,000存储历史转移数据 (s,a,r,s,d)(s, a, r, s', d) 的上限
优化器与学习率Adam (α\alpha)2.5×1042.5 \times 10^{-4}控制当前网络 θ\theta 梯度下降的步长
折扣因子γ\gamma0.99衡量未来长远奖励的衰减权重

Weight γ\gamma:

  • γ0\gamma \rightarrow 0: Greedy, only cares about the immediate reward rtr_t.
  • γ\gamma \rightarrow \infty: Strategic, values long term cumulative return.

Moving Target In standard supervised learning, targets(y) are fixed. In RL the target useing the changing network: Targetyt=r+γmaxaQθ(s,a)\text{Target} \quad y_t = r +\gamma\max_{a'} Q_{\theta}(s',a')

  • When we update weights θ\theta to approach y_t, the target y_t moves as well.
  • Result: Correlation loops, maximization bias, and divergence.

Solution: Twin Network QθQ_{\theta}, a clone of the main network that is frozen in time.

yt=r+γmaxaQϕ(s,a)y_t = r + \gamma \max_{a'} Q_\phi(s', a')
  • Main Net(Qθ)(Q_\theta): Updates every step.
  • Target Net(Qϕ)(Q_\phi): Frozen, Constant.
  • Sync: Every C steps, copy weights.
graph LR
    Main --->|Copy slow| Target
    Target --->|target y_t | Main

How do we encourage trying new things?

  • ϵ\epsilon-greedy (Discrete): Flip a coin. With prob ϵ\epsilon, take random action. Simple.
  • Entropy Reg. (Continuous): Add penalty +αH(π)+\alpha \mathcal{H}(\pi) to loss. Keeps policy uncertain to prevent premature convergence.
  • On-Policy (e.g., PPO): Use data generated only by current πk\pi_k. Stable, but sample inefficient (throws data away).
  • Off-Policy (e.g., DDPG, DQN): Use data from any past policy (Replay Buffer). Efficient, but mathematically risky (bias).

Neural Networks hate correlated data. Fixes:

  • Replay Buffers: Store (s,a,r,s)(s, a, r, s'). Sample random batches to break temporal correlations (i.i.d.).
  • Target Networks: Compute loss targets with a frozen network copy. Prevents “chasing your own tail.”
  • Discrete: Finite choices (e.g., Left/Right). Output is a probability mass. (Ex: DQN, Q-Learning)
  • Continuous: Real-valued vectors (e.g., Torques). Output is a distribution or value. (Ex: DDPG, SAC)
  • Model-Based: Uses dynamics f(s,a)f(s, a) to plan. High sample efficiency. (Ex: Dyna-Q, AlphaZero)
  • Model-Free: Learns mapping sas \rightarrow a via trial-and-error. Low bias, easier impl. (Ex: DDPG, DQN)
  • On-Policy: Learns only from current policy π\pi. Stable but sample heavy. (Ex: PPO, TRPO)
  • Off-Policy: Learns from Replay Buffer (old data). Efficient but complex. (Ex: SAC, DQN)
  • Value-Based: Learns Q(s,a)Q(s, a). Implicit policy. (Ex: DQN - Discrete only)
  • Policy-Based: Optimizes πθ\pi_\theta directly via gradients. (Ex: REINFORCE)
  • Actor-Critic: Hybrid. Actor (π\pi) acts, Critic (VV) judges to reduce variance. (Ex: PPO, SAC)
  • Unconstrained: Maximize J=rJ = \sum r. Violations punished by negative reward. (Ex: Vanilla RL)
  • Constrained (CMDP): Max JJ s.t. cost C<dC < d. Uses Lagrangian multipliers. (Ex: CPO)
  • Safe / Filtered: Safety layer (Model/Barrier) overrides dangerous actions. (Ex: MPC-in-loop)