-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDPG_new.py
241 lines (195 loc) · 9 KB
/
DDPG_new.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
#!/usr/bin/env python
# Deep Deterministic Policy Gradients (DDPG) agent training script
# Chapter 3, TensorFlow 2 Reinforcement Learning Cookbook | Praveen Palanisamy
import argparse
import os
import random
from collections import deque
from datetime import datetime
import gym
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input, Lambda, concatenate
tf.keras.backend.set_floatx("float64")
parser = argparse.ArgumentParser(prog="TFRL-Cookbook-Ch3-DDPG")
parser.add_argument("--env", default="Pendulum-v0")
parser.add_argument("--actor_lr", type=float, default=0.0005)
parser.add_argument("--critic_lr", type=float, default=0.001)
parser.add_argument("--batch_size", type=int, default=64)
parser.add_argument("--tau", type=float, default=0.05)
parser.add_argument("--gamma", type=float, default=0.99)
parser.add_argument("--train_start", type=int, default=2000)
parser.add_argument("--logdir", default="logs")
args = parser.parse_args()
logdir = os.path.join(
args.logdir, parser.prog, args.env, datetime.now().strftime("%Y%m%d-%H%M%S")
)
print(f"Saving training logs to:{logdir}")
writer = tf.summary.create_file_writer(logdir)
class ReplayBuffer:
def __init__(self, capacity=10000):
self.buffer = deque(maxlen=capacity)
def store(self, state, action, reward, next_state, done):
self.buffer.append([state, action, reward, next_state, done])
def sample(self):
sample = random.sample(self.buffer, args.batch_size)
states, actions, rewards, next_states, done = map(np.asarray, zip(*sample))
states = np.array(states).reshape(args.batch_size, -1)
next_states = np.array(next_states).reshape(args.batch_size, -1)
return states, actions, rewards, next_states, done
def size(self):
return len(self.buffer)
class Actor:
def __init__(self, state_dim, action_dim, action_bound):
self.state_dim = state_dim
self.action_dim = action_dim
self.action_bound = action_bound
self.model = self.nn_model()
self.opt = tf.keras.optimizers.Adam(args.actor_lr)
def nn_model(self):
return tf.keras.Sequential(
[
Input((self.state_dim,)),
Dense(32, activation="relu"),
Dense(32, activation="relu"),
Dense(self.action_dim, activation="tanh"),
Lambda(lambda x: x * self.action_bound),
]
)
def train(self, states, q_grads):
with tf.GradientTape() as tape:
grads = tape.gradient(
self.model(states), self.model.trainable_variables, -q_grads
)
self.opt.apply_gradients(zip(grads, self.model.trainable_variables))
def predict(self, state):
return self.model.predict(state)
def get_action(self, state):
state = np.reshape(state, [1, self.state_dim])
return self.model.predict(state)[0]
class Critic:
def __init__(self, state_dim, action_dim):
self.state_dim = state_dim
self.action_dim = action_dim
self.model = self.nn_model()
self.opt = tf.keras.optimizers.Adam(args.critic_lr)
def nn_model(self):
state_input = Input((self.state_dim,))
s1 = Dense(64, activation="relu")(state_input)
s2 = Dense(32, activation="relu")(s1)
action_input = Input((self.action_dim,))
a1 = Dense(32, activation="relu")(action_input)
c1 = concatenate([s2, a1], axis=-1)
c2 = Dense(16, activation="relu")(c1)
output = Dense(1, activation="linear")(c2)
return tf.keras.Model([state_input, action_input], output)
def predict(self, inputs):
return self.model.predict(inputs)
def q_gradients(self, states, actions):
actions = tf.convert_to_tensor(actions)
with tf.GradientTape() as tape:
tape.watch(actions)
q_values = self.model([states, actions])
q_values = tf.squeeze(q_values)
return tape.gradient(q_values, actions)
def compute_loss(self, v_pred, td_targets):
mse = tf.keras.losses.MeanSquaredError()
return mse(td_targets, v_pred)
def train(self, states, actions, td_targets):
with tf.GradientTape() as tape:
v_pred = self.model([states, actions], training=True)
assert v_pred.shape == td_targets.shape
loss = self.compute_loss(v_pred, tf.stop_gradient(td_targets))
grads = tape.gradient(loss, self.model.trainable_variables)
self.opt.apply_gradients(zip(grads, self.model.trainable_variables))
return loss
class Agent:
def __init__(self, env):
self.env = env
self.state_dim = self.env.observation_space.shape[0]
self.action_dim = self.env.action_space.shape[0]
self.action_bound = self.env.action_space.high[0]
self.buffer = ReplayBuffer()
self.actor = Actor(self.state_dim, self.action_dim, self.action_bound)
self.critic = Critic(self.state_dim, self.action_dim)
self.target_actor = Actor(self.state_dim, self.action_dim, self.action_bound)
self.target_critic = Critic(self.state_dim, self.action_dim)
actor_weights = self.actor.model.get_weights()
critic_weights = self.critic.model.get_weights()
self.target_actor.model.set_weights(actor_weights)
self.target_critic.model.set_weights(critic_weights)
def update_target(self):
actor_weights = self.actor.model.get_weights()
t_actor_weights = self.target_actor.model.get_weights()
critic_weights = self.critic.model.get_weights()
t_critic_weights = self.target_critic.model.get_weights()
for i in range(len(actor_weights)):
t_actor_weights[i] = (
args.tau * actor_weights[i] + (1 - args.tau) * t_actor_weights[i]
)
for i in range(len(critic_weights)):
t_critic_weights[i] = (
args.tau * critic_weights[i] + (1 - args.tau) * t_critic_weights[i]
)
self.target_actor.model.set_weights(t_actor_weights)
self.target_critic.model.set_weights(t_critic_weights)
def get_td_target(self, rewards, q_values, dones):
targets = np.asarray(q_values)
for i in range(q_values.shape[0]):
if dones[i]:
targets[i] = rewards[i]
else:
targets[i] = args.gamma * q_values[i]
return targets
def add_ou_noise(self, x, rho=0.15, mu=0, dt=1e-1, sigma=0.2, dim=1):
return (
x + rho * (mu - x) * dt + sigma * np.sqrt(dt) * np.random.normal(size=dim)
)
def replay_experience(self):
for _ in range(10):
states, actions, rewards, next_states, dones = self.buffer.sample()
target_q_values = self.target_critic.predict(
[next_states, self.target_actor.predict(next_states)]
)
td_targets = self.get_td_target(rewards, target_q_values, dones)
self.critic.train(states, actions, td_targets)
s_actions = self.actor.predict(states)
s_grads = self.critic.q_gradients(states, s_actions)
grads = np.array(s_grads).reshape((-1, self.action_dim))
self.actor.train(states, grads)
self.update_target()
def train(self, max_episodes=1000):
with writer.as_default():
for ep in range(max_episodes):
episode_reward, done = 0, False
state = self.env.reset()
bg_noise = np.zeros(self.action_dim)
while not done:
# self.env.render()
action = self.actor.get_action(state)
noise = self.add_ou_noise(bg_noise, dim=self.action_dim)
action = np.clip(
action + noise, -self.action_bound, self.action_bound
)
#ToDo: Replace:
# next_state, reward, done, _ = self.env.step(action)
# By:
# 1- send the action to season II sim.
# 2- Receive KPI reports.
# 3- Compute the new state and the reward.
self.buffer.store(state, action, (reward + 8) / 8, next_state, done)
bg_noise = noise
episode_reward += reward
state = next_state
if (
self.buffer.size() >= args.batch_size
and self.buffer.size() >= args.train_start
):
self.replay_experience()
print(f"Episode#{ep} Reward:{episode_reward}")
tf.summary.scalar("episode_reward", episode_reward, step=ep)
if __name__ == "__main__":
env_name = "Pendulum-v0"
env = gym.make(env_name)
agent = Agent(env)
agent.train()