-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworksAndUniMix.nlogo
3684 lines (3260 loc) · 92.1 KB
/
NetworksAndUniMix.nlogo
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Networks and Universal Mixing: Comparative compartmental disease models.
; (C) Christopher J Watts, 2020.
; Compares the universal mixing in a compartmental model of disease transmission
; with a social-network-based model.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
extensions [rnd csv]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
globals [
Current-View ; Show compartments or network?
last-seed-setup ; Random-number generator seeds
last-seed-go
sorted-people ; List, sorted by who. Avoids use of "ask people []", and hence generation of random permutations of people.
susceptible ; Every model should have this disease state compartment.
exposed ; First compartment after susceptible.
compartments-sorted ; Used for updating in order, and one charts
new-case-transition ; Define this to fit with your disease model.
seed-infection-times ; List of time steps when seed infections occur.
; Disease Statistics
net-cases-new
net-cases-total
net-cases-peak
net-cases-peak-day
net-infections-new
net-infections-total
net-infections-peak
net-infections-peak-day
um-cases-new
um-cases-total
um-cases-peak
um-cases-peak-day
um-infections-new
um-infections-total
um-infections-peak
um-infections-peak-day
; Network statistics
mean-degree
median-degree
min-degree
max-degree
var-degree
assortativity
network-density
clustering-coefficient
mean-cliquishness
median-cliquishness
min-cliquishness
max-cliquishness
mean-dos
min-dos
max-dos
median-dos
mean-closeness
min-closeness
max-closeness
median-closeness
net-diameter
degree-centralization
closeness-centralization
num-components
max-component-size
; sim-time
start-date-as-num
end-date-as-num
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
breed [people person]
undirected-link-breed [slinks slink]
breed [compartments compartment]
directed-link-breed [transitions transition]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
people-own [
p-state ; Person's current disease state (compartment)
p-maturity ; Time leave state, and which state person enters next.
p-infection-time ; False if never infected. Otherwise, time they became infectious.
p-circle ; Social circle
p-cliquishness ; Local clustering coefficient.
p-component ; Network component
p-dos ; Average degree of separation from other nodes = 1 / Closeness
p-tmp ; Used for DOS calc.
p-reach
p-closeness
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
slinks-own [
sl-contacts
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
compartments-own [
c-name
c-color
c-contents
c-rel-inf ; relative infectiousness
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
transitions-own [
ts-weight
ts-maturity-task
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Setup
to setup
clear-all
ask patches [set pcolor white]
set current-view view-of-compartments
setup-disease-compartments
setup-initial-plots
change-view
setup-rng "Seed-Setup"
setup-population
reset-sim
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-rng [given-variable-name]
ifelse 0 = runresult given-variable-name [
run (word "set last-" given-variable-name " " new-seed)
]
[
run (word "set last-" given-variable-name " " given-variable-name)
]
random-seed runresult (word "last-" given-variable-name)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to switch-mechanism
ifelse sim-mechanism = sim-mechanism-network [
set sim-mechanism sim-mechanism-uni-mix
reset-sim
]
[
set sim-mechanism sim-mechanism-network
reset-sim
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report sim-mechanism-network
report "Network"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report sim-mechanism-uni-mix
report "Universal Mixing"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Disease transmission model
to setup-disease-compartments
run (word "setup-disease-compartments-" compartments-design)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-disease-compartments-Susceptible-Infectious
; The simplest model: Susceptible-Infectious
let mid-x mean (list max-pxcor min-pxcor)
let x-step world-width / 8
let y-step world-height / 4
; new-compartment name relative-infectiousness xcor ycor color
let ds-S new-compartment "Susceptible" 0.0 mid-x (max-pycor - 1 * y-step) green
let ds-I new-compartment "Infectious" 1.0 mid-x (max-pycor - 2 * y-step) red
set susceptible ds-S
set exposed ds-I
set compartments-sorted (list ds-S ds-I)
; make-transition-to destination-compartment weight maturity-time-reporter
ask ds-S [make-transition-to ds-I 1.0 [-> ]]
ask ds-S [ask out-transition-to ds-I [set new-case-transition self]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-disease-compartments-SIR
; The classic model introduced by Kermack & McKendrick (1927): Susceptible-Infectious-Removed
let mid-x mean (list max-pxcor min-pxcor)
let x-step world-width / 8
let y-step world-height / 4
; new-compartment name relative-infectiousness xcor ycor color
let ds-S new-compartment "Susceptible" 0.0 mid-x (max-pycor - 1 * y-step) green
let ds-I new-compartment "Infectious" 1.0 mid-x (max-pycor - 2 * y-step) red
let ds-R new-compartment "Removed" 0.0 (mid-x - 0 * x-step) (max-pycor - 3 * y-step) brown
set susceptible ds-S
set exposed ds-I
set compartments-sorted (list ds-S ds-I ds-R)
; make-transition-to destination-compartment weight maturity-time-reporter
ask ds-S [make-transition-to ds-I 1.0 [-> ]]
ask ds-I [make-transition-to ds-R 1.0 [-> random-event-time-gamma 8.0 8.0]]
ask ds-S [ask out-transition-to ds-I [set new-case-transition self]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-disease-compartments-SEI3HRD
; Based on the Covid-19 model from the LSHTM (Davies et al. 2020)
let mid-x mean (list max-pxcor min-pxcor)
let x-step world-width / 8
let y-step world-height / 8
; new-compartment name relative-infectiousness xcor ycor color
; new-compartment name relative-infectiousness xcor ycor
let ds-S new-compartment "Susceptible" 0.0 mid-x (max-pycor - 1 * y-step) green
let ds-E new-compartment "Exposed" 0.0 mid-x (max-pycor - 2 * y-step) yellow
let ds-Ip new-compartment "I-Preclinical" 1.00 (mid-x + 1 * x-step) (max-pycor - 3 * y-step) orange
let ds-Ic new-compartment "I-Clinical" 1.00 (mid-x + 1 * x-step) (max-pycor - 4 * y-step) red
let ds-Is new-compartment "I-Subclinical" 0.50 (mid-x - 1 * x-step) (max-pycor - 4 * y-step) pink
let ds-H new-compartment "Hospitalize?" 0.0 (mid-x + 1 * x-step) (max-pycor - 5 * y-step) sky
let ds-ICU new-compartment "ICU" 0.0 (mid-x + 2 * x-step) (max-pycor - 6 * y-step) (violet - 1)
let ds-Non-ICU new-compartment "Non-ICU" 0.0 (mid-x + 1 * x-step) (max-pycor - 6 * y-step) (violet + 2)
let ds-R new-compartment "Recovered" 0.0 (mid-x - 1 * x-step) (max-pycor - 7 * y-step) brown
let ds-D new-compartment "Dead" 0.0 (mid-x + 2 * x-step) (max-pycor - 7 * y-step) (gray - 4)
set susceptible ds-S
set exposed ds-E
set compartments-sorted (list ds-S ds-E ds-Ip ds-Ic ds-Is ds-H ds-ICU ds-Non-ICU ds-R ds-D)
; make-transition-to destination-compartment weight maturity-time-reporter
ask ds-S [make-transition-to ds-E 1.0 [-> ]]
ask ds-E [make-transition-to ds-Ip (perc-symptomatic / 100) [-> random-event-time-gamma 4.0 4.0]]
ask ds-E [make-transition-to ds-Is ((100 - perc-symptomatic) / 100) [-> random-event-time-gamma 4.0 4.0]]
ask ds-Ip [make-transition-to ds-Ic 1.0 [-> random-event-time-gamma 1.5 4.0]]
ask ds-Ic [make-transition-to ds-H 1.0 [-> random-event-time-gamma 3.5 3.5]]
ask ds-Is [make-transition-to ds-R 1.0 [-> random-event-time-gamma 5.0 4.0]]
ask ds-H [make-transition-to ds-R ((100 - perc-hospitalized) / 100) [-> 0]]
ask ds-H [make-transition-to ds-ICU (perc-hospitalized * Perc-Need-ICU / 10000) [-> random-event-time-gamma 3.5 3.5]]
ask ds-H [make-transition-to ds-Non-ICU (perc-hospitalized * (100 - Perc-Need-ICU) / 10000) [-> random-event-time-gamma 3.5 3.5]]
ask ds-ICU [make-transition-to ds-R (Perc-Hospitalized-Recover / 100) [-> random-event-time-gamma 10.0 10.0]]
ask ds-ICU [make-transition-to ds-D ((100 - Perc-Hospitalized-Recover) / 100) [-> random-event-time-gamma 11.5 11.5]]
ask ds-Non-ICU [make-transition-to ds-R (Perc-Hospitalized-Recover / 100) [-> random-event-time-gamma 8.0 8.0]]
ask ds-Non-ICU [make-transition-to ds-D ((100 - Perc-Hospitalized-Recover) / 100) [-> random-event-time-gamma 11.5 11.5]]
ask ds-Ip [ask out-transition-to ds-Ic [set new-case-transition self]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report new-compartment [given-name given-rel-inf given-x given-y given-color]
let return-object nobody
create-compartments 1 [
set hidden? (Current-View != view-of-compartments)
set return-object self
set shape "square"
set size 2
set c-color given-color
set color c-color
setxy given-x given-y
set label-color black
set c-name given-name
set c-rel-inf given-rel-inf
set c-contents (turtle-set )
relabel-compartment
]
report return-object
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to relabel-compartment
set label (word c-name ": " (count c-contents) " ")
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report compartment-named [given-name]
report min-one-of compartments with [c-name = given-name] [who]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to make-transition-to [given-state given-weight given-maturity-task]
; Run as a compartment
create-transition-to given-state [
set hidden? (Current-View != view-of-compartments)
set color grey
set thickness 0.2
set ts-weight given-weight
set ts-maturity-task given-maturity-task
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report random-event-time-gamma [mu k]
; Where mean=mu, shape=k, in days.
; Returns in days.
report (random-gamma k (k / mu))
report (int ((random-gamma k (k / mu)) / time-step)) * time-step
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-population
setup-social-network
foreach sorted-people [cur-person ->
ask cur-person [
set hidden? (current-view != view-of-network)
set shape "person"
set color red
set size 1
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Social Networks
to setup-social-network
run (word "setup-social-network-" network-architecture)
calc-network-stats
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-slink [given-color]
set hidden? (current-view != view-of-network)
set color given-color
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-sorted-population
set sorted-people []
repeat population [
create-people 1 [set sorted-people fput self sorted-people]
]
set sorted-people reverse sorted-people
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-social-circles
; From Hamill & Gilbert (2009). http://jasss.soc.surrey.ac.uk/12/2/3.html
setup-sorted-population
foreach sorted-people [cur-person ->
ask cur-person [
setxy random-xcor random-ycor
set p-circle 0
set color red - 2
]
]
foreach sorted-people [cur-person ->
ask cur-person [
create-slinks-with other (people in-radius soc-net-radius0) [setup-slink grey + 2]
]
]
let group1 (turtle-set )
ask n-of (perc-soc-net-group1 * (count people) / 100) people [
set p-circle 1
set group1 (turtle-set group1 self)
set color red + 2
]
foreach filter [p -> [1 = p-circle] of p] sorted-people [cur-person ->
ask cur-person [
create-slinks-with other (group1 in-radius soc-net-radius1) [setup-slink grey - 2]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-erdos-renyi-random
setup-sorted-population
foreach sorted-people [ego ->
ask ego [
setxy random-xcor random-ycor
foreach filter [p -> who > [who] of p] sorted-people [alter ->
if er-density > random-float 1 [
ask alter [
create-slink-with ego [
setup-slink grey + 2
]
]
]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-ring
setup-sorted-population
layout-circle (sorted-people) (3 * (min (list world-width world-height)) / 8)
if min-links-per-node = 0 [stop]
let prev-nodes []
let first-nodes []
if min-links-per-node >= count people [user-message (word "Warning: \n\nmin-links-per-node (" min-links-per-node ") is >= Count People")]
foreach sorted-people [ego ->
if min-links-per-node > length first-nodes [
set first-nodes fput ego first-nodes
]
foreach prev-nodes [alter ->
ask ego [
create-slink-with alter [setup-slink grey + 2]
]
]
set prev-nodes fput ego sublist prev-nodes 0 min (list (min-links-per-node - 1) length prev-nodes)
]
foreach reverse first-nodes [ego ->
foreach prev-nodes [alter ->
ask ego [
create-slink-with alter [setup-slink grey + 2]
]
]
set prev-nodes fput ego sublist prev-nodes 0 min (list (min-links-per-node - 1) length prev-nodes)
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-strogatz-watts-small-world
setup-social-network-ring
let ego nobody
let alter nobody
foreach sort slinks [x ->
if sw-rewire-chance > random-float 1.0 [
set ego [end1] of x
ask x [die]
ask ego [
create-slink-with one-of other people with [not slink-neighbor? myself] [setup-slink grey + 2]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-barabasi-albert
; nw:generate-preferential-attachment people slinks population min-links-per-node
; layout-circle sort people (3 * (min (list world-width world-height)) / 8)
; stop
set sorted-people []
repeat min-links-per-node [
create-people 1 [
foreach sorted-people [alter ->
create-slink-with alter [setup-slink grey - 2]
]
set sorted-people fput self sorted-people
]
]
; let sorted-nodes reverse sort people
repeat (population - min-links-per-node) [
create-people 1 [
foreach n-of min-links-per-node sorted-people [alter ->
create-slink-with alter [
setup-slink grey - 2
]
]
set sorted-people fput self sorted-people
]
]
layout-circle sorted-people (3 * (min (list world-width world-height)) / 8)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-grid-positions
let cur-id 0
let row-length sqrt population
if row-length ^ 2 < population [set row-length row-length + 1]
let x-step world-width / row-length
let y-step world-height / row-length
foreach sort people [p ->
ask p [
setxy (x-step * (cur-id mod row-length)) (y-step * (int (cur-id / row-length)))
set cur-id cur-id + 1
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-2d-grid-neighbors [given-neighbors]
; nw:generate-lattice-2d people slinks row-length row-length false
; setup-grid-positions
; stop
set sorted-people []
let row-length int sqrt population
if row-length ^ 2 < population [set row-length row-length + 1]
let cur-id 0
let x-step world-width / (row-length + 1)
let y-step world-height / (row-length + 1)
foreach n-values row-length [? -> ?] [cur-row ->
foreach n-values row-length [? -> ?] [cur-col ->
create-people 1 [
set sorted-people fput self sorted-people
; show (list (x-step * (cur-col)) (y-step * (cur-row)))
setxy (x-step * (cur-col)) (y-step * (cur-row))
]
]
]
let neighbors-radius-pairs (list [4 1.01] [8 1.5] [12 2.01] [20 2.3] [24 2.85] [28 3.1] [36 3.2])
let chosen-item position given-neighbors map [x -> first x] neighbors-radius-pairs
if chosen-item = false [user-message "Fatal: Requested number of neighbors could not be found in list in setup-social-network-2d-grid-neighbors." stop]
let chosen-radius x-step * last item chosen-item neighbors-radius-pairs
foreach sorted-people [ego ->
ask ego [
create-slinks-with other people in-radius chosen-radius [
setup-slink grey + 2
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-social-network-kissler-data
if not file-exists? "Kissler_DataS1.csv" [user-message "FATAL: Cannot find file Kissler_DataS1.csv!" stop]
if not file-exists? "Kissler_DataS2.csv" [user-message "FATAL: Cannot find file Kissler_DataS2.csv!" stop]
let kd1 filter [r -> kd-max-distance >= item 3 r] csv:from-file "Kissler_DataS1.csv" ; Filter out all contacts at large distances.
let node-ids sort remove-duplicates sentence (map [r -> item 1 r] kd1) (map [r -> item 2 r] kd1)
set population length node-ids
setup-sorted-population
layout-circle (sorted-people) (7 * (min (list world-width world-height)) / 16)
let who-adj min [who] of people
foreach kd1 [r ->
ask person (who-adj + position (item 1 r) node-ids) [
if nobody = person (who-adj + position (item 2 r) node-ids) [print (word "Target node does not exist: " r)]
ifelse slink-neighbor? person (who-adj + position (item 2 r) node-ids) [
ask slink-with person (who-adj + position (item 2 r) node-ids) [set sl-contacts sl-contacts + 1]
]
[
create-slink-with person (who-adj + position (item 2 r) node-ids) [setup-slink lime set sl-contacts 1]
]
]
]
let kd2 csv:from-file "Kissler_DataS2.csv"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to change-view
; NB: Use of lists and sort. We don't want to use any random numbers here.
ifelse current-view = view-of-network [
set current-view view-of-compartments
]
[
if current-view = view-of-compartments [
set current-view view-of-network
]
]
if any? people [
foreach sorted-people [p ->
ask p [
set hidden? (current-view != view-of-network)
foreach sort (my-slinks with [end1 = myself]) [s ->
ask s [
set hidden? (current-view != view-of-network)
]
]
]
]
]
foreach compartments-sorted [c ->
ask c [
set hidden? (Current-View != view-of-compartments)
foreach sort (my-out-transitions) [t ->
ask t [
set hidden? (Current-View != view-of-compartments)
]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report view-of-network
report "Network"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report view-of-compartments
report "Compartments"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Disease state transitions
to reset-sim
reset-ticks
set start-date-as-num parsed-date Start-Date
set end-date-as-num parsed-date End-Date
clear-relevant-plot
setup-rng "Seed-Go"
; NB: If given the same RNG seed, this will initialize with the same seed infections after each reset.
; If you want different seed infections, but in the same network, then you need to generate and store them during Setup.
initialize-population
if sim-mechanism = sim-mechanism-network [
set net-cases-new 0
set net-cases-total 0
set net-cases-peak 0
set net-cases-peak-day 0
set net-infections-new 0
set net-infections-total 0
set net-infections-peak 0
set net-infections-peak-day 0
]
if sim-mechanism = sim-mechanism-uni-mix [
set um-cases-new 0
set um-cases-total 0
set um-cases-peak 0
set um-cases-peak-day 0
set um-infections-new 0
set um-infections-total 0
set um-infections-peak 0
set um-infections-peak-day 0
]
do-plots
update-output
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to initialize-population
ask people [
set p-state susceptible
set p-infection-time 0
set p-maturity (list infinity)
recolor-by-state
]
foreach compartments-sorted [compa ->
ask compa [set c-contents (turtle-set )]
]
ask susceptible [set c-contents people]
run schedule-seeds-n-d
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to schedule-seeds [num-seeds num-days]
set seed-infection-times []
foreach n-values num-days [d -> d] [cur-day ->
foreach n-values num-seeds [n -> n] [cur-seed ->
set seed-infection-times fput (cur-seed + round (cur-day / time-step)) seed-infection-times
]
]
set seed-infection-times reverse seed-infection-times
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to become-infected
let infected-person self
ask susceptible [ask out-transition-to exposed [do-transition-by infected-person]]
set p-infection-time sim-time
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to do-transition-by [given-person]
ask end1 [set c-contents c-contents with [self != given-person] relabel-compartment]
ask end2 [set c-contents (turtle-set c-contents given-person) relabel-compartment]
if end1 = susceptible [if end2 = exposed [record-new-infection]]
if self = new-case-transition [record-new-case]
ask given-person [
set p-state [end2] of myself
recolor-by-state
set p-maturity next-state-transition
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to record-new-infection
if sim-mechanism = sim-mechanism-network [
set net-infections-new net-infections-new + 1
if net-infections-new > net-infections-peak [
set net-infections-peak net-infections-new
set net-infections-peak-day sim-time
]
set net-infections-total net-infections-total + 1
stop
]
if sim-mechanism = sim-mechanism-uni-mix [
set um-infections-new um-infections-new + 1
if um-infections-new > um-infections-peak [
set um-infections-peak um-infections-new
set um-infections-peak-day sim-time
]
set um-infections-total um-infections-total + 1
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to record-new-case
if sim-mechanism = sim-mechanism-network [
set net-cases-new net-cases-new + 1
if net-cases-new > net-cases-peak [
set net-cases-peak net-cases-new
set net-cases-peak-day sim-time
]
set net-cases-total net-cases-total + 1
stop
]
if sim-mechanism = sim-mechanism-uni-mix [
set um-cases-new um-cases-new + 1
if um-cases-new > um-cases-peak [
set um-cases-peak um-cases-new
set um-cases-peak-day sim-time
]
set um-cases-total um-cases-total + 1
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report next-state-transition
; run by person on entering p-state
if ([not any? my-out-transitions] of p-state) [report (list infinity)]
let next-transition rnd:weighted-one-of ([my-out-transitions] of p-state) [ts-weight]
report [(list (sim-time + runresult ts-maturity-task) self)] of next-transition
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to recolor-by-state
set color [c-color] of p-state
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report contacts-per-time-step
report time-step * contact-rate / 10
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to do-infections-in-network
; Susceptible ego contacts potentially infectious alter
let inter-contact-time 1.0 / contacts-per-time-step
ask [c-contents] of susceptible [
; let num-contacts time-step * contact-rate / 10
; set num-contacts (int num-contacts) + ifelse-value ((num-contacts mod 1) > random-float 1) [1] [0]
ask slink-neighbors with [
ifelse-value ([c-rel-inf > 0] of p-state) [time-step > random-exponential (inter-contact-time * (count slink-neighbors))] [false]
] [
; ask n-of (random-poisson contacts-per-time-step) slink-neighbors [
if [susceptible != p-state] of myself [stop] ; Ego may have already become infectious, in which case don't waste computer time.
if [c-rel-inf > 0] of p-state [ ; Alter is infectious.
if [c-rel-inf * susceptibility > random-float 100] of p-state [
ask myself [become-infected]
]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to do-infections-by-universal-mixing
let inf-chance 1.0 - exp (0 - force-of-infection * time-step)
ask [c-contents with [inf-chance > random-float 1]] of susceptible [ ; No random-binomial!
become-infected
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report force-of-infection
report susceptibility * contacts-per-time-step * ((sum [c-rel-inf * count c-contents] of compartments) / count people) / 100
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to do-maturation
foreach but-first compartments-sorted [compa ->
ask [c-contents with [sim-time >= first p-maturity]] of compa [
ask last p-maturity [do-transition-by myself]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report infinity
; Arbitrarily large number.
report 2147483648 ; = 2 ^ 31
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
if sim-time = end-date-as-num - start-date-as-num [stop]
if 0 = sim-time mod 1 [ ; New day.
if sim-mechanism = sim-mechanism-network [set net-cases-new 0 set net-infections-new 0]
if sim-mechanism = sim-mechanism-uni-mix [set um-cases-new 0 set um-infections-new 0]
]
; Do seed infections (if any scheduled)
while [ifelse-value (not empty? seed-infection-times) [sim-time = first seed-infection-times] [false]] [
ask susceptible [
if any? c-contents [
ask one-of c-contents [become-infected]
]
]
set seed-infection-times but-first seed-infection-times
]
; Do infections.
if sim-mechanism = sim-mechanism-uni-mix [do-infections-by-universal-mixing]
if sim-mechanism = sim-mechanism-network [do-infections-in-network]
; Do other state transitions in order.
do-maturation
tick
do-plots
update-output
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report sim-time
report ticks * time-step
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Network statistics
to calc-network-stats
calc-components
if not any? slinks [stop]
set mean-degree mean [count my-slinks] of people
set median-degree median [count my-slinks] of people
set min-degree min [count my-slinks] of people
set max-degree max [count my-slinks] of people
set var-degree variance [count my-slinks] of people
set degree-centralization (count people) * (max-degree - mean-degree) / (((count people) - 1) * ((count people) - 2))
set network-density 200 * (count slinks) / ((count people) * (-1 + count people))
if Calculate-Slow-Metrics? [
calc-cliquishness
;if num-components = 1 [calc-dos]
calc-dos
]
let assort-list [(list ([count my-slinks] of end1 ) ([count my-slinks] of end2 ))] of slinks
; let assort-list [(list (count my-slinks) (mean [count my-slinks] of slink-neighbors))] of people
let x-bar mean map [p -> first p] assort-list
let y-bar mean map [p -> last p] assort-list
let s_x sqrt mean map [p -> ((first p) - x-bar) ^ 2] assort-list
let s_y sqrt mean map [p -> ((last p) - y-bar) ^ 2] assort-list
ifelse (s_x * s_y) > 0 [
set assortativity (mean map [p -> ((first p) - x-bar) * ((last p) - y-bar) ] assort-list) / (s_x * s_y)
]
[
set assortativity 1.0
]
do-network-plots
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to calc-cliquishness
let num-triplets 0
let num-triangles 0
foreach sorted-people [ego ->
ask ego [
ifelse 2 < count my-slinks [
set p-cliquishness 200 * (sum [count (my-slinks with [end1 = myself]) with [[slink-neighbor? ego] of end2] ] of slink-neighbors) /
((count my-slinks) * (-1 + count my-slinks))
]
[
set p-cliquishness 0
]
; Count triplets and triangles
foreach sort (slink-neighbors with [who > [who] of myself]) [alter ->
foreach sort (slink-neighbors with [who > [who] of alter]) [tertius ->
set num-triplets num-triplets + 1
if [slink-neighbor? tertius] of alter [
set num-triangles num-triangles + 1
]
]
]
]
]
ifelse num-triplets = 0 [
set clustering-coefficient 0
]
[
set clustering-coefficient 100 * num-triangles / num-triplets
]
set mean-cliquishness mean [p-cliquishness] of people
set median-cliquishness median [p-cliquishness] of people
set min-cliquishness min [p-cliquishness] of people
set max-cliquishness max [p-cliquishness] of people
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to calc-components
let node-stack []
let ego nobody
set num-components 0
set max-component-size 0
let cur-size 0
foreach sorted-people [ ?1 ->
ask ?1 [set p-component 0]
]
foreach sorted-people [ ?1 ->
if [p-component = 0] of ?1 [
set cur-size 0
set num-components 1 + num-components
set node-stack fput ?1 node-stack
]
while [not empty? node-stack] [
set ego first node-stack
set node-stack but-first node-stack
if [p-component = 0] of ego [
ask ego [
set cur-size cur-size + 1
if cur-size > max-component-size [set max-component-size cur-size]
set p-component num-components
ask slink-neighbors [
if p-component = 0 [
set node-stack fput self node-stack
]
]
]
]
]
]