Skip to content

Commit b7b8ac5

Browse files
authored
Merge pull request #1 from Axelrod-Python/Chadys-802-vk
Chadys 802 vk
2 parents a10e2f0 + d70a2ac commit b7b8ac5

File tree

13 files changed

+112
-116
lines changed

13 files changed

+112
-116
lines changed

axelrod/_strategy_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from axelrod.player import update_history
66
from axelrod.actions import Actions
7+
from axelrod.strategies.cooperator import Cooperator
8+
from axelrod.strategies.defector import Defector
79

8-
from axelrod.strategies.cycler import Cycler
910

1011
C, D = Actions.C, Actions.D
1112

@@ -68,13 +69,13 @@ def calculate_scores(p1, p2, game):
6869
def look_ahead(player_1, player_2, game, rounds=10):
6970
"""Looks ahead for `rounds` and selects the next strategy appropriately."""
7071
results = []
71-
7272
# Simulate plays for `rounds` rounds
73+
players = {C: Cooperator(), D: Defector()}
7374
strategies = [C, D]
7475
for strategy in strategies:
7576
# Instead of a deepcopy, create a new opponent and play out the history
7677
opponent_ = player_2.clone()
77-
player_ = Cycler(strategy) # Either cooperator or defector
78+
player_ = players[strategy]
7879
for h1 in player_1.history:
7980
limited_simulate_play(player_, opponent_, h1)
8081

