-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
69 lines (63 loc) · 2.43 KB
/
agent.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
import numpy as np
import random
from collections import defaultdict
class Agent:
def __init__(self, Q, mode="mc_control", nA=6, alpha=0.01, gamma=0.99):
self.Q = Q
self.mode = mode
self.nA = nA
self.alpha = alpha
self.gamma = gamma
if mode == "mc_control":
self.step = self.step_mc_control
self.alpha = 0.01 # Optimal
self.gamma = 0.9 # Optimal
self.episode = list()
elif mode == "q_learning":
self.step = self.step_q_learning
self.alpha = 0.2 # Optimal
self.gamma = 0.8 # Optimal
def select_action(self, state, eps):
"""
Params
======
- state: the current state of the environment
- eps: the threshold value to decide exploration or exploitation
Returns
=======
- action: an integer, compatible with the task's action space
"""
if random.random() > eps:
return np.argmax(self.Q[state])
else:
return np.random.choice(self.nA)
def step_mc_control(self, state, action, reward, next_state, done):
"""
Params
======
- state: the previous state of the environment
- action: the agent's previous choice of action
- reward: last reward received
- next_state: the current state of the environment
- done: whether the episode is complete (True or False)
"""
if done:
rewards = defaultdict(lambda: np.zeros(self.nA))
for history in reversed(self.episode):
state, action, reward = history
rewards[state][action] = reward + self.gamma * rewards[state][action]
self.Q[state][action] += self.alpha * (rewards[state][action] - self.Q[state][action])
self.episode.clear()
else:
self.episode.append((state, action, reward))
def step_q_learning(self, state, action, reward, next_state, done):
"""
Params
======
- state: the previous state of the environment
- action: the agent's previous choice of action
- reward: last reward received
- next_state: the current state of the environment
- done: whether the episode is complete (True or False)
"""
self.Q[state][action] += self.alpha * (reward + self.gamma * np.max(self.Q[next_state]) - self.Q[state][action])