Skip to content

Commit

Permalink
Update MockPlayer and some PEP8 / docstring fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Jan 16, 2017
1 parent aaddf64 commit 34572f2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions axelrod/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Action = NewType('Action', str)


class Actions(object):
C = 'C'
D = 'D'
Expand Down
3 changes: 2 additions & 1 deletion axelrod/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


class Ecosystem(object):
"""Create an ecosystem based on the payoff matrix from an Axelrod tournament."""
"""Create an ecosystem based on the payoff matrix from an Axelrod
tournament."""

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

Expand Down
3 changes: 2 additions & 1 deletion axelrod/eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def power_iteration(mat: numpy.matrix, initial: numpy.ndarray) -> numpy.ndarray:
yield vec


def principal_eigenvector(mat: numpy.matrix, maximum_iterations=1000, max_error=1e-3) -> Tuple[numpy.ndarray, float]:
def principal_eigenvector(mat: numpy.matrix, maximum_iterations=1000,
max_error=1e-3) -> Tuple[numpy.ndarray, float]:
"""
Computes the (normalised) principal eigenvector of the given matrix.
Expand Down
11 changes: 6 additions & 5 deletions axelrod/fingerprint.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axelrod as axl
from collections import namedtuple
from tempfile import NamedTemporaryFile
import numpy as np
import matplotlib.pyplot as plt
import tqdm
from axelrod.strategy_transformers import JossAnnTransformer, DualTransformer
from axelrod.interaction_utils import compute_final_score_per_turn, read_interactions_from_file
import axelrod as axl
from axelrod import on_windows
from collections import namedtuple
from tempfile import NamedTemporaryFile
from axelrod.strategy_transformers import JossAnnTransformer, DualTransformer
from axelrod.interaction_utils import (compute_final_score_per_turn,
read_interactions_from_file)


Point = namedtuple('Point', 'x y')
Expand Down
15 changes: 7 additions & 8 deletions axelrod/match.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
from axelrod import Actions, Game
import axelrod.interaction_utils as iu
from .deterministic_cache import DeterministicCache

import axelrod.interaction_utils as iu

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -85,7 +84,7 @@ def _stochastic(self):
@property
def _cache_update_required(self):
"""
A boolean to show whether the deterministic cache should be updated
A boolean to show whether the deterministic cache should be updated.
"""
return (
not self.noise and
Expand Down Expand Up @@ -135,15 +134,15 @@ def scores(self):
return iu.compute_scores(self.result, self.game)

def final_score(self):
"""Returns the final score for a Match"""
"""Returns the final score for a Match."""
return iu.compute_final_score(self.result, self.game)

def final_score_per_turn(self):
"""Returns the mean score per round for a Match"""
"""Returns the mean score per round for a Match."""
return iu.compute_final_score_per_turn(self.result, self.game)

def winner(self):
"""Returns the winner of the Match"""
"""Returns the winner of the Match."""
winner_index = iu.compute_winner_index(self.result, self.game)
if winner_index is False: # No winner
return False
Expand All @@ -152,11 +151,11 @@ def winner(self):
return self.players[winner_index]

def cooperation(self):
"""Returns the count of cooperations by each player"""
"""Returns the count of cooperations by each player."""
return iu.compute_cooperations(self.result)

def normalised_cooperation(self):
"""Returns the count of cooperations by each player per turn"""
"""Returns the count of cooperations by each player per turn."""
return iu.compute_normalised_cooperation(self.result)

def state_distribution(self):
Expand Down
14 changes: 7 additions & 7 deletions axelrod/match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ def build_single_match_params(self):

def __len__(self):
"""
The size of the generator.
This corresponds to the number of match chunks as it
ignores repetitions.
The size of the generator. This corresponds to the number of match
chunks as it ignores repetitions.
"""
n = len(self.players)
num_matches = int(n * (n - 1) // 2 + n)
Expand Down Expand Up @@ -171,9 +170,10 @@ def estimated_size(self):

def graph_is_connected(edges, players):
"""
Test if a set of edges defines a complete graph on a set of players.
This is used by the spatial tournaments.
Test if the set of edges defines a graph in which each player is connected
to at least one other player. This function does not test if the graph is
fully connected in the sense that each node is reachable from every other
node.
Parameters:
-----------
Expand All @@ -182,7 +182,7 @@ def graph_is_connected(edges, players):
Returns:
--------
boolean : True if the graph is connected
boolean : True if the graph is connected as specified above.
"""
# Check if all players are connected.
player_indices = set(range(len(players)))
Expand Down
24 changes: 16 additions & 8 deletions axelrod/mock_player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import defaultdict
import copy
from axelrod import get_state_distribution_from_history, Player, \
update_history, update_state_distribution, Actions
from axelrod import (Actions, Player, get_state_distribution_from_history,
update_history, update_state_distribution)

C, D = Actions.C, Actions.D

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

if h1 and h2:
# Simulate Plays
s1 = P1.strategy(MockPlayer(P2, h2))
s2 = P2.strategy(MockPlayer(P1, h1))
# Simulate Players
mock_P1 = MockPlayer(P1, h1)
mock_P2 = MockPlayer(P2, h1)
mock_P1.state_distribution = defaultdict(
int, zip(P1.history, P2.history))
mock_P2.state_distribution = defaultdict(
int, zip(P2.history, P1.history))
# Force plays

s1 = P1.strategy(mock_P2)
s2 = P2.strategy(mock_P1)
# Record intended history
# Update Cooperation / Defection counts
update_history(P1, h1)
update_history(P2, h2)
get_state_distribution_from_history(P1, h1, h2)
get_state_distribution_from_history(P2, h2, h1)

update_state_distribution(P1, h1, h2)
update_state_distribution(P2, h2, h1)
return (h1, h2)
else:
s1 = P1.strategy(P2)
Expand Down

0 comments on commit 34572f2

Please sign in to comment.