-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.nlogo
1462 lines (1320 loc) · 30.6 KB
/
simulation.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; A Networked Evolutionairy Trust Game for the Sharing Economy ;;;
;;; ;;;
;;; This simulation is part of the thesis for my Bachelor's degree in Economics and Business Economics ;;;
;;; at Utrecht University. The game is based on a paper by Chica, Chiong, Adam, Damas, & Teubner (2017). ;;;
;;; The structure of the simulation, and some of the specificities are from ABED-1pop (Izquierdo, ;;;
;;; Izquierdo, & Sandholm, 2018), which is avalable under the GNU General Public License version 3. The ;;;
;;; network statistics and layout procedures are from a code snippet send to me by Luis R. Izquierdo. ;;;
;;; I would not have been able to write the other methods and learn NetLogo without the book "Agent-Based ;;;
;;; Evolutionary Game Dynamics" by Izquierdo, Izquierdo, & Sandholm (in press). ;;;
;;; ;;;
;;; This program is not meant to be distributed, and will only be shared for the pupose of verifying my ;;;
;;; results. The code is specific to its application, and readability was often prioritised over ;;;
;;; flexibility. I do not imply any warranty of merchantability or fitness for a particular purpose. ;;;
;;; ;;;
;;; Contact information: ;;;
;;; Joost Gadellaa ;;;
;;; [email protected] ;;;
;;; [email protected] ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
extensions [nw]
globals [
;; network globals
n-of-links
avg-nbrs-within-radius
avg-clustering-coefficient
size-of-greatest-component
;; game globals
payoff-matrix
strategy-names
strategy-colours
max-payoff-difference
sum-of-payoffs
total-payoffs
walking-average-payoff
countdown
exit-code
]
breed [nodes node]
nodes-own [
strategy
payoff
community
ratio-intra-edges
]
links-own [
rewired?
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Scenarios and coloring for demonstration ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to say-cheese
ask patches [set pcolor white]
ask links [set color black]
ask nodes [set label ""]
ask nodes [set color 15 + 10 * (community mod 8 + 1)]
end
to demonstration-general-dynamic-portfolio
set avg-degree 8
set prob-rewiring 0.05
set Reward 30
setup
end
to demonstration-too-much-reinforcement
set avg-degree 18
set prob-rewiring 0.05
set Reward 30
setup
end
to demonstration-rewiring-low
set avg-degree 8
set prob-rewiring 0.8
set Reward 24
setup
end
to demonstration-rewiring-medium
set avg-degree 8
set prob-rewiring 0.8
set Reward 30
setup
end
to demonstration-rewiring-high
set avg-degree 8
set prob-rewiring 0.8
set Reward 36
setup
end
to demonstration-random
set avg-degree one-of [4 6 8 10 12 14 16 18 20]
set prob-rewiring one-of [0.01 0.02 0.04 0.05 0.08 0.1 0.2 0.4 0.5 0.8 1]
set Reward one-of [21 24 27 30 33 36 39]
setup
end
;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
build-initial-network
do-plots
compute-clustering-coefficient
compute-intra-edges
reset-simulation
relax-network
end
to reset-simulation
update-reward
set payoff-matrix read-from-string payoffs
let min-payoff -20 * revision-frequency
let max-payoff 40 * revision-frequency
set max-payoff-difference (max-payoff + 999999) - (min-payoff + 999999)
set strategy-names ["TP" "UP" "TC" "UC"]
set strategy-colours [15 95 55 115]
ask nodes [mutate]
set sum-of-payoffs 0
set total-payoffs 0
set walking-average-payoff 0
set exit-code ""
set countdown 16
reset-ticks
end
to build-initial-network
set-default-shape nodes "circle"
build-network
set n-of-links count links
end
to build-network
if network-model = "small-world" [
nw:generate-watts-strogatz nodes links n-of-nodes (avg-degree / 2) prob-rewiring
]
if network-model = "community" [
generate-community
]
end
to generate-community
;; create nodes with a community and space them out
create-nodes n-of-nodes [
set community (who mod n-of-communities) + 1
set heading community * 360 / n-of-communities
fd max-pxcor - 1
]
;; create links within the communities for each turtle
let i 0
while [i < n-of-nodes] [
let this-community (i mod n-of-communities) + 1
let goal avg-degree / 2
let j 0
while [j < goal] [
let new-neighbor one-of nodes with [community = this-community and not (who = i) and not in-link-neighbor? node i]
if new-neighbor != nobody [
make-edge node i new-neighbor
]
set j j + 1
]
set i i + 1
]
;; always connect networks?
if always-connect-communities? [
connect-communities
]
;; randomly rewire
ask nodes [
let start-degree count my-links
ask my-links [
if random-float 1 < prob-rewiring [die]
]
while [count my-links < start-degree] [
make-new-friend
]
]
end
to make-new-friend
let me who
let new-neighbor one-of nodes with [not (who = me) and not in-link-neighbor? node me]
if new-neighbor != nobody [
create-link-with new-neighbor
]
end
to make-edge [node1 node2]
ask node1 [create-link-with node2]
end
to connect-communities
let i 1
while [i <= n-of-communities] [
let node1 one-of nodes with [community = i]
let node2 one-of nodes with [community = (i mod n-of-communities) + 1]
ask node1 [ask one-of my-links [die]]
make-edge node1 node2
set i i + 1
]
end
to update-reward
set payoffs (word "[[ 0 0 " reward " -20 ]\n [ 0 0 10 10 ]\n [ " reward " -10 0 0 ]\n [ 40 -10 0 0 ]]")
set payoff-matrix read-from-string payoffs
end
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Running Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
if (count nodes with [strategy = 1] = 0) or (count nodes with [strategy = 3] = 0) [
set exit-code "no trust"
set countdown countdown - 1
if (countdown <= 0 and stop-when-done?) [stop]
]
if (count nodes with [strategy = 2] = 0) and (count nodes with [strategy = 4] = 0) [
set exit-code "only trust"
set countdown countdown - 1
if (countdown <= 0 and stop-when-done?) [stop]
]
let i 0
ask nodes [set payoff 0]
while [i < revision-frequency][
ask nodes [play-game]
set i i + 1
]
ask nodes [revise-strategy]
set sum-of-payoffs sum [payoff] of nodes
set total-payoffs total-payoffs + sum-of-payoffs
set walking-average-payoff floor ((15 * walking-average-payoff + sum-of-payoffs) / 16)
tick
end
to play-game
let mate one-of link-neighbors
if mate != nobody [set payoff payoff + item ([(strategy - 1)] of mate) (item (strategy - 1) payoff-matrix)]
end
to revise-strategy
let observed-node one-of link-neighbors
if observed-node != nobody [
let alter-payoff [payoff] of observed-node
if alter-payoff > payoff [
let probability (alter-payoff - payoff) / max-payoff-difference
if random-float 1.0 < probability [
set strategy ([strategy] of observed-node)
update-color-and-label
set label (item (strategy - 1) strategy-names)
]
]
]
if random-float 1.0 < noise [mutate]
end
to mutate
set strategy (random 4 + 1)
update-color-and-label
set payoff 0
end
to update-color-and-label
set label (item (strategy - 1) strategy-names)
set color (item (strategy - 1) strategy-colours)
end
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;
;;; Network Statistics ;;; and ;;; Layout ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;
to compute-clustering-coefficient
set avg-clustering-coefficient mean [ nw:clustering-coefficient ] of turtles
end
to compute-intra-edges
ask nodes [
let my-community community
if count link-neighbors != 0 [
set ratio-intra-edges (count link-neighbors with [community = my-community] / count link-neighbors)
]
]
end
to plot-accessibility
let steps link-radius
if link-radius = "Infinity" [set steps n-of-nodes]
let n-of-nbrs-of-each-player [count nw:turtles-in-radius steps - 1] of nodes
let max-n-of-nbrs-of-each-player max n-of-nbrs-of-each-player
set-current-plot "Neighbors within link-radius"
set-plot-x-range 0 (max-n-of-nbrs-of-each-player + 1) ;; + 1 to make room for the width of the last bar
histogram n-of-nbrs-of-each-player
set avg-nbrs-within-radius mean n-of-nbrs-of-each-player
end
to compute-size-of-greatest-component
set size-of-greatest-component max map count nw:weak-component-clusters
end
to relax-network
;; the number 3 here is arbitrary; more repetitions slows down the
;; model, but too few gives poor layouts
repeat 3 [
;; the more nodes we have to fit into the same amount of space,
;; the smaller the inputs to layout-spring we'll need to use
let factor sqrt count nodes
;; numbers here are arbitrarily chosen for pleasing appearance
layout-spring (nodes with [any? link-neighbors]) links (1 / factor) (7 / factor) (3 / factor)
display ;; for smooth animation
]
;; don't bump the links of the world
let x-offset max [xcor] of nodes + min [xcor] of nodes
let y-offset max [ycor] of nodes + min [ycor] of nodes
;; big jumps look funny, so only adjust a little each time
set x-offset limit-magnitude x-offset 0.1
set y-offset limit-magnitude y-offset 0.1
ask nodes [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]
end
to-report limit-magnitude [number limit]
if number > limit [ report limit ]
if number < (- limit) [ report (- limit) ]
report number
end
to do-plots
plot-accessibility
compute-size-of-greatest-component
end
to drag-and-drop
if mouse-down? [
let candidate min-one-of nodes [distancexy mouse-xcor mouse-ycor]
if [distancexy mouse-xcor mouse-ycor] of candidate < 1 [
;; The WATCH primitive puts a "halo" around the watched turtle.
watch candidate
while [mouse-down?] [
;; If we don't force the view to update, the user won't
;; be able to see the turtle moving around.
display
;; The SUBJECT primitive reports the turtle being watched.
ask subject [ setxy mouse-xcor mouse-ycor ]
]
;; Undoes the effects of WATCH. Can be abbreviated RP.
reset-perspective
]
]
end
@#$#@#$#@
GRAPHICS-WINDOW
189
8
872
692
-1
-1
13.78
1
10
1
1
1
0
0
0
1
-24
24
-24
24
1
1
1
ticks
30.0
SLIDER
6
78
181
111
n-of-nodes
n-of-nodes
2
1024
512.0
1
1
NIL
HORIZONTAL
BUTTON
7
418
180
451
NIL
relax-network
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
463
699
572
744
link-radius
link-radius
1 2 3 4 5 10 20 "Infinity"
0
PLOT
4
549
182
794
Neighbors within link-radius
# of reachable neighbors
# of players
0.0
10.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 1 -16777216 true "" ""
MONITOR
189
699
345
744
Avg. nbrs within radius
avg-nbrs-within-radius
2
1
11
MONITOR
351
699
456
744
Clustering coeff.
avg-clustering-coefficient
2
1
11
MONITOR
190
749
346
794
Size of largest component
size-of-greatest-component
0
1
11
CHOOSER
6
27
181
72
network-model
network-model
"small-world" "community"
1
MONITOR
352
749
455
794
NIL
n-of-links
0
1
11
BUTTON
7
377
180
410
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
6
118
181
151
avg-degree
avg-degree
4
20
8.0
2
1
NIL
HORIZONTAL
SLIDER
6
157
182
190
prob-rewiring
prob-rewiring
0
1
0.01
0.01
1
NIL
HORIZONTAL
BUTTON
7
460
181
494
drag-and-drop
drag-and-drop
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
462
750
571
794
Update
plot-accessibility
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
879
459
934
492
NIL
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
879
276
1054
309
revision-frequency
revision-frequency
0
500
50.0
1
1
NIL
HORIZONTAL
INPUTBOX
878
27
1050
132
payoffs
[[ 0 0 36 -20 ]\n [ 0 0 10 10 ]\n [ 36 -10 0 0 ]\n [ 40 -10 0 0 ]]
1
1
String (reporter)
BUTTON
945
459
1050
492
NIL
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
PLOT
1060
352
1639
689
strategy distribution
NIL
NIL
0.0
1250.0
0.0
512.0
false
true
"" ""
PENS
"TP" 1.0 1 -2674135 true "" "plot n-of-nodes"
"UP" 1.0 1 -13791810 true "" "plot (count nodes with [strategy = 2] + count nodes with [strategy = 3] + count nodes with [strategy = 4])"
"TC" 1.0 1 -10899396 true "" "plot (count nodes with [strategy = 3] + count nodes with [strategy = 4])"
"UC" 1.0 1 -8630108 true "" "plot count nodes with [strategy = 4]"
SLIDER
879
317
1054
350
noise
noise
0
0.1
0.0
0.0001
1
NIL
HORIZONTAL
TEXTBOX
8
10
158
28
Network Settings:
11
0.0
1
TEXTBOX
8
226
158
244
Community Settings
11
0.0
1
SLIDER
7
243
179
276
n-of-communities
n-of-communities
0
(n-of-nodes / 16)
16.0
1
1
NIL
HORIZONTAL
SWITCH
7
285
181
318
always-connect-communities?
always-connect-communities?
0
1
-1000
BUTTON
879
416
1051
449
NIL
reset-simulation
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
878
141
1050
174
Reward
Reward
21
39
36.0
1
1
NIL
HORIZONTAL
BUTTON
878
184
1049
217
NIL
update-reward\n
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
MONITOR
879
503
1050
584
NIL
exit-code
17
1
20
TEXTBOX
8
356
145
374
Network Actions\n
11
0.0
1
TEXTBOX
880
258
1030
276
Evolution Settings
11
0.0
1
TEXTBOX
878
10
1028
28
Payoff Settings\n
11
0.0
1
TEXTBOX
882
395
1032
413
Simulation Actions
11
0.0
1
MONITOR
880
644
1050
689
NIL
walking-average-payoff
17
1
11
MONITOR
1000
592
1050
637
NIL
countdown
17
1
11
PLOT
1060
13
1639
174
strategy counts
NIL
NIL
0.0
10.0
0.0
10.0
true
true
"" ""
PENS
"TP" 1.0 0 -2674135 true "" "plot count turtles with [strategy = 1]"
"UP" 1.0 0 -13791810 true "" "plot count turtles with [strategy = 2]"
"TC" 1.0 0 -10899396 true "" "plot count turtles with [strategy = 3]"
"UC" 1.0 0 -8630108 true "" "plot count turtles with [strategy = 4]"
SWITCH
881
598
994
631
stop-when-done?
stop-when-done?
1
1
-1000
PLOT
1362
188
1639
338
total cumulative payoffs
NIL
NIL
0.0
10.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot total-payoffs"
PLOT
1061
187
1351
337
total payoffs this round
NIL
NIL
0.0
10.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot sum-of-payoffs"
BUTTON
698
727
876
760
NIL
demonstration-random
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
@#$#@#$#@
## WHAT IS IT?
(a general understanding of what the model is trying to show or explain)
## HOW IT WORKS
(what rules the agents use to create the overall behavior of the model)
## HOW TO USE IT
(how to use the model, including a description of each of the items in the Interface tab)
## THINGS TO NOTICE
(suggested things for the user to notice while running the model)
## THINGS TO TRY
(suggested things for the user to try to do (move sliders, switches, etc.) with the model)
## EXTENDING THE MODEL
(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)
## NETLOGO FEATURES
(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)
## RELATED MODELS
(models in the NetLogo Models Library and elsewhere which are of related interest)
## CREDITS AND REFERENCES
(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250
airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135