開始使用免費開始

Expected SARSA 更新規則

在這個練習中,你將實作 Expected SARSA 的更新規則,這是一種時間差分的無模型(model-free)強化學習演算法。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 值。
  • 假設智能體在狀態 2 採取動作 1,移動到狀態 3,並得到獎勵 5,請更新 Q 表 Q

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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)
編輯並執行程式碼