-
Notifications
You must be signed in to change notification settings - Fork 4
/
utilities.jl
581 lines (450 loc) · 14.1 KB
/
utilities.jl
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
"""
$(TYPEDSIGNATURES)
"""
@inline function checkbound(x, y, xdim, ydim)
(x>0)&&(x<=xdim)&&(y>0)&&(y<=ydim)
end
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given agent's patch.
"""
function neighbor_patches_moore(patch::Tuple{Int, Int}, model::SpaceModel2D{T,S,P}, dist::Int=1) where {T,S,P<:PeriodicType} # -range to range for both x and y
# patch_dict = unwrap(model.patches[patch...])
# key = Symbol((metric, dist))
# if haskey(patch_dict, key)
# return patch_dict[key]
# end
x,y = patch
lst = NTuple{2, Int}[]
xdim = model.size[1]
ydim = model.size[2]
for i in -dist:dist
for j in -dist:dist
pnew = (mod1(x+i, xdim), mod1(y+j, ydim))
if pnew != (x,y)
push!(lst, pnew)
end
end
end
sz = min(xdim, ydim)
if div(sz, 2)+ sz%2 < dist+1
unique!(lst)
end
#if dist<=2
# patch_dict[key] = lst
#end
return lst
end
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given agent's patch.
"""
function neighbor_patches_moore(patch::Tuple{Int, Int}, model::SpaceModel2D{T,S,P}, dist::Int=1) where {T,S,P<:NPeriodicType}
x,y = patch
xdim=model.size[1]
ydim=model.size[2]
lst = NTuple{2, Int}[]
for i in -dist:dist
for j in -dist:dist
x_n = x+i
y_n = y+j
if (i,j)!=(0,0) && checkbound(x_n, y_n, xdim, ydim)
push!(lst, (x_n,y_n))
end
end
end
# sz = min(size...)
# if div(sz, 2)+ sz%2 < dist+1
# unique!(lst)
# end
return lst
end
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given patch.
"""
function neighbor_patches_moore(agent::Agent2D, model::SpaceModel2D, dist::Int=1)
patch = getfield(agent, :last_grid_loc)::Tuple{Int, Int}
return neighbor_patches_moore(patch, model, dist)
end
#################
##############
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given agent's patch.
"""
function neighbor_patches_neumann(patch::Tuple{Int, Int}, model::SpaceModel2D{T,S,P}, dist::Int=1) where {T,S,P<:PeriodicType} # -range to range for both x and y
# patch_dict = unwrap(model.patches[patch...])
# key = Symbol((metric, dist))
# if haskey(patch_dict, key)
# return patch_dict[key]
# end
x,y = patch
lst = NTuple{2, Int}[]
xdim = model.size[1]
ydim = model.size[2]
for i in -dist:dist
for j in -dist:dist
if manhattan_distance(patch, (x+i,y+j))<=dist
pnew = (mod1(x+i, xdim), mod1(y+j, ydim))
if pnew != (x,y)
push!(lst, pnew)
end
end
end
end
sz = min(xdim, ydim)
if div(sz, 2)+ sz%2 < dist+1
unique!(lst)
end
#if dist<=2
# patch_dict[key] = lst
#end
return lst
end
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given agent's patch.
"""
function neighbor_patches_neumann(patch::Tuple{Int, Int}, model::SpaceModel2D{T,S,P}, dist::Int=1) where {T,S,P<:NPeriodicType}
x,y = patch
xdim=model.size[1]
ydim=model.size[2]
lst = NTuple{2, Int}[]
for i in -dist:dist
for j in -dist:dist
x_n = x+i
y_n = y+j
if (i,j)!=(0,0) && (manhattan_distance(patch, (x_n,y_n)) <= dist) && checkbound(x_n, y_n, xdim, ydim)
push!(lst, (x_n,y_n))
end
end
end
# sz = min(size...)
# if div(sz, 2)+ sz%2 < dist+1
# unique!(lst)
# end
return lst
end
"""
$(TYPEDSIGNATURES)
Returns patches neighboring the given patch.
"""
function neighbor_patches_neumann(agent::Agent2D, model::SpaceModel2D, dist::Int=1)
patch = getfield(agent, :last_grid_loc)::Tuple{Int, Int}
return neighbor_patches_moore(patch, model, dist)
end
"""
$(TYPEDSIGNATURES)
"""
function _get_neighbors(agent::Agent2D, model::SpaceModel2D{T,S,P}, dist::Int) where {T<:MType, S<:Union{Int,Float64}, P<:PeriodicType}
x,y = getfield(agent, :last_grid_loc)
xdim = model.size[1]
ydim = model.size[2]
id = getfield(agent, :id)
id_list = Int[]
for j in -dist:dist
for i in -dist:dist
x_n = mod1(x+i, xdim)
y_n = mod1(y+j, ydim)
ags = model.patches[x_n, y_n].agents # all these agents are active for any inactive agent is removed from its container
for l in ags
if l != id
push!(id_list, l)
end
end
end
end
sz = min(xdim, ydim)
if div(sz, 2)+ sz%2 < dist+1
unique!(sort!(id_list)) # unique! directly used with Agents list will be highly inefficient
end
return (agent_with_id(l,model) for l in id_list)
end
"""
$(TYPEDSIGNATURES)
"""
function _get_neighbors(agent::Agent2D, model::SpaceModel2D{T,S,P}, dist::Int) where {T<:MType, S<:Union{Int,Float64}, P<:NPeriodicType}
x,y = getfield(agent, :last_grid_loc)::Tuple{Int, Int}
xdim = model.size[1]
ydim = model.size[2]
id = getfield(agent, :id)
id_list = Int[]
for j in -dist:dist
for i in -dist:dist
if checkbound(x+i, y+j, xdim, ydim)
ags = model.patches[x+i,y+j].agents
for l in ags
if l != id::Int
push!(id_list, l)
end
end
end
end
end
# sz = min(model.size...)
# if div(sz, 2)+ sz%2 < dist+1
# unique!(sort!(id_list)) # unique! directly used with Agents list will be highly inefficient
# end
return (agent_with_id(l,model) for l in id_list)
end
#########################
##########################
"""
$(TYPEDSIGNATURES)
"""
function _get_neighbors_neumann(agent::Agent2D, model::SpaceModel2D{T,S,P}, dist::Int) where {T<:MType, S<:Union{Int,Float64}, P<:PeriodicType}
x,y = getfield(agent, :last_grid_loc)
xdim = model.size[1]
ydim = model.size[2]
id = getfield(agent, :id)
id_list = Int[]
for j in -dist:dist
for i in -dist:dist
if manhattan_distance((x+i,y+j),(x,y))<=dist
x_n = mod1(x+i, xdim)
y_n = mod1(y+j, ydim)
ags = model.patches[x_n, y_n].agents # all these agents are active for any inactive agent is removed from its container
for l in ags
if l != id
push!(id_list, l)
end
end
end
end
end
sz = min(xdim, ydim)
if div(sz, 2)+ sz%2 < dist+1
unique!(sort!(id_list)) # unique! directly used with Agents list will be highly inefficient
end
return (agent_with_id(l,model) for l in id_list)
end
"""
$(TYPEDSIGNATURES)
"""
function _get_neighbors_neumann(agent::Agent2D, model::SpaceModel2D{T,S,P}, dist::Int) where {T<:MType, S<:Union{Int,Float64}, P<:NPeriodicType}
x,y = getfield(agent, :last_grid_loc)
xdim = model.size[1]
ydim = model.size[2]
id = getfield(agent, :id)
id_list = Int[]
for j in -dist:dist
for i in -dist:dist
x_n=x+i
y_n=y+j
if checkbound(x_n, y_n, xdim, ydim) && (manhattan_distance((x_n,y_n),(x,y))<=dist)
ags = model.patches[x+i,y+j].agents
for l in ags
if l != id::Int
push!(id_list, l)
end
end
end
end
end
# sz = min(model.size...)
# if div(sz, 2)+ sz%2 < dist+1
# unique!(sort!(id_list)) # unique! directly used with Agents list will be highly inefficient
# end
return (agent_with_id(l,model) for l in id_list)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function toroidal_distancesq(pos1,pos2, xdim, ydim)
x1,y1=pos1
x2,y2=pos2
x1, y1 = mod1(x1, xdim), mod1(y1, ydim)
x2, y2 = mod1(x2, xdim), mod1(y2, ydim)
dx = abs(x2-x1)
dy = abs(y2-y1)
dx = min(dx, xdim-dx)
dy = min(dy, ydim-dy)
return dx^2+dy^2
end
"""
$(TYPEDSIGNATURES)
"""
function _find_eu_neighbors(agent::Agent2D, neighbors_list, model::SpaceModel2D{T, S, P},dist::Real ) where {T<:MType, S<:Union{Int, Float64}, P<:NPeriodicType}
distsq = dist^2
return Iterators.filter(ag->begin vec = ag.pos .- agent.pos; dotprod(vec,vec)<distsq end, neighbors_list)
end
"""
$(TYPEDSIGNATURES)
"""
function _find_eu_neighbors(agent::Agent2D, neighbors_list, model::SpaceModel2D{T, S, P},dist::Real ) where {T<:MType, S<:Union{Int, Float64}, P<:PeriodicType}
distsq = dist^2
xdim, ydim = model.size
return Iterators.filter(ag-> toroidal_distancesq(ag.pos, agent.pos, xdim, ydim)<distsq, neighbors_list)
end
"""
$(TYPEDSIGNATURES)
Returns active neighboring agents to given agent within euclidean distance `dist`.
"""
function neighbors(agent::Agent2D, model::SpaceModel2D{MortalType, S, P}, dist::Real=1.0) where {S<:Union{Int, Float64}, P<:SType}
if !(agent._extras._active::Bool)
return (ag for ag in Agent2D{S, P, MortalType}[])
end
distint = Int(ceil(dist))
neighbors_list = _get_neighbors(agent, model, distint)
eu_neighbors = _find_eu_neighbors(agent, neighbors_list, model, dist)
return eu_neighbors
end
"""
$(TYPEDSIGNATURES)
Returns active neighboring agents to given agent within euclidean distance `dist`.
"""
function neighbors(agent::Agent2D, model::SpaceModel2D{StaticType}, dist::Real=1.0)
distint = Int(ceil(dist))
neighbors_list = _get_neighbors(agent, model, distint)
eu_neighbors = _find_eu_neighbors(agent, neighbors_list, model, dist)
return eu_neighbors
end
"""
$(TYPEDSIGNATURES)
"""
function neighbors_moore(agent::Agent2D, model::SpaceModel2D{MortalType, S, P}, dist::Int=1) where {S<:Union{Int, Float64}, P<:SType}
if !(agent._extras._active::Bool)
return (ag for ag in Agent2D{S, P, MortalType}[])
end
return _get_neighbors(agent, model, dist)
end
"""
$(TYPEDSIGNATURES)
"""
function neighbors_moore(agent::Agent2D, model::SpaceModel2D{StaticType, S, P},dist::Int=1) where {S<:Union{Int, Float64}, P<:SType}
return _get_neighbors(agent, model, dist)#_get_grid_neighbors(agent, model, dist, metric=metric)
end
"""
$(TYPEDSIGNATURES)
"""
function neighbors_neumann(agent::Agent2D, model::SpaceModel2D{MortalType, S, P}, dist::Int=1) where {S<:Union{Int, Float64}, P<:SType}
if !(agent._extras._active::Bool)
return (ag for ag in Agent2D{S, P, MortalType}[])
end
return _get_neighbors_neumann(agent, model, dist)
end
"""
$(TYPEDSIGNATURES)
"""
function neighbors_neumann(agent::Agent2D, model::SpaceModel2D{StaticType},dist::Int=1)
return _get_neighbors_neumann(agent, model, dist)#_get_grid_neighbors(agent, model, dist, metric=metric)
end
"""
$(TYPEDSIGNATURES)
Returns a random patch where no agents are present. Rerurns nothing if there is no such patch.
"""
function random_empty_patch(model::SpaceModel2D)
empty_patches = filter(pt -> length(model.patches[pt...].agents::Vector{Int})==0, model.patch_locs)
if length(empty_patches)>0
return rand(empty_patches)
else
return nothing
end
end
"""
$(TYPEDSIGNATURES)
Returns grid location of the agent.
"""
function get_grid_loc(agent::Agent2D{<:Float64})
return getfield(agent, :last_grid_loc)
end
"""
$(TYPEDSIGNATURES)
Returns grid location of the agent.
"""
function get_grid_loc(agent::Agent2D{Int})
return agent.pos
end
"""
$(TYPEDSIGNATURES)
Returns list of agents at a given patch.
"""
function agents_at(patch, model::SpaceModel2D)
lst = model.patches[patch...].agents
return (agent_with_id(l, model) for l in lst)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function get_agents(model::SpaceModel2D{MortalType, S, P}, condition::Function) where {S<:Union{Int, Float64}, P<:SType}
all_agents = [model.agents, model.agents_added]
all_agents_itr = (ag for i in 1:2 for ag in all_agents[i])
return Iterators.filter(ag-> (ag._extras._active::Bool)&&(condition(ag)), all_agents_itr)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function get_agents(model::SpaceModel2D{MortalType, S, P}) where {S<:Union{Int, Float64}, P<:SType}
all_agents = [model.agents, model.agents_added]
all_agents_itr = (ag for i in 1:2 for ag in all_agents[i])
return Iterators.filter(ag->ag._extras._active::Bool, all_agents_itr)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function get_agents(model::SpaceModel2D{StaticType}, condition::Function )
return Iterators.filter(ag->condition(ag), model.agents)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function get_agents(model::SpaceModel2D{StaticType})
return (ag for ag in model.agents)
end
"""
$(TYPEDSIGNATURES)
Returns agent having given id.
"""
function agent_with_id(i::Int, model::SpaceModel2D{MortalType, S, P}) where {S<:Union{Int, Float64}, P<:SType}
m = model.properties._extras._len_model_agents::Int
if i<=m
for j in i:-1:1 # will work if the list of model agents has not been shuffled
ag = model.agents[j]
if getfield(ag, :id) == i
return ag
end
end
end
for ag in model.agents_added # still assuming that the user will avoid shuffling agents list
if getfield(ag, :id) == i
return ag
end
end
for j in m:-1:1 # check in model.agents list beginning from the end as the initial part has been checked above
ag = model.agents[j]
if getfield(ag, :id) == i
return ag
end
end
for ag in model.agents_killed# finally check in the list of killed agents
if getfield(ag, :id) == i
return ag
end
end
return nothing
end
"""
$(TYPEDSIGNATURES)
Returns agent having given id.
"""
function agent_with_id(i::Int, model::SpaceModel2D{StaticType})
if getfield(model.agents[i], :id) == i # will work if agents list has not been shuffled
return model.agents[i]
end
for ag in model.agents
if getfield(ag, :id) == i
return ag
end
end
return nothing
end
"""
$(TYPEDSIGNATURES)
Sets window size.
"""
function set_window_size(width::Int, height::Int)
gparams.width=width
gparams.height=height
return
end