开始使用免费开始使用

Expected SARSA 更新规则

在本练习中,您将实现 Expected SARSA 更新规则,这是一种时序差分的无模型强化学习算法。Expected SARSA 通过对所有可能动作取平均来估计当前策略的期望值,相比 SARSA 提供更稳定的更新目标。下方给出了 Expected SARSA 使用的公式。

Image showing the mathematical formula of the expected SARSA update rule.

已将 numpy 库导入为 np

本练习是课程的一部分

Python 中的 Gymnasium 强化学习

查看课程

练习说明

  • 计算 next_state 的期望 Q 值。
  • 使用 Expected SARSA 公式更新当前 stateaction 的 Q 值。
  • 更新 Q 表 Q,假设智能体在状态 2 选择动作 1,转移到状态 3,并获得奖励 5

交互式实操练习

通过完成这段示例代码来试试这个练习。

def update_q_table(state, action, next_state, reward):
  	# Calculate the expected Q-value for the next state
    expected_q = ____
    # Update the Q-value for the current state and action
    Q[state, action] = ____
    
Q = np.random.rand(5, 2)
print("Old Q:\n", Q)
alpha = 0.1
gamma = 0.99

# Update the Q-table
update_q_table(____, ____, ____, ____)
print("Updated Q:\n", Q)
编辑并运行代码