Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, cycle=C*2+D):
"""This strategy will repeat the parameter `cycle` endlessly,
e.g. C C D C C D C C D ...
Special Cases
-------------
Cooperator is equivalent to Cycler("C")
Defector is equivalent to Cycler("D")
Alternator is equivalent to Cycler("CD")
"""
Player.__init__(self)
self.cycle = cycle
self.name = "Cycler {}".format(cycle)
self.classifier['memory_depth'] = len(cycle) - 1
from axelrod import Actions, Player
from axelrod._strategy_utils import detect_cycle
C, D = Actions.C, Actions.D
class DefectorHunter(Player):
"""A player who hunts for defectors."""
name = 'Defector Hunter'
classifier = {
'memory_depth': float('inf'), # Long memory
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}
def strategy(self, opponent):
if len(self.history) >= 4 and len(opponent.history) == opponent.defections:
return D
from axelrod import Player
from axelrod.action import Action
C, D = Action.C, Action.D
class ShortMem(Player):
"""
A player starts by always cooperating for the first 10 moves.
From the tenth round on, the player analyzes the last ten actions, and
compare the number of defects and cooperates of the opponent, based in
percentage. If cooperation occurs 30% more than defection, it will
cooperate.
If defection occurs 30% more than cooperation, the program will defect.
Otherwise, the program follows the TitForTat algorithm.
Names:
- ShortMem: [Andre2013]_
"""
name = "ShortMem"
from axelrod import Actions, Player
C, D = Actions.C, Actions.D
class APavlov2006(Player):
"""
APavlov as defined in http://www.cs.nott.ac.uk/~pszjl/index_files/chapter4.pdf
(pages 10-11).
APavlov attempts to classify its opponent as one of five strategies:
Cooperative, ALLD, STFT, PavlovD, or Random. APavlov then responds in a
manner intended to achieve mutual cooperation or to defect against
uncooperative opponents.
"""
name = "Adaptive Pavlov 2006"
classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
Parameters
----------
point : Point
probe : class or instance
A class that must be descended from axelrod.Player or an instance of
axelrod.Player.
Returns
----------
joss_ann: Joss-AnnTitForTat object
`JossAnnTransformer` with parameters that correspond to `point`.
"""
x, y = point
if isinstance(probe, axl.Player):
init_kwargs = probe.init_kwargs
probe = probe.__class__
else:
init_kwargs = {}
if x + y >= 1:
joss_ann = DualTransformer()(JossAnnTransformer((1 - x, 1 - y))(probe))(
**init_kwargs
)
else:
joss_ann = JossAnnTransformer((x, y))(probe)(**init_kwargs)
return joss_ann
}
def strategy(self, opponent):
if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations:
return D
return C
def is_alternator(history):
for i in range(len(history) - 1):
if history[i] == history[i+1]:
return False
return True
class AlternatorHunter(Player):
"""A player who hunts for alternators."""
name = 'Alternator Hunter'
classifier = {
'memory_depth': float('inf'), # Long memory
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}
def __init__(self):
Player.__init__(self)
self.is_alt = False
The initial move
Special Cases
Alternator is equivalent to MemoryOnePlayer((0, 0, 1, 1), C)
Cooperator is equivalent to MemoryOnePlayer((1, 1, 1, 1), C)
Defector is equivalent to MemoryOnePlayer((0, 0, 0, 0), C)
Random is equivalent to MemoryOnePlayer((0.5, 0.5, 0.5, 0.5))
(with a random choice for the initial state)
TitForTat is equivalent to MemoryOnePlayer((1, 0, 1, 0), C)
WinStayLoseShift is equivalent to MemoryOnePlayer((1, 0, 0, 1), C)
See also: The remaining strategies in this file
Multiple strategies in titfortat.py
Grofman, Joss in axelrod_tournaments.py
"""
Player.__init__(self)
self._initial = initial
if four_vector is not None:
self.set_four_vector(four_vector)
if self.name == 'Generic Memory One Player':
self.name = "%s: %s" % (self.name, four_vector)
from axelrod import Actions, Player
from axelrod._strategy_utils import detect_cycle
from .axelrod_first import Joss
C, D = Actions.C, Actions.D
class Calculator(Player):
"""
Plays like (Hard) Joss for the first 20 rounds. If periodic behavior is
detected, defect forever. Otherwise play TFT.
"""
name = "Calculator"
classifier = {
'memory_depth': float('inf'),
'stochastic': True,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}
from axelrod import Player
class GoByRecentMajority10(Player):
"""
A player examines the history of the opponent: if the opponent has more defections than cooperations in the past 10 turns then the player defects
"""
def strategy(self, opponent):
"""
This is affected by the history of the opponent:
As long as the opponent cooperates at least as often as they defect in the past 10 turns then the player will cooperate.
If at any point the opponent has more defections than cooperations the player defects.
"""
recent10 = opponent.history[-10:]
if sum([s == 'D' for s in recent10]) > sum([s == 'C' for s in recent10]):
return 'D'
return 'C'
def __repr__(self):
"""