Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The documentation includes details of how to setup a tournament but here is an
example showing how to create a tournament with all stochastic strategies::

import axelrod
strategies = [s() for s in axelrod.ordinary_strategies if s().stochastic]
strategies = [s() for s in axelrod.ordinary_strategies if s().classifier['stochastic']]
tournament = axelrod.Tournament(strategies)
results = tournament.play()

Expand All @@ -67,10 +67,8 @@ The :code:`results` object now contains all the results we could need::

gives::

['Inverse', 'Forgetful Fool Me Once', 'Nice Average Copier', 'Champion',
'Generous Tit-For-Tat', 'Eatherley', 'ZD-GTFT-2', 'Meta Majority', 'Soft Joss',
'Average Copier', 'Feld', 'Stochastic WSLS', 'Tullock', 'Joss', 'ZD-Extort-2',
'Grofman', 'Random', 'Meta Winner', 'Meta Minority']
['Meta Hunter', 'Inverse', 'Forgetful Fool Me Once', 'GTFT: 0.33', 'Champion', 'ZD-GTFT-2', 'Eatherley', 'Math Constant Hunter', 'Random Hunter', 'Soft Joss: 0.9', 'Meta Majority', 'Nice Average Copier', 'Feld', 'Meta Minority', 'Grofman', 'Stochastic WSLS', 'ZD-Extort-2', 'Tullock', 'Joss: 0.9', 'Arrogant QLearner', 'Average Copier', 'Cautious QLearner', 'Hesitant QLearner', 'Risky QLearner', 'Random: 0.5', 'Meta Winner']


Results
=======
Expand Down
4 changes: 2 additions & 2 deletions axelrod/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import absolute_import

# The order of imports matters!
from .random_ import random_choice

from .plot import *
from .game import *
from .player import *
from .mock_player import *
from .round_robin import *
from .strategies import *
from .tournament import *
from .tournament_manager import *
from .strategies import *
from .ecosystem import *
14 changes: 12 additions & 2 deletions axelrod/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
flip_dict = {C: D, D: C}


# Strategy classifiers

def is_cheater(s):
"""
A function to check if a strategy cheats.
"""
classifier = s.classifier
return classifier['inspects_source'] or\
classifier['manipulates_source'] or\
classifier['manipulates_state']


def update_histories(player1, player2, move1, move2):
"""Updates histories and cooperation / defections counts following play."""
# Update histories
Expand Down Expand Up @@ -42,8 +54,6 @@ def __init__(self):
"""Initiates an empty history and 0 score for a player."""
self.history = []
self.classifier = copy.copy(self.classifier)
self.classifier['stochastic'] = (
"random" in inspect.getsource(self.__class__))
if self.name == "Player":
self.classifier['stochastic'] = False
for dimension in self.default_classifier:
Expand Down
17 changes: 17 additions & 0 deletions axelrod/strategies/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
from ..player import is_cheater
from ._strategies import *

# `from ._strategies import *` import the collection `strategies`
# Now import the Meta strategies. This cannot be done in _strategies
# because it creates circular dependencies

from .meta import MetaMajority, MetaMinority, MetaWinner, MetaHunter
strategies.extend((MetaHunter, MetaMajority, MetaMinority, MetaWinner))

# Distinguished strategy collections in addition to
# `strategies` from _strategies.py

ordinary_strategies = [s for s in strategies if not is_cheater(s())]
cheating_strategies = [s for s in strategies if is_cheater(s())]

# Defined by fiat for demo purposes
basic_strategies = [Alternator, Cooperator, Defector, Random, TitForTat]
104 changes: 48 additions & 56 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
from __future__ import absolute_import

from .alternator import *
from .appeaser import *
from .averagecopier import *
from .axelrod_tournaments import *
from .backstabber import *
from .alternator import Alternator
from .appeaser import Appeaser
from .averagecopier import AverageCopier, NiceAverageCopier
from .axelrod_tournaments import (
Davis, Feld, Grofman, Joss, Shubik, Tullock, Champion, Eatherley, Tester)
from .backstabber import BackStabber, DoubleCrosser
from .calculator import Calculator
from .cooperator import *
from .cycler import *
from .darwin import *
from .defector import *
from .forgiver import *
from .geller import *
from .gobymajority import *
from .grudger import *
from .grumpy import *
from .hunter import *
from .inverse import *
from .mathematicalconstants import *
from .memoryone import *
from .meta import *
from .mindcontrol import *
from .mindreader import *
from .oncebitten import *
from .prober import *
from .punisher import *
from .qlearner import *
from .rand import *
from .retaliate import *
from .titfortat import *
from .cooperator import Cooperator, TrickyCooperator
from .cycler import AntiCycler, CyclerCCD, CyclerCCCD, CyclerCCCCCD
from .darwin import Darwin
from .defector import Defector, TrickyDefector
from .forgiver import Forgiver, ForgivingTitForTat
from .geller import Geller, GellerCooperator, GellerDefector
from .gobymajority import (
GoByMajority, GoByMajority10, GoByMajority20, GoByMajority40,
GoByMajority5)
from .grudger import Grudger, ForgetfulGrudger, OppositeGrudger, Aggravater
from .grumpy import Grumpy
from .hunter import (
DefectorHunter, CooperatorHunter, AlternatorHunter, MathConstantHunter,
RandomHunter)
from .inverse import Inverse
from .mathematicalconstants import Golden, Pi, e
from .memoryone import (
WinStayLoseShift,GTFT, StochasticCooperator, StochasticWSLS, ZDGTFT2,
ZDExtort2, SoftJoss, MemoryOnePlayer)
from .mindcontrol import MindController, MindWarper, MindBender
from .mindreader import MindReader, ProtectedMindReader
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
from .prober import Prober, Prober2, Prober3, HardProber
from .punisher import Punisher, InversePunisher
from .qlearner import RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner
from .rand import Random
from .retaliate import (
Retaliate, Retaliate2, Retaliate3, LimitedRetaliate, LimitedRetaliate2,
LimitedRetaliate3)
from .titfortat import (
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats)

