|
| 1 | +from ctypes import c_int, c_float, POINTER |
| 2 | +import itertools |
| 3 | +import random |
| 4 | +import unittest |
| 5 | + |
1 | 6 | from axelrod_fortran.strategies import all_strategies
|
2 | 7 | from axelrod_fortran import Player
|
3 | 8 | from axelrod import Alternator, Cooperator, Defector, Match, Game
|
4 | 9 | from axelrod.action import Action
|
5 |
| -from ctypes import c_int, c_float, POINTER |
6 |
| - |
7 |
| -import itertools |
8 | 10 |
|
9 | 11 | C, D = Action.C, Action.D
|
10 | 12 |
|
11 | 13 |
|
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: |
70 | 17 | 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] |
0 commit comments