-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
worse and worse refactor new testing framework #927
Changes from 3 commits
1e62e64
d744d2e
7806580
deb0c2b
40b3685
9c42a4a
b1fc5d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
"""Tests for the WorseAndWorse strategies.""" | ||
|
||
import axelrod | ||
from .test_player import TestPlayer | ||
from test_player import TestPlayer | ||
|
||
C, D = axelrod.Actions.C, axelrod.Actions.D | ||
|
||
|
||
class TestWorseAndWorse(TestPlayer): | ||
|
||
name = "Worse and Worse" | ||
|
@@ -19,14 +18,22 @@ class TestWorseAndWorse(TestPlayer): | |
'manipulates_source': False, | ||
'manipulates_state': False | ||
} | ||
|
||
def test_strategy(self): | ||
"""Test that the strategy gives expected behaviour.""" | ||
self.first_play_test(C) | ||
self.responses_test([C] * 6 + [D] + [C] * 3, seed=8) | ||
self.responses_test([C] * 4 + [D] + [C] * 3, [C, C], [D, D], seed=8) | ||
|
||
# 6 Rounds Cooperate given seed | ||
actions = [(C, C)] * 6 + [(D, C)] + [(C, C)] * 3 | ||
self.versus_test(axelrod.Cooperator(), expected_actions=actions, | ||
seed=8) | ||
|
||
# 6 Rounds Cooperate and Defect no matter oponent | ||
actions = [(C, D)] * 6 + [(D, D)] + [(C, D)] * 3 | ||
self.versus_test(axelrod.Defector(), expected_actions=actions, | ||
seed=8) | ||
|
||
|
||
class TestWorseAndWorseRandom(TestPlayer): | ||
|
||
name = "Knowledgeable Worse and Worse" | ||
|
@@ -44,42 +51,23 @@ class TestWorseAndWorseRandom(TestPlayer): | |
def test_strategy(self): | ||
"""Test that the strategy gives expected behaviour.""" | ||
self.first_play_test(C) | ||
# self.responses_test([C] + [D] * 4, seed=1) | ||
# self.responses_test([C] * 4 + [D] + [C] * 3, [C, C], | ||
# [D, D], seed=1) | ||
|
||
axelrod.seed(1) | ||
opponent = axelrod.Cooperator() | ||
player = axelrod.KnowledgeableWorseAndWorse() | ||
match = axelrod.Match((opponent, player), turns=5) | ||
self.assertEqual(match.play(), [('C', 'C'), | ||
('C', 'D'), | ||
('C', 'D'), | ||
('C', 'D'), | ||
('C', 'D')]) | ||
|
||
actions = [(C, C)] + [(D, C)] * 4 | ||
self.versus_test(axelrod.Cooperator(), expected_actions = \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change this and similar lines to be: self.versus_test(axelrod.Cooperator(), expected_actions = actions,
match_attributes={"length":5}, seed=1) |
||
actions, match_attributes={"length":5}, seed=1) | ||
|
||
# Test that behaviour does not depend on opponent | ||
opponent = axelrod.Defector() | ||
player = axelrod.KnowledgeableWorseAndWorse() | ||
axelrod.seed(1) | ||
match = axelrod.Match((opponent, player), turns=5) | ||
self.assertEqual(match.play(), [('D', 'C'), | ||
('D', 'D'), | ||
('D', 'D'), | ||
('D', 'D'), | ||
('D', 'D')]) | ||
|
||
# Test that behaviour changes when does not know length. | ||
axelrod.seed(1) | ||
match = axelrod.Match((opponent, player), turns=5, | ||
match_attributes={'length': float('inf')}) | ||
self.assertEqual(match.play(), [('D', 'C'), | ||
('D', 'C'), | ||
('D', 'C'), | ||
('D', 'C'), | ||
('D', 'C')]) | ||
actions = [(C, D)] + [(D, D)] * 4 | ||
self.versus_test(axelrod.Defector(), expected_actions = \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same request about |
||
actions, match_attributes={"length":5}, seed=1) | ||
|
||
# Test that behaviour changes when does not know length. | ||
actions = [(C, C), (C, D), (C, C), (C, D), (C, C)] | ||
self.versus_test(axelrod.Alternator(), expected_actions = \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same request. |
||
actions, match_attributes= | ||
{"length":-1}, seed=1) | ||
|
||
|
||
class TestWorseAndWorse2(TestPlayer): | ||
|
||
name = "Worse and Worse 2" | ||
|
@@ -96,20 +84,32 @@ class TestWorseAndWorse2(TestPlayer): | |
|
||
def test_strategy(self): | ||
"""Test that the strategy gives expected behaviour.""" | ||
|
||
# Test that first move is C | ||
self.first_play_test('C') | ||
# Test that given a history, next move matches opponent (round <= 20) | ||
self.responses_test([C], [C], [C]) | ||
self.responses_test([D], [C, C], [C, D]) | ||
self.responses_test([C], [C] * 19, [C] * 19) | ||
self.responses_test([D], [C] * 19, [C] * 18 + [D]) | ||
# Test that after round 20, strategy follows stochastic behaviour given | ||
# a seed | ||
self.responses_test([C, D, C, C, C, C, D, C, C, C], [C] * 20, [C] * 20, | ||
seed=8) | ||
self.responses_test([D, D, C, C, D, C, C, C, C, C], [C] * 20, [C] * 20, | ||
seed=2) | ||
|
||
# Test next move matches opponent | ||
actions = [(C, C)] * 19 | ||
self.versus_test(axelrod.Cooperator(), expected_actions \ | ||
= actions) | ||
|
||
actions = [(C, C), (C, C), (C, D),(D, C)] | ||
self.versus_test(opponent=axelrod.MockPlayer([C,C,D,C]), \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment |
||
expected_actions=actions) | ||
|
||
actions = [(C, C)] * 18 + [(C, D), (D, C)] | ||
self.versus_test(opponent=axelrod.MockPlayer([C] * 18 + \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment |
||
[D, C]), expected_actions=actions) | ||
|
||
# After round 20, strategy follows stochastic behavior given a seed | ||
actions = [(C, C)] * 20 + [(C, D), (D, C), (C, C), (C, D)] | ||
self.versus_test(opponent=axelrod.MockPlayer([C] * 20 + [D, C, C, D]), | ||
expected_actions=actions, seed=8) | ||
|
||
actions = [(C, C)] * 20 + [(D, D), (D, C)] + [(C, C)] * 2 + [(D, C)] | ||
self.versus_test(opponent=axelrod.MockPlayer([C] * 20 + [D, C, C, C]), | ||
expected_actions=actions, seed=2) | ||
|
||
|
||
class TestWorseAndWorse3(TestPlayer): | ||
|
||
|
@@ -127,15 +127,25 @@ class TestWorseAndWorse3(TestPlayer): | |
|
||
def test_strategy(self): | ||
"""Test that the strategy gives expected behaviour.""" | ||
|
||
# Test that first move is C | ||
self.first_play_test(C) | ||
|
||
# Test that if opponent only defects, strategy also defects | ||
self.responses_test([D], [D] * 5, [D] * 5) | ||
actions = [(C, D)] + [(D, D)] * 4 | ||
self.versus_test(axelrod.Defector(), expected_actions=actions) | ||
|
||
# Test that if opponent only cooperates, strategy also cooperates | ||
self.responses_test([C], [C] * 5, [C] * 5) | ||
actions = [(C, C)] * 5 | ||
self.versus_test(axelrod.Cooperator(), expected_actions=actions) | ||
|
||
# Test that given a non 0/1 probability of defecting, strategy follows | ||
# stochastic behaviour, given a seed | ||
self.responses_test([D, C, C, D, C, C, C, C, C, C], [C] * 5, | ||
[C, D, C, D, C], seed=8) | ||
self.responses_test([D, D, D, C, C, D, D, D, C, C], [C] * 5, [D] * 5, | ||
seed=2) | ||
|
||
actions = [(C, C), (C, D), (C, C), (D, D), (C, C), (D, C)] | ||
self.versus_test(axelrod.MockPlayer([C,D,C,D,C]), | ||
expected_actions=actions, seed=8) | ||
|
||
actions = [(D, D)] * 3 + [(C, D)] * 2 + [(D, D)] * 3 + [(C, C)] | ||
self.versus_test(axelrod.MockPlayer([D] * 5), expected_actions = \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
actions, seed=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests are currently failing because of this change. You need to change it back to:
If you wanted to run the tests locally you can do this by typing the following at the command line from the root of the Axelrod directory:
More information about that here: http://axelrod.readthedocs.io/en/latest/tutorials/contributing/strategy/running_tests.html