# A list of strategies to quickly create a tournament
basic_strategies = [
Alternator,
Cooperator,
Defector,
Random,
TitForTat,
]

# All the strategies in the tournament
strategies = basic_strategies + [
# Note: Meta* strategies are handled in .__init__.py, so this is not the
# final form of the list

strategies = [
Aggravater,
Alternator,
AlternatorHunter,
AntiCycler,
AntiTitForTat,
Expand All @@ -53,12 +58,14 @@
Calculator,
CautiousQLearner,
Champion,
Cooperator,
CooperatorHunter,
CyclerCCCCCD,
CyclerCCCD,
CyclerCCD,
Darwin,
Davis,
Defector,
DefectorHunter,
DoubleCrosser,
Eatherley,
Expand Down Expand Up @@ -93,10 +100,6 @@
LimitedRetaliate2,
LimitedRetaliate3,
MathConstantHunter,
MetaHunter,
MetaMajority,
MetaMinority,
MetaWinner,
MindBender,
MindController,
MindReader,
Expand All @@ -110,6 +113,7 @@
Prober3,
ProtectedMindReader,
Punisher,
Random,
RandomHunter,
Retaliate,
Retaliate2,
Expand All @@ -121,6 +125,7 @@
StochasticWSLS,
SuspiciousTitForTat,
Tester,
TitForTat,
TitFor2Tats,
TrickyCooperator,
TrickyDefector,
Expand All @@ -130,17 +135,4 @@
ZDExtort2,
ZDGTFT2,
e,
]


def is_cheater(s):
"""
A function to check if a strategy cheats.
"""
classifier = s.classifier
return classifier['inspects_source'] or\
classifier['manipulates_source'] or\
classifier['manipulates_state']

ordinary_strategies = [s for s in strategies if not is_cheater(s)]
cheating_strategies = [s for s in strategies if is_cheater(s)]
]
45 changes: 39 additions & 6 deletions axelrod/strategies/axelrod_tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
Additional strategies from Axelrod's two tournaments.
"""

from axelrod import Player

import random

from axelrod import Player
from.memoryone import MemoryOnePlayer


flip_dict = {'C': 'D', 'D': 'C'}


## First Tournament

class Davis(Player):
Expand Down Expand Up @@ -47,7 +49,7 @@ class Feld(Player):
name = "Feld"
classifier = {
'memory_depth': 200, # Varies actually, eventually becomes depth 1
'stochastic': False,
'stochastic': True,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
Expand Down Expand Up @@ -82,6 +84,36 @@ def strategy(self, opponent):
return 'D'


class Grofman(MemoryOnePlayer):
"""
Cooperates with probability 2/7.
"""

name = "Grofman"

def __init__(self):
p = float(2) / 7
four_vector = (p, p, p, p)
super(self.__class__, self).__init__(four_vector)


class Joss(MemoryOnePlayer):
"""
Cooperates with probability 0.9 when the opponent cooperates, otherwise
emulates Tit-For-Tat.
"""

name = "Joss"

def __init__(self, p=0.9):
four_vector = (p, 0, p, 0)
self.p = p
super(self.__class__, self).__init__(four_vector)

def __repr__(self):
return "%s: %s" % (self.name, round(self.p, 2))


class Shubik(Player):
"""
Plays like Tit-For-Tat with the following modification. After
Expand Down Expand Up @@ -141,6 +173,7 @@ def reset(self):
self.retaliation_length = 0
self.retaliation_remaining = 0


class Tullock(Player):
"""
Cooperates for the first 11 rounds then randomly cooperates 10% less often
Expand All @@ -149,7 +182,7 @@ class Tullock(Player):
name = "Tullock"
classifier = {
'memory_depth': 11, # long memory, modified by init
'stochastic': False,
'stochastic': True,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
Expand Down Expand Up @@ -183,7 +216,7 @@ class Champion(Player):
name = "Champion"
classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'stochastic': True,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
Expand Down Expand Up @@ -217,7 +250,7 @@ class Eatherley(Player):
name = "Eatherley"
classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'stochastic': True,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/calculator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools

from axelrod import Player
from .memoryone import Joss
from .axelrod_tournaments import Joss


class Calculator(Player):
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, cycle="CCD"):
Player.__init__(self)
self.cycle = cycle
self.name += " " + cycle
self.memory_depth = len(cycle)
self.classifier['memory_depth'] = len(cycle) - 1

def strategy(self, opponent):
curent_round = len(self.history)
Expand Down
2 changes: 2 additions & 0 deletions axelrod/strategies/geller.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def strategy(self, opponent):
else:
return opponent.strategy(self)


class GellerCooperator(Geller):
"""Observes what the payer will do (like :code:`Geller`) but if unable to
will cooperate.
Expand All @@ -69,6 +70,7 @@ class GellerCooperator(Geller):
'manipulates_state': False
}


class GellerDefector(Geller):
"""Observes what the payer will do (like :code:`Geller`) but if unable to
will defect.
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/gobymajority.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class GoByMajority(Player):
'stochastic': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
'manipulates_state': False,
'memory_depth': 0 # memory_depth may be altered by __init__
}
# memory_depth is set by __init__

def __init__(self, memory_depth=0, soft=True):
Player.__init__(self)
Expand Down
Loading