Skip to content

Commit

Permalink
Merge pull request #682 from marcharper/style
Browse files Browse the repository at this point in the history
Style and Temp file closing warning fix
  • Loading branch information
meatballs authored Aug 9, 2016
2 parents 33a595d + eebde3e commit 792f402
Show file tree
Hide file tree
Showing 51 changed files with 194 additions and 260 deletions.
3 changes: 1 addition & 2 deletions axelrod/_strategy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def __repr__(self):
@Memoized
def recursive_thue_morse(n):
"""The recursive definition of the Thue-Morse sequence. The first few terms
of the Thue-Morse sequence are:
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 . . ."""
of the Thue-Morse sequence are: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 . . ."""

if n == 0:
return 0
Expand Down
1 change: 0 additions & 1 deletion axelrod/ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import random


Expand Down
4 changes: 2 additions & 2 deletions axelrod/match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def build_single_match_params(self):
Creates a single set of match parameters.
"""
return (
self.sample_length(self.prob_end), self.game, None, self.noise,
self.sample_length(), self.game, None, self.noise,
{'length': float('inf'), 'game': self.game, 'noise': self.noise})

def sample_length(self, prob_end):
def sample_length(self):
"""
Sample length of a game.
Expand Down
7 changes: 3 additions & 4 deletions axelrod/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ def clone(self):

def reset(self):
"""Resets history.
When creating strategies that create new attributes then this method should be
re-written (in the inherited class) and should not only reset history but also
rest all other attributes.
When creating strategies that create new attributes then this method
should be re-written (in the inherited class) and should not only reset
history but also rest all other attributes.
"""
self.history = []
self.cooperations = 0
Expand Down
11 changes: 2 additions & 9 deletions axelrod/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ def winplot(self, title=None):
def _sd_ordering(self):
return self.result_set.ranking

# Sort by median then max
# from operator import itemgetter
# diffs = self.result_set.score_diffs
# to_sort = [(median(d), max(d), i) for (i, d) in enumerate(diffs)]
# to_sort.sort(reverse=True, key=itemgetter(0, 1))
# ordering = [x[-1] for x in to_sort]
# return ordering

@property
def _sdv_plot_dataset(self):
ordering = self._sd_ordering
Expand Down Expand Up @@ -231,7 +223,8 @@ def stackplot(self, eco, title=None, logscale=True):
for i, n in enumerate(self.result_set.ranked_names):
x = -0.01
y = (i + 0.5) * 1.0 / self.result_set.nplayers
ax.annotate(n, xy=(x, y), xycoords=trans, clip_on=False, va='center', ha='right', fontsize=5)
ax.annotate(n, xy=(x, y), xycoords=trans, clip_on=False,
va='center', ha='right', fontsize=5)
ticks.append(y)
ax.set_yticks(ticks)
ax.tick_params(direction='out')
Expand Down
8 changes: 0 additions & 8 deletions axelrod/result_set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import defaultdict
import csv
import tqdm

Expand All @@ -8,13 +7,6 @@
from .game import Game
import axelrod.interaction_utils as iu

try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO


def update_progress_bar(method):
"""A decorator to update a progress bar if it exists"""
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/adaptive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, Game, init_args
from axelrod import Actions, Player, init_args

C, D = Actions.C, Actions.D

Expand Down
10 changes: 5 additions & 5 deletions axelrod/strategies/apavlov.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, flip_action
from axelrod import Actions, Player

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -51,15 +51,15 @@ def strategy(self, opponent):
if self.opponent_class == "STFT":
if len(self.history) % 6 in [0, 1]:
return C
#TFT
# TFT
if opponent.history[-1:] == [D]:
return D
if self.opponent_class == "PavlovD":
# Return D then C for the period
if len(self.history) % 6 == 0:
return D
if self.opponent_class == "Cooperative":
#TFT
# TFT
if opponent.history[-1:] == [D]:
return D
return C
Expand Down Expand Up @@ -112,10 +112,10 @@ def strategy(self, opponent):
if self.opponent_class in ["Random", "ALLD"]:
return D
if self.opponent_class == "STFT":
#TFTT
# TFTT
return D if opponent.history[-2:] == [D, D] else C
if self.opponent_class == "Cooperative":
#TFT
# TFT
return D if opponent.history[-1:] == [D] else C

def reset(self):
Expand Down
2 changes: 0 additions & 2 deletions axelrod/strategies/averagecopier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import random

from axelrod import Actions, Player, random_choice


Expand Down
3 changes: 1 addition & 2 deletions axelrod/strategies/axelrod_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
Additional strategies from Axelrod's first tournament.
"""

