Reinforcement learning studies how an agent improves behavior through interaction rather than
receiving the correct action for every situation. In this gridworld, actions produce immediate
rewards and new states. Q-learning uses those experiences to estimate which action is valuable
from each state.
State-action values and return
Q(s,a) estimates the discounted return obtained after taking action a
in state s and then following a specified policy. Tabular Q-learning is designed to
estimate the optimal action-value function Q* under standard assumptions. The return
combines immediate reward with later rewards, discounted by powers of γ.
The temporal-difference update
For a nonterminal transition from s to s' with reward r,
Q-learning applies
Q(s,a) ← Q(s,a) + α[r + γ maxa'Q(s',a') − Q(s,a)].
The bracketed term is the temporal-difference error. The learning rate α controls
update size. The discount factor γ controls the weight of estimated future return.
At a terminal state there is no future action value, so the target is the terminal reward alone.
Reward information propagates backward through repeated backups. A state several steps from the
goal gains value only after experience has connected it to valuable successor states.
Related algorithms in the selector
- Q-learning: uses the maximum next-state value in its target. It is off-policy
because behavior can be exploratory while the target evaluates a greedy next action.
- SARSA: uses the value of the next action actually selected by the behavior
policy. It is on-policy and therefore incorporates the risk created by continued exploration.
- Expected SARSA: averages next-action values under the current policy instead
of sampling only one next action.
- First-visit Monte Carlo control: waits for an episode to end and updates from
sampled returns without bootstrapping from a next-state estimate.
- Softmax exploration: converts action values into a temperature-controlled
probability distribution rather than using an epsilon-greedy random branch.
Exploration, discounting, and step cost
With epsilon-greedy behavior, the agent selects a random action with probability ε
and otherwise selects a currently greedy action. Setting ε = 0 can prevent reliable
discovery when initial tie-breaking and early experience favor an unhelpful route. Too much
exploration can also prevent behavior from settling.
Setting γ = 0 removes delayed value from the target, so only immediate rewards are
learned. A negative per-step reward makes shorter successful paths more valuable. With zero step
cost, multiple successful routes can have equal return, so the learned policy may be indifferent
among paths of different lengths.
Risk and the cliff comparison
In a stochastic or persistently exploratory environment, SARSA can prefer a safer route because
its target includes the actions the behavior policy may actually take. Q-learning evaluates the
greedy next action and may favor a shorter route near danger. This distinction is most clearly
studied in the dedicated cliff-walking comparison; it is not a guarantee that every pit layout
will produce the same two paths.
What this model leaves out
Convergence results for tabular Q-learning require conditions such as sufficient exploration,
suitable learning-rate schedules, and a stationary finite Markov decision process. Deep RL
replaces the table with function approximation and introduces replay buffers, target networks,
optimization instability, partial observability, and much larger state spaces. The applet keeps
the state and update visible so those later complications have a clear reference point.
For teachers
Curriculum: States, actions, rewards, discounted return, Q-values,
temporal-difference learning, on-policy versus off-policy learning, exploration, and terminal transitions.
Pre-exploration prompts:
- How can a reward at the goal influence an action several steps away?
- What is lost if an agent never tries an action that currently looks worse?
- How should a terminal transition differ from an ordinary transition?
Post-exploration prompts:
- Train from a zero table and trace the first nonzero values backward from a terminal reward.
- Compare
ε = 0 with moderate exploration over several resets, not just one run.
- Set
γ = 0. Which states can acquire nonzero values and why?
- Use slip and a high learning rate. Separate stochastic transition noise from update size.
- Compare Q-learning and SARSA in the cliff panel and explain the policy each target evaluates.
Misconceptions to test: Q-values are a prewritten plan; terminal states
bootstrap from themselves; more exploration is always better; one finite run establishes algorithmic convergence.
强化学习研究智能体如何通过交互改进行为,而不是在每种情形下都获得正确动作。在这个网格世界中,动作会产生即时奖励和新状态。Q 学习利用这些经验估计在每个状态下哪个动作更有价值。
状态动作价值与回报
Q(s,a) 估计在状态 s 采取动作 a 后,再遵循某一策略所获得的折扣回报。表格型 Q 学习在标准假设下用于估计最优动作价值函数 Q*。回报把即时奖励与后续奖励组合起来,后续奖励按 γ 的幂进行折扣。
时序差分更新
对于从 s 转移到非终止状态 s' 并获得奖励 r 的经验,Q 学习执行
Q(s,a) ← Q(s,a) + α[r + γ maxa'Q(s',a') − Q(s,a)]。
方括号中的量是时序差分误差。学习率 α 控制更新幅度,折扣因子 γ 控制估计未来回报的权重。到达终止状态后不存在未来动作价值,因此目标只包含终止奖励。
奖励信息通过反复回传向后传播。距离目标多步的状态,只有在经验把它连接到有价值的后继状态后才会获得价值。
选择器中的相关算法
- Q 学习:目标使用下一状态的最大动作价值。行为可以探索,而目标评估贪心下一动作,因此属于离策略方法。
- SARSA:使用行为策略实际选择的下一动作价值。它属于同策略方法,因此会把持续探索造成的风险纳入目标。
- Expected SARSA:按照当前策略对全部下一动作价值求平均,而不是只抽样一个下一动作。
- 首次访问蒙特卡洛控制:等待回合结束,再根据抽样回报更新,不从下一状态估计进行自举。
- Softmax 探索:把动作价值转换为受温度控制的概率分布,而不是使用 epsilon-greedy 的随机分支。
探索、折扣与步进成本
在 epsilon-greedy 行为中,智能体以概率 ε 随机选择动作,否则选择当前贪心动作。把 ε = 0 可能使发现过程不可靠,因为初始平局处理和早期经验可能偏向无效路线。探索过多也会使行为难以稳定。
把 γ = 0 会从目标中移除延迟价值,因此只学习即时奖励。负的每步奖励会让更短的成功路径具有更高价值。步进成本为零时,多条成功路线可能拥有相同回报,所以学习到的策略可能对不同长度路径没有偏好。
风险与悬崖比较
在随机环境或持续探索环境中,SARSA 的目标包含行为策略实际可能采取的动作,因此可能偏好更安全路线。Q 学习评估贪心下一动作,可能偏好靠近危险的较短路线。这种差异在专门的悬崖行走比较中最清楚,并不保证每一种陷阱布局都会产生相同的两条路线。
这个模型省略了什么
表格型 Q 学习的收敛结果需要充分探索、合适的学习率方案以及平稳有限马尔可夫决策过程等条件。深度强化学习用函数近似取代表格,并引入经验回放、目标网络、优化不稳定、部分可观测和更大的状态空间。本工具让状态和更新保持可见,为后续复杂情况提供清晰参照。
教师指导
课程目标:状态、动作、奖励、折扣回报、Q 值、时序差分学习、同策略与离策略学习、探索和终止转移。
探索前问题:
- 目标处的奖励如何影响相隔多步的动作?
- 如果智能体从不尝试当前看起来更差的动作,会失去什么?
- 终止转移与普通转移应有何不同?
探索后问题:
- 从全零表开始训练,追踪第一批非零价值如何从终止奖励向后传播。
- 在多次重置中比较
ε = 0 与适度探索,不要只看一次运行。
- 把
γ = 0。哪些状态能获得非零价值,为什么?
- 加入滑移并使用较高学习率。区分随机转移噪声与更新幅度。
- 在悬崖面板中比较 Q 学习与 SARSA,并解释每个目标评估的是哪一种策略。
需要检验的误解:Q 值是预先写好的计划;终止状态会从自身自举;探索越多越好;一次有限运行能够证明算法收敛。