Skip to content

Commit 98725d0

Browse files
committed
Update MockPlayer and some PEP8 / docstring fixes
1 parent ef0879c commit 98725d0

File tree

7 files changed

+41
-30
lines changed

7 files changed

+41
-30
lines changed

axelrod/actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Action = NewType('Action', str)
44

5+
56
class Actions(object):
67
C = 'C'
78
D = 'D'

axelrod/ecosystem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
class Ecosystem(object):
5-
"""Create an ecosystem based on the payoff matrix from an Axelrod tournament."""
5+
"""Create an ecosystem based on the payoff matrix from an Axelrod
6+
tournament."""
67

78
def __init__(self, results, fitness=None, population=None):
89

axelrod/eigen.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def power_iteration(mat: numpy.matrix, initial: numpy.ndarray) -> numpy.ndarray:
4545
yield vec
4646

4747

48-
def principal_eigenvector(mat: numpy.matrix, maximum_iterations=1000, max_error=1e-3) -> Tuple[numpy.ndarray, float]:
48+
def principal_eigenvector(mat: numpy.matrix, maximum_iterations=1000,
49+
max_error=1e-3) -> Tuple[numpy.ndarray, float]:
4950
"""
5051
Computes the (normalised) principal eigenvector of the given matrix.
5152

axelrod/fingerprint.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import axelrod as axl
1+
from collections import namedtuple
2+
from tempfile import NamedTemporaryFile
23
import numpy as np
34
import matplotlib.pyplot as plt
45
import tqdm
5-
from axelrod.strategy_transformers import JossAnnTransformer, DualTransformer
6-
from axelrod.interaction_utils import compute_final_score_per_turn, read_interactions_from_file
6+
import axelrod as axl
77
from axelrod import on_windows
8-
from collections import namedtuple
9-
from tempfile import NamedTemporaryFile
8+
from axelrod.strategy_transformers import JossAnnTransformer, DualTransformer
9+
from axelrod.interaction_utils import (compute_final_score_per_turn,
10+
read_interactions_from_file)
1011

1112

1213
Point = namedtuple('Point', 'x y')

axelrod/match.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# -*- coding: utf-8 -*-
21
from axelrod import Actions, Game
2+
import axelrod.interaction_utils as iu
33
from .deterministic_cache import DeterministicCache
44

5-
import axelrod.interaction_utils as iu
65

76
C, D = Actions.C, Actions.D
87

@@ -85,7 +84,7 @@ def _stochastic(self):
8584
@property
8685
def _cache_update_required(self):
8786
"""
88-
A boolean to show whether the deterministic cache should be updated
87+
A boolean to show whether the deterministic cache should be updated.
8988
"""
9089
return (
9190
not self.noise and
@@ -135,15 +134,15 @@ def scores(self):
135134
return iu.compute_scores(self.result, self.game)
136135

137136
def final_score(self):
138-
"""Returns the final score for a Match"""
137+
"""Returns the final score for a Match."""
139138
return iu.compute_final_score(self.result, self.game)
140139

141140
def final_score_per_turn(self):
142-
"""Returns the mean score per round for a Match"""
141+
"""Returns the mean score per round for a Match."""
143142
return iu.compute_final_score_per_turn(self.result, self.game)
144143

145144
def winner(self):
146-
"""Returns the winner of the Match"""
145+
"""Returns the winner of the Match."""
147146
winner_index = iu.compute_winner_index(self.result, self.game)
148147
if winner_index is False: # No winner
149148
return False
@@ -152,11 +151,11 @@ def winner(self):
152151
return self.players[winner_index]
153152

154153
def cooperation(self):
155-
"""Returns the count of cooperations by each player"""
154+
"""Returns the count of cooperations by each player."""
156155
return iu.compute_cooperations(self.result)
157156

158157
def normalised_cooperation(self):
159-
"""Returns the count of cooperations by each player per turn"""
158+
"""Returns the count of cooperations by each player per turn."""
160159
return iu.compute_normalised_cooperation(self.result)
161160

162161
def state_distribution(self):

axelrod/match_generator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ def build_single_match_params(self):
7777

7878
def __len__(self):
7979
"""
80-
The size of the generator.
81-
This corresponds to the number of match chunks as it
82-
ignores repetitions.
80+
The size of the generator. This corresponds to the number of match
81+
chunks as it ignores repetitions.
8382
"""
8483
n = len(self.players)
8584
num_matches = int(n * (n - 1) // 2 + n)
@@ -170,9 +169,10 @@ def estimated_size(self):
170169

171170
def graph_is_connected(edges, players):
172171
"""
173-
Test if a set of edges defines a complete graph on a set of players.
174-
175-
This is used by the spatial tournaments.
172+
Test if the set of edges defines a graph in which each player is connected
173+
to at least one other player. This function does not test if the graph is
174+
fully connected in the sense that each node is reachable from every other
175+
node.
176176
177177
Parameters:
178178
-----------
@@ -181,7 +181,7 @@ def graph_is_connected(edges, players):
181181
182182
Returns:
183183
--------
184-
boolean : True if the graph is connected
184+
boolean : True if the graph is connected as specified above.
185185
"""
186186
# Check if all players are connected.
187187
player_indices = set(range(len(players)))

axelrod/mock_player.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from collections import defaultdict
12
import copy
2-
from axelrod import get_state_distribution_from_history, Player, \
3-
update_history, update_state_distribution, Actions
3+
from axelrod import (Actions, Player, get_state_distribution_from_history,
4+
update_history, update_state_distribution)
45

56
C, D = Actions.C, Actions.D
67

@@ -31,16 +32,23 @@ def simulate_play(P1, P2, h1=None, h2=None):
3132
"""
3233

3334
if h1 and h2:
34-
# Simulate Plays
35-
s1 = P1.strategy(MockPlayer(P2, h2))
36-
s2 = P2.strategy(MockPlayer(P1, h1))
35+
# Simulate Players
36+
mock_P1 = MockPlayer(P1, h1)
37+
mock_P2 = MockPlayer(P2, h1)
38+
mock_P1.state_distribution = defaultdict(
39+
int, zip(P1.history, P2.history))
40+
mock_P2.state_distribution = defaultdict(
41+
int, zip(P2.history, P1.history))
42+
# Force plays
43+
44+
s1 = P1.strategy(mock_P2)
45+
s2 = P2.strategy(mock_P1)
3746
# Record intended history
3847
# Update Cooperation / Defection counts
3948
update_history(P1, h1)
4049
update_history(P2, h2)
41-
get_state_distribution_from_history(P1, h1, h2)
42-
get_state_distribution_from_history(P2, h2, h1)
43-
50+
update_state_distribution(P1, h1, h2)
51+
update_state_distribution(P2, h2, h1)
4452
return (h1, h2)
4553
else:
4654
s1 = P1.strategy(P2)

0 commit comments

Comments
 (0)