-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy path_strategy_utils.py
195 lines (158 loc) · 5.31 KB
/
_strategy_utils.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
"""Utilities used by various strategies."""
import itertools
from functools import lru_cache
from axelrod.action import Action
from axelrod.strategies.cooperator import Cooperator
from axelrod.strategies.defector import Defector
C, D = Action.C, Action.D
def detect_cycle(history, min_size=1, max_size=12, offset=0):
"""Detects cycles in the sequence history.
Mainly used by hunter strategies.
Parameters
----------
history: sequence of C and D
The sequence to look for cycles within
min_size: int, 1
The minimum length of the cycle
max_size: int, 12
The maximum length of the cycle
offset: int, 0
The amount of history to skip initially
Returns
-------
Tuple of C and D
The cycle detected in the input history
"""
history_tail = history[offset:]
new_max_size = min(len(history_tail) // 2, max_size)
for i in range(min_size, new_max_size + 1):
has_cycle = True
cycle = tuple(history_tail[:i])
for j, elem in enumerate(history_tail):
if elem != cycle[j % len(cycle)]:
has_cycle = False
break
if has_cycle:
return cycle
return None
def inspect_strategy(inspector, opponent):
"""Inspects the strategy of an opponent.
Simulate one round of play with an opponent, unless the opponent has
an inspection countermeasure.
Parameters
----------
inspector: Player
The player doing the inspecting
opponent: Player
The player being inspected
Returns
-------
Action
The action that would be taken by the opponent.
"""
if hasattr(opponent, "foil_strategy_inspection"):
return opponent.foil_strategy_inspection()
else:
return opponent.strategy(inspector)
def _limited_simulate_play(player_1, player_2, h1):
"""Simulates a player's move.
After inspecting player_2's next move (allowing player_2's strategy
method to set any internal variables as needed), update histories
for both players. Note that player_1's move is an argument.
If you need a more complete simulation, see `simulate_play` in
player.py. This function is specifically designed for the needs
of MindReader.
Parameters
----------
player_1: Player
The player whose move is already known.
player_2: Player
The player the we want to inspect.
h1: Action
The next action for first player.
"""
h2 = inspect_strategy(player_1, player_2)
player_1.update_history(h1, h2)
player_2.update_history(h2, h1)
def simulate_match(player_1, player_2, strategy, rounds=10):
"""Simulates a number of rounds with a constant strategy.
Parameters
----------
player_1: Player
The player that will have a constant strategy.
player_2: Player
The player we want to simulate.
strategy: Action
The constant strategy to use for first player.
rounds: int
The number of rounds to play.
"""
for match in range(rounds):
_limited_simulate_play(player_1, player_2, strategy)
def _calculate_scores(p1, p2, game):
"""Calculates the scores for two players based their history.
Parameters
----------
p1: Player
The first player.
p2: Player
The second player.
game: Game
Game object used to score rounds in the players' histories.
Returns
-------
int, int
The scores for the two input players.
"""
s1, s2 = 0, 0
for pair in zip(p1.history, p2.history):
score = game.score(pair)
s1 += score[0]
s2 += score[1]
return s1, s2
def look_ahead(player1, player2, game, rounds=10):
"""Returns a constant action that maximizes score by looking ahead.
Parameters
----------
player_1: Player
The player that will look ahead.
player_2: Player
The opponent that will be inspected.
game: Game
The Game object used to score rounds.
rounds: int
The number of rounds to look ahead.
Returns
-------
Action
The action that maximized score if it is played constantly.
"""
results = {}
possible_strategies = {C: Cooperator(), D: Defector()}
for action, player in possible_strategies.items():
# Instead of a deepcopy, create a new opponent and replay the history to it.
opponent_ = player2.clone()
if opponent_.classifier["stochastic"]:
opponent_.set_seed(player2._seed)
for h in player1.history:
_limited_simulate_play(player, opponent_, h)
# Now play forward with the constant strategy.
simulate_match(player, opponent_, action, rounds)
results[action] = _calculate_scores(player, opponent_, game)
return C if results[C] > results[D] else D
@lru_cache()
def recursive_thue_morse(n):
"""The recursive definition of the Thue-Morse sequence.
The first few terms of the Thue-Morse sequence are:
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 . . .
"""
if n == 0:
return 0
if n % 2 == 0:
return recursive_thue_morse(n / 2)
if n % 2 == 1:
return 1 - recursive_thue_morse((n - 1) / 2)
def thue_morse_generator(start=0):
"""A generator for the Thue-Morse sequence."""
for n in itertools.count(start):
yield recursive_thue_morse(n)