axelrod/strategies/darwin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Darwin(Player):
3333
classifier = {
3434
'memory_depth': float('inf'),
3535
'stochastic': False,
36-
'inspects_source': False,
36+
'inspects_source': True, # Checks to see if opponent is using simulated matches.
3737
'long_run_time': False,
3838
'makes_use_of': set(),
3939
'manipulates_source': False,

axelrod/strategies/hmm.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from numpy.random import choice
22

3-
from axelrod.actions import Actions
3+
from axelrod.actions import Actions , Action
44
from axelrod.player import Player
55
from axelrod.random_ import random_choice
66

77
C, D = Actions.C, Actions.D
88

9+
#Type Hinting has not finished yet
10+
#Lines 9 and 10 will be deleted
911

10-
def is_stochastic_matrix(m, ep=1e-8):
12+
def is_stochastic_matrix(m, ep=1e-8) -> bool:
1113
"""Checks that the matrix m (a list of lists) is a stochastic matrix."""
1214
for i in range(len(m)):
1315
for j in range(len(m[i])):
@@ -29,7 +31,7 @@ class SimpleHMM(object):
2931
"""
3032

3133
def __init__(self, transitions_C, transitions_D, emission_probabilities,
32-
initial_state):
34+
initial_state) -> None:
3335
"""
3436
Params
3537
------
@@ -43,7 +45,7 @@ def __init__(self, transitions_C, transitions_D, emission_probabilities,
4345
self.emission_probabilities = emission_probabilities
4446
self.state = initial_state
4547

46-
def is_well_formed(self):
48+
def is_well_formed(self) -> bool:
4749
"""
4850
Determines if the HMM parameters are well-formed:
4951
- Both matrices are stochastic
@@ -61,7 +63,7 @@ def is_well_formed(self):
6163
return False
6264
return True
6365

64-
def __eq__(self, other):
66+
def __eq__(self, other) -> bool:
6567
"""Equality of two HMMs"""
6668
check = True
6769
for attr in ["transitions_C", "transitions_D",
@@ -70,7 +72,7 @@ def __eq__(self, other):
7072
return check
7173

7274

73-
def move(self, opponent_action):
75+
def move(self, opponent_action) -> Action:
7476
"""Changes state and computes the response action.
7577
7678
Parameters
@@ -111,7 +113,7 @@ class HMMPlayer(Player):
111113

112114
def __init__(self, transitions_C=None, transitions_D=None,
113115
emission_probabilities=None, initial_state=0,
114-
initial_action=C):
116+
initial_action=C) -> None:
115117
super().__init__()
116118
if not transitions_C:
117119
transitions_C = [[1]]
@@ -126,7 +128,7 @@ def __init__(self, transitions_C=None, transitions_D=None,
126128
self.state = self.hmm.state
127129
self.classifier['stochastic'] = self.is_stochastic()
128130

129-
def is_stochastic(self):
131+
def is_stochastic(self) -> bool:
130132
"""Determines if the player is stochastic."""
131133
# If the transitions matrices and emission_probabilities are all 0 or 1
132134
# Then the player is stochastic
@@ -138,7 +140,7 @@ def is_stochastic(self):
138140
return True
139141
return False
140142

141-
def strategy(self, opponent):
143+
def strategy(self, opponent) -> Action:
142144
if len(self.history) == 0:
143145
return self.initial_action
144146
else:
@@ -148,7 +150,7 @@ def strategy(self, opponent):
148150
self.state = self.hmm.state
149151
return action
150152

151-
def reset(self):
153+
def reset(self) -> None:
152154
super().reset()
153155
self.hmm.state = self.initial_state
154156
self.state = self.hmm.state
@@ -175,7 +177,7 @@ class EvolvedHMM5(HMMPlayer):
175177
'manipulates_state': False
176178
}
177179

178-
def __init__(self):
180+
def __init__(self) -> None:
179181
initial_state = 3
180182
initial_action = C
181183
t_C = [[1, 0, 0, 0, 0],

axelrod/tests/strategies/test_better_and_better.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ def test_strategy(self):
2525

2626
self.first_play_test(D, seed=3) # C is very unlikely
2727
self.first_play_test(C, seed=1514) # first seed (starting from seed=0) that cooperates on first round
28-
self.versus_test(axelrod.Defector(), [(D, D), (D, D), (D, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)],
28+
self.versus_test(axelrod.Defector(),
29+
expected_actions=[(D, D), (D, D), (D, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)],
2930
seed=6)
30-
self.versus_test(axelrod.Cooperator(), [(D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C)],
31+
self.versus_test(axelrod.Cooperator(),
32+
expected_actions=[(D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C)],
3133
seed=8)
3234
actions = []
3335
for index in range(200):

axelrod/tests/strategies/test_calculator.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_twenty_rounds_joss_then_defects_for_cyclers(self):
3232
twenty_test_actions = get_joss_strategy_actions(twenty_alternator_actions, flip_indices)
3333

3434
expected_actions = twenty_test_actions + [(D, C), (D, D), (D, C), (D, D)]
35-
self.versus_test(axelrod.Alternator(), twenty_test_actions, seed=seed)
36-
self.versus_test(axelrod.Alternator(), expected_actions, seed=seed)
35+
self.versus_test(axelrod.Alternator(), expected_actions=twenty_test_actions, seed=seed)
36+
self.versus_test(axelrod.Alternator(), expected_actions=expected_actions, seed=seed)
3737

3838
def test_twenty_rounds_joss_then_tit_for_tat_for_non_cyclers(self):
3939
"""Uses axelrod.strategies.axelrod_first.Joss strategy for first 20 rounds"""
@@ -48,8 +48,9 @@ def test_twenty_rounds_joss_then_tit_for_tat_for_non_cyclers(self):
4848

4949
opponent_actions = twenty_non_cyclical_actions + subsequent_opponent_actions
5050
test_actions = twenty_test_actions + subsequent_test_actions
51-
self.versus_test(axelrod.MockPlayer(twenty_non_cyclical_actions), twenty_test_actions, seed=seed)
52-
self.versus_test(axelrod.MockPlayer(opponent_actions), test_actions, seed=seed)
51+
self.versus_test(axelrod.MockPlayer(twenty_non_cyclical_actions),
52+
expected_actions=twenty_test_actions, seed=seed)
53+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=test_actions, seed=seed)
5354

5455
def test_edge_case_calculator_sees_cycles_of_size_ten(self):
5556
seed = 3
@@ -59,7 +60,7 @@ def test_edge_case_calculator_sees_cycles_of_size_ten(self):
5960
ten_cycle_twenty_rounds = get_joss_strategy_actions(ten_length_cycle * 2, indices_to_flip=[16])
6061
opponent_actions = ten_length_cycle * 2 + [C, D, C]
6162
expected = ten_cycle_twenty_rounds + [(D, C), (D, D), (D, C)]
62-
self.versus_test(axelrod.MockPlayer(opponent_actions), expected, seed=seed)
63+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=expected, seed=seed)
6364

6465
def test_edge_case_calculator_ignores_cycles_gt_len_ten(self):
6566
seed = 3
@@ -71,7 +72,8 @@ def test_edge_case_calculator_ignores_cycles_gt_len_ten(self):
7172
self.assertEqual(detect_cycle(opponent_actions), tuple(eleven_length_cycle))
7273

7374
uses_tit_for_tat_after_twenty_rounds = twenty_rounds + [(D, C), (C, D)]
74-
self.versus_test(axelrod.MockPlayer(opponent_actions), uses_tit_for_tat_after_twenty_rounds, seed=seed)
75+
self.versus_test(axelrod.MockPlayer(opponent_actions),
76+
expected_actions=uses_tit_for_tat_after_twenty_rounds, seed=seed)
7577

7678
def attribute_equality_test(self, player, clone):
7779
"""Overwrite the default test to check Joss instance"""

axelrod/tests/strategies/test_cooperator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ def test_cooperates_in_first_three_rounds(self):
5656
against_defector = [(C, D)] * 3
5757
against_cooperator = [(C, C)] * 3
5858
against_alternator = [(C, C), (C, D), (C, C)]
59-
self.versus_test(axelrod.Defector(), against_defector)
60-
self.versus_test(axelrod.Cooperator(), against_cooperator)
61-
self.versus_test(axelrod.Alternator(), against_alternator)
59+
self.versus_test(axelrod.Defector(), expected_actions=against_defector)
60+
self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator)
61+
self.versus_test(axelrod.Alternator(), expected_actions=against_alternator)
6262

6363
def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten(self):
6464
against_cooperator = [(C, C)] * 3 + [(D, C)] * 20
65-
self.versus_test(axelrod.Cooperator(), against_cooperator)
65+
self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator)
6666

