Skip to content

Commit 82a22ca

Browse files
committed
Various updates following Nik's review
1 parent 23a7cf4 commit 82a22ca

14 files changed

+39
-41
lines changed

axelrod/strategies/axelrod_second.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class SecondByGrofman(Player):
433433
- If its own previous move was D and the opponent has defected more than
434434
once in the last 8\* rounds, defect
435435
436-
\* The code looks at the first 7 of the last 8 rounds, ignoring the most
436+
The code looks at the first 7 of the last 8 rounds, ignoring the most
437437
recent round.
438438
439439
Names:
@@ -1530,7 +1530,7 @@ class SecondByWhite(Player):
15301530
Strategy submitted to Axelrod's second tournament by Edward C White (K72R)
15311531
and came in thirteenth in that tournament.
15321532
1533-
* Cooperate in the first ten turns
1533+
* Cooperate in the first ten turns.
15341534
* If the opponent Cooperated last turn then Cooperate.
15351535
* Otherwise Defect if and only if:
15361536
floor(log(turn)) * opponent Defections >= turn

axelrod/strategies/calculator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self) -> None:
3232
self.joss_instance = Joss()
3333
super().__init__()
3434

35-
def set_seed(self, seed=None):
35+
def set_seed(self, seed: int = None):
3636
super().set_seed(seed)
3737
self.joss_instance.set_seed(seed)
3838

axelrod/strategies/finite_state_machines.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import itertools
2-
from typing import Any, Dict, Sequence, Text, Tuple
2+
from typing import Any, Dict, List, Sequence, Text, Tuple
33

44
from axelrod.action import Action
55
from axelrod.evolvable_player import (
@@ -200,7 +200,7 @@ def random_params(self, num_states: int) -> Tuple[Tuple[Transition, ...], int, A
200200
initial_action = self._random.choice(actions)
201201
return tuple(rows), initial_state, initial_action
202202

203-
def mutate_rows(self, rows, mutation_probability):
203+
def mutate_rows(self, rows: List[List], mutation_probability: float):
204204
rows = list(rows)
205205
randoms = self._random.random(len(rows))
206206
# Flip each value with a probability proportional to the mutation rate
@@ -239,7 +239,7 @@ def mutate(self):
239239
initial_action=initial_action,
240240
)
241241

242-
def crossover_rows(self, rows1, rows2):
242+
def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]:
243243
num_states = len(rows1) // 2
244244
cross_point = 2 * self._random.randint(0, num_states)
245245
new_rows = copy_lists(rows1[:cross_point])

axelrod/strategies/gambler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ def __init__(
8989
# The mutate and crossover methods are mostly inherited from EvolvableLookerUp, except for the following
9090
# modifications.
9191

92-
def random_value(self):
92+
def random_value(self) -> float:
9393
return self._random.random()
9494

95-
def mutate_value(self, value):
95+
def mutate_value(self, value: float) -> float:
9696
ep = self._random.uniform(-1, 1) / 4
9797
value += ep
9898
if value < 0:

axelrod/strategies/memorytwo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MemoryTwoPlayer(Player):
5353
}
5454

5555
def __init__(
56-
self, sixteen_vector: Tuple[float, ...] = None, initial: Optional[Tuple[Action, Action]] = None
56+
self, sixteen_vector: Optional[Tuple[float, ...]] = None, initial: Optional[Tuple[Action, Action]] = None
5757
) -> None:
5858
"""
5959
Parameters
@@ -69,14 +69,14 @@ def __init__(
6969
self._initial = initial
7070
self.set_initial_sixteen_vector(sixteen_vector)
7171

72-
def set_initial_sixteen_vector(self, sixteen_vector):
72+
def set_initial_sixteen_vector(self, sixteen_vector: Optional[Tuple[float, ...]]):
7373
if sixteen_vector is None:
7474
sixteen_vector = tuple([1] * 16)
7575
warnings.warn("Memory two player is set to default, Cooperator.")
7676

7777
self.set_sixteen_vector(sixteen_vector)
7878

79-
def set_sixteen_vector(self, sixteen_vector: Tuple):
79+
def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]):
8080
if not all(0 <= p <= 1 for p in sixteen_vector):
8181
raise ValueError(
8282
"An element in the probability vector, {}, is not "
@@ -92,7 +92,7 @@ def set_sixteen_vector(self, sixteen_vector: Tuple):
9292
) # type: Dict[tuple, float]
9393

9494
@staticmethod
95-
def compute_memory_depth(sixteen_vector):
95+
def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> int:
9696
values = set(list(sixteen_vector.values()))
9797

9898
# Memory-depth 0

axelrod/tests/property.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def prob_end_tournaments(
153153
min_repetitions : integer
154154
The minimum number of repetitions
155155
max_repetitions : integer
156-
The maximum number of
156+
The maximum number of repetitions
157157
seed : integer
158158
Random seed
159159
"""

