-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrisonersDilemma.fsx
294 lines (242 loc) · 9.49 KB
/
PrisonersDilemma.fsx
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
#r "nuget: Plotly.NET, 4.2.0"
open System
open Plotly.NET
module Util =
let rec combinations acc size set = seq {
match size, set with
| n, x::xs ->
if n > 0 then yield! combinations (x::acc) (n - 1) xs
if n >= 0 then yield! combinations acc n xs
| 0, [] -> yield acc
| _, [] -> () }
module GameLogic =
open Util
type Move = | Cooperate | Defect
type Rounds = (Move * Move) array
type Player = {
Id : Guid
Name : string
Strategy : (Move array -> Move array -> Move)
Recreate : unit -> Player
}
type Game = {
Player1 : Player
Player1Moves : Move array
Player1Score : int option
Player2 : Player
Player2Moves : Move array
Player2Score : int option
}
type Tournament = {
Players : Player array
Games : Game array
}
let evaluateRound (p1: Move) (p2: Move) =
match p1, p2 with
| Cooperate , Cooperate -> 3,3
| Defect , Defect -> 1,1
| Cooperate , Defect -> 0,5
| Defect , Cooperate -> 5,0
let rnd() = System.Random()
let play (player1: Player) (player2 : Player) (rounds: int) =
let rec playRound (game: Game) (rounds:int) (counter:int) : Game =
if counter > rounds
then game
else
let updatedGame : Game =
let p1Moves = [|(game.Player1.Strategy game.Player1Moves game.Player2Moves)|] |> Array.append game.Player1Moves
let p2Moves = [|(game.Player2.Strategy game.Player2Moves game.Player1Moves)|] |> Array.append game.Player2Moves
{game with Player1Moves = p1Moves; Player2Moves = p2Moves}
playRound updatedGame rounds (counter + 1)
let g: Game = {Player1 = player1; Player1Moves = [||]; Player1Score = None; Player2 = player2 ; Player2Moves = [||]; Player2Score = None }
playRound g rounds 1
let evaluateGame (game: Game) =
game.Player2Moves
|> Array.zip game.Player1Moves
|> Array.map(fun (p1,p2) -> evaluateRound p1 p2)
|> Array.unzip
|> fun (p1, p2) ->
let evaluatedGame =
{
game with Player1Score = p1 |> Array.sum |> Some; Player2Score = p2 |> Array.sum |> Some
}
//printfn "%A" evaluatedGame
evaluatedGame
let createPairings (players: Player array) =
let result =
combinations [] 2 (players |> Array.toList)
|> Seq.append (players |> Array.map(fun s -> [s;s;] ))
result
let runTournament players =
createPairings players
|> Seq.map(fun c ->
play c[0] c[1] 200
|> evaluateGame)
|> Seq.toArray
|> fun games ->
players
|> Array.map(fun p ->
p,
games
|> Array.filter(fun g -> g.Player1.Id = p.Id)
|> Array.choose(fun g -> g.Player1Score)
|> Array.append (
games
|> Array.filter(fun g -> g.Player2.Id = p.Id)
|> Array.choose(fun g -> g.Player2Score)
)
|> Array.sum
)
type GenerationResult = {
Generation : int
StrategyPopulation : Map<string , int>
}
let getLine (results: GenerationResult array) strategyName =
let x, y =
results
|> Array.map(fun gr ->
gr.Generation,
(
gr.StrategyPopulation
|> Map.find strategyName
)
)
|> Array.unzip
Chart.Line(x = x,y = y, Name = strategyName)
let runExperiment startingPlayers generations =
let initResults =
{
Generation = 0;
StrategyPopulation =
startingPlayers
|> Array.groupBy(fun p -> p.Name)
|> Array.map(fun p -> p |> fst, p |> snd |> Array.length)
|> Map
}
let rec go players generations counter (results: GenerationResult array) =
if counter >= generations
then results
else
let tournamentResults = runTournament players
let nextGen =
tournamentResults
|> Array.groupBy(fun (p,s) -> p.Name)
|> Array.map(fun (_, scores) -> scores |> Array.head |> fst, scores |> Array.sumBy snd)
|> fun scores ->
let totalScore = scores |> Array.sumBy snd |> float
scores
|> Array.map(fun (p, s) -> p, float s / totalScore * 100. |> int)
|> Array.map(fun (p, s) ->
[|0..s|]
|> Array.map(fun _ -> p.Recreate())
)
|> Array.concat
let result =
{
Generation = counter;
StrategyPopulation =
nextGen
|> Array.groupBy(fun p -> p.Name)
|> Array.map(fun p -> p |> fst, p |> snd |> Array.length)
|> Map
}
go nextGen generations (counter + 1) ( [|result|] |> Array.append results)
go startingPlayers generations 0 [|initResults|]
module Strategies =
open GameLogic
let rec tester() : Player =
let name = "Tester"
let strategy =
fun (playerMoves: Move array) (opponentMoves : Move array) ->
if playerMoves.Length = 0
then Defect
elif playerMoves.Length <= 3
then Cooperate
elif
not
(opponentMoves
|> Array.exists(fun m -> m = Defect))
then
if playerMoves.Length % 2 = 0
then Defect
else Cooperate
elif // this is wrong test should alternate until other side defects.
opponentMoves |> Array.last = Defect
&&
opponentMoves
|> Array.filter(fun m -> m = Defect)
|> Array.length
= 1
then Cooperate
else
opponentMoves
|> Array.last
{Id = Guid.NewGuid(); Name = name; Strategy = strategy; Recreate = tester}
let rec titForTwoTats() : Player =
let name = "TitForTwoTats"
let strategy =
fun (playerMoves: Move array) (opponentMoves : Move array) ->
if playerMoves.Length < 2
then Cooperate
elif
opponentMoves
|> Array.rev
|> Array.take 2
|> Array.exists(fun m -> m = Cooperate)
then Cooperate
else Defect
{Id = Guid.NewGuid(); Name = name; Strategy = strategy; Recreate = titForTwoTats}
let rec titForTat() : Player =
let name = "TitForTat"
let strategy =
fun (playerMoves: Move array) (opponentMoves : Move array) ->
if playerMoves.Length = 0
then Cooperate
else
opponentMoves
|> Array.last
{Id = Guid.NewGuid(); Name = name; Strategy = strategy; Recreate = titForTat}
let rec grudger() : Player =
let name = "Grudger"
let strategy =
fun (playerMoves: Move array) (opponentMoves : Move array) ->
if playerMoves.Length = 0
then Cooperate
else
if
opponentMoves
|> Array.exists(fun m -> m = Defect)
then Defect
else Cooperate
{Id = Guid.NewGuid(); Name = name; Strategy = strategy; Recreate = grudger}
let rec random() : Player =
let name = "Random"
let strategy =
fun _ _->
let randomBit = rnd().Next(0,2)
if randomBit = 0 then Cooperate else Defect
{Id = Guid.NewGuid();Name = name; Strategy = strategy; Recreate = random}
let rec alwaaysDefect() : Player =
let name = "AlwaaysDefect"
let strategy =
fun _ _ ->
Defect
{Id = Guid.NewGuid();Name = name; Strategy = strategy; Recreate = alwaaysDefect}
let rec alwaaysCooperate() : Player =
let name = "AlwaaysCooperate"
let strategy =
fun _ _ ->
Cooperate
{Id = Guid.NewGuid();Name = name; Strategy = strategy; Recreate = alwaaysCooperate}
open GameLogic
open Strategies
let players =
[|titForTat(); titForTwoTats();random(); alwaaysDefect(); alwaaysCooperate(); grudger(); tester()|]
//let players =
// [|tester(); titForTwoTats();|]
runExperiment players 100
|> fun results ->
players
|> Array.map(fun p -> getLine results p.Name)
|> Chart.combine
|> Chart.show