-
Notifications
You must be signed in to change notification settings - Fork 0
/
sarsa.py
105 lines (81 loc) · 3.53 KB
/
sarsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import random
import numpy as np
class sarsa:
def __init__(self, game, num_episodes, alpha, epsilon, gamma):
self.num_episodes = num_episodes
self.alpha = alpha
self.epsilon = epsilon
self.gamma = gamma
self.game = game
self.Q = {}
def choose_action_epsilon_greedy(self, state):
if state not in self.Q:
return random.randint(0, len(self.game.action_space)-1)
else:
greedy_action_index = np.argmax(self.Q[state])
l = len(self.game.action_space)
probabilities = [1.0 - self.epsilon *
(1-(1/l))] + [self.epsilon/l for i in range(l-1)]
action_choices = [greedy_action_index] + \
[x for x in range(l) if x != greedy_action_index]
# print('probabilities {} action_choices {}'.format(probabilities,action_choices))
action = np.random.choice(action_choices, p=probabilities)
return action
def checkQ(self, state):
if state not in self.Q:
self.Q[state] = np.zeros(len(self.game.action_space))
def get_state(self, pacman, ghost, food_pellets):
return '{}, {}, {}'.format(pacman, ghost, sorted(food_pellets))
def train_agent(self,):
episode_steps = 0
total_steps = 0
for episode in range(self.num_episodes):
self.game.init()
pacman = self.game.pacman
ghost = self.game.ghost
food = self.game.food_pellets
state = self.get_state(pacman, ghost, food)
action = self.choose_action_epsilon_greedy(state)
self.checkQ(state)
total_steps += episode_steps
episode_steps = 0
while True:
episode_steps += 1
self.game.step(action)
reward = self.game.reward
next_pacman = self.game.pacman
next_ghost = self.game.ghost
next_food_pellets = self.game.food_pellets
next_state = self.get_state(
next_pacman, next_ghost, next_food_pellets)
next_action = self.choose_action_epsilon_greedy(next_state)
self.checkQ(next_state)
# SARSA
# print('state {} action {} next {} na {}'.format(state,action,next_state,next_action))
# print(self.Q)
self.Q[state][action] = self.Q[state][action] + self.alpha * \
(reward + self.gamma*self.Q[next_state]
[next_action] - self.Q[state][action])
state = next_state
action = next_action
if self.game.is_end():
break
if episode != 0 and episode % 1000 == 0:
print('Episode: {} Steps this episode: {} Average steps: {}'.format(
episode, episode_steps, round(total_steps/episode, 2)))
if episode % 10000 == 0:
print(
'-----------------------------------------------------------------------')
def test_agent(self,):
self.game.init()
self.game.display()
while True:
pacman = self.game.pacman
ghost = self.game.ghost
food = self.game.food_pellets
state = self.get_state(pacman, ghost, food)
action = self.choose_action_epsilon_greedy(state)
self.game.step(action)
self.game.display()
if self.game.is_end():
break