from math import sqrt
import random

from axelrod import Actions, Game, Player, init_args, flip_action, random_choice
from axelrod import Actions, Player, init_args, flip_action, random_choice

from.memoryone import MemoryOnePlayer

Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/backstabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
C, D = Actions.C, Actions.D


@FinalTransformer((D, D), name_prefix=None) # End with two defections
@FinalTransformer((D, D), name_prefix=None) # End with two defections
class BackStabber(Player):
"""
Forgives the first 3 defections but on the fourth
Expand Down
3 changes: 1 addition & 2 deletions axelrod/strategies/calculator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import itertools

from axelrod import Actions, Player
from .axelrod_first import Joss
from axelrod._strategy_utils import detect_cycle


class Calculator(Player):
"""
Plays like (Hard) Joss for the first 20 rounds. If periodic behavior is
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/cooperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

C, D = Actions.C, Actions.D


class Cooperator(Player):
"""A player who only ever cooperates."""

Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

C, D = Actions.C, Actions.D


class Darwin(Player):
""" A strategy which accumulates a record (the 'genome') of what the most
favourable response in the previous round should have been, and naively
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/finite_state_machines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, Game, init_args
from axelrod import Actions, Player, init_args

C, D = Actions.C, Actions.D

Expand Down
13 changes: 6 additions & 7 deletions axelrod/strategies/gambler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from itertools import product

from axelrod import Actions, Player, init_args, random_choice
from axelrod.strategy_transformers import FinalTransformer
from .lookerup import LookerUp, create_lookup_table_keys
Expand All @@ -8,7 +6,8 @@
C, D = Actions.C, Actions.D


@FinalTransformer((D, D), name_prefix=None) # End with two defections if tournament length is known
# End with two defections if tournament length is known
@FinalTransformer((D, D), name_prefix=None)
class Gambler(LookerUp):
"""
A LookerUp class player which will select randomly an action in some cases.
Expand All @@ -32,10 +31,10 @@ def __init__(self, lookup_table=None):
"""
if not lookup_table:
lookup_table = {
('', 'C', 'D') : 0,
('', 'D', 'D') : 0,
('', 'C', 'C') : 1,
('', 'D', 'C') : 1,
('', 'C', 'D'): 0,
('', 'D', 'D'): 0,
('', 'C', 'C'): 1,
('', 'D', 'C'): 1,
}
LookerUp.__init__(self, lookup_table=lookup_table, value_length=None)

Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/handshake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, Game, init_args
from axelrod import Actions, Player, init_args

C, D = Actions.C, Actions.D

Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

C, D = Actions.C, Actions.D


class Inverse(Player):
"""A player who defects with a probability that diminishes relative to how
long ago the opponent defected."""
Expand Down
55 changes: 22 additions & 33 deletions axelrod/strategies/lookerup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,49 @@ class LookerUp(Player):
opponents first n actions, self's last m actions, and opponents last m
actions, all as strings. The values are the actions to play on this round.
For example, in the case of m=n=1, if
- the opponent started by playing C
- my last action was a C the opponents
- last action was a D
then the corresponding key would be
* the opponent started by playing C
* my last action was a C the opponents
* last action was a D
then the corresponding key would be::
('C', 'C', 'D')
and the value would contain the action to play on this turn.
Some well-known strategies can be expressed as special cases; for example
Cooperator is given by the dict:
Cooperator is given by the dict::
{('', '', '') : C}
where m and n are both zero. Tit-For-Tat is given by:
where m and n are both zero. Tit-For-Tat is given by::
{
('', 'C', 'D') : D,
('', 'D', 'D') : D,
('', 'C', 'C') : C,
('', 'D', 'C') : C,
}
{('', 'C', 'D'): D,
('', 'D', 'D'): D,
('', 'C', 'C'): C,
('', 'D', 'C'): C}
where m=1 and n=0.
Lookup tables where the action depends on the opponent's first actions (as
opposed to most recent actions) will have a non-empty first string in the
tuple. For example, this fragment of a dict:
{
tuple. For example, this fragment of a dict::
...
('C', 'C', 'C') : C.
('D', 'C', 'C') : D,
...
}
{('C', 'C', 'C'): C,
('D', 'C', 'C'): D}
states that if self and opponent both cooperated on the previous turn, we
should cooperate this turn unless the opponent started by defecting, in
which case we should defect.
To denote lookup tables where the action depends on sequences of actions
(so m or n are greater than 1), simply concatenate the strings together.
Below is an incomplete example where m=3 and n=2.
{
Below is an incomplete example where m=3 and n=2::
...
('CC', 'CDD', 'CCC') : C.
('CD', 'CCD', 'CCC') : D,
...
{('CC', 'CDD', 'CCC'): C,
('CD', 'CCD', 'CCC'): D}
}
"""

