-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs.py
491 lines (382 loc) · 14.4 KB
/
funcs.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import numpy as np
import matplotlib.pyplot as plt
import itertools as it
import seaborn as sns
import pandas as pd
import random,time
def levy_flight(num_steps,alpha):
x_start, y_start = 0,0
x,y = [x_start], [y_start]
x_curr, y_curr = x_start, y_start
for i in range(num_steps-1):
r = np.random.pareto(alpha)
theta = np.random.uniform(0,2*np.pi)
x_jump, y_jump = r*np.cos(theta), r*np.sin(theta)
x_curr, y_curr = x_curr + x_jump, y_curr + y_jump
x.append(x_curr)
y.append(y_curr)
x = np.array(x)
y = np.array(y)
return x,y
def levy_jump(x_curr, y_curr, alpha, box_size):
r = np.random.pareto(alpha)
theta = np.random.uniform(0,2*np.pi)
x_jump, y_jump = r*np.cos(theta) / box_size, r*np.sin(theta) / box_size
x_curr, y_curr = int(x_curr + x_jump), int(y_curr + y_jump)
return x_curr, y_curr
def revisit(visited_places):
""" Chooses a place to revist, where place i is chosen
with prob \propto S_i, S_i = number of visits to i
"""
freqs = np.array(list(visited_places.values()))
freqs = freqs / (1.0*sum(freqs))
places_indices = range(len(freqs))
go_to = np.random.choice(places_indices, p=freqs)
x_curr, y_curr = list(visited_places.keys())[go_to]
visited_places[(x_curr,y_curr)] += 1
return x_curr, y_curr, visited_places
def preferential_return_old(x_start,y_start,alpha,rho,gamma, Ngrid,num_steps, box_size):
#Parameters
x,y = [x_start], [y_start]
x_curr, y_curr = x_start, y_start
visited_places = {} # {(x,y):freq}
visited_places[(x_start,y_start)] = 1
#Preferential return
for i in range(num_steps-1):
#Find odds of exploring new location
num_visited = len(visited_places)
prob_new = rho*num_visited**(-gamma) #defined in the Song model
temp = np.random.rand()
#Go to new location
if temp <= prob_new:
x_curr, y_curr = levy_jump(x_curr, y_curr, alpha, box_size)
#If jump has taken you outside the box, stop
if x_curr < 0 or x_curr >= Ngrid or y_curr < 0 or y_curr >= Ngrid:
return visited_places
#Add to new places
if (x_curr, y_curr) not in visited_places:
visited_places[(x_curr, y_curr)] = 1
else:
visited_places[(x_curr, y_curr)] += 1
#Return to previously visted location
else:
x_curr, y_curr, visited_places = revisit(visited_places)
return visited_places
def xy_to_cell_id(x,y,Ngrid):
return x + y*Ngrid
def cell_id_to_xy(cell_id, Ngrid):
y,x = divmod(cell_id, Ngrid)
return x,y
def dist(x1,y1,x2,y2):
d = (x2-x1)**2 + (y2-y1)**2
return np.sqrt(d)
def find_cell_with_highest_count(data):
max_so_far = 0
for key in data:
val = data[key]
if len(val) > max_so_far:
key_max = key
max_so_far = len(val)
return key_max
def update_data(x_curr, y_curr, home, data, agent_id, Ngrid):
f = 1 #we know its a new place
x_home, y_home = cell_id_to_xy(home, Ngrid)
r = dist(x_curr, y_curr, x_home, y_home)
key = xy_to_cell_id(x_curr, y_curr, Ngrid)
val = [agent_id, f, home, r, r*f]
if key not in data:
data[key] = [val]
else:
rows = data[key]
for i,row in enumerate(rows):
if row[0] == agent_id:
[agent_id, f, home, r, E] = row
new_row = [agent_id, f+1, home, r, r*(f+1)]
data[key][i] = new_row
return data
data[key].append(val)
return data
def add_to_visited_places(x_curr, y_curr, visited_places):
if (x_curr, y_curr) not in visited_places:
visited_places[(x_curr, y_curr)] = 1
else:
visited_places[(x_curr, y_curr)] += 1
return visited_places
def grab_rs_for_given_f(data,cell,f):
vals = data[cell]
rs = []
for agent_ID, f_temp, home, r, E in vals:
if f_temp == f:
rs.append(r)
return rs
def grab_Es(data,cell):
vals = data[cell]
Es = []
for agent_ID, f_temp, home, r, E in vals:
Es.append(E)
return Es
def preferential_return(num_steps,data,alpha,rho,gamma,x_curr,y_curr,agent_id,Ngrid,box_size):
#Update the data
home = xy_to_cell_id(x_curr,y_curr,Ngrid)
f, r, E = 1,0,0
val = [agent_id, f, home, r, E]
if home not in data:
data[home] = [val]
else:
data[home].append(val)
visited_places = {} # {(x,y):freq}
visited_places[(x_curr,y_curr)] = 1
for i in range(num_steps-1):
#Find odds of exploring new location
num_visited = len(visited_places)
prob_new = rho*num_visited**(-gamma) #defined in the Song model
temp = np.random.rand()
#Go to new location
if temp <= prob_new:
x_curr, y_curr = levy_jump(x_curr, y_curr, alpha, box_size)
#If jump has taken you outside the box, stop
if x_curr < 0 or x_curr >= Ngrid or y_curr < 0 or y_curr >= Ngrid:
break
visited_places = add_to_visited_places(x_curr, y_curr, visited_places)
data = update_data(x_curr, y_curr, home, data, agent_id, Ngrid)
#Return to previously visited location
else:
x_curr, y_curr, visited_places = revisit(visited_places)
cell_id = xy_to_cell_id(x_curr, y_curr, Ngrid)
#print x_curr, y_curr, cell_id
#print data.keys()
list_of_agents = data[cell_id]
#find index of
for j in range(len(list_of_agents)):
if list_of_agents[j][0] == agent_id:
break
#then update that list
[agent_id, f, home, r, E] = list_of_agents[j]
new_row = [agent_id, f+1, home, r, r*(f+1)]
data[cell_id][j] = new_row
return data
def merge(v1, v2):
""" merges two dictionaries """
for key in v2:
if key not in v1:
v1[key] = v2[key]
else:
v1[key] += v2[key]
return v1
def make_freq_matrix(visited_places, Ngrid):
freqs = np.zeros((Ngrid, Ngrid))
for key in visited_places:
freqs[key[0]][key[1]] += visited_places[key]
return freqs
def levy_jump_with_PE(x_curr, y_curr, alpha, R, nu, box_size, data, Ngrid):
""" Does a levy flight, except now the
angle is chosen according to Preferential
Exploration.
"""
r = np.random.pareto(alpha)
theta = sample_angle(x_curr, y_curr, data, R, nu, Ngrid)
x_jump, y_jump = r*np.cos(theta) / box_size, r*np.sin(theta) / box_size
x_curr, y_curr = int(x_curr + x_jump), int(y_curr + y_jump)
return x_curr, y_curr
def find_neighbours(x_curr, y_curr, R, Ngrid):
""" Return all neighbours on a grid
in the first R layers
So if R = 1, then you return
the eight neighbours surrounding
a given cell
"""
neighbours = [(x_curr + col, y_curr + row) for row in range(-R,R+1) for col in range(-R,R+1) \
if 0 <= x_curr + col <= Ngrid-1 and 0 <= y_curr + row <= Ngrid-1 ]
if len(neighbours) > 0:
neighbours.remove((x_curr, y_curr))
return neighbours
def get_energies(neighbours, data, Ngrid):
Es = np.ones(len(neighbours))
for i,n in enumerate(neighbours):
key = xy_to_cell_id(n[0], n[1], Ngrid)
E = 0
if key not in data:
Es[i] = E
else:
for row in data[key]:
E += row[-1]
Es[i] += E
return Es
def sample_angle(x_curr, y_curr, data, R, nu, Ngrid):
if R == 0:
return np.random.uniform(0,2*np.pi)
#Find which neighbour to jump to
neighbours = find_neighbours(x_curr, y_curr,R, Ngrid)
energies = get_energies(neighbours, data, Ngrid)
energies += np.ones(len(energies))
energies = energies**nu
if sum(energies) == 0:
index_of_chosen_neighbour = np.random.choice(range(len(neighbours)))
else:
energies /= sum(energies)
index_of_chosen_neighbour = np.random.choice(range(len(neighbours)), p = energies)
#Covert this to a jump angle
x1,y1 = x_curr, y_curr
(x2,y2) = neighbours[index_of_chosen_neighbour]
angle = find_angle(x1,y1,x2,y2)
#I need to fill in the missing angles here
#Now I want the final angle to be Uniform(angle-X, angle+X)
#where X is the nearest angle.
angle_to_neighbours = [abs(find_angle(x1,y1,x2,y2) - angle) for (x2,y2) in neighbours if (x2,y2) != (x1,y1)]
angle_to_neighbours = [x for x in angle_to_neighbours if x != 0]
X = min(angle_to_neighbours)
angle_final = np.random.uniform(angle-X,angle+X)
return angle_final
def find_angle(x1,y1,x2,y2):
#Find angle
dx, dy = x2-x1, y2-y1
r = np.sqrt( dx**2 + dy**2 )
angle = np.arccos(dx / r)
#Find quandrant
if dy < 0:
angle = np.pi + angle
return angle
def clean_Es(Es):
return [ x for x in Es if x != 0 ]
def preferential_exploration(num_steps,data,alpha,rho,gamma,R, nu, x_curr,y_curr,agent_id,Ngrid,box_size):
#Update the data
home = xy_to_cell_id(x_curr,y_curr,Ngrid)
f, r, E = 1,0,0
val = [agent_id, f, home, r, E]
if home not in data:
data[home] = [val]
else:
data[home].append(val)
visited_places = {} # {(x,y):freq}
visited_places[(x_curr,y_curr)] = 1
for i in range(num_steps-1):
#Find odds of exploring new location
num_visited = len(visited_places)
prob_new = rho*num_visited**(-gamma) #defined in the Song model
temp = np.random.rand()
#Go to new location
if temp <= prob_new:
x_curr, y_curr = levy_jump_with_PE(x_curr, y_curr, alpha, R, nu, box_size, data, Ngrid)
#If jump has taken you outside the box, stop
if x_curr < 0 or x_curr >= Ngrid or y_curr < 0 or y_curr >= Ngrid:
break
visited_places = add_to_visited_places(x_curr, y_curr, visited_places)
data = update_data(x_curr, y_curr, home, data, agent_id, Ngrid)
#Return to previously visited location
else:
x_curr, y_curr, visited_places = revisit(visited_places)
cell_id = xy_to_cell_id(x_curr, y_curr, Ngrid)
list_of_agents = data[cell_id]
#find index of
for j in range(len(list_of_agents)):
if list_of_agents[j][0] == agent_id:
break
#then update that list
[agent_id, f, home, r, E] = list_of_agents[j]
new_row = [agent_id, f+1, home, r, r*(f+1)]
data[cell_id][j] = new_row
return data
def spatial_plot(data, homes, Ngrid):
V, E = np.zeros((Ngrid, Ngrid)), np.zeros((Ngrid, Ngrid)),
for key in data.keys():
#Find visitation
vals = data[key]
x,y = cell_id_to_xy(key,Ngrid)
visitation = len(vals)
V[x][y] = visitation
#Find energy
Es = []
for agent_ID, f_temp, home, r, E1 in vals:
Es.append(E1)
E_mean = np.mean(Es)
E[x][y] = E_mean
#Homes
H = np.zeros((Ngrid, Ngrid))
for x,y in homes:
H[x][y] += 1
plt.figure(figsize=(12,6))
ax1 = plt.subplot(131)
plt.imshow(V)
plt.title('Visitation')
ax2 = plt.subplot(132)
plt.imshow(E)
plt.title('Effective travel distance per visitor')
ax3 = plt.subplot(133)
plt.imshow(H)
plt.title('Homes')
return
def run(num_steps,num_trials,box_size,alpha,rho,gamma,R,nu,Ngrid,num_agents):
#Setup
n_lower, n_upper = int(0.25*Ngrid), int(0.75*Ngrid)
possible_homes = [(i,j) for i in range(n_lower, n_upper) for j in range(n_lower,n_upper)]
homes = [random.choice(possible_homes) for _ in range(num_agents)]
#Do simulation
data = {}
t1 = time.time()
for i,(x_start, y_start) in enumerate(homes):
agent_id = i
x_curr, y_curr = x_start, y_start
data = preferential_exploration(num_steps,data,alpha,rho,gamma,R,nu,x_curr,y_curr,agent_id,Ngrid,box_size)
t2 = time.time()
print('took ' + str( (t2-t1)/60.0 ) + ' mins')
return data, homes
def plot_rf(data, par):
alpha, rho, gamma, num_agents = par
#Stuff for plotting
num_bins = 20
fs = [1,2,3,4,5]
plt.figure(figsize=(20,5))
plt.subplot(131)
#Plot N versus r
for f1 in fs:
rs = []
for cell in data.keys():
vals = data[cell]
for agent_ID, f_temp, home, r, E in vals:
if f_temp == f1:
if r != 0:
rs.append(r)
#Plot rs
bins = np.linspace(min(rs),10**2,num_bins)
frq, edges = np.histogram(rs, bins)
mid = [0.5*(edges[i] + edges[i+1]) for i in range(len(edges)-1) ]
plt.loglog(mid, frq,'o-')
plt.legend(['f = ' + str(f1) for f1 in fs])
plt.xlabel('$r$', fontsize=18)
plt.ylabel('$N_f(r)$', fontsize=18)
#Plot N versus r*f
plt.subplot(132)
for f1 in fs:
rs = []
for cell in data.keys():
vals = data[cell]
for agent_ID, f_temp, home, r, E in vals:
if f_temp == f1:
if r != 0:
rs.append(r*f_temp)
#Plot rs
bins = np.linspace(min(rs),10**2,num_bins)
frq, edges = np.histogram(rs, bins)
mid = [0.5*(edges[i] + edges[i+1]) for i in range(len(edges)-1) ]
plt.loglog(mid, frq,'o-')
plt.legend(['f = ' + str(f1) for f1 in fs])
plt.xlabel('$r f$', fontsize=18)
#Plot N versus r*f^2
plt.subplot(133)
for f1 in fs:
rs = []
for cell in data.keys():
vals = data[cell]
for agent_ID, f_temp, home, r, E in vals:
if f_temp == f1:
if r != 0:
rs.append(r*f_temp**2)
#Plot rs
bins = np.linspace(min(rs),10**3,num_bins)
frq, edges = np.histogram(rs, bins)
mid = [0.5*(edges[i] + edges[i+1]) for i in range(len(edges)-1) ]
plt.loglog(mid, frq,'o-')
plt.legend(['f = ' + str(f1) for f1 in fs])
plt.xlabel('$r f^2$', fontsize=18)
filename = 'figures/EPE_lattice/alpha_{}_rho_{}_gamma_{}_Nagent_{}.pdf'.format(alpha,rho,gamma,num_agents)
plt.savefig(filename)