Skip to content

Commit 48ca52c

Browse files
committed
Merge pull request #474 from Axelrod-Python/284
Interactions and RoundRobin
2 parents d5e440e + 01bcf8f commit 48ca52c

20 files changed

+519
-499
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ _build/
66
dist/
77
MANIFEST
88
Axelrod.egg-info
9-
.hypothesis/eval_source/
9+
.hypothesis/
10+
basic_strategies.csv
11+
cache.txt
12+
test.csv

.hypothesis/examples.db

-7 KB
Binary file not shown.

axelrod/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from .player import init_args, is_basic, obey_axelrod, update_history, Player
99
from .mock_player import MockPlayer, simulate_play
1010
from .match import Match
11-
from .round_robin import RoundRobin
1211
from .strategies import *
12+
from .tournament_type import *
1313
from .tournament import Tournament
1414
from .tournament_manager import TournamentManager
1515
from .tournament_manager_factory import TournamentManagerFactory

axelrod/_strategy_utils.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import collections
44
import itertools
55

6-
from axelrod import RoundRobin, update_history
6+
from axelrod import update_history
77
from axelrod import Actions
88

99
from axelrod.strategies.cycler import Cycler
1010

1111
C, D = Actions.C, Actions.D
1212

13+
1314
def detect_cycle(history, min_size=1, offset=0):
1415
"""Detects cycles in the sequence history.
1516
@@ -47,11 +48,23 @@ def limited_simulate_play(player_1, player_2, h1):
4748
update_history(player_1, h1)
4849
update_history(player_2, h2)
4950

51+
5052
def simulate_match(player_1, player_2, strategy, rounds=10):
5153
"""Simulates a number of matches."""
5254
for match in range(rounds):
5355
limited_simulate_play(player_1, player_2, strategy)
5456

57+
58+
def calculate_scores(p1, p2, game):
59+
"""Calculates the score for two players based their history"""
60+
s1, s2 = 0, 0
61+
for pair in zip(p1.history, p2.history):
62+
score = game.score(pair)
63+
s1 += score[0]
64+
s2 += score[1]
65+
return s1, s2
66+
67+
5568
def look_ahead(player_1, player_2, game, rounds=10):
5669
"""Looks ahead for `rounds` and selects the next strategy appropriately."""
5770
results = []
@@ -61,29 +74,28 @@ def look_ahead(player_1, player_2, game, rounds=10):
6174
for strategy in strategies:
6275
# Instead of a deepcopy, create a new opponent and play out the history
6376
opponent_ = player_2.clone()
64-
player_ = Cycler(strategy) # Either cooperator or defector
77+
player_ = Cycler(strategy) # Either cooperator or defector
6578
for h1 in player_1.history:
6679
limited_simulate_play(player_, opponent_, h1)
6780

68-
round_robin = RoundRobin(players=[player_, opponent_], game=game,
69-
turns=rounds)
7081
simulate_match(player_, opponent_, strategy, rounds)
71-
results.append(round_robin._calculate_scores(player_, opponent_)[0])
82+
results.append(calculate_scores(player_, opponent_, game))
7283

7384
return strategies[results.index(max(results))]
7485

7586

7687
class Memoized(object):
77-
"""Decorator. Caches a function's return value each time it is called.
78-
If called later with the same arguments, the cached value is returned
79-
(not reevaluated). From:
80-
https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
81-
"""
82-
def __init__(self, func):
88+
"""Decorator. Caches a function's return value each time it is called.
89+
If called later with the same arguments, the cached value is returned
90+
(not reevaluated). From:
91+
https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
92+
"""
93+
94+
def __init__(self, func):
8395
self.func = func
8496
self.cache = {}
8597

86-
def __call__(self, *args):
98+
def __call__(self, *args):
8799
if not isinstance(args, collections.Hashable):
88100
# uncacheable. a list, for instance.
89101
# better to not cache than blow up.
@@ -95,11 +107,11 @@ def __call__(self, *args):
95107
self.cache[args] = value
96108
return value
97109

98-
def __repr__(self):
110+
def __repr__(self):
99111
"""Return the function's docstring."""
100112
return self.func.__doc__
101113