name = 'LookerUp'
Expand All @@ -90,10 +79,10 @@ def __init__(self, lookup_table=None, value_length=1):

if not lookup_table:
lookup_table = {
('', 'C', 'D') : D,
('', 'D', 'D') : D,
('', 'C', 'C') : C,
('', 'D', 'C') : C,
('', 'C', 'D'): D,
('', 'D', 'D'): D,
('', 'C', 'C'): C,
('', 'D', 'C'): C,
}

self.lookup_table = lookup_table
Expand Down
9 changes: 6 additions & 3 deletions axelrod/strategies/mathematicalconstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@ def strategy(self, opponent):


class Golden(CotoDeRatio):
"""The player will always aim to bring the ratio of co-operations to defections closer to the golden mean"""
"""The player will always aim to bring the ratio of co-operations to
defections closer to the golden mean"""

name = '$\phi$'
ratio = (1 + sqrt(5)) / 2


class Pi(CotoDeRatio):
"""The player will always aim to bring the ratio of co-operations to defections closer to the pi"""
"""The player will always aim to bring the ratio of co-operations to
defections closer to the pi"""

name = '$\pi$'
ratio = pi


class e(CotoDeRatio):
"""The player will always aim to bring the ratio of co-operations to defections closer to the e"""
"""The player will always aim to bring the ratio of co-operations to
defections closer to the e"""

name = '$e$'
ratio = e
5 changes: 2 additions & 3 deletions axelrod/strategies/memoryone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from axelrod import Actions, Player, init_args, random_choice
import copy

"""IPD Strategies: http://www.prisoners-dilemma.com/strategies.html"""

Expand Down Expand Up @@ -64,7 +63,7 @@ def strategy(self, opponent):
return self._initial
# Determine which probability to use
p = self._four_vector[(self.history[-1], opponent.history[-1])]
# Draw a random number in [0,1] to decide
# Draw a random number in [0, 1] to decide
return random_choice(p)


Expand All @@ -84,7 +83,7 @@ class WinStayLoseShift(MemoryOnePlayer):
@init_args
def __init__(self, initial=C):
Player.__init__(self)
self.set_four_vector([1,0,0,1])
self.set_four_vector([1, 0, 0, 1])
self._initial = initial


Expand Down
Loading

0 comments on commit 792f402

Please sign in to comment.