Skip to content

Commit e175c73

Browse files
Fix failing tests rebased for the dev branch (#1372)
* Added new SpitefulCC Player to grudger.py, updated the list of all strategies and added coverage test in test_grudger.py * changed 236 strategies to 237 in docs/index.rst * changed SpitefulCC classifier and added strategy description * changed description spitefulCC strategy * Fix doctest. This was failing on #1364 but the fix is trivial. * Correct expected actions for meta strategy. * Run black and isort. Co-authored-by: Laura Romero <[email protected]>
1 parent 7cbe2d7 commit e175c73

30 files changed

+231
-84
lines changed

axelrod/ecosystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(
3333
population: List[int] = None,
3434
) -> None:
3535
"""Create a new ecosystem.
36-
36+
3737
Parameters
3838
----------
3939
results: ResultSet
@@ -83,7 +83,7 @@ def __init__(
8383

8484
def reproduce(self, turns: int):
8585
"""Reproduce populations according to the payoff matrix.
86-
86+
8787
Parameters
8888
----------
8989
turns: int

axelrod/game.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(
2020
self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1
2121
) -> None:
2222
"""Create a new game object.
23-
23+
2424
Parameters
2525
----------
2626
r: int or float

axelrod/graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99

1010
class Graph(object):
1111
"""Weighted and directed graph class.
12-
12+
1313
This class is intended for the graph associated to a Markov process,
1414
since it gives easy access to the neighbors of a particular state.
1515
1616
Vertices can be any hashable Python object.
17-
17+
1818
Initialize with a list of edges:
1919
[[node1, node2, weights], ...]
2020
Weights can be omitted for an undirected graph.
2121
2222
For efficiency, neighbors are cached in dictionaries. Undirected
2323
graphs are implemented as directed graphs in which every edge (s, t)
2424
has the opposite edge (t, s).
25-
25+
2626
Attributes
2727
----------
2828
directed: Boolean indicating whether the graph is directed
@@ -31,7 +31,7 @@ class Graph(object):
3131
all tails to their edge weights (None means no weight)
3232
in_mapping: a dictionary mapping all tails to dictionaries that map
3333
all heads to their edge weights (none means to weight)
34-
34+
3535
Properties
3636
----------
3737
vertices: the set of vertices in the graph

axelrod/load_data_.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ def load_pso_tables(filename="pso_gambler.csv", directory="data"):
5656
rows = load_file(filename, directory)
5757
d = dict()
5858
for row in rows:
59-
name, a, b, c, = str(row[0]), int(row[1]), int(row[2]), int(row[3])
59+
name, a, b, c, = (
60+
str(row[0]),
61+
int(row[1]),
62+
int(row[2]),
63+
int(row[3]),
64+
)
6065
values = list(map(float, row[4:]))
6166
d[(name, int(a), int(b), int(c))] = values
6267
return d

axelrod/result_set.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ def _reshape_out(
152152
"DD to C count",
153153
"DD to D count",
154154
]
155-
self.state_to_action_distribution = self._build_state_to_action_distribution(
156-
sum_per_player_opponent_df[columns]
155+
self.state_to_action_distribution = (
156+
self._build_state_to_action_distribution(
157+
sum_per_player_opponent_df[columns]
158+
)
157159
)
158160
self.normalised_state_to_action_distribution = (
159161
self._build_normalised_state_to_action_distribution()

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
GrudgerAlternator,
143143
OppositeGrudger,
144144
SoftGrudger,
145+
SpitefulCC,
145146
)
146147
from .grumpy import Grumpy
147148
from .handshake import Handshake
@@ -467,6 +468,7 @@
467468
SolutionB1,
468469
SolutionB5,
469470
SpitefulTitForTat,
471+
SpitefulCC,
470472
Stalker,
471473
StochasticCooperator,
472474
StochasticWSLS,

axelrod/strategies/cooperator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def strategy(self, opponent: Player) -> Action:
5959
After 3 rounds, if opponent has not defected to a max history depth of
6060
10, defect.
6161
"""
62-
if self._has_played_enough_rounds_to_be_tricky() and self._opponents_has_cooperated_enough_to_be_tricky(
63-
opponent
62+
if (
63+
self._has_played_enough_rounds_to_be_tricky()
64+
and self._opponents_has_cooperated_enough_to_be_tricky(opponent)
6465
):
6566
return D
6667
return C

axelrod/strategies/grudger.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def __init__(self) -> None:
167167
self.grudge_memory = 0
168168

169169
def strategy(self, opponent: Player) -> Action:
170-
"""Begins by playing C, then plays D, D, D, D, C, C against a defection
171-
"""
170+
"""Begins by playing C, then plays D, D, D, D, C, C against a defection"""
172171
if self.grudged:
173172
strategy = [D, D, D, C, C][self.grudge_memory]
174173
self.grudge_memory += 1
@@ -310,3 +309,35 @@ def strategy(self, opponent: Player) -> Action:
310309

311310
def __repr__(self) -> str:
312311
return "%s: n=%s,d=%s,c=%s" % (self.name, self.n, self.d, self.c)
312+
313+
314+
class SpitefulCC(Player):
315+
"""
316+
Behaves like Grudger after cooperating for 2 turns
317+
318+
Names:
319+
320+
- spiteful_cc: [Mathieu2015]_
321+
"""
322+
323+
name = "SpitefulCC"
324+
classifier = {
325+
"memory_depth": float("inf"), # Long memory
326+
"stochastic": False,
327+
"long_run_time": False,
328+
"inspects_source": False,
329+
"manipulates_source": False,
330+
"manipulates_state": False,
331+
}
332+
333+
@staticmethod
334+
def strategy(opponent: Player) -> Action:
335+
"""
336+
Cooperates until the oponent defects, then defects forever.
337+
Always cooperates twice at the start.
338+
"""
339+
if len(opponent.history) < 2:
340+
return C
341+
elif opponent.defections:
342+
return D
343+
return C

axelrod/strategies/human.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ def _status_messages(self):
118118
if PROMPT2
119119
else lambda cli: [(token_toolbar, self._history_toolbar())]
120120
)
121-
print_statement = "{}Turn {}: {} played {}, opponent played {}".format(
122-
linesep,
123-
len(self.history),
124-
self.human_name,
125-
self.symbols[self.history[-1]],
126-
self.symbols[self.history.coplays[-1]],
121+
print_statement = (
122+
"{}Turn {}: {} played {}, opponent played {}".format(
123+
linesep,
124+
len(self.history),
125+
self.human_name,
126+
self.symbols[self.history[-1]],
127+
self.symbols[self.history.coplays[-1]],
128+
)
127129
)
128130
else:
129131
toolbar = None

axelrod/strategies/lookerup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ def mutate(self):
535535
if r < self.mutation_probability:
536536
initial_actions[i] = initial_actions[i].flip()
537537
return self.create_new(
538-
lookup_dict=lookup_dict, initial_actions=tuple(initial_actions),
538+
lookup_dict=lookup_dict,
539+
initial_actions=tuple(initial_actions),
539540
)
540541

541542
def crossover(self, other):

0 commit comments

Comments
 (0)