-
Notifications
You must be signed in to change notification settings - Fork 1
/
sarsa.py
27 lines (21 loc) · 994 Bytes
/
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
import numpy as np
class SARSA:
def __init__(self, alpha, gamma, epsilon, number_of_states, number_of_actions):
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
self.number_of_states = number_of_states
self.number_of_actions = number_of_actions
self.q_table = np.zeros((self.number_of_states, self.number_of_actions))
def choose_action(self, state):
if np.random.uniform(0, 1) < self.epsilon:
action = np.random.choice(self.number_of_actions)
else:
action = np.argmax(self.q_table[state, :])
return action
def update(self, state, action, reward, next_state, next_action):
predict = self.q_table[state, action]
target = reward + self.gamma * self.q_table[next_state, next_action]
self.q_table[state, action] += self.alpha * (target - predict)
def reset(self):
self.q_table = np.zeros((self.number_of_states, self.number_of_actions))