-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoleOfInstitutions.nlogo
3094 lines (2924 loc) · 73 KB
/
RoleOfInstitutions.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
extensions [array matrix r]
__includes["geo_functions.nls" "social_functions.nls" "r_functions.nls" "resource_functions.nls"]
globals [
all-links
attrib-max
char-bits
average-path-length
transitivity
num-generations
total-generations
]
patches-own [
blevel
bmax
]
turtles-own [
bbits
vbits
boldness
vengefulness
payoff
defector
punisher
meta-defector
meta-punisher
punished-defections
punished-meta-defections
was-punished-defecting
was-punished-meta-defecting
]
undirected-link-breed [geo-links geo-link]
undirected-link-breed [social-links social-link]
social-links-own [
rewired?
]
to setup
clear-all
set char-bits 3
set attrib-max 7
r-load
r-init-vars
;random-seed -339421936
resize-world 0 torus-size - 1 0 torus-size - 1
set-patch-size floor (400 / torus-size)
;set attrib-max (2 ^ char-bits - 1)
make-patches
make-turtles
set all-links (link-set geo-links social-links)
reset-ticks
set num-generations 0
set total-generations 10 ^ generations-log10
end
to go
if (num-generations < total-generations) [
run-generation
; Te
imitation-evolving
; Ta
if (num-generations mod social-strategy-ratio = 0) [
; social evolving degenerates into eliminating all links
;social-evolving
if (endogenous-institution) [
r:put "endog.b" median [boldness] of turtles
r:put "endog.v" median [vengefulness] of turtles
r:eval "inst.attr <- list(boldness = endog.b, vengefulness = endog.v)"
]
]
set num-generations num-generations + 1
r-init-vars
tick
]
end
to run-generation
ask turtles [
set payoff 0
]
let num-ticks 0
while [num-ticks < ticks-per-generation] [
reset-actions
define-roles
make-actions
r-put-coord
make-observations
calculate-payoffs
if (walk-away-rule) [
walk-away
]
regrow-patches
set num-ticks num-ticks + 1
]
end
to walk-away
foreach ([who] of turtles with [not defector]) [
walk-to neighborhood-type neighborhood-radius (turtle ?) (turtles with [defector]) 0.05
]
end
to randomize-attribs
ask turtles [
set bbits array:from-list n-values char-bits [random 2]
set vbits array:from-list n-values char-bits [random 2]
set boldness calc-attrib bbits
set vengefulness calc-attrib vbits
]
end
to make-turtles
create-ordered-turtles torus-size * torus-size * population-density / 100
r:put "pop.size" count turtles
ask turtles [
set shape "person"
set size 0.25
set color yellow
set defector false
set punisher false
set meta-defector false
set meta-punisher false
ifelse (population-density = 100) [
move-to patch who floor (who / torus-size)
][
move-to one-of patches with [not any? turtles-here]
]
]
ifelse (random-attribs) [
randomize-attribs
] [
ask turtles [
set boldness initial-boldness
set vengefulness initial-vengefulness
]
]
r-init-attrs
r-put-coord
r-make-ba-social ba-power
links-from-matrix r:get "soc.mat"
end
to-report code-attrib [bits]
let n 0
let attrib 0
let len array:length bits
let code ""
while [n < len] [
set code word code array:item bits n
set n n + 1
]
report code
end
to-report calc-attrib [bits]
let n 0
let attrib 0
let len array:length bits
while [n < len] [
set attrib attrib + array:item bits n * (2 ^ n)
set n n + 1
]
report attrib
end
to reset-actions
ask turtles [
set punished-defections 0
set punished-meta-defections 0
set was-punished-defecting 0
set was-punished-meta-defecting 0
]
end
to define-roles
ask turtles [
set defector false
set punisher false
set meta-defector false
set meta-punisher false
]
r-define-roles
r-get-roles
ask turtles with [defector] [ set color red ]
ask turtles with [not defector and not punisher] [ set color blue ]
ask turtles with [punisher] [ set color yellow ]
end
to-report define-observers [observed]
let observers []
let patch-neighbors patch-here
let geo-neighbors []
ifelse (neighborhood-type = "Complete Graph") [
ask observed [
set geo-neighbors other turtles
]
] [
repeat neighborhood-radius [
ask patch-neighbors [
ifelse (neighborhood-type = "Moore") [
set patch-neighbors (patch-set patch-neighbors neighbors)
] [
set patch-neighbors (patch-set patch-neighbors neighbors4)
]
]
]
set geo-neighbors turtles-on patch-neighbors
]
if (defection-observing = "Geo only") [
ask observed [
set observers geo-neighbors
]
]
if (defection-observing = "Social only") [
ask observed [
set observers social-link-neighbors
]
]
if (defection-observing = "Geo and Social") [
ask observed [
set observers (turtle-set geo-neighbors social-link-neighbors)
]
]
report observers
end
to make-actions
cooperate-harvest
defect-harvest
end
to make-observations
r-make-obs
r-get-actions
end
to calculate-payoffs
if (not metanorms) [
ask turtles [
set punished-meta-defections 0
set was-punished-meta-defecting 0
]
]
ask turtles [
set payoff payoff - cost-of-effort - (punished-defections + punished-meta-defections) * enforcement-cost - (was-punished-defecting + was-punished-meta-defecting) * punishment-cost
]
r-put-payoff
end
to imitation-evolving
r-imitation
r-get-attribs
end
to social-evolving
r-social
links-from-matrix r:get "soc.new"
r:eval "soc.mat[soc.mat == -1] <- 0"
end
to cooperate-harvest
ask turtles with [not defector] [
ifelse (blevel > low-effort) [
set payoff payoff + low-effort
set blevel max (list (blevel - low-effort) 0)
] [
let empty-patches neighbors with [not any? turtles-here]
let destination empty-patches with [blevel > low-effort]
ifelse (any? destination) [
move-to one-of destination
] [ if (any? empty-patches) [
move-to one-of empty-patches
]
]
]
]
end
to defect-harvest
ask turtles with [defector] [
ifelse (blevel > high-effort) [
set payoff payoff + high-effort
set blevel max (list (blevel - high-effort) 0)
] [
set payoff payoff + blevel
let empty-patches neighbors with [not any? turtles-here]
let destination empty-patches with [blevel > high-effort]
ifelse (any? destination) [
move-to one-of destination
] [ if (any? empty-patches) [
move-to one-of empty-patches
]
]
]
]
end
to-report average-boldness
report mean [boldness] of turtles
end
to-report average-vengefulness
report mean [vengefulness] of turtles
end
to-report average-payoff
report mean [payoff] of turtles
end
to-report defector-agents
report turtles with [defector]
end
to-report cooperator-agents
report turtles with [not defector and not punisher]
end
to-report punisher-agents
report turtles with [punisher]
end
to-report count-defectors
report count defector-agents
end
to-report count-cooperators
report count cooperator-agents
end
to-report count-punishers
report count punisher-agents
end
to-report mean-degree
report mean [count social-link-neighbors] of turtles
end
to-report mean-degree-defectors
report mean [count social-link-neighbors] of defector-agents
end
to-report mean-degree-punishers
report mean [count social-link-neighbors] of punisher-agents
end
to-report mean-degree-cooperators
report mean [count social-link-neighbors] of cooperator-agents
end
to-report resource-level
report mean [blevel] of patches
end
to-report mean-payoff
report mean [payoff] of turtles / ticks-per-generation
end
@#$#@#$#@
GRAPHICS-WINDOW
388
330
798
761
-1
-1
40.0
1
10
1
1
1
0
1
1
1
0
9
0
9
0
0
1
ticks
30.0
BUTTON
18
553
190
609
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
16
148
188
181
torus-size
torus-size
2
15
10
1
1
NIL
HORIZONTAL
BUTTON
18
618
190
675
NIL
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
15
231
189
276
neighborhood-type
neighborhood-type
"Von Neumann" "Moore" "Complete Graph"
1
SWITCH
17
439
189
472
metanorms
metanorms
0
1
-1000
CHOOSER
18
316
193
361
defection-observing
defection-observing
"Geo only" "Social only" "Geo and Social"
2
SLIDER
14
35
186
68
ticks-per-generation
ticks-per-generation
1
10
4
1
1
NIL
HORIZONTAL
SLIDER
14
75
186
108
generations-log10
generations-log10
0
10
4
1
1
NIL
HORIZONTAL
PLOT
389
16
659
167
Attributes
Rounds
NIL
0.0
10.0
0.0
7.0
true
true
"" ""
PENS
"Boldness" 1.0 0 -16777216 true "" "plot average-boldness"
"Vengefulness" 1.0 0 -2674135 true "" "plot average-vengefulness"
SWITCH
18
477
190
510
must-obs-orig-defection
must-obs-orig-defection
0
1
-1000
PLOT
389
176
661
326
Population
Rounds
%
0.0
10.0
0.0
100.0
true
true
"" ""
PENS
"Cooperators" 1.0 0 -13345367 true "" "plot 100 * count-cooperators / count turtles"
"Punishers" 1.0 0 -1184463 true "" "plot 100 * count-punishers / count turtles"
"Defectors" 1.0 0 -2674135 true "" "plot 100 * count-defectors / count turtles"
SLIDER
679
217
851
250
high-effort
high-effort
0
1
0.3
0.01
1
NIL
HORIZONTAL
SLIDER
679
282
851
315
low-effort
low-effort
0
1
0.1
0.01
1
NIL
HORIZONTAL
SLIDER
857
178
1029
211
cost-of-effort
cost-of-effort
0
1
0.05
0.01
1
NIL
HORIZONTAL
SLIDER
861
244
1033
277
enforcement-cost
enforcement-cost
0
1
0.2
0.01
1
NIL
HORIZONTAL
SLIDER
860
283
1032
316
punishment-cost
punishment-cost
0
1
0.9
0.01
1
NIL
HORIZONTAL
SLIDER
671
53
842
86
initial-resource-level
initial-resource-level
0
100
80
1
1
%
HORIZONTAL
SLIDER
671
92
843
125
resource-growth-rate
resource-growth-rate
0
100
50
1
1
%
HORIZONTAL
SLIDER
16
189
188
222
population-density
population-density
0
100
50
1
1
%
HORIZONTAL
SLIDER
21
367
193
400
neighborhood-radius
neighborhood-radius
1
5
1
1
1
NIL
HORIZONTAL
SLIDER
203
399
375
432
institutional-boldness
institutional-boldness
0
7
0
1
1
NIL
HORIZONTAL
SLIDER
203
436
376
469
institutional-vengefulness
institutional-vengefulness
0
7
7
1
1
NIL
HORIZONTAL
SLIDER
203
502
378
535
institutional-weight
institutional-weight
0
100
5
1
1
%
HORIZONTAL
SLIDER
203
539
378
572
institutional-minimum
institutional-minimum
0
100
2
1
1
%
HORIZONTAL
SLIDER
203
247
375
280
imitation-strength
imitation-strength
0
10
3
1
1
NIL
HORIZONTAL
SLIDER
204
603
377
636
institutional-strength
institutional-strength
0
10
2
1
1
NIL
HORIZONTAL
SLIDER
203
286
375
319
wrong-imitation
wrong-imitation
0
100
10
1
1
%
HORIZONTAL
SLIDER
1282
77
1454
110
adjust-strength
adjust-strength
0
10
3
1
1
NIL
HORIZONTAL
SLIDER
1283
116
1455
149
rewire-prob
rewire-prob
0
100
0
1
1
%
HORIZONTAL
SLIDER
1284
235
1456
268
tryad-strength
tryad-strength
0
10
3
1
1
NIL
HORIZONTAL
SLIDER
1285
275
1457
308
tryad-basic-prob
tryad-basic-prob
0
100
0
1
1
%
HORIZONTAL
SLIDER
1283
156
1455
189
geo-strength
geo-strength
0
10
3
1
1
NIL
HORIZONTAL
SLIDER
1284
195
1456
228
geo-basic-prob
geo-basic-prob
0
100
0
1
1
%
HORIZONTAL
PLOT
854
10
1239
168
Resource
NIL
NIL
0.0
10.0
0.0
1.0
true
false
"" ""
PENS
"Level" 1.0 0 -14439633 true "" "plot resource-level"
PLOT
815
431
1120
571
Degree Distribution
NIL
NIL
0.0
70.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 1 -16777216 true "" "let max-degree max [count social-link-neighbors] of turtles\nplot-pen-reset ;; erase what we plotted before\nset-plot-x-range 1 ((max (list 1 max-degree)) + 1) ;; + 1 to make room for the width of the last bar\nhistogram [count social-link-neighbors] of turtles"
SLIDER
206
727
378
760
social-strategy-ratio
social-strategy-ratio
1
10
4
1
1
NIL
HORIZONTAL
SLIDER
203
205
375
238
mutation-rate
mutation-rate
0
100
3
1
1
%
HORIZONTAL
SLIDER
815
387
987
420
ba-power
ba-power
-3
3
-1
1
1
NIL
HORIZONTAL
SLIDER
202
98
374
131
initial-boldness
initial-boldness
0
7
7
1
1
NIL
HORIZONTAL
SLIDER
203
135
375
168
initial-vengefulness
initial-vengefulness
0
7
0
1
1
NIL
HORIZONTAL
SWITCH
203
60
374
93
random-attribs