開始使用免費開始

使用 Q-learning 解 8x8 Frozen Lake

在這個練習中,你將套用 Q-learning 演算法,學習在 8x8 Frozen Lake 環境中導航的最適策略,這次會啟用「slippery」條件。這項挑戰會引入隨機轉移,使代理的移動變得不可預測,更貼近真實情境。

我們已為你初始化並載入一個 Q-table Q,同時提供上一個練習的 update_q_table() 函式,還有一個空的串列 rewards_per_episode,用來記錄每個 episode 的總累積報酬。

本練習屬於課程

使用 Python 的 Gymnasium 進行強化學習

檢視課程

練習說明

  • 每個 episode 中,執行所選動作並觀察 reward 和下一個狀態。
  • 更新 Q-table。
  • total_reward 加到 rewards_per_episode 串列。

動手互動練習

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

for episode in range(10000):
    state, info = env.reset()
    total_reward = 0
    terminated = False
    while not terminated:
        action = env.action_space.sample()
        # Execute the action
        next_state, reward, terminated, truncated, info = ____
        # Update the Q-table
        ____
        state = next_state
        total_reward += reward
    # Append the total reward to the rewards list    
    rewards_per_episode.____(____)
print("Average reward per random episode: ", np.mean(rewards_per_episode))
編輯並執行程式碼