-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolvingGamePlayers.nlogo
6597 lines (6119 loc) · 187 KB
/
EvolvingGamePlayers.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Evolving Game Players: Evolutionary Games & Population Dynamics
;; This program (C) Christopher J Watts, 2021.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
extensions [rnd csv]
globals [
; Last Seeds used by random number generator.
prev-seed-setup
prev-seed-go
min-payoff
max-payoff
sorted-patches
sorted-populations
; Crude method G2's belief x > G1's belief y etc.
num-pops-with-group1-dom
num-pops-with-group2-dom
num-pops-with-groups-equal
group1-payoff ; Mean over populations of mean over group members of last payoff received
group2-payoff
num-pops-g1-dominant
num-pops-g2-dominant
num-pops-none-dominant
num-pops-g1-hawkish
num-pops-g2-hawkish
num-pops-none-hawkish
stat-avg ; For displaying average of multiple populations
]
breed [arrows arrow]
breed [texts text]
breed [populations population]
breed [players player]
breed [stats stat] ; Agents used to represent statistical aggregates of populations
populations-own [
x
y
po-players
po-group1
po-group2
po-dominant-group ; No players left who beliefs could move them over the V/C line.
po-round-dominant
po-expectant-group ; Mean Beliefs of one group < those of the other
po-round-expectant
po-hawkish-group ; One group's out-actions are all H, while other's are all D.
po-round-hawkish
po-dom-groups ; Dominant group at round 50, 100, 150, ...
po-exp-groups ; Expectant group at round 50, 100, 150, ...
po-haw-groups ; Hawkish group at round 50, 100, 150, ...
]
players-own [
pl-population
pl-group
pl-strategy
pl-action
pl-opponents-action
pl-played-out-group?
pl-fitness
pl-memory
pl-memory-length
pl-other-interactions ; Number of interactions with members of other groups.
pl-belief ; Number of "1" actions performed against this agent and entered into memory (but doesn't forget).
pl-history ; Number of "1" actions performed by this agent in personal history.
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set sorted-patches sort patches
foreach sorted-patches [pa ->
ask pa [
set pcolor white
set plabel-color black
]
]
if Draw-X-And-Y-Axes? [draw-axes]
set min-payoff min map [A -> min payoffs (first A) (last A)] (list [0 0] [0 1] [1 0] [1 1]) ; Used to remove negative payoffs, and for scale-color.
set max-payoff max map [A -> max payoffs (first A) (last A)] (list [0 0] [0 1] [1 0] [1 1]) ; Used for scale-color.
reset-ticks
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-rng [given-variable-name]
ifelse 0 = runresult given-variable-name [
run (word "set prev-" given-variable-name " " new-seed)
]
[
run (word "set prev-" given-variable-name " " given-variable-name)
]
random-seed runresult (word "prev-" given-variable-name)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Payoff table cells
to-report payoffs [a-move b-move]
if game = "Hawk-Dove" [report payoffs-hawk-dove a-move b-move]
if game = "Hawk-Dove Optimal" [report payoffs-hawk-dove-optimal a-move b-move]
if game = "Mutualism" [report payoffs-mutualism a-move b-move]
if game = "Mini Nash Demand" [report payoffs-mini-nash-demand a-move b-move]
if game = "Prisoner's Dilemma" [report payoffs-Prisoners-Dilemma a-move b-move]
if game = "Donation" [report payoffs-donation a-move b-move]
; if game = "Battle of the Sexes" [report payoffs-battle-of-the-sexes a-move b-move ([pl-group] of ego) ([pl-group] of alter)]
if game = "Battle of the Sexes" [report payoffs-battle-of-the-sexes a-move b-move 1 2]
report false
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Mutualism [A-Move B-Move]
report item (A-Move * 2 + B-Move) (list
(list k k) ; Genererous-Genererous
(list 1 2) ; Genererous-Selfish
(list 2 1) ; ; Selfish-Genererous
(list 0 0) ; Selfish-Selfish
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Hawk-Dove [A-Move B-Move]
report item (A-Move * 2 + B-Move) (list
(list (value / 2) (value / 2)) ; Dove-Dove
(list 0 value) ; ; Dove-Hawk
(list value 0) ; Hawk-Dove
(list ((value - cost) / 2) ((value - cost) / 2)) ; Hawk-Hawk
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Hawk-Dove-Optimal [A-Move B-Move]
let cur-payoffs Payoffs-Hawk-Dove a-move b-move
report ifelse-value (first Payoffs-Hawk-Dove a-move b-move >= first Payoffs-Hawk-Dove (1 - a-move) b-move) [
ifelse-value (last Payoffs-Hawk-Dove a-move b-move >= last Payoffs-Hawk-Dove a-move (1 - b-move)) [[1 1]] [[1 0]]
] [
ifelse-value (last Payoffs-Hawk-Dove a-move b-move >= last Payoffs-Hawk-Dove a-move (1 - b-move)) [[0 1]] [[0 0]]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Mini-Nash-Demand [A-Move B-Move]
report item (A-Move * 2 + B-Move) (list
(list mnd-Low mnd-Low) ; Low-Low
(list mnd-Low MND-High) ; ; Low-High
(list MND-High mnd-Low) ; High-Low
(list 0 0) ; High-High
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report MND-Low
report mnd-high * (100 - HLDiff-As-Perc-Of-H) * 0.01
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Prisoners-Dilemma [A-Move B-Move]
report item (A-Move * 2 + B-Move) (list
(list reward reward) ; Cooperate-Cooperate
(list sucker temptation) ; ; C-D
(list temptation sucker) ; D-C
(list punishment punishment) ; Defect-Defect
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Battle-Of-The-Sexes [A-Move B-Move A-group B-group]
report item (A-Move * 2 + B-Move) (list
(list (ifelse-value (a-group = 1) [my-preference] [your-preference]) (ifelse-value (b-group = 1) [my-preference] [your-preference])) ; We both went with A's ideal.
(list 0 0) ; ; We missed each other.
(list 0 0) ; ; We missed each other.
(list (ifelse-value (a-group = 2) [my-preference] [your-preference]) (ifelse-value (b-group = 2) [my-preference] [your-preference])) ; We both went with B's ideal.
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report Payoffs-Donation [A-Move B-Move]
; Donation Game is a special case of the Prisoner's Dilemma.
; We parameterise it with Value ("Benefit") and Cost, like Hawk-Dove.
report item (A-Move * 2 + B-Move) (list
(list (value - cost) (value - cost)) ; Cooperate (donate)-Cooperate
(list (0 - cost) value) ; ; C-D
(list value (0 - cost)) ; D-C
(list 0 0) ; Defect (don't donate)-Defect
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report cost
report value * 100 / Value-As-Perc-Of-Cost
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report made-neat [given-list]
; Don't display all the decimal places
report map [a -> precision a 2] given-list
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report payoff-for-A-move-given-perc [A-move given-prop]
report ((given-prop * first Payoffs A-move 1) +
((1.0 - given-prop) * first Payoffs A-move 0))
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report payoff-for-B-move-given-perc [B-move given-prop]
report ((given-prop * last Payoffs 1 B-move) +
((1.0 - given-prop) * last Payoffs 0 B-move))
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Visuals
to draw-axes
ask arrows [die]
; X-axis
create-arrows 1 [
set shape "default"
set color black
set pen-size 2
set heading 90
setxy -1 0
pen-down
setxy max-pxcor 0
]
; Y-axis
create-arrows 1 [
set shape "default"
set color black
set pen-size 2
set heading 0
setxy 0 -1
pen-down
setxy 0 max-pycor
]
; Diagonal
if Draw-Diagonal? [
create-arrows 1 [
set color black
set pen-size 2
set heading 45
setxy 0 0
foreach (range 2 100 2) [a100 ->
setxy (x-max * a100 / 100) (y-max * a100 / 100)
ifelse 0 = a100 mod 4 [pen-down] [pen-up]
]
die
]
]
; MSNE
if Draw-MSNE-Lines? [
create-arrows 1 [
set color grey
set pen-size 2
set heading 90
setxy 0 (msne * y-max)
foreach (range 2 100 2) [a100 ->
setxy (x-max * a100 / 100) (msne * y-max)
ifelse 0 = a100 mod 4 [pen-down] [pen-up]
]
pen-up
setxy (msne * x-max) 0
foreach (range 2 100 2) [a100 ->
setxy (msne * x-max) (y-max * a100 / 100)
ifelse 0 = a100 mod 4 [pen-down] [pen-up]
]
die
]
]
; Labels
create-texts 1 [
set size 0
set color black
set label-color black
set label "1"
set pen-size 2
setxy x-max 0
pen-down
setxy x-max -0.25
pen-up
setxy x-max -1
]
create-texts 1 [
set size 0
set color black
set label-color black
set label "0.5"
set pen-size 2
setxy (0.5 * x-max) 0
pen-down
setxy (0.5 * x-max) -0.25
pen-up
setxy (0.5 * x-max) -1
]
create-texts 1 [
set size 0
set color black
set label-color black
set label "1"
set pen-size 2
setxy 0 y-max
pen-down
setxy -0.25 y-max
pen-up
setxy -0.25 y-max
]
create-texts 1 [
set size 0
set color black
set label-color black
set label "0.5"
set pen-size 2
setxy 0 (0.5 * y-max)
pen-down
setxy -0.25 (0.5 * y-max)
pen-up
setxy -0.25 (0.5 * y-max)
]
create-texts 1 [
set size 0
set color black
set label-color black
set label "O"
setxy -0.5 -1
]
if Draw-Diagonal? [
create-texts 1 [
set size 0
set label-color black
set label "y = x"
setxy (x-max + 0.5) (y-max)
]
]
if Draw-MSNE-Lines? [
create-texts 1 [
set size 0
set color black
set label-color black
set label "MSNE" ;"V/C"
setxy 0 (msne * y-max)
set pen-size 2
pen-down
setxy -0.25 (msne * y-max)
pen-up
setxy -0.25 (msne * y-max)
]
create-texts 1 [
set size 0
set color black
set label-color black
set label "MSNE" ;"V / C"
setxy (msne * x-max) 0
set pen-size 2
pen-down
setxy (msne * x-max) -0.25
pen-up
setxy (1 + msne * x-max) -1
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report mouse-payoffs
if not mouse-inside? [report ""]
let p1 mouse-xcor / x-max
let p2 mouse-ycor / y-max
report payoffs-xy p1 p2
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report payoffs-xy [p1 p2]
report (list
(precision ((p1 * payoff-for-A-move-given-perc 1 p2) + (1 - p1) * payoff-for-A-move-given-perc 0 p2) 3)
(precision ((p2 * payoff-for-B-move-given-perc 1 p1) + (1 - p2) * payoff-for-B-move-given-perc 0 p1) 3)
)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to Color-Patches-By-Payoff [first-not-last?]
foreach sorted-patches [pa ->
ask pa [
if (pxcor >= 0 and pycor >= 0 and pxcor <= x-max and pycor <= y-max) [
let cur-payoffs payoffs-xy (pxcor / x-max) (pycor / y-max)
ifelse first-not-last? [
set pcolor scale-color blue (first cur-payoffs) min-payoff max-payoff
]
[
set pcolor scale-color red (last cur-payoffs) min-payoff max-payoff
]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to recolor-pops-by-players
foreach sorted-populations [po ->
ask po [
; Assuming pl-fitness is payoff from just one round.
set color scale-color green (mean map [a -> [pl-fitness] of a] po-players) (-1 + min-payoff) (1 + max-payoff)
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to recolor-pops-by-xy
foreach sorted-populations [po ->
ask po [
; Assuming pl-fitness is payoff from just one round.
let cur-payoffs payoffs-xy x y
set color scale-color green (first cur-payoffs) (-1 + min-payoff) (1 + max-payoff)
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report x-max
report max-pxcor - 2
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report y-max
report max-pycor - 2
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Agent-Based Simulation using Replication and/or Memory
to Setup-Stochastic-Sim
; Setup populations at evenly-spaced points in state space.
; Each population contains a number of player agents,
; divided between two groups.
; Players will then:
; either base play same pl-action move each match,
; after which population is updated using stochastic replication,
; or players will base decisions on sampling and memory,
; and memory will be updated after each match against
; a member of the opposite group.
setup
setup-rng "Seed-Setup"
setup-initial-populations [[xx yy] -> setup-population-abm xx yy]
set sorted-populations sort populations
; recolor-pops-by-players
reset-ticks
reposition-pops
reposition-players
setup-stats
; Draw paths?
if Population-Pen-Down? [
foreach sorted-populations [po ->
ask po [ifelse (Population-Pen-Down? and memory-initialization != "Empty") [pen-down] [pen-up]]
; If Empty, initial position is meaningless.
]
]
setup-rng "Seed-Go"
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-initial-populations [setup-population-task]
if initial-populations = "21x21 Evenly Spaced" [
foreach (range 0 105 5) [x100 ->
foreach (range 0 105 5) [y100 ->
(run setup-population-task x100 y100)
]
]
stop
]
if initial-populations = "11x11 Evenly Spaced" [
foreach (range 0 110 10) [x100 ->
foreach (range 0 110 10) [y100 ->
(run setup-population-task x100 y100)
]
]
stop
]
if initial-populations = "6x6 Evenly Spaced" [
foreach (range 0 120 20) [x100 ->
foreach (range 0 120 20) [y100 ->
(run setup-population-task x100 y100)
]
]
stop
]
if initial-populations = "1 at Initial-X/Y" [
(run setup-population-task initial-x initial-y)
stop
]
if initial-populations = "10 at Initial-X/Y" [
repeat 10 [(run setup-population-task initial-x initial-y)]
stop
]
if initial-populations = "100 at Initial-X/Y" [
repeat 100 [(run setup-population-task initial-x initial-y)]
stop
]
if initial-populations = "1 at MSNE" [
(run setup-population-task (100 * msne) (100 * msne))
stop
]
if initial-populations = "10 at MSNE" [
repeat 10 [(run setup-population-task (100 * msne) (100 * msne))]
stop
]
if initial-populations = "100 at MSNE" [
repeat 100 [(run setup-population-task (100 * msne) (100 * msne))]
stop
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-population-abm [x100 y100]
create-populations 1 [
set hidden? hide-populations?
set shape "default"
;set color yellow
setxy (x-max * x100 / 100) (y-max * y100 / 100)
set x x100 / 100
set y y100 / 100
setxy (x-max * x) (y-max * y)
facexy (x-max * next-x) (y-max * next-y)
; if Population-Pen-Down? [pen-down]
; Create Population's players
set po-players []
set po-group1 []
set po-group2 []
set po-dominant-group 0 ; 0 means neither group 1 nor group 2 dominates.
set po-round-dominant false ; false means no group dominant yet.
set po-dom-groups [] ; Dominant group at round 50, 100, 150, ...
set po-exp-groups [] ; Expectant group at round 50, 100, 150, ...
set po-haw-groups [] ; Hawkish group at round 50, 100, 150, ...
let num-red round (population-size * perc-group2 / 100)
; 4 sub-populations (2 groups * 2 strategies)
(foreach (list
(round ((population-size - num-red) * x))
((population-size - num-red) - (round ((population-size - num-red) * x)))
(round (num-red * y))
(num-red - (round (num-red * y)))
)
[ 1 1 2 2 ]
[ 1 0 1 0 ]
[[n g s] ->
hatch-players n [
set hidden? hide-players?
set shape "dot"
set color item g (list grey blue red)
set pl-population myself
set pl-group g
ask pl-population [
set po-players fput myself po-players
ifelse g = 1 [
set po-group1 fput myself po-group1
]
[
set po-group2 fput myself po-group2
]
if player-pen-down? [pen-down]
]
set pl-strategy strategy-reporter
set pl-action s
set pl-played-out-group? true
]
]
)
foreach po-players [pl ->
ask pl [setup-initial-memory]
]
; Actual population might be slightly different from ideal.
set x mean map [a -> [pl-action] of a] po-group1
set y mean map [a -> [pl-action] of a] po-group2
setxy (x-max * x) (y-max * y)
facexy (x-max * next-x) (y-max * next-y)
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup-initial-memory
; Run as player.
set pl-memory []
set pl-memory-length ifelse-value (pl-group = 1) [Memory-Length-1] [Memory-Length-2]
; (Actually, we're currently using belief instead of memory.)
if memory-initialization = "Empty" [
set pl-belief 0
set pl-other-interactions 0
stop
]
set pl-other-interactions ifelse-value (pl-group = 1) [Memory-Initial-Weight-1] [Memory-Initial-Weight-2]
let prob mean map [pl -> [pl-action] of pl] ifelse-value (pl-group = 1) [[po-group1] of pl-population] [[po-group2] of pl-population]
set pl-history round (prob * pl-other-interactions)
if memory-initialization = "Fixed-Proportion-50:50" [
; Assumes past opponents played "1" in 50% of matches, "0" otherwise.
set prob 0.5
set pl-belief round (prob * pl-other-interactions)
stop
]
if memory-initialization = "Fixed-Proportion-MSNE" [
; Assumes past opponents played the Mixed-Strategy Nash Equilibrium.
set prob msne
set pl-belief round (prob * pl-other-interactions)
stop
]
if memory-initialization = "Fixed-Proportion-xy" [
; Makes use of the fact that players have been initialised with pl-action
; values in proportion to the x and y positions of the population.
set prob mean map [pl -> [pl-action] of pl] ifelse-value (pl-group = 1) [[po-group2] of pl-population] [[po-group1] of pl-population]
set pl-belief round (prob * pl-other-interactions)
stop
]
; Random memories, using the fixed proportions as probabilities
; and Bernoulli-distributed random sampling.
if memory-initialization = "Random-50:50" [
set prob 0.5
]
if memory-initialization = "Random-MSNE" [
set prob msne
]
if memory-initialization = "Random-xy" [
set prob mean map [pl -> [pl-action] of pl] ifelse-value (pl-group = 1) [[po-group2] of pl-population] [[po-group1] of pl-population]
]
ifelse unlimited-memory? [
; (NetLogo doesn't have a random-binomial procedure, sum bernoulli trials instead.)
set pl-belief sum n-values pl-other-interactions [ifelse-value (prob > random-float 1) [1] [0]]
]
[
; (list opponents-action pl-action players-payoff given-opponent)
set pl-memory (map [[x1 x2 x3 x4] -> (list x1 x2 x3 x4)]
(n-values pl-other-interactions [ifelse-value (prob > random-float 1) [1] [0]])
(n-values pl-other-interactions [[]]) ; Not in use yet.
(n-values pl-other-interactions [0]) ; Not in use yet.
(n-values pl-other-interactions [nobody]) ; Not in use yet.
)
set pl-belief sum map [a -> first a] pl-memory
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to play-game
foreach sorted-populations [po ->
ask po [
let participants shuffle po-players
ifelse Opponents-Include-Own-Group? [
(foreach (sublist participants 0 (int ((length participants) / 2))) (sublist participants (int ((length participants) / 2)) (length participants)) [[ego alter] ->
play-given-participants ego alter
])
]
[
let alter nobody
foreach participants [ego ->
set alter one-of ifelse-value ([pl-group = 1] of ego) [po-group2] [po-group1]
play-given-participants ego alter
]
]
update-at-end-of-round
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to play-given-participants [ego alter]
let ego-action 0
let alter-action 0
let cur-payoffs [0.0 0.0]
repeat 1 [
ask ego [play-move alter]
ask alter [play-move ego]
set cur-payoffs payoffs ([pl-action] of ego) ([pl-action] of alter)
ask ego [update-after-match-given alter cur-payoffs]
ask alter [update-after-match-given ego (reverse cur-payoffs)]
]
end
to play-move [opponent]
set pl-action (runresult pl-strategy opponent)
if 0 < playing-noise [
if playing-noise > random-float 100 [set pl-action 1 - pl-action]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-after-match-given [alter cur-payoffs]
set pl-played-out-group? pl-group != [pl-group] of alter
if Print-Out-Match-Results? [if pl-played-out-group? [print (word self " played " alter " : " pl-action ", " ([pl-action] of alter) " for payoffs " (map [p -> precision p 2] cur-payoffs))]]
set pl-opponents-action [pl-action] of alter
set pl-fitness (first cur-payoffs) ; For evolutionary dynamics
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to play-game-old
foreach sorted-populations [po ->
ask po [
foreach shuffle po-players [ego -> ; Giving everyone a chance to be updated.
ask ego [
; set pl-fitness 0
let alter nobody
let selected-item 0
let opponents []
ifelse Opponents-Include-Own-Group? [
set opponents [po-players] of pl-population
]
[
ifelse pl-group = 1 [
set opponents [po-group2] of pl-population
]
[
set opponents [po-group1] of pl-population
]
]
let ego-action 0
let alter-action 0
let cur-payoffs [0.0 0.0]
repeat 1 [
set alter one-of opponents
while [alter = ego] [set alter one-of opponents]
set ego-action (runresult pl-strategy alter)
set alter-action [(runresult pl-strategy ego)] of alter
if 0 < playing-noise [
if playing-noise > random-float 100 [set ego-action 1 - ego-action]
if playing-noise > random-float 100 [set alter-action 1 - alter-action]
]
set cur-payoffs payoffs ego-action alter-action
set pl-played-out-group? pl-group != [pl-group] of alter
if Print-Out-Match-Results? [if pl-played-out-group? [print (word ego " played " alter " : " ego-action ", " alter-action " for payoffs " (map [p -> precision p 2] cur-payoffs))]]
; if pl-played-out-group? [set pl-action ego-action] ; Should we record the pre-noise version instead?
set pl-action ego-action ; Should we record the pre-noise version instead?
set pl-opponents-action alter-action
set pl-fitness (first cur-payoffs) ; For evolutionary dynamics
; update-after-match ego alter alter-action (first cur-payoffs)
; update-after-match alter-action (first cur-payoffs)
; NB: alter doesn't get get any payoff from this.
;update-after-match alter ego ego-action (last cur-payoffs)
]
]
]
update-at-end-of-round
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-after-match
;to update-after-match [given-player given-opponent opponents-action players-payoff]
;set pl-fitness pl-fitness + players-payoff ; For evolutionary dynamics
if pl-played-out-group? [
ifelse not Unlimited-Memory? [
; Only remembering interactions with other groups.
set pl-memory fput (list pl-opponents-action pl-action) sublist pl-memory 0 (min (list (length pl-memory) pl-memory-length)) ; For cultural models
; set pl-memory fput (list opponents-action pl-action players-payoff given-opponent) sublist pl-memory 0 (min (list (length pl-memory) pl-memory-length)) ; For cultural models
set pl-belief sum map [m -> first m] pl-memory
set pl-history sum map [m -> item 1 m] pl-memory
set pl-other-interactions length pl-memory
]
[
; If using "unlimited" memory, then storing in pl-memory is a waste. Better to just keep a count.
set pl-belief pl-belief + pl-opponents-action ; NB: Updating beliefs during the round. Ego could now become an opponent in this same round, and base actions on this updated belief.
set pl-history pl-history + pl-action
set pl-other-interactions pl-other-interactions + 1
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-at-end-of-round
foreach filter [pl -> [pl-played-out-group? ] of pl] po-players [pl ->
ask pl [
update-after-match
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report strategy-reporter
; For belief learning
if playing-strategy = "Play-MSNE" [report [a -> bl-playing msne a]]
if playing-strategy = "Expect-MSNE" [report [a -> bl-expecting msne a]]
if playing-strategy = "Play-Initial-X-Y" [report [a -> bl-playing (ifelse-value (pl-group = 1) [initial-x / 100] [initial-y / 100]) a]]
if playing-strategy = "Expect-Initial-X-Y" [report [a -> bl-expecting (ifelse-value (pl-group = 1) [initial-y / 100] [initial-x / 100]) a]]
if playing-strategy = "Play-Neutrally-Initial-X-Y" [report [a -> bl-neut-playing (ifelse-value (pl-group = 1) [initial-x / 100] [initial-y / 100]) a]]
if playing-strategy = "Expect-Neutrally-Initial-X-Y" [report [a -> bl-neut-expecting (ifelse-value (pl-group = 1) [initial-y / 100] [initial-x / 100]) a]]
if playing-strategy = "Play-Random" [report [a -> bl-playing 0.5 a]]
if playing-strategy = "Play-H-D" [report [a -> bl-playing (ifelse-value (pl-group = 1) [1] [0]) a]]
if playing-strategy = "Play-D-H" [report [a -> bl-playing (ifelse-value (pl-group = 1) [0] [1]) a]]
if playing-strategy = "Expect-D-D" [report [a -> bl-expecting 0 a]]
if playing-strategy = "Expect-H-H" [report [a -> bl-expecting 1 a]]
if playing-strategy = "Expect-H-0.5" [report [a -> bl-expecting 0.5 a]]
; Other methods for decisions (for testing)
if playing-strategy = "MSNE" [report [a -> bl-playing msne a]]
if playing-strategy = "Memory" [report [a -> playing-memory]]
if playing-strategy = "Stochastic Memory" [report [a -> playing-memory-stochastic]]
; For replicator dynamics
if playing-strategy = "Last Action" [report [a -> pl-action]]
user-message (word "Warning! Playing-Strategy " playing-strategy " was not identified.")
report [-> pl-action]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report best-response-to [given-expectation]
; Recall:
; MSNE = ( (first payoffs 0 0) - (first payoffs 1 0) ) / ( (first payoffs 1 1) - (first payoffs 1 0) - (first payoffs 0 1) + (first payoffs 0 0) )
; NB: MSNE and payoffs could be non-integer, and given-expectation is non-integer.
; i.e. Potential problem from comparing results of floating point arithmetic?
if given-expectation * ((first payoffs 1 1) - (first payoffs 0 1)) > ((1.0 - given-expectation) * ((first payoffs 0 0) - (first payoffs 1 0))) [report 1]
if given-expectation * ((first payoffs 1 1) - (first payoffs 0 1)) < ((1.0 - given-expectation) * ((first payoffs 0 0) - (first payoffs 1 0))) [report 0]
if (given-expectation < msne) [report 1]
if (given-expectation > msne) [report 0]
report random 2
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report neutral-best-response-to [given-expectation]
if given-expectation * ((first payoffs 1 1) - (first payoffs 0 1)) > ((1.0 - given-expectation) * ((first payoffs 0 0) - (first payoffs 1 0))) [report 1]
if given-expectation * ((first payoffs 1 1) - (first payoffs 0 1)) < ((1.0 - given-expectation) * ((first payoffs 0 0) - (first payoffs 1 0))) [report 0]
if (given-expectation < msne) [report 1]
if (given-expectation > msne) [report 0]
report ifelse-value (msne > random-float 1.0) [1] [0]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report bl-playing [given-prob given-alter]
if pl-group = [pl-group] of given-alter [report playing-mixed-strategy msne]
if pl-other-interactions = 0 [report playing-mixed-strategy given-prob]
report playing-memory
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report bl-neut-playing [given-prob given-alter]
; Neutral in the sense that equal expected payoffs result in playing MSNE, not 50:50.
if pl-group = [pl-group] of given-alter [report playing-mixed-strategy msne]
if pl-other-interactions = 0 [report playing-mixed-strategy given-prob]
report playing-memory-neutrally
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report bl-expecting [given-prob given-alter]
if pl-group = [pl-group] of given-alter [report playing-mixed-strategy msne]
if pl-other-interactions = 0 [report best-response-to given-prob]
report playing-memory
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report bl-neut-expecting [given-prob given-alter]
; Neutral in the sense that equal expected payoffs result in playing MSNE, not 50:50.
if pl-group = [pl-group] of given-alter [report playing-mixed-strategy msne]
if pl-other-interactions = 0 [report neutral-best-response-to given-prob]
report playing-memory-neutrally
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report playing-mixed-strategy [given-prob]
report ifelse-value given-prob > random-float 1 [1] [0]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report MSNE
; If their opponent plays this mixed strategy,
; (i.e. a probabilistic strategy),
; then player should be indifferent between actions.
; pi(1,1)*p + pi(1,0)*(1-p) = pi(0,1)*p + pi(0,0)*(1-p)
; p = ( p(0,0) - pi(1,0) ) / ( pi(1,1) - pi(1,0) - pi(0,1) + pi(0,0) )
report ( (first payoffs 0 0) - (first payoffs 1 0) ) / ( (first payoffs 1 1) - (first payoffs 1 0) - (first payoffs 0 1) + (first payoffs 0 0) )
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report MSNE-Payoff
let prob msne
report (prob * first payoffs 1 1) + ((1 - prob) * first payoffs 1 0)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report playing-memory
if pl-other-interactions = 0 [report playing-mixed-strategy msne]
; Would work for any payoffs table
let a (pl-belief * first payoffs 0 1) + ((pl-other-interactions - pl-belief) * first payoffs 0 0)
let b (pl-belief * first payoffs 1 1) + ((pl-other-interactions - pl-belief) * first payoffs 1 0)
if a > b [report 0]
if b > a [report 1]
report random 2 ; Not neutral! Should have played MSNE instead!
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report playing-memory-neutrally
if pl-other-interactions = 0 [report playing-mixed-strategy msne]
; Would work for any payoffs table
let a (pl-belief * first payoffs 0 1) + ((pl-other-interactions - pl-belief) * first payoffs 0 0)
let b (pl-belief * first payoffs 1 1) + ((pl-other-interactions - pl-belief) * first payoffs 1 0)
if a > b [report 0]
if b > a [report 1]
report playing-mixed-strategy msne ; Neutral. Not 50:50 coin flipping.
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report playing-memory-stochastic
if pl-other-interactions = 0 [report playing-mixed-strategy msne]
let p pl-belief / pl-other-interactions
let a (p * first payoffs 0 1) + ((1 - p) * first payoffs 0 0)
let b (p * first payoffs 1 1) + ((1 - p) * first payoffs 1 0)
if a > random-float (a + b) [report 0]
report 1
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Reproduction of population by random sampling
to replicate