good enough i guess
parent
85f81e5f23
commit
48a351518d
|
|
@ -72,20 +72,18 @@ def q_init():
|
|||
# print(list(q_table.items())[:5]) # Uncomment to see the first 5 entries
|
||||
return q_table
|
||||
|
||||
def epsilon_greedy(q, s, epsilon=0.1):
|
||||
def epsilon_greedy(q, s, epsilon=0.025):
|
||||
"""
|
||||
Return which direction Pacman should move to using epsilon-greedy algorithm
|
||||
With probability epsilon, choose a random action. Otherwise choose the greedy action.
|
||||
Avoids actions that would result in collision with ghost.
|
||||
"""
|
||||
# if np.random.random() < epsilon:
|
||||
# # Explore: choose random action (excluding blocked actions with Q=0)
|
||||
# valid_actions = [i for i in range(len(q[s])) if q[s][i] is not None]
|
||||
# if valid_actions:
|
||||
# return np.random.choice(valid_actions)
|
||||
# else:
|
||||
# return np.random.randint(0, len(q[s]))
|
||||
# else:
|
||||
if np.random.random() < epsilon:
|
||||
# Explore: choose random action (excluding blocked actions with Q=0)
|
||||
valid_actions = [i for i in range(len(q[s])) if q[s][i] is not None]
|
||||
return np.random.choice(valid_actions)
|
||||
|
||||
else:
|
||||
# Get all valid (non-blocked) actions with their Q-values
|
||||
valid_actions = [(i, q[s][i]) for i in range(len(q[s])) if q[s][i] is not None]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue