Skip to content

Commit a8a4bf2

Browse files
committed
Fix stochastic behavior and refactor tests to use unittest
1 parent b43c060 commit a8a4bf2

File tree

3 files changed

+115
-103
lines changed

3 files changed

+115
-103
lines changed

src/axelrod_fortran/player.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import random
2+
13
import axelrod as axl
24
from axelrod.interaction_utils import compute_final_score
35
from axelrod.action import Action
@@ -57,15 +59,15 @@ def original_function(self, value):
5759
self.__original_function.restype = c_int
5860

5961
def original_strategy(
60-
self, their_last_move, move_number, my_score, their_score, noise,
62+
self, their_last_move, move_number, my_score, their_score, random_value,
6163
my_last_move
6264
):
6365
args = (
6466
c_int(their_last_move), c_int(move_number), c_int(my_score),
65-
c_int(their_score), c_float(noise), c_int(my_last_move))
67+
c_int(their_score), c_float(random_value), c_int(my_last_move))
6668
return self.original_function(*[byref(arg) for arg in args])
6769

68-
def strategy(self, opponent, noise=0):
70+
def strategy(self, opponent):
6971
if not self.history:
7072
their_last_move = 0
7173
scores = (0, 0)
@@ -76,8 +78,9 @@ def strategy(self, opponent, noise=0):
7678
game=self.game)
7779
my_last_move = original_actions[self.history[-1]]
7880
move_number = len(self.history) + 1
81+
random_value = random.random()
7982
original_action = self.original_strategy(
80-
their_last_move, move_number, scores[0], scores[1], noise,
83+
their_last_move, move_number, scores[0], scores[1], random_value,
8184
my_last_move)
8285
return actions[original_action]
8386

tests/test_player.py

Lines changed: 90 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,99 @@
1+
from ctypes import c_int, c_float, POINTER
2+
import itertools
3+
import random
4+
import unittest
5+
16
from axelrod_fortran.strategies import all_strategies
27
from axelrod_fortran import Player
38
from axelrod import Alternator, Cooperator, Defector, Match, Game
49
from axelrod.action import Action
5-
from ctypes import c_int, c_float, POINTER
6-
7-
import itertools
810

911
C, D = Action.C, Action.D
1012

1113

12-
def test_init():
13-
for strategy in all_strategies:
14-
player = Player(strategy)
15-
assert player.classifier["stochastic"]
16-
assert player.original_name == strategy
17-
assert player.original_function.argtypes == (
18-
POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int),
19-
POINTER(c_float))
20-
assert player.original_function.restype == c_int
21-
22-
23-
def test_matches():
24-
for strategy in all_strategies:
25-
for opponent in (Alternator, Cooperator, Defector):
26-
players = (Player(strategy), opponent())
27-
match = Match(players, turns=200)
28-
assert all(
29-
action in (C, D) for interaction in match.play()
30-
for action in interaction)
31-
32-
def test_noisy_matches():
33-
for strategy in all_strategies:
34-
for opponent in (Alternator, Cooperator, Defector):
35-
players = (Player(strategy), opponent())
36-
match = Match(players, turns=200, noise=0.5)
37-
assert all(
38-
action in (C, D) for interaction in match.play()
39-
for action in interaction)
40-
41-
def test_probend_matches():
42-
for strategy in all_strategies:
43-
for opponent in (Alternator, Cooperator, Defector):
44-
players = (Player(strategy), opponent())
45-
match = Match(players, prob_end=0.5, noise=0.5)
46-
assert all(
47-
action in (C, D) for interaction in match.play()
48-
for action in interaction)
49-
50-
def test_matches_with_different_game():
51-
for strategy in all_strategies:
52-
for opponent in (Alternator, Cooperator, Defector):
53-
game = Game(r=4,s=0,p=2,t=6)
54-
players = (Player(strategy, game=game), opponent())
55-
match = Match(players, turns=200, game=game)
56-
assert all(
57-
action in (C, D) for interaction in match.play()
58-
for action in interaction)
59-
60-
61-
def test_original_strategy():
62-
"""
63-
Test original strategy against all possible first 5 moves of a Match
64-
"""
65-
actions_to_scores = {(0, 0): (3, 3), (0, 1): (0, 5),
66-
(1, 0): (5, 0), (1, 1): (1, 1)}
67-
for strategy in all_strategies:
68-
for opponent_sequence in itertools.product((0, 1), repeat=5):
69-
14+
class TestAll(unittest.TestCase):
15+
def test_init(self):
16+
for strategy in all_strategies:
7017
player = Player(strategy)
71-
72-
# Initial set up for empty history
73-
my_score, their_score = 0, 0
74-
move_number = 1
75-
their_previous_action, my_action = 0, 0
76-
77-
for action in opponent_sequence:
78-
my_action = player.original_strategy(
79-
their_last_move=their_previous_action,
80-
move_number=move_number,
81-
my_score=my_score,
82-
their_score=their_score,
83-
noise=0,
84-
my_last_move=my_action)
85-
86-
assert my_action in [0, 1]
87-
88-
scores = actions_to_scores[my_action, action]
89-
their_previous_action = action
90-
my_score += scores[0]
91-
their_score += scores[1]
18+
assert player.classifier["stochastic"]
19+
assert player.original_name == strategy
20+
assert player.original_function.argtypes == (
21+
POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int),
22+
POINTER(c_float))
23+
assert player.original_function.restype == c_int
24+
25+
def test_matches(self):
26+
for strategy in all_strategies:
27+
for opponent in (Alternator, Cooperator, Defector):
28+
players = (Player(strategy), opponent())
29+
match = Match(players, turns=200)
30+
assert all(
31+
action in (C, D) for interaction in match.play()
32+
for action in interaction)
33+
34+
def test_noisy_matches(self):
35+
for strategy in all_strategies:
36+
for opponent in (Alternator, Cooperator, Defector):
37+
players = (Player(strategy), opponent())
38+
match = Match(players, turns=200, noise=0.5)
39+
assert all(
40+
action in (C, D) for interaction in match.play()
41+
for action in interaction)
42+
43+
def test_probend_matches(self):
44+
for strategy in all_strategies:
45+
for opponent in (Alternator, Cooperator, Defector):
46+
players = (Player(strategy), opponent())
47+
match = Match(players, prob_end=0.5)
48+
assert all(
49+
action in (C, D) for interaction in match.play()
50+
for action in interaction)
51+
52+
def test_matches_with_different_game(self):
53+
for strategy in all_strategies:
54+
for opponent in (Alternator, Cooperator, Defector):
55+
game = Game(r=4,s=0,p=2,t=6)
56+
players = (Player(strategy, game=game), opponent())
57+
match = Match(players, turns=200, game=game)
58+
assert all(
59+
action in (C, D) for interaction in match.play()
60+
for action in interaction)
61+
62+
def test_random(self):
63+
random.seed(10)
64+
players = (Player("KRANDOMC"), Player("KRANDOMC"))
65+
match = Match(players, 5)
66+
expected = [(C, D), (C, D), (C, C), (C, D), (C, D)]
67+
assert match.play() == expected
68+
69+
def test_original_strategy(self):
70+
"""
71+
Test original strategy against all possible first 5 moves of a Match
72+
"""
73+
actions_to_scores = {(0, 0): (3, 3), (0, 1): (0, 5),
74+
(1, 0): (5, 0), (1, 1): (1, 1)}
75+
for strategy in all_strategies:
76+
for opponent_sequence in itertools.product((0, 1), repeat=5):
77+
78+
player = Player(strategy)
79+
80+
# Initial set up for empty history
81+
my_score, their_score = 0, 0
82+
move_number = 1
83+
their_previous_action, my_action = 0, 0
84+
85+
for action in opponent_sequence:
86+
my_action = player.original_strategy(
87+
their_last_move=their_previous_action,
88+
move_number=move_number,
89+
my_score=my_score,
90+
their_score=their_score,
91+
random_value=0,
92+
my_last_move=my_action)
93+
94+
assert my_action in [0, 1]
95+
96+
scores = actions_to_scores[my_action, action]
97+
their_previous_action = action
98+
my_score += scores[0]
99+
their_score += scores[1]

tests/test_titfortat.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import unittest
2+
13
from axelrod_fortran.player import Player
24
import axelrod as axl
35
from axelrod.action import Action
46

57
C, D = Action.C, Action.D
68

79

8-
def test_versus_alternator():
9-
players = (Player('ktitfortatc'), axl.Alternator())
10-
match = axl.Match(players, 5)
11-
expected = [(C, C), (C, D), (D, C), (C, D), (D, C)]
12-
assert match.play() == expected
13-
14-
15-
def test_versus_cooperator():
16-
players = (Player('ktitfortatc'), axl.Cooperator())
17-
match = axl.Match(players, 5)
18-
expected = [(C, C), (C, C), (C, C), (C, C), (C, C)]
19-
assert match.play() == expected
10+
class TestAll(unittest.TestCase):
11+
def test_versus_alternator(self):
12+
players = (Player('ktitfortatc'), axl.Alternator())
13+
match = axl.Match(players, 5)
14+
expected = [(C, C), (C, D), (D, C), (C, D), (D, C)]
15+
assert match.play() == expected
2016

17+
def test_versus_cooperator(self):
18+
players = (Player('ktitfortatc'), axl.Cooperator())
19+
match = axl.Match(players, 5)
20+
expected = [(C, C), (C, C), (C, C), (C, C), (C, C)]
21+
assert match.play() == expected
2122

22-
def test_versus_defector():
23-
players = (Player('ktitfortatc'), axl.Defector())
24-
match = axl.Match(players, 5)
25-
expected = [(C, D), (D, D), (D, D), (D, D), (D, D)]
26-
assert match.play() == expected
23+
def test_versus_defector(self):
24+
players = (Player('ktitfortatc'), axl.Defector())
25+
match = axl.Match(players, 5)
26+
expected = [(C, D), (D, D), (D, D), (D, D), (D, D)]
27+
assert match.play() == expected

0 commit comments

Comments
 (0)