Q-learning で 8x8 Frozen Lake を攻略する
この演習では、Q-learning アルゴリズムを用いて、8x8 の Frozen Lake 環境で最適な方策を学習します。今回は "slippery"(滑りやすい)設定が有効になっています。この条件により遷移が確率的になり、エージェントの動きが予測しにくくなるため、より現実に近い状況を再現できます。
あらかじめ初期化済みの Q-table Q と、前の演習で使った update_q_table() 関数、そして各エピソードで得られた合計報酬を格納する空のリスト rewards_per_episode が読み込まれています。
この演習はコースの一部です
Pythonで学ぶGymnasiumによるReinforcement Learning
演習の手順
- 各エピソードで、選択した行動を実行し、報酬と次状態を観測します。
- 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))