-
Notifications
You must be signed in to change notification settings - Fork 0
/
SarsaAgent.py
32 lines (29 loc) · 1.08 KB
/
SarsaAgent.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
import numpy as np
from collections import defaultdict
from EpsilonDecay import EpsilonDecay
class SarsaAgent:
def __init__(self, nA, alpha, epsilon, gamma):
self.nA = nA
self.alpha = alpha
self.epsilon = epsilon
self.gamma = gamma
self.Q = defaultdict(lambda: np.zeros(self.nA))
def reset(self, state):
self.last_state = state
action = self.epsilon.greedy(self.Q[state])
self.last_action = action
return action
def step(self, next_state, reward, done):
# update previous timestep
state = self.last_state
action = self.last_action
# choose next action
self.epsilon.decay()
next_action = self.epsilon.greedy(self.Q[next_state])
# update Q for last state and last action
target = reward + self.gamma * self.Q[next_state][next_action]
self.Q[state][action] += self.alpha * (target - self.Q[state][action])
# update last state and last action
self.last_state = next_state
self.last_action = next_action
return next_action