-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
164 lines (143 loc) · 5.15 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import build_graph
import numpy as np
import tensorflow as tf
from collections import deque
# state transition cache for N-step update
class Cache:
def __init__(self, n_step, gamma):
self.gamma = gamma
self.states = deque(maxlen=n_step - 1)
self.actions = deque(maxlen=n_step - 1)
self.rewards = deque(maxlen=n_step - 1)
self.encodes = deque(maxlen=n_step - 1)
def add(self, state, action, reward, encode):
self.states.append(state)
self.actions.append(action)
self.rewards.append(reward)
self.encodes.append(encode)
def pop(self, bootstrap_value):
# calculate N-step value
R = 0
for i, r in enumerate(self.rewards):
R += r * (self.gamma ** i)
R += bootstrap_value * (self.gamma ** (i + 1))
# remove oldest values
reward = self.rewards.popleft()
state = self.states.popleft()
action = self.actions.popleft()
encode = self.encodes.popleft()
return state, action, encode, R
def flush(self):
self.states.clear()
self.actions.clear()
self.rewards.clear()
self.encodes.clear()
def size(self):
return len(self.states)
class Agent(object):
def __init__(self,
network,
dnds,
actions,
state_shape,
replay_buffer,
exploration,
constants,
phi=lambda s: s,
run_options=None,
run_metadata=None):
self.actions = actions
self.num_actions = len(actions)
self.replay_buffer = replay_buffer
self.exploration = exploration
self.constants = constants
self.dnds = dnds
self.phi = phi
self.cache = Cache(constants.N_STEP, constants.GAMMA)
self.last_obs = None
self.t = 0
self.t_in_episode = 0
# TODO: remove
self.run_options = run_options
self.run_metadata = run_metadata
if constants.OPTIMIZER == 'adam':
optimizer = tf.train.AdamOptimizer(constants.LR)
else:
optimizer = tf.train.RMSPropOptimizer(
learning_rate=constants.LR,
momentum=constants.MOMENTUM,
epsilon=constants.EPSILON
)
self._act,\
self._write,\
self._train = build_graph.build_train(
encode=network,
num_actions=self.num_actions,
state_shape=state_shape,
optimizer=optimizer,
dnds=self.dnds,
key_size=constants.DND_KEY_SIZE,
grad_clipping=constants.GRAD_CLIPPING,
run_options=self.run_options,
run_metadata=self.run_metadata
)
# TODO: remove
def get_epsize(self):
''' a helper function to get each episode size of dnds
'''
sizes = map(lambda m: min([m.curr_epsize.eval(), m.capacity]), self.dnds)
return list(sizes)
# append state transition to DND and replay memory
def append_experience(self, value):
state, action, encode, R = self.cache.pop(value)
state = np.array(state * 255, dtype=np.uint8)
self.replay_buffer.append(obs_t=state, action=action, value=R)
self._write[action](encode, R, self.get_epsize())
def act(self, obs, reward, training=True):
# preprocess for HWC manner
obs = self.phi(obs)
action, values, encoded_state = self._act([obs], self.get_epsize())
action = action[0]
encoded_state = encoded_state[0]
values = values[0]
# epsilon greedy exploration
if training:
action = self.exploration.select_action(
self.t, action, self.num_actions)
value = values[action]
if training and self.t > self.t > self.constants.LEARNING_START_STEP:
if self.t % self.constants.UPDATE_INTERVAL == 0:
obs_t, actions, values = self.replay_buffer.sample(
self.constants.BATCH_SIZE)
obs_t = np.array(obs_t / 255., dtype=np.float32)
td_errors = self._train(obs_t, actions, values, self.get_epsize())
if training:
if self.last_obs is not None:
self.cache.add(
self.last_obs,
self.last_action,
reward,
self.last_encoded_state
)
if self.t_in_episode >= self.constants.N_STEP:
self.append_experience(value)
self.t += 1
self.t_in_episode += 1
self.last_obs = obs
self.last_encoded_state = encoded_state
self.last_action = action
return self.actions[action]
def stop_episode(self, obs, reward, training=True):
if training:
self.cache.add(
self.last_obs,
self.last_action,
reward,
self.last_encoded_state
)
while self.cache.size() > 0:
self.append_experience(0)
self.last_obs = None
self.last_action = 0
self.t_in_episode = 0
self.cache.flush()