-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomm.py
More file actions
324 lines (256 loc) · 12.2 KB
/
comm.py
File metadata and controls
324 lines (256 loc) · 12.2 KB
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
import torch
import torch.nn.functional as F
from torch import nn
import itertools
from math import sqrt
import numpy as np
from models import MLP
from action_utils import select_action, translate_action
import sys
sys.path.insert(0, '/home/eseraj3/HetNet_MARL_Communication/test/IC3Net/utils.py')
from utils import real_comm_loss, get_comm_loss_bitless_k
class CommNetMLP(nn.Module):
"""
MLP based CommNet. Uses communication vector to communicate info
between agents
"""
def __init__(self, args, num_inputs, SSN_state_len):
"""Initialization method for this class, setup various internal networks
and weights
Arguments:
MLP {object} -- Self
args {Namespace} -- Parse args namespace
num_inputs {number} -- Environment observation dimension for agents
SSN_state_len {number} -- non location state bits
"""
super(CommNetMLP, self).__init__()
self.args = args
self.nagents = args.nagents
self.hid_size = args.hid_size
self.comm_passes = args.comm_passes
self.recurrent = args.recurrent
self.lossy_comm = args.lossy_comm
self.min_comm_loss = args.min_comm_loss
self.max_comm_loss = args.max_comm_loss
self.num_P = args.nfriendly_P
self.num_A = args.nfriendly_A
self.comm_range_P = args.comm_range_P
self.comm_range_A = args.comm_range_A
self.SSN_state_len = SSN_state_len
self.k = get_comm_loss_bitless_k(self.comm_range_P, self.max_comm_loss)
self.continuous = args.continuous
if self.continuous:
self.action_mean = nn.Linear(args.hid_size, args.dim_actions)
self.action_log_std = nn.Parameter(torch.zeros(1, args.dim_actions))
else:
self.heads = nn.ModuleList([nn.Linear(args.hid_size, o)
for o in args.naction_heads])
self.init_std = args.init_std if hasattr(args, 'comm_init_std') else 0.2
# Mask for communication
if self.args.comm_mask_zero:
self.comm_mask = torch.zeros(self.nagents, self.nagents)
else:
self.comm_mask = torch.ones(self.nagents, self.nagents) \
- torch.eye(self.nagents, self.nagents)
# Since linear layers in PyTorch now accept * as any number of dimensions
# between last and first dim, num_agents dimension will be covered.
# The network below is function r in the paper for encoding
# initial environment stage
self.encoder = nn.Linear(num_inputs, args.hid_size)
# if self.args.env_name == 'starcraft':
# self.state_encoder = nn.Linear(num_inputs, num_inputs)
# self.encoder = nn.Linear(num_inputs * 2, args.hid_size)
if args.recurrent:
self.hidd_encoder = nn.Linear(args.hid_size, args.hid_size)
if args.recurrent:
self.init_hidden(args.batch_size)
self.f_module = nn.LSTMCell(args.hid_size, args.hid_size)
else:
if args.share_weights:
self.f_module = nn.Linear(args.hid_size, args.hid_size)
self.f_modules = nn.ModuleList([self.f_module
for _ in range(self.comm_passes)])
else:
self.f_modules = nn.ModuleList([nn.Linear(args.hid_size, args.hid_size)
for _ in range(self.comm_passes)])
# else:
# raise RuntimeError("Unsupported RNN type.")
# Our main function for converting current hidden state to next state
# self.f = nn.Linear(args.hid_size, args.hid_size)
if args.share_weights:
self.C_module = nn.Linear(args.hid_size, args.hid_size)
self.C_modules = nn.ModuleList([self.C_module
for _ in range(self.comm_passes)])
else:
self.C_modules = nn.ModuleList([nn.Linear(args.hid_size, args.hid_size)
for _ in range(self.comm_passes)])
# self.C = nn.Linear(args.hid_size, args.hid_size)
# initialise weights as 0
if args.comm_init == 'zeros':
for i in range(self.comm_passes):
self.C_modules[i].weight.data.zero_()
self.tanh = nn.Tanh()
# print(self.C)
# self.C.weight.data.zero_()
# Init weights for linear layers
# self.apply(self.init_weights)
self.value_head = nn.Linear(self.hid_size, 1)
def get_agent_mask(self, batch_size, info):
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(1, 1, n)
agent_mask = agent_mask.expand(batch_size, n, n).unsqueeze(-1)
return num_agents_alive, agent_mask.clone()
def forward_state_encoder(self, x):
hidden_state, cell_state = None, None
if self.args.recurrent:
x, extras = x
x = self.encoder(x)
if self.args.rnn_type == 'LSTM':
hidden_state, cell_state = extras
else:
hidden_state = extras
# hidden_state = self.tanh( self.hidd_encoder(prev_hidden_state) + x)
else:
x = self.encoder(x)
x = self.tanh(x)
hidden_state = x
return x, hidden_state, cell_state
def get_agent_locs(self, x):
dim = int(sqrt(x[0].shape[2] - self.SSN_state_len))
locs = []
for agent_idx in range(x[0].shape[1]):
idx = x[0][0][agent_idx][:dim**2].nonzero(as_tuple=True)[0]
if idx.shape[0] == 0:
continue
idx = idx[0]
x_pos = idx // dim
y_pos = idx % dim
locs.append(torch.tensor([x_pos, y_pos]))
return locs
def forward(self, x, info={}):
# TODO: Update dimensions
"""Forward function for CommNet class, expects state, previous hidden
and communication tensor.
B: Batch Size: Normally 1 in case of episode
N: number of agents
Arguments:
x {tensor} -- State of the agents (N x num_inputs)
prev_hidden_state {tensor} -- Previous hidden state for the networks in
case of multiple passes (1 x N x hid_size)
comm_in {tensor} -- Communication tensor for the network. (1 x N x N x hid_size)
Returns:
tuple -- Contains
next_hidden {tensor}: Next hidden state for network
comm_out {tensor}: Next communication tensor
action_data: Data needed for taking next action (Discrete values in
case of discrete, mean and std in case of continuous)
v: value head
"""
# if self.args.env_name == 'starcraft':
# maxi = x.max(dim=-2)[0]
# x = self.state_encoder(x)
# x = x.sum(dim=-2)
# x = torch.cat([x, maxi], dim=-1)
# x = self.tanh(x)
if self.lossy_comm:
locs = self.get_agent_locs(x)
x, hidden_state, cell_state = self.forward_state_encoder(x)
batch_size = x.size()[0]
n = self.nagents
num_agents_alive, agent_mask = self.get_agent_mask(batch_size, info)
# Hard Attention - action whether an agent communicates or not
if self.args.hard_attn:
comm_action = torch.tensor(info['comm_action'])
comm_action_mask = comm_action.expand(batch_size, n, n).unsqueeze(-1)
# action 1 is talk, 0 is silent i.e. act as dead for comm purposes.
agent_mask *= comm_action_mask.double()
agent_mask_transpose = agent_mask.transpose(1, 2)
for i in range(self.comm_passes):
# Choose current or prev depending on recurrent
# import pdb; pdb.set_trace()
# (PxA)x(PxA) matrix showing comm from agent x (row) to agent y (col)
comm = hidden_state.view(batch_size, n, self.hid_size) if self.args.recurrent else hidden_state
# Get the next communication vector based on next hidden state
comm = comm.unsqueeze(-2).expand(-1, n, n, self.hid_size)
if self.lossy_comm:
sender = list(range(self.num_P + self.num_A))
permutations = itertools.permutations(sender, 2)
dists = np.zeros((self.num_P + self.num_A,self.num_P + self.num_A))
for p in permutations:
if dists[p[0]][p[1]] != 0:
continue
p1 = locs[p[0]]
p2 = locs[p[1]]
dist = sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
dists[p[0]][p[1]] = dist
dists[p[1]][p[0]] = dist
noise = torch.zeros(comm.shape)
# Apply loss to comm messages
for sender in range(self.num_P):
for receiver in range(self.num_P + self.num_A):
dist = torch.tensor(dists[sender][receiver])
comm_msg = comm[0][sender][receiver].reshape(1, -1)
comm_loss = real_comm_loss(np.array([dist]), comm_msg, self.k).reshape((-1))
comm_loss_noise = torch.normal(mean=comm_loss, std=0.3).expand(comm[0][sender][receiver].shape)
noise[0][sender][receiver] = comm_loss_noise
comm = comm.clone()
comm += noise
# Create mask for masking self communication
mask = self.comm_mask.view(1, n, n)
mask = mask.expand(comm.shape[0], n, n)
mask = mask.unsqueeze(-1)
mask = mask.expand_as(comm)
comm = comm * mask
if hasattr(self.args, 'comm_mode') and self.args.comm_mode == 'avg' \
and num_agents_alive > 1:
comm = comm / (num_agents_alive - 1)
# Mask comm_in
# Mask communcation from dead agents
comm = comm * agent_mask
# Mask communication to dead agents
comm = comm * agent_mask_transpose
# Combine all of C_j for an ith agent which essentially are h_j
comm_sum = comm.sum(dim=1)
c = self.C_modules[i](comm_sum)
if self.args.recurrent:
# skip connection - combine comm. matrix and encoded input for all agents
inp = x + c
inp = inp.view(batch_size * n, self.hid_size)
output = self.f_module(inp, (hidden_state, cell_state))
hidden_state = output[0]
cell_state = output[1]
else: # MLP|RNN
# Get next hidden state from f node
# and Add skip connection from start and sum them
hidden_state = sum([x, self.f_modules[i](hidden_state), c])
hidden_state = self.tanh(hidden_state)
# v = torch.stack([self.value_head(hidden_state[:, i, :]) for i in range(n)])
# v = v.view(hidden_state.size(0), n, -1)
value_head = self.value_head(hidden_state)
h = hidden_state.view(batch_size, n, self.hid_size)
if self.continuous:
action_mean = self.action_mean(h)
action_log_std = self.action_log_std.expand_as(action_mean)
action_std = torch.exp(action_log_std)
# will be used later to sample
action = (action_mean, action_log_std, action_std)
else:
# discrete actions
action = [F.log_softmax(head(h), dim=-1) for head in self.heads]
if self.args.recurrent:
return action, value_head, (hidden_state.clone(), cell_state.clone())
else:
return action, value_head
def init_weights(self, m):
if type(m) == nn.Linear:
m.weight.data.normal_(0, self.init_std)
def init_hidden(self, batch_size):
# dim 0 = num of layers * num of direction
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)))