Follow a Policy چالش 5 از جلسهی 59، سخت
تابع follow(policy) را خودت بنویس: از START شروع کند و در هر خانه عملِ policy[state] را انجام دهد، تا وقتی به GOAL برسد یا ۲۰ قدم برود. لیست عملها و پاداش کل را برگرداند. برنامه یک سیاست خوب و یک سیاست بد (که بین دو خانه گیر میکند) را امتحان میکند. خروجی باید این باشد:good: R D D R R | reward 6bad: 20 steps | reward -20
MAZE = [
"S..#",
"#.#.",
"...G",
]
ACTIONS = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
START, GOAL = (0, 0), (2, 3)
def step(state, action):
r, c = state
dr, dc = ACTIONS[action]
nr, nc = r + dr, c + dc
if 0 <= nr < len(MAZE) and 0 <= nc < len(MAZE[0]) and MAZE[nr][nc] != "#":
return (nr, nc)
return state
def reward(state):
return 10 if state == GOAL else -1
def follow(policy):
moves = []
total = 0
# walk with the policy here
return moves, total
good = {(0, 0): "R", (0, 1): "D", (1, 1): "D", (2, 1): "R", (2, 2): "R"}
bad = {(0, 0): "R", (0, 1): "R", (0, 2): "L"}
moves, total = follow(good)
print("good:", " ".join(moves), "| reward", total)
moves, total = follow(bad)
print("bad:", len(moves), "steps | reward", total)
راهنما و جواب این چالش · برگشت به درس جلسهی 59: یادگیری تقویتی ۱ · چالش بعدی: Discounted Return
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال