定義 epsilon-greedy 函式
在 RL 中,epsilon-greedy 策略是在探索與開發之間取得平衡。此方法會以機率 epsilon 選擇隨機動作,以機率 1-epsilon 選擇目前已知最好的動作。實作 epsilon_greedy() 函式對於 Q-learning 和 SARSA 等演算法至關重要,因為它同時確保代理人能探索環境並善用已知回饋,促進學習流程;這也是本練習的目標。
已經將 numpy 函式庫匯入為 np。
本練習屬於課程
使用 Python 的 Gymnasium 進行強化學習
練習說明
- 在函式內,寫出代理人進行環境探索時的合適條件。
- 探索時,選擇隨機的
action。 - 開發時,依照
q_table選擇最佳的action。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
epsilon = 0.2
env = gym.make('FrozenLake')
q_table = np.random.rand(env.observation_space.n, env.action_space.n)
def epsilon_greedy(state):
# Implement the condition to explore
if ____ < ____:
# Choose a random action
action = ____
else:
# Choose the best action according to q_table
action = ____
return action