6767
def test_defects_when_opponent_has_no_defections_to_history_depth_ten(self):
6868
opponent_actions = [D] + [C] * 10 + [D, C]
6969
expected_actions = [(C, D)] + [(C, C)] * 10 + [(D, D), (C, C)]
70-
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions)
70+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=expected_actions)

axelrod/tests/strategies/test_cycler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_strategy(self):
4141
against_defector = list(zip(anticycler_rounds, [D] * num_elements))
4242
against_cooperator = list(zip(anticycler_rounds, [C] * num_elements))
4343

44-
self.versus_test(axelrod.Defector(), against_defector)
45-
self.versus_test(axelrod.Cooperator(), against_cooperator)
44+
self.versus_test(axelrod.Defector(), expected_actions=against_defector)
45+
self.versus_test(axelrod.Cooperator(), expected_actions=against_cooperator)
4646

4747

4848
class TestBasicCycler(TestPlayer):
@@ -68,7 +68,7 @@ def test_memory_depth_is_len_cycle_minus_one(self):
6868

6969
def test_cycler_works_as_expected(self):
7070
expected = [(C, D), (D, D), (D, D), (C, D)] * 2
71-
self.versus_test(axelrod.Defector(), expected, init_kwargs={'cycle': 'CDDC'})
71+
self.versus_test(axelrod.Defector(), expected_actions=expected, init_kwargs={'cycle': 'CDDC'})
7272

7373
def test_cycle_raises_value_error_on_bad_cycle_str(self):
7474
self.assertRaises(ValueError, Cycler, 'CdDC')
@@ -95,7 +95,7 @@ def test_strategy(self):
9595
match_len = 20
9696
actions_generator = _get_actions_cycle_against_cooperator(cycle_str)
9797
test_actions = [next(actions_generator) for _ in range(match_len)]
98-
self.versus_test(axelrod.Cooperator(), test_actions)
98+
self.versus_test(axelrod.Cooperator(), expected_actions=test_actions)
9999

100100
return TestCyclerChild
101101

axelrod/tests/strategies/test_darwin.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,41 @@ class TestDarwin(TestPlayer):
1515
'stochastic': False,
1616
'makes_use_of': set(),
1717
'long_run_time': False,
18-
'inspects_source': False,
18+
'inspects_source': True,
1919
'manipulates_source': False,
2020
'manipulates_state': True
2121
}
2222

23+
def setUp(self):
24+
"""Each test starts with a fresh genome."""
25+
self.player.genome = [C]
26+
super(TestDarwin, self).setUp()
27+
28+
def test_setup(self):
29+
player = self.player()
30+
self.assertEqual(player.genome, [C])
31+
self.assertEqual(player.history, [])
32+
2333
def test_strategy(self):
2434
p1 = self.player()
25-
p2 = axelrod.Cooperator()
26-
self.assertEqual(p1.strategy(p2), C) # Always cooperate first.
27-
for i in range(10):
28-
p1.play(p2)
29-
self.assertEqual(p1.strategy(p2), C)
35+
p1.reset()
36+
self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 5, attrs={'genome': [C] * 5})
3037

31-
p1 = self.player()
32-
p2 = axelrod.Defector()
33-
self.assertEqual(p1.strategy(p2), C) # Always cooperate first.
34-
for i in range(10):
35-
p1.play(p2)
36-
self.assertEqual(p1.strategy(p2), C)
38+
expected_genome = [D] * 4 + [C]
39+
self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] * 5, attrs={'genome': expected_genome})
40+
41+
# uses genome
42+
expected_actions = [(C, C)] + [(D, C)] * 3 + [(C, C)] * 2
43+
self.versus_test(axelrod.Cooperator(), expected_actions)
44+
45+
def test_against_geller_and_mindreader(self):
46+
self.versus_test(axelrod.GellerCooperator(), expected_actions=[(C, C)] * 2, attrs={'genome': [C, C]})
47+
48+
self.versus_test(axelrod.MindReader(), expected_actions=[(C, D)] * 2, attrs={'genome': [D, C]})
3749

