-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
95 lines (84 loc) · 2.8 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
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
import network
import build_graph
import numpy as np
import tensorflow as tf
class Agent:
def __init__(self,
q_func,
actions,
state_shape,
replay_buffer,
exploration,
optimizer,
gamma,
grad_norm_clipping,
phi=lambda s: s,
batch_size=32,
train_freq=4,
learning_starts=1e4,
target_network_update_freq=1e4):
self.batch_size = batch_size
self.train_freq = train_freq
self.actions = actions
self.learning_starts = learning_starts
self.target_network_update_freq = target_network_update_freq
self.exploration = exploration
self.replay_buffer = replay_buffer
self.phi = phi
self._act,\
self._train,\
self._update_target,\
self._q_values = build_graph.build_train(
q_func=q_func,
num_actions=len(actions),
state_shape=state_shape,
optimizer=optimizer,
gamma=gamma,
grad_norm_clipping=grad_norm_clipping
)
self.last_obs = None
self.t = 0
def act(self, obs, reward, training):
# transpose state shape to WHC
obs = self.phi(obs)
# take the best action
action = self._act([obs])[0]
# epsilon greedy exploration
if training:
action = self.exploration.select_action(
self.t, action, len(self.actions))
if training:
if self.t % self.target_network_update_freq == 0:
self._update_target()
if self.t > self.learning_starts and self.t % self.train_freq == 0:
obs_t,\
actions,\
rewards,\
obs_tp1,\
dones = self.replay_buffer.sample(self.batch_size)
td_errors = self._train(obs_t, actions, rewards, obs_tp1, dones)
if self.last_obs is not None:
self.replay_buffer.append(
obs_t=self.last_obs,
action=self.last_action,
reward=reward,
obs_tp1=obs,
done=False
)
self.t += 1
self.last_obs = obs
self.last_action = action
return self.actions[action]
def stop_episode(self, obs, reward, training=True):
if training:
# transpose state shape to WHC
obs = self.phi(obs)
self.replay_buffer.append(
obs_t=self.last_obs,
action=self.last_action,
reward=reward,
obs_tp1=obs,
done=True
)
self.last_obs = None
self.last_action = 0