-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathComplex Networks Model 6.nlogo
2240 lines (1921 loc) · 50.8 KB
/
Complex Networks Model 6.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
;-----------------------------------------------------------------------------------
;
; The usual norm is:
; Don't change anything in this file. It's the heart of the system.
; But if you change something, be careful...
;
; Write your scripts in the scripts.nls file (open it from "Included Files" chooser)
;
; Please, read the Info Tab for instructions...
;
;-----------------------------------------------------------------------------------
extensions [ nw rnd]
__includes [ "scripts.nls" ]
breed [nodes node]
nodes-own [
degree
betweenness
eigenvector
closeness
clustering
page-rank
community
phi
visits
rank
new-rank
infected
typ
]
globals [
diameter
components
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main procedures
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to startup
clear
ask patch 8 0 [
set plabel-color black
set plabel "PLEASE, READ INFO TAB FOR INSTRUCTIONS"]
wait 2
clear
end
to clear
clear-turtles
clear-links
clear-patches
clear-all-plots
set-default-shape nodes "circle"
ask patches [set pcolor white]
end
; Auxiliary reports to split a string using a substring
to-report split-aux [s s1]
ifelse member? s1 s
[ let p position s1 s
report (list (substring s 0 p) (substring s (p + (length s1)) (length s)))
]
[ report (list s "")
]
end
to-report split [s s1]
ifelse member? s1 s
[
let sp split-aux s s1
report (fput (first sp) (split (last sp) s1))
]
[ report (list s) ]
end
to-report join [s c]
report reduce [[s1 s2] -> (word s1 c s2)] s
end
to-report replace [s c1 c2]
report join (split s c1) c2
end
to-report store [val l]
report lput val l
end
to inspect-node
if mouse-down? [
ask nodes [stop-inspecting self]
let selected min-one-of nodes [distancexy mouse-xcor mouse-ycor]
if selected != nobody [
ask selected [
if distancexy mouse-xcor mouse-ycor < 1 [inspect self]
]
]
wait .2
]
end
to remove-node [prob]
let selected ifelse-value (prob = "uniform")[one-of nodes] [rnd:weighted-one-of nodes [run-result prob]]
ask selected [die]
end
to plotTable [Lx Ly]
set-current-plot "General"
clear-plot
set-plot-x-range (precision (min Lx) 2) (precision (max Lx) 2)
set-plot-y-range (precision (min Ly) 2) (precision (max Ly) 2)
(foreach Lx Ly
[ [x y] ->
plotxy x y
])
end
to print-csv [val]
ifelse is-list? val
[ print reduce [[x y] -> (word x ", " y)] val]
[ print val]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Generators / Utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to ER-RN [N p]
create-nodes N [
setxy random-xcor random-ycor
set color red
]
ask nodes [
ask other nodes with [who < [who] of myself] [
if random-float 1 < p [
create-link-with myself
]
]
]
post-process
end
to WS [N k p]
create-nodes N [
set color red
]
layout-circle sort nodes max-pycor * 0.9
let lis (n-values (K / 2) [ [i] -> i + 1 ])
ask nodes [
let w who
foreach lis [ [i] -> create-link-with (node ((w + i) mod N)) ]
]
rewire p
post-process
end
to rewire [p]
ask links [
let rewired? false
if (random-float 1) < p
[
;; "a" remains the same
let node1 end1
;; if "a" is not connected to everybody
if [ count link-neighbors ] of node1 < (count nodes - 1)
[
;; find a node distinct from node1 and not already a neighbor of node1
let node2 one-of nodes with [ (self != node1) and (not link-neighbor? node1) ]
;; wire the new edge
ask node1 [ create-link-with node2 [ set rewired? true ] ]
]
]
;; remove the old edge
if (rewired?)
[
die
]
]
end
to BA-PA [N m0 m]
create-nodes m0 [
set color red
]
ask nodes [
create-links-with other nodes
]
repeat (N - m0) [
create-nodes 1 [
set color blue
let new-partners turtle-set map [find-partner] (n-values m [ [i] -> i ])
create-links-with new-partners
]
]
post-process
end
to-report find-partner
report [one-of both-ends] of one-of links
end
to KE [N m0 mu]
create-nodes m0 [
set color red
]
ask nodes [
create-links-with other nodes
]
let active nodes with [self = self]
let no-active no-turtles
repeat (N - m0) [
create-nodes 1 [
set color blue
foreach shuffle (sort active) [ [ac] ->
ifelse (random-float 1 < mu or count no-active = 0)
[
create-link-with ac
]
[
let cut? false
while [not cut?] [
let nodej one-of no-active
let kj [count my-links] of nodej
let S sum [count my-links] of no-active
if (kj / S) > random-float 1 [
create-link-with nodej
set cut? true
]
]
]
]
set active (turtle-set active self)
let cut? false
while [not cut?] [
let nodej one-of active
let kj [count my-links] of nodej
let S sum [1 / (count my-links)] of active
let P (1 / (kj * S))
if P > random-float 1 [
set no-active (turtle-set no-active nodej)
set active active with [self != nodej]
set cut? true
]
]
]
]
post-process
end
to Geom [N r]
create-nodes N [
setxy random-xcor random-ycor
set color blue
]
ask nodes [
create-links-with other nodes in-radius r
]
post-process
end
to SCM [N g]
create-nodes N [
setxy random-xcor random-ycor
set color blue
]
let num-links (g * N) / 2
while [count links < num-links ]
[
ask one-of nodes
[
let choice (min-one-of (other nodes with [not link-neighbor? myself])
[distance myself])
if choice != nobody [ create-link-with choice ]
]
]
post-process
end
to Grid [N M torus?]
nw:generate-lattice-2d nodes links N M torus?
ask nodes [set color blue]
post-process
end
to BiP [nb-nodes nb-links]
create-nodes nb-nodes [
set typ one-of [0 1]
]
let P0 nodes with [typ = 0]
let P1 nodes with [typ = 1]
repeat nb-links [
ask one-of P0 [
create-link-with one-of P1
]
]
post-process
end
to Edge-Copying [Iter pncd k beta pecd]
repeat Iter [
; Creation / Deletion of nodes
ifelse random-float 1 > pncd
[
ask one-of nodes [die]
]
[
create-nodes 1 [
setxy random-xcor random-ycor
set color blue
]
]
; Edge Creation
let v one-of nodes
ifelse random-float 1 < beta
[
;creation
ask v [
let other-k-nodes (other nodes) with [not link-neighbor? v]
if count other-k-nodes >= k
[
set other-k-nodes n-of k other-k-nodes
]
create-links-with other-k-nodes
]
]
[
; copy
let n k
while [n > 0] [
let u one-of other nodes
let other-nodes (([link-neighbors] of u) with [self != v])
if count other-nodes > k [
set other-nodes n-of k other-nodes
]
ask v [
create-links-with other-nodes
]
set n n - (count other-nodes)
]
]
; Creation / Deletion of edges
ifelse random-float 1 < pecd [
ask one-of nodes with [count my-links < (count nodes - 1)][
let othernode one-of other nodes with [not link-neighbor? myself]
create-link-with othernode
]
]
[
ask one-of links [die]
]
]
post-process
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Centrality Measures
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Takes a centrality measure as a reporter task, runs it for all nodes
;; and set labels, sizes and colors of turtles to illustrate result
to compute-centralities
nw:set-context nodes links
ask nodes [
set degree (count my-links)
set betweenness nw:betweenness-centrality
set eigenvector nw:eigenvector-centrality
set closeness nw:closeness-centrality
set clustering nw:clustering-coefficient
set page-rank nw:page-rank
]
update-plots
end
to do-plot [Dk where]
let M max Dk
set-current-plot (word where " Distribution")
set-plot-x-range 0 (ceiling M)
set-plot-y-range 0 1
set-histogram-num-bars 100
histogram Dk
end
to plots
clear-all-plots
compute-centralities
carefully [do-plot ([page-rank] of nodes) "PageRank"][]
carefully [do-plot ([degree] of nodes) "Degree"][]
carefully [do-plot ([nw:betweenness-centrality] of nodes) "Betweenness"][]
carefully [do-plot ([nw:eigenvector-centrality] of nodes) "Eigenvector"][]
carefully [do-plot ([nw:closeness-centrality] of nodes) "Closeness"][]
carefully [do-plot ([nw:clustering-coefficient] of nodes) "Clustering"][]
carefully [set diameter compute-diameter 1000][]
end
;; We want the size of the turtles to reflect their centrality, but different measures
;; give different ranges of size, so we normalize the sizes according to the formula
;; below. We then use the normalized sizes to pick an appropriate color.
to normalize-sizes-and-colors [c]
if count nodes > 0 [
let sizes sort [ size ] of nodes ;; initial sizes in increasing order
let delta last sizes - first sizes ;; difference between biggest and smallest
ifelse delta = 0 [ ;; if they are all the same size
ask nodes [ set size 1 ]
]
[ ;; remap the size to a range between 0.5 and 2.5
ask nodes [ set size ((size - first sizes) / delta) * 1.5 + 0.4 ]
]
ask nodes [ set color lput 200 extract-rgb scale-color c size 3.8 0] ; using a higher range max not to get too white...
]
end
; The diameter is cpmputed from a random search on distances between nodes
to-report compute-diameter [n]
let s 0
repeat n [
ask one-of nodes [
set s max (list s (nw:distance-to one-of other nodes))
]
]
report s
end
to compute-components
set components nw:weak-component-clusters
end
;to compute-phi
; ask nodes [
; set phi sum [exp -1 * ((nw:distance-to myself) ^ 2 / 100)] of nodes
; ]
;end
to-report Average-Path-Length
report nw:mean-path-length
end
to-report Average-Clustering
report mean [clustering] of nodes
end
to-report Average-Betweenness
report mean [betweenness] of nodes
end
to-report Average-Closeness
report mean [closeness] of nodes
end
to-report Average-PageRank
report mean [page-rank] of nodes
end
to-report Average-Eigenvector
report mean [eigenvector] of nodes
end
to-report Average-Degree
report mean [count my-links] of nodes
end
to-report Number-Nodes
report count nodes
end
to-report Number-Links
report count Links
end
to-report Density
report 2 * (count links) / ( (count nodes) * (-1 + count nodes))
end
to-report All-Measures
report (list Number-Nodes
Number-Links
Density
Diameter
length Components
Average-Degree
Average-Path-Length
Average-Clustering
Average-Betweenness
Average-Eigenvector
Average-Closeness
Average-PageRank
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Layouts & Visuals
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to layout [tl]
if tl = "radial" and count nodes > 1 [
layout-radial nodes links ( max-one-of nodes [ count my-links ] )
]
if tl = "spring" [
repeat 1000 [spring]
]
if tl = "circle" [
layout-circle sort nodes max-pycor * 0.9
]
if tl = "bipartite" [
layout-bipartite
]
if tl = "tutte" [
layout-circle sort nodes max-pycor * 0.9
repeat 10 [
layout-tutte max-n-of (count nodes * 0.5) nodes [ count my-links ] links 12
]
]
end
to layout-bipartite
let P0 nodes with [typ = 0]
let incp0 world-width / (1 + count p0)
let P1 nodes with [typ = 1]
let incp1 world-width / (1 + count p1)
let x min-pxcor
ask P0 [
set color red
setxy x max-pycor - 1
set x x + incp0]
set x min-pxcor
ask P1 [
set color blue
setxy x min-pycor + 1
set x x + incp1]
end
to refresh
ask nodes [
set size Size-N
set label ""
; set color red
]
ask links [
set color [150 150 150 100]
]
end
to post-process
ask links [
;set color black
set color [100 100 100 100]
]
set diameter compute-diameter 1000
compute-components
end
to spring
layout-spring turtles links spring-K length-K rep-K
ask nodes [
setxy (xcor * (1 - gravity / 1000)) (ycor * (1 - gravity / 1000))
]
end
to help
user-message (word " HELP (Details in Info Tab)" "\n"
"----------------------------------------------------------" "\n"
"Generators:" "\n"
"* ER-RN (N, p) * WS (N, k, p)" "\n"
"* BA-PA (N, m0, m) * KE (N, m0, mu)" "\n"
"* Geom (N, r) * SCM (N, g)" "\n"
"* Grid (N,M,t?) * BiP (N, M)" "\n"
"* Edge-Copying (N, pn, k, b, pe)" "\n"
"----------------------------------------------------------" "\n"
"Utilities:" "\n"
"* Compute-centralities * Communities" "\n"
"* PRank (Iter) * Rewire (p)" "\n"
"* ContCA (Iter, pIn, p) * Layout (type)" "\n"
"* Print (measure) * Print-csv (data)" "\n"
"* DiscCA (Iter, pIn, p0_ac, p1_ac)" "\n"
"* Spread (Ni, ps, pr, pin, Iter)" "\n"
"----------------------------------------------------------" "\n"
"Global Measures:" "\n"
" Number-Nodes, Number-Links, Density, Average-Degree," "\n"
" Average-Path-Length, Diameter, Average-Clustering," "\n"
" Average-Betweenness, Average-Eigenvector," "\n"
" Average-Closeness, Average-PageRank, Components" "\n"
"----------------------------------------------------------" "\n"
"Layouts: circle, radial, tutte, spring, bipartite" "\n"
"----------------------------------------------------------" "\n"
"* Save, Load" "\n"
"* Export (view)" "\n"
" Views: Degree, Clustering, Betweenness, Eigenvector, " "\n"
" Closeness, PageRank" "\n"
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Saving and loading of network files
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to save
nw:set-context nodes links
carefully [
nw:save-graphml user-new-file
][]
end
to load
nw:set-context nodes links
nw:load-graphml user-file
end
to export [view]
let file (word view "-" (replace date-and-time ":" "_") ".csv")
set view (word view " Distribution")
export-plot view file
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Page Rank
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to PRank [n]
let damping-factor 0.85
;ask links [ set color gray set thickness 0 ]
ask nodes [
set rank 1 / count nodes
set new-rank 0 ]
repeat N [
ask nodes
[
ifelse any? link-neighbors
[
let rank-increment rank / count link-neighbors
ask link-neighbors [
set new-rank new-rank + rank-increment
]
]
[
let rank-increment rank / count nodes
ask nodes [
set new-rank new-rank + rank-increment
]
]
]
ask nodes
[
;; set current rank to the new-rank and take the damping-factor into account
set rank (1 - damping-factor) / count nodes + damping-factor * new-rank
]
]
let total-rank sum [rank] of nodes
let max-rank max [rank] of nodes
ask nodes [
set size 0.2 + 2 * (rank / max-rank)
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Spread
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to spread [N-mi ps pr pin Iter]
let t 0
set-current-plot "General"
clear-plot
foreach [["Green" green] ["Red" red] ["Blue" blue]] [ [c] ->
create-temporary-plot-pen first c
set-current-plot-pen first c
set-plot-pen-color last c
set-plot-pen-mode 0
]
ask nodes [
set infected 0
set color green
]
ask n-of N-mi nodes [
set infected 1
set color red
]
repeat Iter [
ask nodes with [infected = 1]
[ ask link-neighbors with [infected = 0]
[ if random-float 1 < ps
[ set infected 1
set color red
] ] ]
ask nodes with [infected = 1]
[ if random-float 1 < pr
[ set color green
set infected 0
if random-float 1 < pin
[ set color blue
set infected 2
]
] ]
set t t + 1
set-current-plot-pen "Green"
plotxy t count nodes with [infected = 0]
set-current-plot-pen "Red"
plotxy t count nodes with [infected = 1]
set-current-plot-pen "Blue"
plotxy t count nodes with [infected = 2]
display
wait 2 / Iter
]
end
to-report spread-summary
let s count nodes with [infected = 0]
let i count nodes with [infected = 1]
let r count nodes with [infected = 2]
report (list s i r)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Cellular Automata
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Uses Typ = [current-state new-state] to store the state of the node
; Discrete value states
; Iter - Number of iterations
; pI - Initial Probability of activation
; p0_ac - ratio of activated neighbors to activate if the node is 0
; p1_ac - ratio of activated neighbors to activate if the node is 1
to DiscCA [Iter pIn p0_ac p1_ac]
set-current-plot "General"
clear-plot
set-plot-y-range 0 1
let t 0
ask nodes [
ifelse random-float 1 < pIn
[ set typ [1]]
[ set typ [0]]
set color ifelse-value (current_state = 0) [red][blue]
]
repeat Iter [
no-display
ask nodes [
let s current_state
let pn 0
if any? link-neighbors [
set pn count (link-neighbors with [current_state = 1]) / count link-neighbors
]
ifelse s = 0
[
ifelse pn >= p0_ac
[ new-state 1 ]
[ new-state 0 ]
]
[
ifelse pn >= p1_ac
[ new-state 1 ]
[ new-state 0 ]
]
]
ask nodes [
set-state
set color ifelse-value (current_state = 0) [red][blue]
]
plotxy t count (nodes with [current_state = 1]) / count nodes
set t t + 1
display
;wait .01
]
end
; Continuous value states
; Iter - Number of iterations
; pI - Initial Probability of activation
; p - ratio of memory in the new state
to ContCA [Iter pIn p]
set-current-plot "General"
clear-plot
set-plot-y-range 0 1
let t 0
ask nodes [
set typ (list random-float pIn)
set color scale-color blue current_state 0 1
]
repeat Iter [
no-display
ask nodes [
let s current_state
let pn sum ([current_state] of link-neighbors) / count link-neighbors
new-state (p * current_state + (1 - p) * pn)
]
ask nodes [
set-state
set color scale-color blue current_state 0 1
]
plotxy t sum ([current_state] of nodes) / count nodes
set t t + 1
display
;wait .01
]
end
; Get the current state of the node
to-report current_state
report first typ
end
; Set the new state of the node to s
to new-state [s]
set typ (lput s typ)
end
; Move the new state to the current state
to set-state
set typ (list (last typ))
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Communities Detection
to communities
carefully [
let com nw:louvain-communities
let n-com length com
(foreach com (range 1 (n-com + 1) 1)[
[comm c] ->
ask comm [
set community c
set color (item (c mod 13) base-colors)
]
])
ask patches [set pcolor 3 + [color] of min-one-of nodes [distance myself]]
]
[]
end
@#$#@#$#@
GRAPHICS-WINDOW
10
10
606
385
-1
-1
11.1
1
12
1
1
1
0
0
0
1
-26
26
-16
16
0
0
0
ticks
30.0
PLOT
610
10
810
130
Degree Distribution
Degree
Nb Nodes
0.0
100.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 1 -7500403 true "" "histogram [count my-links] of nodes"
CHOOSER
10
390
120
435
Select-Layout
Select-Layout
"circle" "radial" "tutte" "bipartite" "spring"
4
SLIDER
125
390
217
423
spring-K
spring-K
0
1
0.53
.01
1
NIL
HORIZONTAL
SLIDER
220
390
312
423
length-K
length-K
0
5
1.0
.01
1
NIL
HORIZONTAL
SLIDER
315
390
407
423
rep-K
rep-K
0
2
0.06
.001
1
NIL
HORIZONTAL
SLIDER
125
425
217
458
size-N
size-N
0
2
0.7
.1
1
NIL
HORIZONTAL
BUTTON
10
435
65
468
Layout
layout Select-Layout
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
65
435
120
468
Spring
spring
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
220
425
275
458
NIL
refresh
NIL
1
T
OBSERVER