102-
def __get__(self, obj, objtype):
114+
def __get__(self, obj, objtype):
103115
"""Support instance methods."""
104116
return functools.partial(self.__call__, obj)
105117

axelrod/cooperation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from math import sqrt
21
from . import eigen
32
from axelrod import Actions
43
from axelrod.payoff import player_count
54

65
C, D = Actions.C, Actions.D
76

87

9-
# As yet unused until RoundRobin returns interactions
108
def cooperation_matrix(interactions):
119
"""
1210
The cooperation matrix from a single round robin.

axelrod/match.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def __init__(self, players, turns, deterministic_cache=None,
2727
The probability that a player's intended action should be flipped
2828
"""
2929
self.result = []
30-
self._player1 = players[0]
31-
self._player2 = players[1]
30+
self.player1 = players[0]
31+
self.player2 = players[1]
3232
self._classes = (players[0].__class__, players[1].__class__)
3333
self._turns = turns
3434
self._prob_end = prob_end
@@ -48,8 +48,8 @@ def _stochastic(self):
4848
return (
4949
self._prob_end or
5050
self._noise or
51-
self._player1.classifier['stochastic'] or
52-
self._player2.classifier['stochastic'])
51+
self.player1.classifier['stochastic'] or
52+
self.player2.classifier['stochastic'])
5353

5454
@property
5555
def _cache_update_required(self):
@@ -60,8 +60,8 @@ def _cache_update_required(self):
6060
not self._prob_end and
6161
not self._noise and
6262
self._cache_mutable and not (
63-
self._player1.classifier['stochastic'] or
64-
self._player2.classifier['stochastic'])
63+
self.player1.classifier['stochastic'] or
64+
self.player2.classifier['stochastic'])
6565
)
6666

6767
def play(self):
@@ -102,12 +102,12 @@ def play(self):
102102

103103
if (self._stochastic or self._classes not in self._cache):
104104
turn = 0
105-
self._player1.reset()
106-
self._player2.reset()
105+
self.player1.reset()
106+
self.player2.reset()
107107
while turn < min(self._turns, end_turn):
108108
turn += 1
109-
self._player1.play(self._player2, self._noise)
110-
result = list(zip(self._player1.history, self._player2.history))
109+
self.player1.play(self.player2, self._noise)
110+
result = list(zip(self.player1.history, self.player2.history))
111111

112112
if self._cache_update_required:
113113
self._cache[self._classes] = result
@@ -119,6 +119,6 @@ def play(self):
119119

120120
def sparklines(self, c_symbol=u'█', d_symbol=u' '):
121121
return (
122-
sparkline(self._player1.history, c_symbol, d_symbol) +
122+
sparkline(self.player1.history, c_symbol, d_symbol) +
123123
u'\n' +
124-
sparkline(self._player2.history, c_symbol, d_symbol))
124+
sparkline(self.player2.history, c_symbol, d_symbol))

axelrod/payoff.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def player_count(interactions):
6060
return int((math.sqrt(len(interactions) * 8 + 1) - 1) / 2)
6161

6262

63-
# As yet unused until RoundRobin returns interactions
6463
def payoff_matrix(interactions, game):
6564
"""
6665
The payoff matrix from a single round robin.
@@ -115,7 +114,6 @@ def payoff_matrix(interactions, game):
115114
return payoffs
116115

117116

118-
# As yet unused until RoundRobin returns interactions
119117
def interaction_payoff(actions, game):
120118
"""
121119
The payoff values for a single interaction of n turns between two players.

axelrod/result_set.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import math
21
import csv
32

4-
#from .eigen import *
53
from axelrod import payoff as ap, cooperation as ac
64

75
try:
@@ -22,7 +20,7 @@ def __init__(self, players, turns, repetitions, outcome,
2220
players (list): a list of player objects.
2321
turns (int): the number of turns per interaction.
2422
repetitions (int): the number of time the round robin was repeated.
25-
outcome (dict): returned from the RoundRobin class and containing
23+
outcome (dict): returned from the Tournament class and containing
2624
various sets of results for processing by this class.
2725
with_morality (bool): a flag to determine whether morality metrics
2826
should be calculated.

axelrod/round_robin.py

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)