开始使用免费开始使用

实现 SARSA 更新规则

SARSA 是一种基于策略(on-policy)的强化学习算法,它根据在当前状态下采取的动作以及在下一状态中选择的动作来更新动作价值函数。该方法不仅学习当前状态-动作对的价值,也学习后续状态-动作对的价值,从而能够获得考虑未来动作的策略。下面给出了 SARSA 的更新规则。您的任务是实现一个函数,按照该规则更新 Q 表。

NumPy 库已按 np 导入。

Image showing the mathematical formula of the SARSA update rule.

本练习是课程的一部分

Python 中的 Gymnasium 强化学习

查看课程

练习说明

  • 读取给定状态-动作对的当前 Q 值。
  • 找到下一状态-动作对的 Q 值。
  • 使用 SARSA 公式更新当前状态-动作对的 Q 值。
  • 在智能体于状态 0 采取动作 0、获得奖励 5、转移到状态 1 并在该状态执行动作 1 的条件下,更新 Q 表 Q

交互式实操练习

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

def update_q_table(state, action, reward, next_state, next_action):
  	# Get the old value of the current state-action pair
    old_value = ____
    # Get the value of the next state-action pair
    next_value = ____
    # Compute the new value of the current state-action pair
    Q[(state, action)] = ____

alpha = 0.1
gamma  = 0.8
Q = np.array([[10,0],[0,20]], dtype='float32')
# Update the Q-table for the ('state1', 'action1') pair
____
print(Q)
编辑并运行代码