-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
313 lines (269 loc) · 12.9 KB
/
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
import re
import sys
import time
import signal
import argparse
import numpy as np
import torch
import visdom
import data
from models import *
from comm import CommNetMLP
from utils import *
from action_utils import parse_action_args
from trainer import Trainer
from multi_processing import MultiProcessTrainer
torch.utils.backcompat.broadcast_warning.enabled = True
torch.utils.backcompat.keepdim_warning.enabled = True
torch.set_default_tensor_type('torch.DoubleTensor')
parser = argparse.ArgumentParser(description='PyTorch RL trainer')
# training
# note: number of steps per epoch = epoch_size X batch_size x nprocesses
# IC3net中总步数为2000*16*500*10 = 160000000(medium)
parser.add_argument('--num_epochs', default=100, type=int,
help='number of training epochs')
parser.add_argument('--epoch_size', type=int, default=10,
help='number of update iterations in an epoch')
parser.add_argument('--batch_size', type=int, default=500,
help='number of steps before each update (per thread)')
# for medium, 500//40 + 1 = 13 episodes before each update. The last episode is not 40 steps.
parser.add_argument('--nprocesses', type=int, default=16,
help='How many processes to run')
# model
parser.add_argument('--hid_size', default=64, type=int,
help='hidden layer size')
parser.add_argument('--recurrent', action='store_true', default=False,
help='make the model recurrent in time')
# optimization
parser.add_argument('--gamma', type=float, default=1.0,
help='discount factor')
parser.add_argument('--tau', type=float, default=1.0,
help='gae (remove?)')
parser.add_argument('--seed', type=int, default=-1,
help='random seed. Pass -1 for random seed') # TODO: works in thread?
parser.add_argument('--normalize_rewards', action='store_true', default=False,
help='normalize rewards in each batch')
parser.add_argument('--lrate', type=float, default=0.001,
help='learning rate')
parser.add_argument('--entr', type=float, default=0,
help='entropy regularization coeff')
parser.add_argument('--value_coeff', type=float, default=0.01,
help='coeff for value loss term')
# environment
parser.add_argument('--env_name', default="Cartpole",
help='name of the environment to run')
parser.add_argument('--max_steps', default=20, type=int,
help='force to end the game after this many steps')
parser.add_argument('--nactions', default='1', type=str,
help='the number of agent actions (0 for continuous). Use N:M:K for multiple actions')
parser.add_argument('--action_scale', default=1.0, type=float,
help='scale action output from model')
# other
parser.add_argument('--plot', action='store_true', default=False,
help='plot training progress')
parser.add_argument('--plot_env', default='main', type=str,
help='plot env name')
parser.add_argument('--save', default='', type=str,
help='save the model after training')
parser.add_argument('--save_every', default=0, type=int,
help='save the model after every n_th epoch')
parser.add_argument('--load', default='', type=str,
help='load the model')
parser.add_argument('--display', action="store_true", default=False,
help='Display environment state')
parser.add_argument('--random', action='store_true', default=False,
help="enable random model")
# CommNet specific args
parser.add_argument('--commnet', action='store_true', default=False,
help="enable commnet model")
parser.add_argument('--ic3net', action='store_true', default=False,
help="enable commnet model")
parser.add_argument('--nagents', type=int, default=1,
help="Number of agents (used in multiagent)")
parser.add_argument('--comm_mode', type=str, default='avg',
help="Type of mode for communication tensor calculation [avg|sum]")
parser.add_argument('--comm_passes', type=int, default=1,
help="Number of comm passes per step over the model")
parser.add_argument('--comm_mask_zero', action='store_true', default=False,
help="Whether communication should be there")
parser.add_argument('--mean_ratio', default=1.0, type=float,
help='how much coooperative to do? 1.0 means fully cooperative, 0.0 means individual rewards')
parser.add_argument('--rnn_type', default='MLP', type=str,
help='type of rnn to use. [LSTM|MLP]')
parser.add_argument('--detach_gap', default=10000, type=int,
help='detach hidden state and cell state for rnns at this interval.'
+ ' Default 10000 (very high)')
parser.add_argument('--comm_init', default='uniform', type=str,
help='how to initialise comm weights [uniform|zeros]')
parser.add_argument('--hard_attn', default=False, action='store_true',
help='Whether to use hard attention: action - talk|silent')
parser.add_argument('--comm_action_one', default=False, action='store_true',
help='Whether to always talk, sanity check for hard attention.')
parser.add_argument('--advantages_per_action', default=False, action='store_true',
help='Whether to multipy log porb for each chosen action with advantages')
parser.add_argument('--share_weights', default=False, action='store_true',
help='Share weights for hops')
init_args_for_env(parser)
args = parser.parse_args()
if args.ic3net:
args.commnet = 1
args.hard_attn = 1
args.mean_ratio = 0
# For TJ set comm action to 1 as specified in paper to showcase
# importance of individual rewards even in cooperative games
if args.env_name == "traffic_junction":
args.comm_action_one = True
# Enemy comm
args.nfriendly = args.nagents
if hasattr(args, 'enemy_comm') and args.enemy_comm:
if hasattr(args, 'nenemies'):
args.nagents += args.nenemies
else:
raise RuntimeError("Env. needs to pass argument 'nenemy'.")
env = data.init(args.env_name, args, False)
num_inputs = env.observation_dim # 61
args.num_actions = env.num_actions # 2
# Multi-action
if not isinstance(args.num_actions, (list, tuple)): # single action case
args.num_actions = [args.num_actions] # [2]
args.dim_actions = env.dim_actions # 1
args.num_inputs = num_inputs # 61
# Hard attention
if args.hard_attn and args.commnet:
# add comm_action as last dim in actions
args.num_actions = [*args.num_actions, 2]
args.dim_actions = env.dim_actions + 1
# Recurrence
if args.commnet and (args.recurrent or args.rnn_type == 'LSTM'):
args.recurrent = True
args.rnn_type = 'LSTM'
parse_action_args(args)
if args.seed == -1:
args.seed = np.random.randint(0,10000)
torch.manual_seed(args.seed)
res_file = open('test_result.txt', mode='a+')
print(args)
'''
Namespace(action_scale=1.0, add_rate_max=0.05, add_rate_min=0.2, advantages_per_action=False, batch_size=500, comm_action_one=False, comm_init='uniform', comm_mask_zero=False, comm_mode='avg', comm_passes=1, commnet=True, continuous=False, curr_end=1250.0, curr_start=250.0, detach_gap=10, difficulty='medium', dim=14, dim_actions=1, display=False, entr=0, env_name='traffic_junction', epoch_size=10, gamma=1.0, hard_attn=False, hid_size=128, ic3net=False, load='', lrate=0.001, max_steps=40, mean_ratio=1.0, naction_heads=[2], nactions='1', nagents=10, nfriendly=10, normalize_rewards=False, nprocesses=1, num_actions=[2], num_epochs=2000, num_inputs=61, plot=False, plot_env='main', random=False, recurrent=True, rnn_type='LSTM', save='', save_every=0,
seed=6943, share_weights=False, tau=1.0, value_coeff=0.01, vision=0, vocab_type='bool')
'''
if args.commnet:
policy_net = CommNetMLP(args, num_inputs)
elif args.random:
policy_net = Random(args, num_inputs)
elif args.recurrent:
policy_net = RNN(args, num_inputs)
else:
policy_net = MLP(args, num_inputs)
if not args.display:
display_models([policy_net])
# share parameters among threads, but not gradients
for p in policy_net.parameters():
p.data.share_memory_()
if args.nprocesses > 1:
trainer = MultiProcessTrainer(args, lambda: Trainer(args, policy_net, data.init(args.env_name, args)))
else:
trainer = Trainer(args, policy_net, data.init(args.env_name, args))
disp_trainer = Trainer(args, policy_net, data.init(args.env_name, args, False))
disp_trainer.display = True
def disp():
x = disp_trainer.get_episode()
log = dict()
# LogField = namedtuple('LogField', ('data', 'plot', 'x_axis', 'divide_by'))
log['epoch'] = LogField(list(), False, None, None)
log['reward'] = LogField(list(), True, 'epoch', 'num_episodes')
log['enemy_reward'] = LogField(list(), True, 'epoch', 'num_episodes')
log['success'] = LogField(list(), True, 'epoch', 'num_episodes')
log['steps_taken'] = LogField(list(), True, 'epoch', 'num_episodes')
log['add_rate'] = LogField(list(), True, 'epoch', 'num_episodes')
log['comm_action'] = LogField(list(), True, 'epoch', 'num_steps')
log['enemy_comm'] = LogField(list(), True, 'epoch', 'num_steps')
log['value_loss'] = LogField(list(), True, 'epoch', 'num_steps')
log['action_loss'] = LogField(list(), True, 'epoch', 'num_steps')
log['entropy'] = LogField(list(), True, 'epoch', 'num_steps')
if args.plot:
vis = visdom.Visdom(env=args.plot_env)
def run(num_epochs):
for ep in range(num_epochs):
epoch_begin_time = time.time()
stat = dict()
for n in range(args.epoch_size):
if n == args.epoch_size - 1 and args.display:
trainer.display = True
s = trainer.train_batch(ep)
merge_stat(s, stat)
trainer.display = False
epoch_time = time.time() - epoch_begin_time
epoch = len(log['epoch'].data) + 1
for k, v in log.items():
if k == 'epoch':
v.data.append(epoch)
else:
if k in stat and v.divide_by is not None and stat[v.divide_by] > 0:
stat[k] = stat[k] / stat[v.divide_by]
v.data.append(stat.get(k, 0))
np.set_printoptions(precision=2)
print('Epoch {} \t Reward {} \tTime {:.2f}s'.format(
epoch, stat['reward'], epoch_time))
res_file.write('Epoch {} \t Reward {} \tTime {:.2f}s'.format(
epoch, stat['reward'], epoch_time))
if 'enemy_reward' in stat.keys():
print('Enemy-Reward: {}'.format(stat['enemy_reward']))
res_file.write('\nEnemy-Reward: {}'.format(stat['enemy_reward']))
if 'add_rate' in stat.keys():
print('Add-Rate: {:.2f}'.format(stat['add_rate']))
res_file.write('\nAdd-Rate: {:.2f}'.format(stat['add_rate']))
if 'success' in stat.keys():
print('Success: {:.5f}'.format(stat['success']))
res_file.write('\nSuccess: {:.5f}\n'.format(stat['success']))
# if 'steps_taken' in stat.keys():
# print('\nSteps-taken: {:.2f}'.format(stat['steps_taken']))
# res_file.write('Steps-taken: {:.2f}'.format(stat['steps_taken']))
if 'comm_action' in stat.keys():
print('Comm-Action: {}'.format(stat['comm_action']))
res_file.write('\nComm-Action: {}'.format(stat['comm_action']))
if 'enemy_comm' in stat.keys():
print('Enemy-Comm: {}'.format(stat['enemy_comm']))
res_file.write('\nEnemy-Comm: {}'.format(stat['enemy_comm']))
if args.plot:
for k, v in log.items():
if v.plot and len(v.data) > 0:
vis.line(np.asarray(v.data), np.asarray(log[v.x_axis].data[-len(v.data):]),
win=k, opts=dict(xlabel=v.x_axis, ylabel=k))
# save: false
if args.save_every and ep and args.save != '' and ep % args.save_every == 0:
# fname, ext = args.save.split('.')
# save(fname + '_' + str(ep) + '.' + ext)
save(args.save + '_' + str(ep))
if args.save != '':
save(args.save)
def save(path):
d = dict()
d['policy_net'] = policy_net.state_dict()
d['log'] = log
d['trainer'] = trainer.state_dict()
torch.save(d, path)
def load(path):
d = torch.load(path)
# log.clear()
policy_net.load_state_dict(d['policy_net'])
log.update(d['log'])
trainer.load_state_dict(d['trainer'])
def signal_handler(signal, frame):
print('You pressed Ctrl+C! Exiting gracefully.')
if args.display:
env.end_display()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
if args.load != '':
load(args.load)
run(args.num_epochs)
res_file.close()
if args.display:
env.end_display()
if args.save != '':
save(args.save)
if sys.flags.interactive == 0 and args.nprocesses > 1:
trainer.quit()
import os
os._exit(0)