axelrod/tests/strategies/test_averagecopier.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def test_defect_if_opponent_always_defects2(self):
4040
self.versus_test(axl.Defector(), expected_actions=actions, seed=2)
4141

4242
def test_random_behavior1(self):
43-
# Variable behaviour based on the history and stochastic
4443
actions = [
4544
(C, C),
4645
(C, D),
@@ -118,17 +117,16 @@ class TestNiceAverageCopier(TestPlayer):
118117
}
119118

120119
def test_cooperate_if_opponent_always_cooperates(self):
121-
# Tests that if opponent has played all C then player chooses C.
120+
"""Tests that if opponent has played all C then player chooses C."""
122121
actions = [(C, C)] * 10
123122
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1)
124123

125124
def test_defect_if_opponent_always_defects(self):
126-
# Tests that if opponent has played all D then player chooses D.
125+
"""Tests that if opponent has played all D then player chooses D."""
127126
actions = [(C, D)] + [(D, D)] * 9
128127
self.versus_test(axl.Defector(), expected_actions=actions, seed=1)
129128

130129
def test_random_behavior1(self):
131-
# Variable behaviour based on the history and stochastic behaviour
132130
actions = [
133131
(C, C),
134132
(C, D),

axelrod/tests/strategies/test_axelrod_first.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_cooperate_first_ten_rounds3(self):
3838

3939
def test_retaliation_after_ten_rounds1(self):
4040
"""If opponent defects at any point then the player will defect forever
41-
(after 10 rounds)"""
41+
(after 10 rounds)."""
4242
opponent = axl.MockPlayer(actions=[C] * 10 + [D])
4343
actions = [(C, C)] * 10 + [(C, D), (D, C)]
4444
self.versus_test(opponent, expected_actions=actions)

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ class TestChampion(TestPlayer):
2222

2323
def test_initial_rounds(self):
2424
# Cooperates for first 10 rounds
25-
actions = [(C, D)] * 10 # Cooperate for ten rounds
25+
actions = [(C, D)] * 10
2626
self.versus_test(axl.Defector(), expected_actions=actions)
2727
# Mirror partner for next phase
28-
actions += [(D, D)] * 7 # Mirror opponent afterwards
28+
actions += [(D, D)] * 7
2929
self.versus_test(axl.Defector(), expected_actions=actions)
3030

3131
def test_cooperate_until_defect(self):
32-
actions = [(C, C), (C, D)] * 5 # Cooperate for ten rounds
33-
actions += [(D, C), (C, D)] * 7 # Mirror opponent afterwards
32+
# Cooperates for first 10 rounds
33+
actions = [(C, C), (C, D)] * 5
34+
# Mirror partner for next phase
35+
actions += [(D, C), (C, D)] * 7
3436
# Cooperate unless the opponent defected, has defected at least 40% of
3537
actions += [(D, C), (C, D), (C, C), (C, D)] * 3
3638
self.versus_test(axl.Alternator(), expected_actions=actions, seed=2)

axelrod/tests/strategies/test_cycler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class TestAntiCycler(TestPlayer):
3030
}
3131

3232
def test_has_no_cycles(self):
33-
# test_range = 100
3433
player = axl.AntiCycler()
3534
opponent = axl.Cooperator()
3635
match = axl.Match((player, opponent), turns=100)

0 commit comments

Comments
 (0)