-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmaddpg_tag.py
290 lines (251 loc) · 11.7 KB
/
maddpg_tag.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
import gym
import numpy as np
import tensorflow as tf
import argparse
import itertools
import time
import os
import pickle
import code
import random
from functools import reduce
from maddpg import Actor, Critic
from memory import Memory
from ornstein_uhlenbeck import OrnsteinUhlenbeckActionNoise
from make_env import make_env
import general_utilities
import simple_tag_utilities
def play(episodes, is_render, is_testing, checkpoint_interval,
weights_filename_prefix, csv_filename_prefix, batch_size):
# init statistics. NOTE: simple tag specific!
statistics_header = ["episode"]
statistics_header.append("steps")
statistics_header.extend(["reward_{}".format(i) for i in range(env.n)])
statistics_header.extend(["loss_{}".format(i) for i in range(env.n)])
statistics_header.extend(["collisions_{}".format(i) for i in range(env.n)])
statistics_header.extend(["ou_theta_{}".format(i) for i in range(env.n)])
statistics_header.extend(["ou_mu_{}".format(i) for i in range(env.n)])
statistics_header.extend(["ou_sigma_{}".format(i) for i in range(env.n)])
statistics_header.extend(["ou_dt_{}".format(i) for i in range(env.n)])
statistics_header.extend(["ou_x0_{}".format(i) for i in range(env.n)])
print("Collecting statistics {}:".format(" ".join(statistics_header)))
statistics = general_utilities.Time_Series_Statistics_Store(
statistics_header)
for episode in range(args.episodes):
states = env.reset()
episode_losses = np.zeros(env.n)
episode_rewards = np.zeros(env.n)
collision_count = np.zeros(env.n)
steps = 0
while True:
steps += 1
# render
if args.render:
env.render()
# act
actions = []
for i in range(env.n):
action = np.clip(
actors[i].choose_action(states[i]) + actors_noise[i](), -2, 2)
actions.append(action)
# step
states_next, rewards, done, info = env.step(actions)
# learn
if not args.testing:
size = memories[0].pointer
batch = random.sample(range(size), size) if size < batch_size else random.sample(
range(size), batch_size)
for i in range(env.n):
if done[i]:
rewards[i] -= 500
memories[i].remember(states, actions, rewards[i],
states_next, done[i])
if memories[i].pointer > batch_size * 10:
s, a, r, sn, _ = memories[i].sample(batch, env.n)
r = np.reshape(r, (batch_size, 1))
loss = critics[i].learn(s, a, r, sn)
actors[i].learn(actors, s)
episode_losses[i] += loss
else:
episode_losses[i] = -1
states = states_next
episode_rewards += rewards
collision_count += np.array(
simple_tag_utilities.count_agent_collisions(env))
# reset states if done
if any(done):
episode_rewards = episode_rewards / steps
episode_losses = episode_losses / steps
statistic = [episode]
statistic.append(steps)
statistic.extend([episode_rewards[i] for i in range(env.n)])
statistic.extend([episode_losses[i] for i in range(env.n)])
statistic.extend(collision_count.tolist())
statistic.extend([actors_noise[i].theta for i in range(env.n)])
statistic.extend([actors_noise[i].mu for i in range(env.n)])
statistic.extend([actors_noise[i].sigma for i in range(env.n)])
statistic.extend([actors_noise[i].dt for i in range(env.n)])
statistic.extend([actors_noise[i].x0 for i in range(env.n)])
statistics.add_statistics(statistic)
if episode % 25 == 0:
print(statistics.summarize_last())
break
if episode % checkpoint_interval == 0:
statistics.dump("{}_{}.csv".format(
csv_filename_prefix, episode))
if not os.path.exists(weights_filename_prefix):
os.makedirs(weights_filename_prefix)
save_path = saver.save(session, os.path.join(
weights_filename_prefix, "models"), global_step=episode)
print("saving model to {}".format(save_path))
if episode >= checkpoint_interval:
os.remove("{}_{}.csv".format(csv_filename_prefix,
episode - checkpoint_interval))
return statistics
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--env', default='simple_tag_guided', type=str)
parser.add_argument('--video_dir', default='videos/', type=str)
parser.add_argument('--learning_rate', default=0.001, type=float)
parser.add_argument('--episodes', default=100000, type=int)
parser.add_argument('--video_interval', default=1000, type=int)
parser.add_argument('--render', default=False, action="store_true")
parser.add_argument('--benchmark', default=False, action="store_true")
parser.add_argument('--experiment_prefix', default=".",
help="directory to store all experiment data")
parser.add_argument('--weights_filename_prefix', default='/save/tag-maddpg',
help="where to store/load network weights")
parser.add_argument('--csv_filename_prefix', default='/save/statistics-maddpg',
help="where to store statistics")
parser.add_argument('--checkpoint_frequency', default=500, type=int,
help="how often to checkpoint")
parser.add_argument('--testing', default=False, action="store_true",
help="reduces exploration substantially")
parser.add_argument('--load_weights_from_file', default='',
help="where to load network weights")
parser.add_argument('--random_seed', default=2, type=int)
parser.add_argument('--memory_size', default=10000, type=int)
parser.add_argument('--batch_size', default=32, type=int)
parser.add_argument('--ou_mus', nargs='+', type=float,
help="OrnsteinUhlenbeckActionNoise mus for each action for each agent")
parser.add_argument('--ou_sigma', nargs='+', type=float,
help="OrnsteinUhlenbeckActionNoise sigma for each agent")
parser.add_argument('--ou_theta', nargs='+', type=float,
help="OrnsteinUhlenbeckActionNoise theta for each agent")
parser.add_argument('--ou_dt', nargs='+', type=float,
help="OrnsteinUhlenbeckActionNoise dt for each agent")
parser.add_argument('--ou_x0', nargs='+', type=float,
help="OrnsteinUhlenbeckActionNoise x0 for each agent")
args = parser.parse_args()
general_utilities.dump_dict_as_json(vars(args),
args.experiment_prefix + "/save/run_parameters.json")
# init env
env = make_env(args.env, args.benchmark)
# Extract ou initialization values
if args.ou_mus is not None:
if len(args.ou_mus) == sum([env.action_space[i].n for i in range(env.n)]):
ou_mus = []
prev_idx = 0
for space in env.action_space:
ou_mus.append(
np.array(args.ou_mus[prev_idx:prev_idx + space.n]))
prev_idx = space.n
print("Using ou_mus: {}".format(ou_mus))
else:
raise ValueError(
"Must have enough ou_mus for all actions for all agents")
else:
ou_mus = [np.zeros(env.action_space[i].n) for i in range(env.n)]
if args.ou_sigma is not None:
if len(args.ou_sigma) == env.n:
ou_sigma = args.ou_sigma
else:
raise ValueError("Must have enough ou_sigma for all agents")
else:
ou_sigma = [0.3 for i in range(env.n)]
if args.ou_theta is not None:
if len(args.ou_theta) == env.n:
ou_theta = args.ou_theta
else:
raise ValueError("Must have enough ou_theta for all agents")
else:
ou_theta = [0.15 for i in range(env.n)]
if args.ou_dt is not None:
if len(args.ou_dt) == env.n:
ou_dt = args.ou_dt
else:
raise ValueError("Must have enough ou_dt for all agents")
else:
ou_dt = [1e-2 for i in range(env.n)]
if args.ou_x0 is not None:
if len(args.ou_x0) == env.n:
ou_x0 = args.ou_x0
else:
raise ValueError("Must have enough ou_x0 for all agents")
else:
ou_x0 = [None for i in range(env.n)]
# set random seed
env.seed(args.random_seed)
random.seed(args.random_seed)
np.random.seed(args.random_seed)
tf.set_random_seed(args.random_seed)
# init actors and critics
session = tf.Session()
n_actions = []
actors = []
actors_noise = []
memories = []
eval_actions = []
target_actions = []
state_placeholders = []
state_next_placeholders = []
for i in range(env.n):
n_action = env.action_space[i].n
state_size = env.observation_space[i].shape[0]
state = tf.placeholder(tf.float32, shape=[None, state_size])
state_next = tf.placeholder(tf.float32, shape=[None, state_size])
speed = 0.8 if env.agents[i].adversary else 1
actors.append(Actor('actor' + str(i), session, n_action, speed,
state, state_next))
actors_noise.append(OrnsteinUhlenbeckActionNoise(
mu=ou_mus[i],
sigma=ou_sigma[i],
theta=ou_theta[i],
dt=ou_dt[i],
x0=ou_x0[i]))
memories.append(Memory(args.memory_size))
n_actions.append(n_action)
eval_actions.append(actors[i].eval_actions)
target_actions.append(actors[i].target_actions)
state_placeholders.append(state)
state_next_placeholders.append(state_next)
critics = []
for i in range(env.n):
n_action = env.action_space[i].n
reward = tf.placeholder(tf.float32, [None, 1])
critics.append(Critic('critic' + str(i), session, n_actions,
eval_actions, target_actions, state_placeholders,
state_next_placeholders, reward))
actors[i].add_gradients(critics[i].action_gradients[i])
session.run(tf.global_variables_initializer())
saver = tf.train.Saver(max_to_keep=10000000)
if args.load_weights_from_file != "":
saver.restore(session, args.load_weights_from_file)
print("restoring from checkpoint {}".format(
args.load_weights_from_file))
start_time = time.time()
# play
statistics = play(args.episodes, args.render, args.testing,
args.checkpoint_frequency,
args.experiment_prefix + args.weights_filename_prefix,
args.experiment_prefix + args.csv_filename_prefix,
args.batch_size)
# bookkeeping
print("Finished {} episodes in {} seconds".format(args.episodes,
time.time() - start_time))
tf.summary.FileWriter(args.experiment_prefix +
args.weights_filename_prefix, session.graph)
save_path = saver.save(session, os.path.join(
args.experiment_prefix + args.weights_filename_prefix, "models"), global_step=args.episodes)
print("saving model to {}".format(save_path))
statistics.dump(args.experiment_prefix + args.csv_filename_prefix + ".csv")