-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagic.py
316 lines (265 loc) · 14.6 KB
/
magic.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
import torch
import torch.nn.functional as F
from torch import nn
import numpy as np
from action_utils import select_action, translate_action
from gnn_layers import GraphAttention
class MAGIC(nn.Module):
"""
The communication protocol of Multi-Agent Graph AttentIon Communication (MAGIC)
"""
def __init__(self, args):
super(MAGIC, self).__init__()
"""
Initialization method for the MAGIC communication protocol (2 rounds of communication)
Arguements:
args (Namespace): Parse arguments
"""
self.args = args
self.nagents = args.nagents
self.hid_size = args.hid_size
dropout = 0
negative_slope = 0.2
# initialize sub-processors
self.sub_processor1 = GraphAttention(args.hid_size, args.gat_hid_size, dropout=dropout, negative_slope=negative_slope, num_heads=args.gat_num_heads, self_loop_type=args.self_loop_type1, average=False, normalize=args.first_gat_normalize)
self.sub_processor2 = GraphAttention(args.gat_hid_size*args.gat_num_heads, args.hid_size, dropout=dropout, negative_slope=negative_slope, num_heads=args.gat_num_heads_out, self_loop_type=args.self_loop_type2, average=True, normalize=args.second_gat_normalize)
# initialize the gat encoder for the Scheduler
if args.use_gat_encoder:
self.gat_encoder = GraphAttention(args.hid_size, args.gat_encoder_out_size, dropout=dropout, negative_slope=negative_slope, num_heads=args.ge_num_heads, self_loop_type=1, average=True, normalize=args.gat_encoder_normalize)
self.obs_encoder = nn.Linear(args.obs_size, args.hid_size)
self.init_hidden(args.batch_size)
self.lstm_cell= nn.LSTMCell(args.hid_size, args.hid_size)
# initialize mlp layers for the sub-schedulers
if not args.first_graph_complete:
if args.use_gat_encoder:
self.sub_scheduler_mlp1 = nn.Sequential(
nn.Linear(args.gat_encoder_out_size*2, args.gat_encoder_out_size//2),
nn.ReLU(),
nn.Linear(args.gat_encoder_out_size//2, args.gat_encoder_out_size//2),
nn.ReLU(),
nn.Linear(args.gat_encoder_out_size//2, 2))
else:
self.sub_scheduler_mlp1 = nn.Sequential(
nn.Linear(self.hid_size*2, self.hid_size//2),
nn.ReLU(),
nn.Linear(self.hid_size//2, self.hid_size//8),
nn.ReLU(),
nn.Linear(self.hid_size//8, 2))
if args.learn_second_graph and not args.second_graph_complete:
if args.use_gat_encoder:
self.sub_scheduler_mlp2 = nn.Sequential(
nn.Linear(args.gat_encoder_out_size*2, args.gat_encoder_out_size//2),
nn.ReLU(),
nn.Linear(args.gat_encoder_out_size//2, args.gat_encoder_out_size//2),
nn.ReLU(),
nn.Linear(args.gat_encoder_out_size//2, 2))
else:
self.sub_scheduler_mlp2 = nn.Sequential(
nn.Linear(self.hid_size*2, self.hid_size//2),
nn.ReLU(),
nn.Linear(self.hid_size//2, self.hid_size//8),
nn.ReLU(),
nn.Linear(self.hid_size//8, 2))
if args.message_encoder:
self.message_encoder = nn.Linear(args.hid_size, args.hid_size)
if args.message_decoder:
self.message_decoder = nn.Linear(args.hid_size, args.hid_size)
# initialize weights as 0
if args.comm_init == 'zeros':
if args.message_encoder:
self.message_encoder.weight.data.zero_()
if args.message_decoder:
self.message_decoder.weight.data.zero_()
if not args.first_graph_complete:
self.sub_scheduler_mlp1.apply(self.init_linear)
if args.learn_second_graph and not args.second_graph_complete:
self.sub_scheduler_mlp2.apply(self.init_linear)
# initialize the action head (in practice, one action head is used)
self.action_heads = nn.ModuleList([nn.Linear(2*args.hid_size, o)
for o in args.naction_heads])
# # initialize the value head
# self.value_head = nn.Linear(2 * self.hid_size, 1)
# initialize the value head
self.cave = args.cave
if args.cave:
# print("First graph complete:", args.first_graph_complete)
# print("Second graph complete:", args.second_graph_complete)
# print("Learn second graph:", args.learn_second_graph)
# Include the adjacency data as input to the value head
if args.first_graph_complete and args.second_graph_complete:
raise ValueError("CAVE is not applicable with trivial (fully-connected) communication networks")
elif (not args.first_graph_complete) and (self.args.second_graph_complete or not self.args.learn_second_graph):
# Learning just first graph
value_input_size = (2 * self.hid_size) + (args.nagents * args.nagents)
elif args.first_graph_complete and args.learn_second_graph and not args.second_graph_complete:
# Learning just second graph
value_input_size = (2 * self.hid_size) + (args.nagents * args.nagents)
elif (not args.first_graph_complete) and args.learn_second_graph and not args.second_graph_complete:
# Learning first and second graph
value_input_size = (2 * self.hid_size) + 2 * (args.nagents * args.nagents)
else:
raise ValueError("I don't know how to deal with your sub-scheduler setup. Check your args around first_graph and second_graph.")
self.value_head = nn.Linear(value_input_size, 1)
else:
self.value_head = nn.Linear(2 * self.hid_size, 1)
def forward(self, x, info={}):
"""
Forward function of MAGIC (two rounds of communication)
Arguments:
x (list): a list for the input of the communication protocol [observations, (previous hidden states, previous cell states)]
observations (tensor): the observations for all agents [1 (batch_size) * n * obs_size]
previous hidden/cell states (tensor): the hidden/cell states from the previous time steps [n * hid_size]
Returns:
action_out (list): a list of tensors of size [1 (batch_size) * n * num_actions] that represent output policy distributions
value_head (tensor): estimated values [n * 1]
next hidden/cell states (tensor): next hidden/cell states [n * hid_size]
"""
# n: number of agents
obs, extras = x
# encoded_obs: [1 (batch_size) * n * hid_size]
encoded_obs = self.obs_encoder(obs)
hidden_state, cell_state = extras
batch_size = encoded_obs.size()[0]
n = self.nagents
num_agents_alive, agent_mask = self.get_agent_mask(batch_size, info)
# if self.args.comm_mask_zero == True, block the communiction (can also comment out the protocol to make training faster)
if self.args.comm_mask_zero:
agent_mask *= torch.zeros(n, 1)
hidden_state, cell_state = self.lstm_cell(encoded_obs.squeeze(), (hidden_state, cell_state))
# comm: [n * hid_size]
comm = hidden_state
if self.args.message_encoder:
comm = self.message_encoder(comm)
# mask communcation from dead agents (only effective in Traffic Junction)
comm = comm * agent_mask
comm_ori = comm.clone()
# sub-scheduler 1
# if args.first_graph_complete == True, sub-scheduler 1 will be disabled
if not self.args.first_graph_complete:
if self.args.use_gat_encoder:
adj_complete = self.get_complete_graph(agent_mask)
encoded_state1 = self.gat_encoder(comm, adj_complete)
adj1 = self.sub_scheduler(self.sub_scheduler_mlp1, encoded_state1, agent_mask, self.args.directed)
else:
adj1 = self.sub_scheduler(self.sub_scheduler_mlp1, comm, agent_mask, self.args.directed)
else:
adj1 = self.get_complete_graph(agent_mask)
# sub-processor 1
comm = F.elu(self.sub_processor1(comm, adj1))
# sub-scheduler 2
if self.args.learn_second_graph and not self.args.second_graph_complete:
if self.args.use_gat_encoder:
if self.args.first_graph_complete:
adj_complete = self.get_complete_graph(agent_mask)
encoded_state2 = self.gat_encoder(comm_ori, adj_complete)
else:
encoded_state2 = encoded_state1
adj2 = self.sub_scheduler(self.sub_scheduler_mlp2, encoded_state2, agent_mask, self.args.directed)
else:
adj2 = self.sub_scheduler(self.sub_scheduler_mlp2, comm_ori, agent_mask, self.args.directed)
elif not self.args.learn_second_graph and not self.args.second_graph_complete:
adj2 = adj1
else:
adj2 = self.get_complete_graph(agent_mask)
# sub-processor 2
comm = self.sub_processor2(comm, adj2)
# mask communication to dead agents (only effective in Traffic Junction)
comm = comm * agent_mask
if self.args.message_decoder:
comm = self.message_decoder(comm)
if self.cave:
# Flatten the adjacency data and add it to the hidden state to go into the value estimate
if (not self.args.first_graph_complete) and (self.args.second_graph_complete or not self.args.learn_second_graph):
# Learning just first graph
# print("hidden state:", hidden_state.shape)
# print("comm:", comm.shape)
# print("adj:", torch.flatten(adj1).expand((n, -1)).shape) #.expand((batch_size, n, -1)).shape)
value_input = torch.cat([hidden_state, comm,
torch.flatten(adj1).expand((n, -1))], dim=-1)
elif self.args.first_graph_complete and self.args.learn_second_graph and not self.args.second_graph_complete:
# Learning just second graph
value_input = torch.cat([hidden_state, comm,
torch.flatten(adj2).expand((n, -1))], dim=-1)
elif (not self.args.first_graph_complete) and self.args.learn_second_graph and not self.args.second_graph_complete:
# Learning first and second graph
value_input = torch.cat([hidden_state, comm,
torch.flatten(adj1).expand((n, -1)),
torch.flatten(adj2).expand((n, -1))], dim=-1)
else:
raise ValueError("I don't know how to deal with your sub-scheduler setup. Check your args around first_graph and second_graph.")
value_head = self.value_head(value_input)
else:
value_head = self.value_head(torch.cat((hidden_state, comm), dim=-1))
# value_head = self.value_head(torch.cat((hidden_state, comm), dim=-1))
h = hidden_state.view(batch_size, n, self.hid_size)
c = comm.view(batch_size, n, self.hid_size)
action_out = [F.log_softmax(action_head(torch.cat((h, c), dim=-1)), dim=-1) for action_head in self.action_heads]
return action_out, value_head, (hidden_state.clone(), cell_state.clone())
def get_agent_mask(self, batch_size, info):
"""
Function to generate agent mask to mask out inactive agents (only effective in Traffic Junction)
Returns:
num_agents_alive (int): number of active agents
agent_mask (tensor): [n, 1]
"""
n = self.nagents
if 'alive_mask' in info:
agent_mask = torch.from_numpy(info['alive_mask'])
num_agents_alive = agent_mask.sum()
else:
agent_mask = torch.ones(n)
num_agents_alive = n
agent_mask = agent_mask.view(n, 1).clone()
return num_agents_alive, agent_mask
def init_linear(self, m):
"""
Function to initialize the parameters in nn.Linear as o
"""
if type(m) == nn.Linear:
m.weight.data.fill_(0.)
m.bias.data.fill_(0.)
def init_hidden(self, batch_size):
"""
Function to initialize the hidden states and cell states
"""
return tuple(( torch.zeros(batch_size * self.nagents, self.hid_size, requires_grad=True),
torch.zeros(batch_size * self.nagents, self.hid_size, requires_grad=True)))
def sub_scheduler(self, sub_scheduler_mlp, hidden_state, agent_mask, directed=True):
"""
Function to perform a sub-scheduler
Arguments:
sub_scheduler_mlp (nn.Sequential): the MLP layers in a sub-scheduler
hidden_state (tensor): the encoded messages input to the sub-scheduler [n * hid_size]
agent_mask (tensor): [n * 1]
directed (bool): decide if generate directed graphs
Return:
adj (tensor): a adjacency matrix which is the communication graph [n * n]
"""
# hidden_state: [n * hid_size]
n = self.args.nagents
hid_size = hidden_state.size(-1)
# hard_attn_input: [n * n * (2*hid_size)]
hard_attn_input = torch.cat([hidden_state.repeat(1, n).view(n * n, -1), hidden_state.repeat(n, 1)], dim=1).view(n, -1, 2 * hid_size)
# hard_attn_output: [n * n * 2]
if directed:
hard_attn_output = F.gumbel_softmax(sub_scheduler_mlp(hard_attn_input), hard=True)
else:
hard_attn_output = F.gumbel_softmax(0.5*sub_scheduler_mlp(hard_attn_input)+0.5*sub_scheduler_mlp(hard_attn_input.permute(1,0,2)), hard=True)
# hard_attn_output: [n * n * 1]
hard_attn_output = torch.narrow(hard_attn_output, 2, 1, 1)
# agent_mask and agent_mask_transpose: [n * n]
agent_mask = agent_mask.expand(n, n)
agent_mask_transpose = agent_mask.transpose(0, 1)
# adj: [n * n]
adj = hard_attn_output.squeeze() * agent_mask * agent_mask_transpose
return adj
def get_complete_graph(self, agent_mask):
"""
Function to generate a complete graph, and mask it with agent_mask
"""
n = self.args.nagents
adj = torch.ones(n, n)
agent_mask = agent_mask.expand(n, n)
agent_mask_transpose = agent_mask.transpose(0, 1)
adj = adj * agent_mask * agent_mask_transpose
return adj