Skip to content

Commit b37cb12

Browse files
l-liciniuslucullusmarcharper
l-liciniuslucullus
authored andcommitted
Added AdaptiveTitForTat (#697)
* Added Strategy AdaptiveTitForTat and tests.
1 parent 4231730 commit b37cb12

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

axelrod/strategies/_strategies.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@
5757
from .titfortat import (
5858
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
5959
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
60-
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats)
60+
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats, AdaptiveTitForTat)
6161

6262

6363
# Note: Meta* strategies are handled in .__init__.py
6464

6565
all_strategies = [
6666
Adaptive,
67+
AdaptiveTitForTat,
6768
Aggravater,
6869
ALLCorALLD,
6970
Alternator,
@@ -197,4 +198,5 @@
197198
ZDGen2,
198199
ZDSet2,
199200
e,
201+
200202
]

axelrod/strategies/titfortat.py

+76
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,79 @@ def strategy(self, opponent):
429429

430430
# Otherwise cooperate
431431
return C
432+
433+
class AdaptiveTitForTat(Player):
434+
"""ATFT - Adaptive Tit for Tat (Basic Model)
435+
436+
Algorithm
437+
438+
if (opponent played C in the last cycle) then
439+
world = world + r*(1-world)
440+
else
441+
world = world + r*(0-world)
442+
If (world >= 0.5) play C, else play D
443+
444+
Attributes
445+
446+
world : float [0.0, 1.0], set to 0.5
447+
continuous variable representing the world's image
448+
1.0 - total cooperation
449+
0.0 - total defection
450+
other values - something in between of the above
451+
updated every round, starting value shouldn't matter as long as
452+
it's >= 0.5
453+
454+
Parameters
455+
456+
rate : float [0.0, 1.0], default=0.5
457+
adaptation rate - r in Algorithm above
458+
smaller value means more gradual and robust
459+
to perturbations behaviour
460+
461+
Names
462+
463+
- Adaptive Tit For Tat: [Tzafestas2000]_
464+
"""
465+
466+
name = 'Adaptive Tit For Tat'
467+
classifier = {
468+
'memory_depth': float('inf'),
469+
'stochastic': False,
470+
'makes_use_of': set(),
471+
'long_run_time': False,
472+
'inspects_source': False,
473+
'manipulates_source': False,
474+
'manipulates_state': False
475+
}
476+
world = 0.5
477+
478+
@init_args
479+
def __init__(self, rate=0.5):
480+
481+
Player.__init__(self)
482+
self.rate, self.starting_rate = rate, rate
483+
484+
def strategy(self, opponent):
485+
486+
if len(opponent.history) == 0:
487+
return C
488+
489+
if opponent.history[-1] == C:
490+
self.world += self.rate * (1. - self.world)
491+
else:
492+
self.world -= self.rate * self.world
493+
494+
if self.world >= 0.5:
495+
return C
496+
497+
return D
498+
499+
def reset(self):
500+
501+
Player.reset(self)
502+
self.world = 0.5
503+
self.rate = self.starting_rate
504+
505+
def __repr__(self):
506+
507+
return "%s: %s" % (self.name, round(self.rate, 2))

axelrod/tests/unit/test_titfortat.py

+34
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,39 @@ def test_effect_of_strategy(self):
462462
self.responses_test([C]*3, [C, D, C], [C])
463463
self.responses_test([C]*3, [C, D, D], [D])
464464

465+
class TestAdaptiveTitForTat(TestPlayer):
465466

467+
name = "Adaptive Tit For Tat: 0.5"
468+
player = axelrod.AdaptiveTitForTat
469+
expected_classifier = {
470+
'memory_depth': float('inf'),
471+
'stochastic': False,
472+
'makes_use_of': set(),
473+
'inspects_source': False,
474+
'manipulates_source': False,
475+
'manipulates_state': False
476+
}
477+
478+
def test_strategy(self):
479+
"""Start by cooperating."""
480+
self.first_play_test(C)
481+
482+
def test_effect_of_strategy(self):
483+
484+
self.markov_test(['C', 'D', 'C', 'D'])
485+
486+
p1, p2 = self.player(), self.player()
487+
p1.play(p2)
488+
p1.play(p2)
489+
self.assertEqual(p2.world, 0.75)
490+
491+
def test_world_rate_reset(self):
492+
p1, p2 = self.player(), self.player()
493+
p1.play(p2)
494+
p1.play(p2)
495+
p2.reset()
496+
self.assertEqual(p2.world, 0.5)
497+
self.assertEqual(p2.rate, 0.5)
498+
499+
466500

docs/reference/bibliography.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ documentation.
1717
.. [Singer-Clark2014] Singer-Clark, T. (2014). Morality Metrics On Iterated Prisoner’s Dilemma Players.
1818
.. [Stewart2012] Stewart, a. J., & Plotkin, J. B. (2012). Extortion and cooperation in the Prisoner’s Dilemma. Proceedings of the National Academy of Sciences, 109(26), 10134–10135. http://doi.org/10.1073/pnas.1208087109
1919
.. [Szabó1992] Szabó, G., & Fáth, G. (2007). Evolutionary games on graphs. Physics Reports, 446(4-6), 97–216. http://doi.org/10.1016/j.physrep.2007.04.004
20+
.. [Tzafestas2000] Tzafestas, E. (2000). Toward adaptive cooperative behavior. From Animals to Animals: Proceedings of the 6th International Conference on the Simulation of Adaptive Behavior {(SAB-2000)}, 2, 334–340.

0 commit comments

Comments
 (0)