-
Notifications
You must be signed in to change notification settings - Fork 5
/
ipd.rb
74 lines (60 loc) · 1.63 KB
/
ipd.rb
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
require 'hamster/list'
require 'hamster/hash'
module Prisoner
def prisoner(name, strategy)
Hamster.hash(name: name, strategy: strategy, moves: Hamster.list, total: 0)
end
end
module Strategy
def strategy(strategy)
-> mover, other { strategy.(mover[:moves], other[:moves]) }
end
def always_cooperate(mover, other); cooperate(mover); end
def always_defect(mover, other); defect(mover); end
end
module Moves
def cooperate(moves); 1; end
def defect(moves); 0; end
end
module Game
def to_both(prisoners)
prisoners.zip(prisoners.reverse).map do |mover, opp|
yield mover, opp
end
end
def game(prisoners)
to_both(prisoners) do |mover, opp|
move = mover[:strategy].(mover, opp)
mover.put(:moves, mover[:moves].cons(move))
end
end
def iterated_game(iterations, prisoners)
(1..iterations).reduce(prisoners) do |prisoners, _|
total_score(game(prisoners))
end
end
def total_score(prisoners)
to_both(prisoners) do |mover, opp|
score = mover[:moves].zip(opp[:moves]).reduce(0) do |score, (m, o)|
score += compute_score(m, o)
end
mover.put(:total, score)
end
end
def compute_score(a, b)
@scores ||= {
[0, 1] => 5,
[1, 1] => 3,
[0, 0] => 1,
[1, 0] => 0
}
@scores[[a,b]]
end
end
[Prisoner, Moves, Strategy, Game].each { |m| include m }
prisoners = iterated_game(10, Hamster.list(
prisoner(:a, strategy(method(:always_cooperate))),
prisoner(:b, strategy(method(:always_defect)))
))
puts "Prisoner A (AlwaysCooperate): %d" % prisoners.head[:total]
puts "Prisoner B (AlwaysDefect) : %d" % prisoners.last[:total]