-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathgambler.py
240 lines (184 loc) · 6.29 KB
/
gambler.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
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
"""Stochastic variants of Lookup table based-strategies, trained with particle
swarm algorithms.
For the original see:
https://gist.github.com/GDKO/60c3d0fd423598f3c4e4
"""
from typing import Any
from axelrod.action import Action, actions_to_str, str_to_actions
from axelrod.load_data_ import load_pso_tables
from axelrod.player import Player
from .lookerup import (
EvolvableLookerUp,
LookerUp,
LookupTable,
Plays,
create_lookup_table_keys,
)
C, D = Action.C, Action.D
tables = load_pso_tables("pso_gambler.csv", directory="data")
class Gambler(LookerUp):
"""
A stochastic version of LookerUp which will select randomly an action in
some cases.
Names:
- Gambler: Original name by Georgios Koutsovoulos
"""
name = "Gambler"
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."""
actions_or_float = super(Gambler, self).strategy(opponent)
if isinstance(actions_or_float, Action):
return actions_or_float
return self._random.random_choice(actions_or_float)
class EvolvableGambler(Gambler, EvolvableLookerUp):
name = "EvolvableGambler"
def __init__(
self,
lookup_dict: dict = None,
initial_actions: tuple = None,
pattern: Any = None, # pattern is str or tuple of Actions.
parameters: Plays = None,
mutation_probability: float = None,
seed: int = None,
) -> None:
EvolvableLookerUp.__init__(
self,
lookup_dict=lookup_dict,
initial_actions=initial_actions,
pattern=pattern,
parameters=parameters,
mutation_probability=mutation_probability,
seed=seed,
)
self.pattern = list(self.pattern)
Gambler.__init__(
self,
lookup_dict=self.lookup_dict,
initial_actions=self.initial_actions,
pattern=self.pattern,
parameters=self.parameters,
)
self.overwrite_init_kwargs(
lookup_dict=self.lookup_dict,
initial_actions=self.initial_actions,
pattern=self.pattern,
parameters=self.parameters,
mutation_probability=self.mutation_probability,
)
# The mutate and crossover methods are mostly inherited from EvolvableLookerUp, except for the following
# modifications.
def random_value(self) -> float:
return self._random.random()
def mutate_value(self, value: float) -> float:
ep = self._random.uniform(-1, 1) / 4
value += ep
if value < 0:
value = 0
elif value > 1:
value = 1
return value
def receive_vector(self, vector):
"""Receives a vector and updates the player's pattern. Ignores extra parameters."""
self.pattern = vector
self_depth, op_depth, op_openings_depth = self.parameters
self._lookup = LookupTable.from_pattern(
self.pattern, self_depth, op_depth, op_openings_depth
)
def create_vector_bounds(self):
"""Creates the bounds for the decision variables. Ignores extra parameters."""
size = len(self.pattern)
lb = [0.0] * size
ub = [1.0] * size
return lb, ub
class PSOGamblerMem1(Gambler):
"""
A 1x1x0 PSOGambler trained with pyswarm. This is the 'optimal' memory one
strategy trained against the set of short run time strategies in the
Axelrod library.
Names:
- PSO Gambler Mem1: Original name by Marc Harper
"""
name = "PSO Gambler Mem1"
def __init__(self) -> None:
pattern = tables[("PSO Gambler Mem1", 1, 1, 0)]
parameters = Plays(self_plays=1, op_plays=1, op_openings=0)
super().__init__(parameters=parameters, pattern=pattern)
class PSOGambler1_1_1(Gambler):
"""
A 1x1x1 PSOGambler trained with pyswarm.
Names:
- PSO Gambler 1_1_1: Original name by Marc Harper
"""
name = "PSO Gambler 1_1_1"
def __init__(self) -> None:
pattern = tables[("PSO Gambler 1_1_1", 1, 1, 1)]
parameters = Plays(self_plays=1, op_plays=1, op_openings=1)
super().__init__(parameters=parameters, pattern=pattern)
class PSOGambler2_2_2(Gambler):
"""
A 2x2x2 PSOGambler trained with a particle swarm algorithm (implemented in
pyswarm). Original version by Georgios Koutsovoulos.
Names:
- PSO Gambler 2_2_2: Original name by Marc Harper
"""
name = "PSO Gambler 2_2_2"
def __init__(self) -> None:
pattern = tables[("PSO Gambler 2_2_2", 2, 2, 2)]
parameters = Plays(self_plays=2, op_plays=2, op_openings=2)
super().__init__(parameters=parameters, pattern=pattern)
class PSOGambler2_2_2_Noise05(Gambler):
"""
A 2x2x2 PSOGambler trained with pyswarm with noise=0.05.
Names:
- PSO Gambler 2_2_2 Noise 05: Original name by Marc Harper
"""
name = "PSO Gambler 2_2_2 Noise 05"
def __init__(self) -> None:
pattern = tables[("PSO Gambler 2_2_2 Noise 05", 2, 2, 2)]
parameters = Plays(self_plays=2, op_plays=2, op_openings=2)
super().__init__(parameters=parameters, pattern=pattern)
class ZDMem2(Gambler):
"""
A memory two generalization of a zero determinant player.
Names:
- ZDMem2: Original name by Marc Harper
- Unnamed [LiS2014]_
"""
name = "ZD-Mem2"
classifier = {
"memory_depth": 2,
"stochastic": True,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def __init__(self) -> None:
pattern = [
11 / 12,
4 / 11,
7 / 9,
1 / 10,
5 / 6,
3 / 11,
7 / 9,
1 / 10,
2 / 3,
1 / 11,
7 / 9,
1 / 10,
3 / 4,
2 / 11,
7 / 9,
1 / 10,
]
parameters = Plays(self_plays=2, op_plays=2, op_openings=0)
super().__init__(parameters=parameters, pattern=pattern)