Last active
August 4, 2019 16:51
-
-
Save toomasv/77884524435d80941c000de196ab2764 to your computer and use it in GitHub Desktop.
Prisoner's dilemma tournament
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Description: "Prisoner's dilemma tournament" | |
Date: 3-Aug-2019 | |
Author: "Toomas Vooglaid" | |
Licence: "Public domain" | |
] | |
;set [R P T S] [-1 -3 0 -6] ; One turn (original) | |
set [R P T S] [3 1 5 0] | |
choose: func [A B][reduce case [ | |
all [A B] [[R R]] ;A and B [[R R]] | |
all [A not B] [[S T]] ;A and not B [[S T]] | |
all [not A B] [[T S]] ;(not A) and B [[T S]] | |
not any [A B] [[P P]] ;(not A) and not b [[P P]] | |
]] | |
consider: func ['strategy act][ | |
reduce switch strategy [ | |
egoistic [[first choose act true first choose act false]] | |
collegial [[sum choose act true sum choose act false]] | |
] | |
] | |
decide: func ['strategy /explicit /iter i /self me /other adv][ | |
i: any [i 1] | |
switch/default strategy [ | |
tit-for-tat [either i = 1 [true][either (length? me/steps) < length? adv/steps [adv/steps/2][adv/steps/1]]] | |
tit-for-tat0 [either i = 1 [false][either (length? me/steps) < length? adv/steps [adv/steps/2][adv/steps/1]]] | |
tit-for-2tat [case [ | |
i = 1 [true] | |
(length? me/steps) < length? adv/steps [any [adv/steps/2 adv/steps/3]] | |
true [any [adv/steps/1 adv/steps/2]] | |
]] | |
random [random true] | |
grudging [case [i = 1 [true] not adv/steps/1 [me/strategy: 'egoistic adv/steps/1] true [true]]] | |
altern [i % 2 = 0] | |
][ | |
either explicit [ | |
either (sum consider :strategy true) >= (sum consider :strategy false) ['cooperate]['defect] | |
][ | |
(sum consider :strategy true) >= (sum consider :strategy false) | |
] | |
] | |
] | |
iter: func [n A B][ | |
clear A/steps clear B/steps | |
A/score: B/score: 0 | |
initialA: A/strategy | |
initialB: B/strategy | |
repeat i n [ | |
insert A/steps decide/other/self/iter :A/strategy B A i | |
insert B/steps decide/other/self/iter :B/strategy A B i | |
A/score: A/score + first choose A/steps/1 B/steps/1 | |
B/score: B/score + second choose A/steps/1 B/steps/1 | |
] | |
A/overall: A/overall + A/score | |
;B/overall: B/overall + B/score | |
print [pad/left A/score 4 "|" pad/left B/score 4 initialB] | |
] | |
tournament: does [ | |
strategies: [egoistic collegial tit-for-tat tit-for-tat0 tit-for-2tat random grudging altern] | |
clear A/total | |
foreach strategyA strategies [ | |
A/overall: B/overall: 0 | |
print ["===== A:" strategyA "====="] | |
print [" A | B"] | |
foreach strategyB strategies [ | |
A/strategy: strategyA | |
B/strategy: strategyB | |
iter 200 A B | |
] | |
repend A/total [strategyA A/overall] | |
] | |
sort/skip/all/compare A/total 2 func [a b][a/2 > b/2] | |
print "=======================^/" | |
print "Results:^/" | |
foreach [strategy points] A/total [print [pad strategy 13 points]] | |
] | |
A: object [ | |
strategy: 'tit-for-tat | |
steps: make block! 50 | |
score: 0 | |
overall: 0 | |
total: make block! 20 | |
;probability: 1.0 | |
] | |
B: make A [];[strategy: 'egoistic] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Some individual commands:
Individualistic
Collectivistic
Individual rounds: