-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddpg_main.py
420 lines (330 loc) · 17.1 KB
/
ddpg_main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
## 有bug,返回的数必须有变化,否则PSCAD会警告,导致输出始终为0
##Main code will only run one time
#
# num_states = 1
# num_actions = 1
#
# upper_bound = 5
# lower_bound = -5
#
#
# ## Define noise class for action output
class OUActionNoise:
def __init__(self, mean, std_deviation, theta=0.15, dt=1e-2, x_initial=None):
self.theta = theta
self.mean = mean
self.std_dev = std_deviation
self.dt = dt
self.x_initial = x_initial
self.reset()
def __call__(self):
# Formula taken from https://www.wikipedia.org/wiki/Ornstein-Uhlenbeck_process.
x = (
self.x_prev
+ self.theta * (self.mean - self.x_prev) * self.dt
+ self.std_dev * np.sqrt(self.dt) * np.random.normal(size=self.mean.shape)
)
# Store x into x_prev
# Makes next noise dependent on current one
self.x_prev = x
return x
def reset(self):
if self.x_initial is not None:
self.x_prev = self.x_initial
else:
self.x_prev = np.zeros_like(self.mean)
## Define Buffer class
class Buffer:
def __init__(self, buffer_capacity=100000, batch_size=64):
# Number of "experiences" to store at max
self.buffer_capacity = buffer_capacity
# Num of tuples to train on.
self.batch_size = batch_size
# Its tells us num of times record() was called.
self.buffer_counter = 0
# Instead of list of tuples as the exp.replay concept go
# We use different np.arrays for each tuple element
self.state_buffer = np.zeros((self.buffer_capacity, num_states))
self.action_buffer = np.zeros((self.buffer_capacity, num_actions))
self.reward_buffer = np.zeros((self.buffer_capacity, 1))
self.next_state_buffer = np.zeros((self.buffer_capacity, num_states))
# Takes (s,a,r,s') obervation tuple as input
def record(self, obs_tuple):
# Set index to zero if buffer_capacity is exceeded,
# replacing old records
index = self.buffer_counter % self.buffer_capacity
self.state_buffer[index] = obs_tuple[0]
self.action_buffer[index] = obs_tuple[1]
self.reward_buffer[index] = obs_tuple[2]
self.next_state_buffer[index] = obs_tuple[3]
self.buffer_counter = self.buffer_counter + 1
# Eager execution is turned on by default in TensorFlow 2. Decorating with tf.function allows
# TensorFlow to build a static graph out of the logic and computations in our function.
# This provides a large speed up for blocks of code that contain many small TensorFlow operations such as this one.
# @tf.function
def update(
self, state_batch, action_batch, reward_batch, next_state_batch
):
# Training and updating Actor & Critic networks.
# See Pseudo Code.
with tf.GradientTape() as tape:
target_actions = target_actor(next_state_batch, training=True)
y = reward_batch + gamma * target_critic(
[next_state_batch, target_actions], training=True
)
critic_value = critic_model([state_batch, action_batch], training=True)
critic_loss = tf.math.reduce_mean(tf.math.square(y - critic_value))
critic_grad = tape.gradient(critic_loss, critic_model.trainable_variables)
critic_optimizer.apply_gradients(
zip(critic_grad, critic_model.trainable_variables)
)
with tf.GradientTape() as tape:
actions = actor_model(state_batch, training=True)
critic_value = critic_model([state_batch, actions], training=True)
# Used `-value` as we want to maximize the value given
# by the critic for our actions
actor_loss = -tf.math.reduce_mean(critic_value)
actor_grad = tape.gradient(actor_loss, actor_model.trainable_variables)
actor_optimizer.apply_gradients(
zip(actor_grad, actor_model.trainable_variables)
)
# We compute the loss and update parameters
def learn(self):
# Get sampling range
record_range = min(self.buffer_counter, self.buffer_capacity)
# Randomly sample indices
batch_indices = np.random.choice(record_range, self.batch_size)
# Convert to tensors
state_batch = tf.convert_to_tensor(self.state_buffer[batch_indices])
action_batch = tf.convert_to_tensor(self.action_buffer[batch_indices])
reward_batch = tf.convert_to_tensor(self.reward_buffer[batch_indices])
reward_batch = tf.cast(reward_batch, dtype=tf.float32)
next_state_batch = tf.convert_to_tensor(self.next_state_buffer[batch_indices])
self.update(state_batch, action_batch, reward_batch, next_state_batch)
# @tf.function
def update_target(target_weights, weights, tau):
for (a, b) in zip(target_weights, weights):
a.assign(b * tau + a * (1 - tau))
## Define actor network function
def get_actor():
# Initialize weights between -3e-3 and 3-e3
last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
inputs = layers.Input(shape=(num_states,))
out = layers.Dense(256, activation="relu")(inputs)
out = layers.Dense(256, activation="relu")(out)
outputs = layers.Dense(1, activation="tanh", kernel_initializer=last_init)(out)
# Our upper bound is 2.0 for Pendulum.
outputs = outputs * upper_bound
model = tf.keras.Model(inputs, outputs)
return model
## Define critic network function
def get_critic():
# State as input
state_input = layers.Input(shape=(num_states))
state_out = layers.Dense(16, activation="relu")(state_input)
state_out = layers.Dense(32, activation="relu")(state_out)
# Action as input
action_input = layers.Input(shape=(num_actions))
action_out = layers.Dense(32, activation="relu")(action_input)
# Both are passed through seperate layer before concatenating
concat = layers.Concatenate()([state_out, action_out])
out = layers.Dense(256, activation="relu")(concat)
out = layers.Dense(256, activation="relu")(out)
outputs = layers.Dense(1)(out)
# Outputs single value for give state-action
model = tf.keras.Model([state_input, action_input], outputs)
return model
## Define policy function for taking action
def policy(state, noise_object):
sampled_actions = tf.squeeze(actor_model(state))
noise = noise_object()
# Adding noise to action
sampled_actions = sampled_actions.numpy() + noise
# We make sure action is within bounds
legal_action = np.clip(sampled_actions, lower_bound, upper_bound)
return [np.squeeze(legal_action)]
## ddpg function for returning to MATLAB-PSCAD
def ddpg(state_1, reward, Done, Simu_Step_In):
import math
import numpy as np
import random
import tensorflow as tf
from tensorflow.keras import layers
## Environment setting
num_states = 1
num_actions = 1
upper_bound = 5
lower_bound = -5
## Training hyperparameters
std_dev = 0.2
ou_noise = OUActionNoise(mean=np.zeros(1), std_deviation=float(std_dev) * np.ones(1))
actor_model = get_actor()
critic_model = get_critic()
target_actor = get_actor()
target_critic = get_critic()
# Making the weights equal initially
target_actor.set_weights(actor_model.get_weights())
target_critic.set_weights(critic_model.get_weights())
# Learning rate for actor-critic models
critic_lr = 0.0002
actor_lr = 0.0001
critic_optimizer = tf.keras.optimizers.Adam(critic_lr)
actor_optimizer = tf.keras.optimizers.Adam(actor_lr)
# Discount factor for future rewards
gamma = 0.9
# Used to update target networks
tau = 0.0005
buffer = Buffer(50000, 64)
episodic_reward = 0
episodic_reward_store = []
## Train or not Train
Train = True
## Start Training
if Train:
if (Simu_Step_In == 0):
### Not the fist episode: Load the weights
actor_model.load_weights("./PSCAD_actor/PSCAD_actor")
critic_model.load_weights("./PSCAD_critic/PSCAD_critic")
target_actor.load_weights("./PSCAD_target_actor/PSCAD_target_actor")
target_critic.load_weights("./PSCAD_target_critic/PSCAD_target_critic")
### First episode: Save the weights
actor_model.save_weights("./PSCAD_actor/PSCAD_actor")
critic_model.save_weights("./PSCAD_critic/PSCAD_critic")
target_actor.save_weights("./PSCAD_target_actor/PSCAD_target_actor")
target_critic.save_weights("./PSCAD_target_critic/PSCAD_target_critic")
# Save the experience buffer
np.save('buffer_counter_store.npy', buffer.buffer_counter)
np.save('state_buffer_store.npy', buffer.state_buffer)
np.save('action_buffer_store.npy', buffer.action_buffer)
np.save('reward_buffer_store.npy', buffer.reward_buffer)
np.save('next_state_buffer_store.npy', buffer.next_state_buffer)
# Save the episode reward for final plotting
np.save('episodic_reward_store.npy', episodic_reward_store)
### First step in each episode: store the 'episodic_reward' and 'prev_state'
np.save('episodic_reward.npy', episodic_reward)
prev_state = np.array([state_1])
np.save('prev_state.npy', prev_state)
tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
np.save('tf_prev_state.npy', tf_prev_state)
# Execute action and save it
sampled_actions = tf.squeeze(actor_model(tf_prev_state))
noise = ou_noise()
# Adding noise to action
sampled_actions = sampled_actions.numpy() + noise
# We make sure action is within bounds
legal_action = np.clip(sampled_actions, lower_bound, upper_bound)
action = [np.squeeze(legal_action)]
np.save('action.npy', action)
# action = policy(tf_prev_state, ou_noise) ##卡在这一步,很奇怪,明明python运行没有问题(测试证明这是一个bug,我直接不用函数,直接写函数里面的code可以成功)
# np.save('action.npy',action)
action_1 = float(action[0])
Simu_Step_Out = Simu_Step_In + 1
# 把当前的动作执行到PSCAD
else:
# Load previous state and previous action
prev_state = np.load('prev_state.npy')
action = np.load('action.npy')
state = np.array([state_1])
# Load the weights
actor_model.load_weights("./PSCAD_actor/PSCAD_actor")
critic_model.load_weights("./PSCAD_critic/PSCAD_critic")
target_actor.load_weights("./PSCAD_target_actor/PSCAD_target_actor")
target_critic.load_weights("./PSCAD_target_critic/PSCAD_target_critic")
buffer_counter_store = np.load('buffer_counter_store.npy')
state_buffer_store = np.load('state_buffer_store.npy')
action_buffer_store = np.load('action_buffer_store.npy')
reward_buffer_store = np.load('reward_buffer_store.npy')
next_state_buffer_store = np.load('next_state_buffer_store.npy')
buffer.buffer_counter = int(buffer_counter_store)
buffer.state_buffer = state_buffer_store
buffer.action_buffer = action_buffer_store
buffer.reward_buffer = reward_buffer_store
buffer.next_state_buffer = next_state_buffer_store
buffer.record((prev_state, action, reward, state))
episodic_reward = np.load('episodic_reward.npy')
episodic_reward = float(episodic_reward) + reward ##此处也有一个bug,不能使用episodic_reward += Reward格式
np.save('episodic_reward.npy', episodic_reward)
# buffer.learn() ## 此处也有bug,所以把code从Buffer类中直接提取出来运行
# Get sampling range
record_range = min(buffer.buffer_counter, buffer.buffer_capacity)
# Randomly sample indices
batch_indices = np.random.choice(record_range, buffer.batch_size)
# Convert to tensors
state_batch = tf.convert_to_tensor(buffer.state_buffer[batch_indices])
action_batch = tf.convert_to_tensor(buffer.action_buffer[batch_indices])
reward_batch = tf.convert_to_tensor(buffer.reward_buffer[batch_indices])
reward_batch = tf.cast(reward_batch, dtype=tf.float32)
next_state_batch = tf.convert_to_tensor(buffer.next_state_buffer[batch_indices])
# buffer.update(state_batch, action_batch, reward_batch, next_state_batch) ## 此处也有bug,所以把code从Buffer类中直接提取出来运行
# Training and updating Actor & Critic networks.
# See Pseudo Code.
with tf.GradientTape() as tape:
target_actions = target_actor(next_state_batch, training=True)
y = reward_batch + gamma * target_critic([next_state_batch, target_actions], training=True)
critic_value = critic_model([state_batch, action_batch], training=True)
critic_loss = tf.math.reduce_mean(tf.math.square(y - critic_value))
critic_grad = tape.gradient(critic_loss, critic_model.trainable_variables)
critic_optimizer.apply_gradients(zip(critic_grad, critic_model.trainable_variables))
with tf.GradientTape() as tape:
actions = actor_model(state_batch, training=True)
critic_value = critic_model([state_batch, actions], training=True)
# Used '-value' as we want to maximize the value given
# by the critic for our actions
actor_loss = -tf.math.reduce_mean(critic_value)
actor_grad = tape.gradient(actor_loss, actor_model.trainable_variables)
actor_optimizer.apply_gradients(zip(actor_grad, actor_model.trainable_variables))
update_target(target_actor.variables, actor_model.variables, tau)
update_target(target_critic.variables, critic_model.variables, tau)
np.save('buffer_counter_store.npy', buffer.buffer_counter)
np.save('state_buffer_store.npy', buffer.state_buffer)
np.save('action_buffer_store.npy', buffer.action_buffer)
np.save('reward_buffer_store.npy', buffer.reward_buffer)
np.save('next_state_buffer_store.npy', buffer.next_state_buffer)
# Save the weights
actor_model.save_weights("./PSCAD_actor/PSCAD_actor")
critic_model.save_weights("./PSCAD_critic/PSCAD_critic")
target_actor.save_weights("./PSCAD_target_actor/PSCAD_target_actor")
target_critic.save_weights("./PSCAD_target_critic/PSCAD_target_critic")
tf_state = tf.expand_dims(tf.convert_to_tensor(state), 0)
sampled_actions = tf.squeeze(actor_model(tf_state))
noise = ou_noise()
# Adding noise to action
sampled_actions = sampled_actions.numpy() + noise
# We make sure action is within bounds
legal_action = np.clip(sampled_actions, lower_bound, upper_bound)
action = [np.squeeze(legal_action)]
np.save('action.npy', action)
prev_state = state
np.save('prev_state.npy', prev_state)
if (Simu_Step_In == 5000):
episodic_reward_store = np.load('episodic_reward_store.npy')
episodic_reward_store = np.append(episodic_reward_store, episodic_reward)
np.save('episodic_reward_store.npy', episodic_reward_store)
action_1 = float(action[0])
Simu_Step_Out = Simu_Step_In + 1
## Not train, just run
else:
# Load the weights
actor_model.load_weights("./PSCAD_actor/PSCAD_actor")
prev_state = np.array([state_1])
tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
sampled_actions = tf.squeeze(actor_model(tf_prev_state))
noise = ou_noise()
# Adding noise to action
sampled_actions = sampled_actions.numpy() + noise
# sampled_actions = sampled_actions.numpy()
# We make sure action is within bounds
legal_action = np.clip(sampled_actions, lower_bound, upper_bound)
action = [np.squeeze(legal_action)]
# np.save('action.npy',action)
# action = policy(tf_prev_state, ou_noise) ##卡在这一步,很奇怪,明明python运行没有问题(测试证明这是一个bug,我直接不用函数,直接写函数里面的code可以成功)
# np.save('action.npy',action)
action_1 = float(action[0])
Simu_Step_Out = Simu_Step_In + 1
return action_1, Simu_Step_Out
def add(a,b):
import math
import random
import tensorflow as tf
from tensorflow.keras import layers
return a*b, a+b