用 SARSA 解 8x8 Frozen Lake
在這個練習中,你會套用 SARSA 演算法,並結合你先前實作的 update_q_table() 函式,來為 8x8 Frozen Lake 環境學得一個最佳策略。這個環境與經典的 4x4 版本相同,只是規模更大。你將使用 SARSA 演算法,根據從環境獲得的報酬,反覆改進智能體的策略。
我們已經為你初始化並載入了一個 Q-table Q,同時也提供了前一個練習中的 update_q_table() 函式。
本練習屬於課程
使用 Python 的 Gymnasium 進行強化學習
練習說明
- 在訓練過程的每個 episode 中,執行所選的
action。 - 隨機選擇
next_action。 - 針對給定的
state與action更新 Q-table。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
for episode in range(num_episodes):
state, info = env.reset()
action = env.action_space.sample()
terminated = False
while not terminated:
# Execute the action
next_state, reward, terminated, truncated, info = ____
# Choose the next action randomly
next_action = ____
# Update the Q-table
____
state, action = next_state, next_action
render_policy(get_policy())