-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_resistor_model.py
457 lines (322 loc) · 9.21 KB
/
mpi_resistor_model.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import numpy as np
pi = np.pi
from numpy.random import rand
from numpy.random import randint
from mpi4py import MPI
from cython_utils import frac_neighbours, mean_neighbours
import networkx as nx
class Controller(object):
def __init__(self,N_agents,Ri=2,R=25,U=10,comm_error=0.1,\
min_gain=lambda: 0.0001,agentClass=None,budget=1,silent=False,turns=1000,G=None,\
generate_graph=False,selfishness=lambda: 0.5):
self.silent = silent
self.comm = MPI.COMM_WORLD
self.mpi_size = self.comm.Get_size()
self.rank = self.comm.Get_rank()
if G is None:
if self.rank == 0:
if generate_graph:
G = nx.connected_watts_strogatz_graph(N_agents,4,0.5)
else:
G = nx.cycle_graph(N_agents)
self._G = self.comm.bcast(G,root=0)
if agentClass is None:
agentClass = Agent
R = float(R) * N_agents
self._N = N_agents # Number of global agents
self._Ri = float(Ri)
self._R = float(R)
self._U = float(U)
self.budget = float(budget)
self._turns = turns
# number of agents for each worker
base = np.floor(self._N / float(self.mpi_size))
# hangover
leftover = self._N % self.mpi_size
sizes = np.ones(self.mpi_size)*base
sizes[:leftover]+=1
offsets = np.zeros(self.mpi_size)
offsets[1:] = np.cumsum(sizes)[:-1]
self.mpi_gather_sizes = sizes
self.mpi_gather_offsets = offsets
start = offsets[self.rank]
end = start + sizes[self.rank]
agent_idxs = np.arange(start,end)
self.local_agents = [agentClass(idx=i,R=R,
s=max([0,min([0.99,selfishness()])]),
# s=0.9,
controller=self,
min_gain=min_gain(),
turns=turns) for i in agent_idxs]
if not(self.silent):
print "number of agents in rank {}:{}\n{}".format(self.rank,\
len(self.local_agents),agent_idxs)
for agent in self.local_agents:
# print agent.min_gain,agent.s
agent.k = randint(10)+1
agent.budget=self.budget/self._N
agent.alpha = 0.3
agent.omega = 3+rand()
# agent.nn = [agent.idx-1,(agent.idx+1)%self._N]
agent.nn = self._G.neighbors(agent._idx)
self._comm_error = comm_error
self._I = []
self._Pout = []
self._Us = []
self._Rg = []
self._n = 0 # Number of steps we did
self.is_constrained = True
self._global_P = [1]
self._global_dP_list = [1]
self._global_dP = 0
self.calc_global_dP = False
self.globalGameState = {0:np.zeros(self._N,dtype=np.int8)}
pass
@property
def turns(self):
return self._turns
@property
def global_P(self):
return self._global_P[-1]
@global_P.setter
def global_P(self,P):
self._global_P.append(P)
if self.calc_global_dP:
y = self._global_P[-self.trend_horizon:]
x = np.arange(len(y))
p = np.polyfit(x,y,1)
self._global_dP = p[0]
self._global_dP_list.append(p[0])
@property
def global_dP(self):
return self._global_dP
@property
def N_agents(self):
return self._N
@property
def comm_error(self):
return self._comm_error
@property
def R(self):
return self._R
@property
def Ri(self):
return self._Ri
@property
def U(self):
return self._U
@property
def I(self):
return self._I[-1]
@I.setter
def I(self,I_new):
self._I.append(I_new)
@property
def P_out(self):
return self._Pout[-1]
@P_out.setter
def P_out(self,P_new):
self._Pout.append(P_new)
@property
def Us(self):
return self._Us[-1]
@Us.setter
def Us(self,U_new):
self._Us.append(U_new)
@property
def Rg(self):
return self._Rg[-1]
@Rg.setter
def Rg(self,R_new):
self._Rg.append(R_new)
def getGlobalR(self):
local_R_vec = np.array([a.R for a in self.local_agents],dtype=np.float64)
global_R_vec = np.zeros(self._N,dtype=np.float64)
# all subgroups sync together
# everyone transmits his parts and they get conected together
self.comm.Allgatherv(local_R_vec,[global_R_vec,self.mpi_gather_sizes,\
self.mpi_gather_offsets,MPI.DOUBLE])
return global_R_vec
def getAllResistors(self):
local_vec = np.array([a.k for a in self.local_agents],dtype=np.int16)
#TODO: apply error?
global_vec = np.zeros(self._N,dtype=np.int16)
self.comm.Allgatherv(local_vec,[global_vec,\
self.mpi_gather_sizes,self.mpi_gather_offsets,MPI.INT16_T])
return global_vec
def getGlobalGameState(self):
local_Game_vec = np.array([a.decision[self._n-1] for a in self.local_agents],dtype=np.int8)
# we apply the communicatoin error at this point so everyone gets the same "error"
if self.comm_error>0:
rand_vec = np.random.rand(len(local_Game_vec))
rand_messages = np.random.randint(-1,2,len(local_Game_vec))
local_Game_vec[rand_vec<self.comm_error]=rand_messages[rand_vec<self.comm_error]
global_Game_vec = np.zeros(self._N,dtype=np.int8)
# all subgroups sync together
# everyone transmits his parts and they get conected together
self.comm.Allgatherv(local_Game_vec,[global_Game_vec,\
self.mpi_gather_sizes,self.mpi_gather_offsets,MPI.INT8_T])
self.globalGameState = {self._n:global_Game_vec}
# @profile
def update_physics(self):
self._n += 1
if self.rank == 0:
global_n = self._n
else:
global_n = None
global_n = self.comm.bcast(global_n,root=0)
assert global_n == self._n
R = self._Ri
# this was the fastest version. Allreduce was slower
C_global = 0
C_local = sum((1/a.R for a in self.local_agents))
C_global=self.comm.allreduce(C_local,op=MPI.SUM)
R += 1/C_global
self.Rg = R
self.I = self._U / R
self.Us = self._U - self._Ri * self.I
P = 0
P_local = 0
U2 = self.Us**2
for agent in self.local_agents:
P = U2/agent.R
agent.P = P
P_local += P
# get and sum up all local P values
P_global = self.comm.allreduce(P_local,op=MPI.SUM)
self.global_P = P_global
assert self._n-1 in self.globalGameState
gameState = self.globalGameState[self._n-1]
message = dict()
message['n'] = self._n
message['sender'] = self
message['neighbours'] = gameState
message['allResistors'] = self.getAllResistors()
message['distress'] = self.Us < self._U/2
for agent in self.local_agents:
# we tell the agents that it is time to update themselfs and what
# time n we have and what their neighbours did
agent.update(message)
def run(self):
import time
t = time.time()
for i in range(self.turns):
self.update_physics()
self.getGlobalGameState()
print "processing time: {}".format(time.time()-t)
def collectAgents(self):
if self.rank == 0:
self._agents = self.local_agents
for i in range(1,self.mpi_size):
if self.rank == 0:
remote_agents = self.comm.recv(source=i,tag=0)
self._agents.extend(remote_agents)
if self.rank == i:
self.comm.send(self.local_agents,dest=0,tag=0)
def __str__(self):
return 'Controller'
def __repr__(self):
return 'Controller'
class Agent(object):
def __init__(self,idx,R,controller,s,P=1e-10,min_gain=0.001,budget=0,turns=1):
self.R0 = float(R)
self._R = self.R0
self.min_gain = min_gain
self.budget = budget
self._k = 1
# self._k_que = deque([1])
self._k_que = np.zeros(turns+2)
self._k_que[0] = self._k
self._P = P
# self._P_que = deque([P])
self._P_que = np.zeros(turns+1)
self._P_que[0] = self._P
# deltas
self._exp_P = 1
self._exp_k = 1
self.s = s
self._idx = idx
self.controller = controller
# game decisions
self.decision = np.zeros(turns+1,dtype=np.int8)
self._n = 0 # Number of steps we did
self._nn = []
self.nn_mask = np.zeros(controller.N_agents)
pass
@property
def nn(self):
return self._nn
@nn.setter
def nn(self,nn_new):
self._nn = nn_new
self.nn_mask = np.zeros(self.controller.N_agents,dtype=np.uint8)
self.nn_mask[nn_new] = 1
@property
def idx(self):
return self._idx
@property
def R(self):
return self._R
@property
def k(self):
return self._k
@k.setter
def k(self,k_new):
self._k_que[self._n]=int(k_new)
self._exp_k = k_new - self._k
self._k = max(int(k_new),1)
self._R = self.R0/k_new
@property
def P(self):
return self._P
@P.setter
def P(self,P_new):
self._P_que[self._n]=P_new
self._exp_P = (P_new - self._P)/self._P
self._P = P_new
def expected_dP(self):
return self._exp_P
def delta_k(self):
return self._exp_k
# @profile
def update(self,message):
if message['n']>self._n and message['sender']==self.controller:
self._n = message['n']
n = self._n
if self.expected_dP() > self.min_gain:
# we do what we did before
if self.delta_k() > 0:
self.k = self.k + 1 # add appliance
self.decision[n]=-1
elif self.delta_k() < 0:
if self.k > 1:
self.k = self.k - 1
else:
self.k = self.k
self.decision[n]=1
elif self.delta_k()==0:
self.decision[n]=0
self.k = self.k
else:
f = frac_neighbours(message['neighbours'],self.nn_mask)
if (rand() > self.s or (f >= 0.5)):
self.decision[n] = 1
if self.k > 1:
self.k = self.k - 1
else:
self.k = self.k
else:
# if both neighbours defected or we just feel like it today we defect
if rand() < self.s:
# we are very selfish
self.decision[n] = -1
self.k = self.k + 1
else:
self.decision[n]=0
self.k = self.k
else:
pass # we can ignore the message since we are allready up to date
def __str__(self):
return 'Agent '+str(self._idx)