3850
def test_play(self):
3951
"""valid_callers must contain at least one entry..."""
40-
self.assertTrue(len(self.player.valid_callers)>0)
52+
self.assertTrue(len(self.player.valid_callers) > 0)
4153
"""...and should allow round_robin.play to call"""
4254
self.assertTrue("play" in self.player.valid_callers)
4355
self.play()
@@ -52,15 +64,23 @@ def play(self):
5264
# Genome contains only valid responses.
5365
self.assertEqual(p1.genome.count(C) + p1.genome.count(D), len(p1.genome))
5466

55-
def test_reset(self):
56-
"""Is instance correctly reset between rounds"""
67+
def test_reset_only_resets_first_move_of_genome(self):
68+
self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4)
69+
5770
p1 = self.player()
71+
self.assertEqual(p1.genome, [D, C, C, C, D])
5872
p1.reset()
5973
self.assertEqual(p1.history, [])
60-
self.assertEqual(p1.genome[0], C)
74+
self.assertEqual(p1.genome, [C, C, C, C, D])
6175

62-
def test_unique_genome(self):
63-
"""Ensure genome remains unique class property"""
76+
def test_all_darwin_instances_share_one_genome(self):
6477
p1 = self.player()
6578
p2 = self.player()
66-
self.assertTrue(p1.genome is p2.genome)
79+
self.assertIs(p1.genome, p2.genome)
80+
81+
self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4)
82+
83+
self.assertEqual(p2.genome, [D, C, C, C, D])
84+
self.assertIs(p1.genome, p2.genome)
85+
p3 = self.player()
86+
self.assertIs(p3.genome, p2.genome)

axelrod/tests/strategies/test_defector.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,18 @@ def test_strategy(self):
4444
# Starts by defecting.
4545
self.first_play_test(D)
4646
self.second_play_test(D, D, D, D)
47-
# Test if strategy tries to trick opponent.
48-
self.responses_test([D], [C, C, C], [C, C, C])
49-
self.responses_test([D], [C, C, C, D, D], [C, C, C, C, D])
50-
history = [C, C, C, D, D] + [C] * 11
51-
opponent_history = [C, C, C, C, D] + [D] + [C] * 10
52-
self.responses_test([D], history, opponent_history)
47+
48+
def test_cooperates_if_opponent_history_has_C_and_last_three_are_D(self):
49+
opponent_actions = [D, C] + [D] * 5
50+
actions = [(D, D), (D, C)] + [(D, D)] * 3 + [(C, D)] * 2
51+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=actions)
52+
53+
def test_defects_if_opponent_never_cooperated(self):
54+
opponent_actions = [D] * 7
55+
actions = [(D, D)] * 7
56+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=actions)
57+
58+
def test_defects_if_opponent_last_three_are_not_D(self):
59+
opponent_actions = [C] + [D] * 3 + [C, D]
60+
actions = [(D, C)] + [(D, D)] * 3 + [(C, C), (D, D)]
61+
self.versus_test(axelrod.MockPlayer(opponent_actions), expected_actions=actions)

axelrod/tests/strategies/test_doubler.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ def test_strategy(self):
2424
# Starts by cooperating
2525
self.first_play_test(C)
2626

27-
# Defects when the opponent has defected and
28-
# the opponent's cooperation count
29-
# is less than twice their defection count
30-
self.responses_test([D], [C], [D])
31-
self.responses_test([D], [D, D], [D, D])
32-
self.responses_test([D], [C, D], [C, D])
33-
self.responses_test([D], [C, C, D], [C, C, D])
34-
35-
# Cooperates otherwise
36-
self.responses_test([C], [C], [C])
37-
self.responses_test([C], [C, C, C, C], [C, C, C, D])
27+
def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperations(self):
28+
opponent_plays = [C] * 7 + [D] * 4 + [C]
29+
actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, C)]
30+
self.versus_test(axelrod.MockPlayer(opponent_plays), expected_actions=actions)
31+
32+
def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperations(self):
33+
opponent_plays = [C] * 8 + [D] * 4 + [C]
34+
actions = [(C, C)] * 8 + [(C, D)] * 4 + [(D, C)]
35+
self.versus_test(axelrod.MockPlayer(opponent_plays), expected_actions=actions)
36+
37+
def test_cooperates_if_opponent_last_play_is_C(self):
38+
opponent_first_five = [D] * 5
39+
actions_first_five = [(C, D)] + [(D, D)] * 4
40+
opponent_plays = opponent_first_five + [C] + [D]
41+
actions = actions_first_five + [(D, C)] + [(C, D)]
42+
self.versus_test(axelrod.MockPlayer(opponent_plays), expected_actions=actions)

0 commit comments

Comments
 (0)