Sarsa(λ) and Q(λ) in Tabular Case

简介: ‘Thanks R. S. Sutton and A. G. Barto for their great work in Reinforcement Learning: An Introduction.Eligibility Traces in Prediction ProblemsIn the backward view of TD(λ)TD(\lambda), t

‘Thanks R. S. Sutton and A. G. Barto for their great work in Reinforcement Learning: An Introduction.

Eligibility Traces in Prediction Problems

In the backward view of TD(λ), there is a memory variable associated with each state, its eligibility trace. The eligibility trace for state s at time t is a random variable denoted Zt(s)∈R+. On each step, the eligibility traces for all states decay by γλ, and the eligibility trace for the one state visited on the step is incremented by 1:

Zt(s)={γλZt−1(s)γλZt−1(s)+1s≠Sts=St

for all nonterminal states s, where γ is the discount rate and λ is the λ-return 1 parameter or trace-decay parameter. This kind of eligibility trace is called an accumulating trace. The global TD error signal triggers proportional updates to all recently visited states, as signaled by their nonzero traces:
ΔVt(s)=αδtZt(s),∀s∈S

where
δt=Rt+1+γVt(St+1)−Vt(St)

A complete prediction algorithm for on-line TD(λ) is given as follows:
  1. Initialize V(s) arbitrarily.
  2. Repeat for each episode:
    1. Initialize Z(s)=0 for all s∈S.
    2. Repeat for each step of episode:
      1. A← action given by π for S.
      2. Observe reward R and the next state S′.
      3. δ←R+γV(S′)−V(S)
      4. Z(S)←Z(S)+1
      5. For all s∈S:
        1. V(s)←V(s)+αδZ(s)
        2. Z(s)←γλZ(s)
      6. S←S′
    3. Until S is terminal.

Sarsa(λ)

The main idea of control is simply to learn action values rather than state values. The idea in Sarsa(λ) is to apply the TD(λ) prediction method to state-action pairs. Let Zt(s,a) denote the trace for state-action pair s,a. Substitute state-action variables for state variables, i.e.

Qt+1(s,a)=Qt(s,a)+αδtZt(s,a),∀s,a

where
δt=Rt+1+γQt(St+1,At+1)−Qt(St,At)

and
Zt(s,a)={γλZt−1(s,a)+1γλZt−1(s,a)s=St,a=Atotherwise for all s,a

The complete Sarsa( λ) algorithm is given as follows:
  1. Initialize Q(s,a) arbitrarily for all s∈S,a∈A(s)
  2. Repeat for each episode:
    1. Z(s,a)=0 for all s∈S,a∈A(s)
    2. Repeat for each step of episode:
      1. Take action A, observe R, S′.
      2. Choose A′ from S′ using policy derived from Q.
      3. δ←R+γQ(S′,A′)−Q(S,A)
      4. Z(S,A)←Z(S,A)+1
      5. For all s∈S,a∈A(s):
        1. Q(s,a)←Q(s,a)+αδZ(s,a)
        2. Z(s,a)←γλZ(s,a)
      6. S←S′,A←A′
    3. Unitl S is terminal.

Q(λ)

There are two different methods have been proposed that combine eligibility traces and Q-Learning: the Watkins’s Q(λ) and Peng’s Q(λ). Here we focus on Watkin’s Q(λ) only and give a brief description of Peng’s Q(λ).

Unlike TD(λ) and Sarsa(λ), Watkins’s Q(λ) does not look ahead all the way to the end of the episode in its backup. It only looks ahead as far as the next exploratory action. For example, suppose the first action, At+1, is exploratory. Watkins’s Q(λ) would still do the one-step update of Qt(St,At) toward Rt+1+γmaxaQ(St+1,a). In general, if At+n is the first exploratory action, then the longest backup is toward

Rt+1+γRt+1+⋯+γn−1Rt+n+γnmaxaQ(St+n,a)

where we assume off-line updating.

The mechanistic or backward view of Watkins’s Q(λ) is also very simple. Eligibility traces are used just as in Sarsa(λ), except that they are set to zero whenever an exploratory action is taken. That is to say. First, the traces for all state-action pairs are either decayed by γλ or, if an exploratory action was taken, set to 0. Second, the trace corresponding to the current state and action is incremented by 1, i.e.

Zt(s,a)=IsSt⋅IaAt+⎧⎩⎨γλZt−1(s,a)0 if Qt−1(St,At)=maxaQt−1(St,a) otherwise

where Ixy is an identity indicator function, equal to 1 if x=y and 0 otherwise. The rest of the algorithm is defined by
Qt+1(s,a)=Qt(s,a)+αδtZt(s,a),∀s∈S,a∈A(s)

where
δt=Rt+1+γmaxa′Qt(St+1,a′)−Qt(St,At)

The complete algorithm is given below:
  1. Initialize Q(s,a) arbitrarily, for all s∈S,a∈A(s).
  2. Repeat for each episode:
    1. Z(s,a)=0 for all s∈S,a∈A(s)
    2. Repeat for each step of episode:
      1. Take action A, observe R, S′
      2. Choose A′ from S′ using policy derived from Q.
      3. A∗←argmaxaQ(S′,a), ifA′ ties for the max, then A∗←A′
      4. δ←R+γQ(S′,A∗)−Q(S,A)
      5. Z(S,A)←Z(S,A)+1
      6. For all s∈S,a∈A(s):
        1. Q(s,a)←Q(s,a)+αδZ(s,a)
        2. If A′=A∗, then Z(s,a)←γλZ(s,a), else Z(s,a)←0.
      7. S←S′,A←A′
    3. Until S is terminal

Unfortunately, cutting off traces every time an exploratory action is taken loses much of the advantage of using eligibility traces. Peng’s Q(λ) is an alternate version of Q(λ) meant to remedy this, which can be thought of as a hybrid of Sarsa(λ) and Watkins’s Q(λ). In Peng’s Q(λ), there is no distinction between exploratory and greedy actions. Each component beckup is over many steps of actual experiences, and all but the last are capped by a final maximization over actions. For a fixed nongreedy policy, Qt converges to neither qπ nor q∗, but to some hybrid of the two. However, if the policy is gradually made more greedy, then the method may still converge to q∗.

Replacing Traces

In some cases significantly better performance can be obtained by using a slightly modified kind of trace known as replacing trace:

Zt(s)={γλZt−1(s)1s≠Sts=St

There are several possible ways to generalize replacing eligibility traces for use in control methods. In some cases, the state-action traces are updated by the following:
Zt(s,a)=⎧⎩⎨⎪⎪10γλZt−1(s,a)s=St,a=Ats=St,a≠Ats≠St

  1. λ-return: Gλt=(1−λ)∑∞n=1λn−1G(n)t↩
相关文章
|
人工智能 JavaScript Java
阿里云百炼API调用教程:准备API-Key、配置环境变量和调用API流程
本文介绍阿里云百炼API调用全流程:注册登录阿里云账号,开通百炼服务,创建并配置API Key至环境变量,避免硬编码风险。支持通过Python的OpenAI兼容接口或DashScope SDK调用大模型,亦可在Node.js、Java等环境中使用。附详细命令与代码示例,助您快速上手百炼AI大模型平台。
5818 1
|
监控 网络安全 数据库
2025 ERPNext 一键部署方案
随着企业数字化转型加速,开源ERP系统ERPNext因其功能全面、灵活易定制等特点备受青睐。然而,其部署涉及Python环境、数据库配置等技术门槛。本文介绍如何通过Websoft9自动化工具一键部署ERPNext,简化流程,降低技术难度,帮助用户快速搭建稳定可用的企业管理系统。
2243 0
2025 ERPNext 一键部署方案
|
Web App开发 前端开发 JavaScript
《WASM驱动本地PDF与Excel预览组件的深度实践》
本文围绕前端浏览器端本地文件处理痛点,提出以WASM驱动PDF、Excel等复杂格式文件解析与预览的解决方案。首先剖析传统前端解析方案的性能短板,阐述WASM将底层解析逻辑移植到浏览器的核心价值;接着拆解组件构建关键环节,包括WASM模块与前端的通信设计、文件流本地处理策略、跨格式解析适配逻辑,以及预览渲染层的优化思路;还探讨兼容性适配、内存管理等核心难点,结合实际场景验证方案在加载速度、资源占用上的优势,为前端实现高效、安全的本地文件处理提供了可落地的技术路径。
464 5
|
安全 算法 Shell
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8057 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18278 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全