-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathworse_and_worse.py
125 lines (100 loc) · 3.66 KB
/
worse_and_worse.py
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
from axelrod.action import Action
from axelrod.player import Player
C, D = Action.C, Action.D
class WorseAndWorse(Player):
"""
Defects with probability of 'current turn / 1000'. Therefore
it is more and more likely to defect as the round goes on.
Source code available at the download tab of [Prison1998]_
Names:
- Worse and Worse: [Prison1998]_
"""
name = "Worse and Worse"
classifier = {
"memory_depth": float("inf"),
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1
probability = 1 - current_round / 1000
return self._random.random_choice(probability)
class KnowledgeableWorseAndWorse(Player):
"""
This strategy is based on 'Worse And Worse' but will defect with probability
of 'current turn / total no. of turns'.
Names:
- Knowledgeable Worse and Worse: Original name by Adam Pohl
"""
name = "Knowledgeable Worse and Worse"
classifier = {
"memory_depth": float("inf"),
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1
expected_length = self.match_attributes["length"]
probability = 1 - current_round / expected_length
return self._random.random_choice(probability)
class WorseAndWorse2(Player):
"""
Plays as tit for tat during the first 20 moves.
Then defects with probability (current turn - 20) / current turn.
Therefore it is more and more likely to defect as the round goes on.
Names:
- Worse and Worse 2: [Prison1998]_
"""
name = "Worse and Worse 2"
classifier = {
"memory_depth": float("inf"),
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1
if current_round == 1:
return C
elif current_round <= 20:
return opponent.history[-1]
else:
probability = 20 / current_round
return self._random.random_choice(probability)
class WorseAndWorse3(Player):
"""
Cooperates in the first turn.
Then defects with probability no. of opponent defects / (current turn - 1).
Therefore it is more likely to defect when the opponent defects for a larger
proportion of the turns.
Names:
- Worse and Worse 3: [Prison1998]_
"""
name = "Worse and Worse 3"
classifier = {
"memory_depth": float("inf"),
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
current_round = len(self.history) + 1
if current_round == 1:
return C
else:
probability = 1 - opponent.defections / (current_round - 1)
return self._random.random_choice(probability)