-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.py
318 lines (264 loc) · 13.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import numpy as np
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tf.compat.v1.reset_default_graph()
def random_uniform(f1,f2):
return tf.compat.v1.initializers.random_uniform(f1,f2)
class OUActionNoise(object):
def __init__(self, mu, sigma=0.1, theta=.1, dt=1e-2, x0=None):
self.theta = theta
self.mu = mu
self.sigma = sigma
self.dt = dt
self.x0 = x0
self.reset()
def __call__(self):
x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + \
self.sigma * np.sqrt(self.dt) * np.random.normal(size=self.mu.shape)
self.x_prev = x
return x
def reset(self):
self.x_prev = self.x0 if self.x0 is not None else np.zeros_like(self.mu)
def __repr__(self):
return 'OrnsteinUhlenbeckActionNoise(mu={}, sigma={})'.format(
self.mu, self.sigma)
class ReplayBuffer(object):
def __init__(self, max_size, input_shape, n_actions):
self.mem_size = max_size
self.mem_cntr = 0
self.state_memory = np.zeros((self.mem_size, *input_shape))
self.new_state_memory = np.zeros((self.mem_size, *input_shape))
self.action_memory = np.zeros((self.mem_size, n_actions))
self.reward_memory = np.zeros(self.mem_size)
self.terminal_memory = np.zeros(self.mem_size, dtype=np.float32)
def store_transition(self, state, action, reward, state_, done):
index = self.mem_cntr % self.mem_size
self.state_memory[index] = state
self.new_state_memory[index] = state_
self.action_memory[index] = action
self.reward_memory[index] = reward
self.terminal_memory[index] = 1 - done
self.mem_cntr += 1
def sample_buffer(self, batch_size):
max_mem = min(self.mem_cntr, self.mem_size)
batch = np.random.choice(max_mem, batch_size)
states = self.state_memory[batch]
actions = self.action_memory[batch]
rewards = self.reward_memory[batch]
states_ = self.new_state_memory[batch]
terminal = self.terminal_memory[batch]
return states, actions, rewards, states_, terminal
class Actor(object):
def __init__(self, lr, n_actions, name, input_dims, sess, fc1_dims,
fc2_dims, action_bound, batch_size=64, chkpt_dir='tmp/ddpg'):
self.lr = lr
self.n_actions = n_actions
self.name = name
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.chkpt_dir = chkpt_dir
self.input_dims = input_dims
self.batch_size = batch_size
self.sess = sess
self.action_bound = action_bound
self.build_network()
self.params = tf.compat.v1.trainable_variables(scope=self.name)
self.saver = tf.compat.v1.train.Saver()
self.checkpoint_file = os.path.join(chkpt_dir, name +'_ddpg.ckpt')
self.unnormalized_actor_gradients = tf.gradients(
ys=self.mu, xs=self.params, grad_ys=-self.action_gradient)
self.actor_gradients = list(map(lambda x: tf.compat.v1.div(x, self.batch_size),
self.unnormalized_actor_gradients))
self.optimize = tf.compat.v1.train.AdamOptimizer(self.lr).\
apply_gradients(zip(self.actor_gradients, self.params))
def build_network(self):
with tf.compat.v1.variable_scope(self.name):
self.input = tf.compat.v1.placeholder(tf.float32,
shape=[None, *self.input_dims],
name='inputs')
self.action_gradient = tf.compat.v1.placeholder(tf.float32,
shape=[None, self.n_actions],
name='gradients')
f1 = 1. / np.sqrt(self.fc1_dims)
dense1 = tf.compat.v1.layers.dense(self.input, units=self.fc1_dims,
kernel_initializer=random_uniform(-f1, f1),
bias_initializer=random_uniform(-f1, f1))
batch1 = tf.compat.v1.layers.batch_normalization(dense1) #ensure iid
layer1_activation=tf.nn.softmax(batch1)
f2 = 1. / np.sqrt(self.fc2_dims)
dense2 = tf.compat.v1.layers.dense(layer1_activation, units=self.fc2_dims,
kernel_initializer=random_uniform(-f2, f2),
bias_initializer=random_uniform(-f2, f2))
batch2 = tf.compat.v1.layers.batch_normalization(dense2)#ensure iid
layer2_activation = tf.nn.sigmoid(batch2)
f3 = 0.003
mu = tf.compat.v1.layers.dense(layer2_activation, units=self.n_actions,
activation='tanh',
kernel_initializer= random_uniform(-f3, f3),
bias_initializer=random_uniform(-f3, f3))
self.mu = tf.multiply(mu, self.action_bound)
def predict(self, inputs):
return self.sess.run(self.mu, feed_dict={self.input: inputs})
def train(self, inputs, gradients):
self.sess.run(self.optimize,
feed_dict={self.input: inputs,
self.action_gradient: gradients})
def load_checkpoint(self):
print("...Loading checkpoint...")
self.saver.restore(self.sess, self.checkpoint_file)
def save_checkpoint(self):
print("...Saving checkpoint...")
self.saver.save(self.sess, self.checkpoint_file)
class Critic(object):
def __init__(self, lr, n_actions, name, input_dims, sess, fc1_dims, fc2_dims,
batch_size=64, chkpt_dir='tmp/ddpg'):
self.lr = lr
self.n_actions = n_actions
self.name = name
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.chkpt_dir = chkpt_dir
self.input_dims = input_dims
self.batch_size = batch_size
self.sess = sess
self.build_network()
self.params = tf.compat.v1.trainable_variables(scope=self.name)
self.saver = tf.compat.v1.train.Saver()
self.checkpoint_file = os.path.join(chkpt_dir, name +'_ddpg.ckpt')
self.optimize = tf.compat.v1.train.AdamOptimizer(self.lr).minimize(self.loss)
self.action_gradients = tf.gradients(ys=self.q, xs=self.actions)
def build_network(self):
with tf.compat.v1.variable_scope(self.name):
self.input = tf.compat.v1.placeholder(tf.float32,
shape=[None, *self.input_dims],
name='inputs')
self.actions = tf.compat.v1.placeholder(tf.float32,
shape=[None, self.n_actions],
name='actions')
self.q_target = tf.compat.v1.placeholder(tf.float32,
shape=[None,1],
name='targets')
f1 = 1. / np.sqrt(self.fc1_dims)
dense1 = tf.compat.v1.layers.dense(self.input, units=self.fc1_dims,
kernel_initializer=random_uniform(-f1, f1),
bias_initializer=random_uniform(-f1, f1))
batch1 = tf.compat.v1.layers.batch_normalization(dense1)
# batch1 = dense1
layer1_activation = tf.nn.softmax(batch1)
f2 = 1. / np.sqrt(self.fc2_dims)
dense2 = tf.compat.v1.layers.dense(layer1_activation, units=self.fc2_dims,
kernel_initializer=random_uniform(-f2, f2),
bias_initializer=random_uniform(-f2, f2))
batch2 = tf.compat.v1.layers.batch_normalization(dense2)
# batch2 = dense2
action_in = tf.compat.v1.layers.dense(self.actions, units=self.fc2_dims,
activation='relu')
state_actions = tf.add(batch2, action_in)
state_actions = tf.nn.sigmoid(state_actions)
f3 = 0.003
self.q = tf.compat.v1.layers.dense(state_actions, units=1,
kernel_initializer=random_uniform(-f3, f3),
bias_initializer=random_uniform(-f3, f3),
kernel_regularizer=tf.keras.regularizers.l2(0.01))
# self.loss = tf.compat.v1.losses.mean_squared_error(self.q_target, self.q)
self.loss = tf.compat.v1.losses.mean_squared_error(self.q_target, self.q)
def predict(self, inputs, actions):
return self.sess.run(self.q,
feed_dict={self.input: inputs,
self.actions: actions})
def train(self, inputs, actions, q_target):
return self.sess.run(self.optimize,
feed_dict={self.input: inputs,
self.actions: actions,
self.q_target: q_target})
def get_action_gradients(self, inputs, actions):
return self.sess.run(self.action_gradients,
feed_dict={self.input: inputs,
self.actions: actions})
def load_checkpoint(self):
print("...Loading checkpoint...")
self.saver.restore(self.sess, self.checkpoint_file)
def save_checkpoint(self):
print("...Saving checkpoint...")
self.saver.save(self.sess, self.checkpoint_file)
class Agent(object):
def __init__(self, alpha, beta, input_dims, tau, env, gamma=0.9, n_actions=2,
buffer_max_size=1000000, layer1_size=128, layer2_size=128,
batch_size=16):
self.gamma = gamma
self.tau = tau
self.n_actions = n_actions
self.memory = ReplayBuffer(buffer_max_size, input_dims, n_actions)
self.batch_size = batch_size
self.sess = tf.compat.v1.Session()
self.actor = Actor(alpha, n_actions, 'Actor', input_dims, self.sess,
layer1_size, layer2_size, env.action_space.high)
self.critic = Critic(beta, n_actions, 'Critic', input_dims,self.sess,
layer1_size, layer2_size)
self.target_actor = Actor(alpha, n_actions, 'TargetActor',
input_dims, self.sess, layer1_size,
layer2_size, env.action_space.high)
self.target_critic = Critic(beta, n_actions, 'TargetCritic', input_dims,
self.sess, layer1_size, layer2_size)
self.noise = OUActionNoise(mu=np.zeros(n_actions))
# define ops here in __init__ otherwise time to execute the op
# increases with each execution.
self.update_critic = \
[self.target_critic.params[i].assign(
tf.multiply(self.critic.params[i], self.tau) \
+ tf.multiply(self.target_critic.params[i], 1. - self.tau))
for i in range(len(self.target_critic.params))]
self.update_actor = \
[self.target_actor.params[i].assign(
tf.multiply(self.actor.params[i], self.tau) \
+ tf.multiply(self.target_actor.params[i], 1. - self.tau))
for i in range(len(self.target_actor.params))]
self.sess.run(tf.compat.v1.global_variables_initializer())
self.update_network_parameters(first=True)
def update_network_parameters(self, first=False):
if first:
old_tau = self.tau
self.tau = 1.0
self.target_critic.sess.run(self.update_critic)
self.target_actor.sess.run(self.update_actor)
self.tau = old_tau
else:
self.target_critic.sess.run(self.update_critic)
self.target_actor.sess.run(self.update_actor)
def remember(self, state, action, reward, new_state, done):
self.memory.store_transition(state, action, reward, new_state, done)
def choose_action(self, state):
state = state[np.newaxis, :]
mu = self.actor.predict(state) # returns list of list
noise = self.noise()
mu_prime = mu + noise
return mu_prime[0]
def learn(self):
if self.memory.mem_cntr < self.batch_size:
return
state, action, reward, new_state, done = \
self.memory.sample_buffer(self.batch_size)
critic_value_ = self.target_critic.predict(new_state,
self.target_actor.predict(new_state))
target = []
for j in range(self.batch_size):
target.append(reward[j] + self.gamma*critic_value_[j]*done[j])
target = np.reshape(target, (self.batch_size, 1))
_ = self.critic.train(state, action, target)
a_outs = self.actor.predict(state)
# print("a_outs",a_outs)
grads = self.critic.get_action_gradients(state, a_outs)
# print("actor:",grads[0])
self.actor.train(state, grads[0])
self.update_network_parameters()
def save_models(self):
self.actor.save_checkpoint()
self.target_actor.save_checkpoint()
self.critic.save_checkpoint()
self.target_critic.save_checkpoint()
def load_models(self):
self.actor.load_checkpoint()
self.target_actor.load_checkpoint()
self.critic.save_checkpoint()
self.target_critic.save_checkpoint()