比較不同的策略
在 MyGridWorld 環境中,你有兩個對應於不同策略的狀態價值函式(value_function_1 與 value_function_2)。你的任務是逐一比較各狀態的價值,判斷哪一個策略更有效。
變數 num_states 已可供你使用。
本練習屬於課程
使用 Python 的 Gymnasium 進行強化學習
練習說明
- 建立布林值清單
one_is_better,其中每個元素用來檢查value_function_1中該狀態的數值是否大於或等於value_function_2中該狀態的數值。 - 建立布林值清單
two_is_better,其中每個元素用來檢查value_function_2中該狀態的數值是否大於或等於value_function_1中該狀態的數值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
value_function_1 = {0: 1, 1: 2, 2: 3, 3: 7, 4: 6, 5: 4, 6: 8, 7: 10, 8: 0}
value_function_2 = {0: 7, 1: 8, 2: 9, 3: 7, 4: 9, 5: 10, 6: 8, 7: 10, 8: 0}
# Check for each value in policy 1 if it is better than policy 2
one_is_better = [____ >= ____ for state in range(num_states)]
# Check for each value in policy 2 if it is better than policy 1
two_is_better = [____ >= ____ for state in range(num_states)]
if all(one_is_better):
print("Policy 1 is better.")
elif all(two_is_better):
print("Policy 2 is better.")
else:
print("Neither policy is uniformly better across all states.")