Skip to content

Commit

Permalink
Merge pull request #1153 from gaffney2010/master
Browse files Browse the repository at this point in the history
Implemented Leyvraz, K86R from Axelrod's second
  • Loading branch information
drvinceknight committed Dec 14, 2017
2 parents 3776881 + 835764a commit bc9cd5a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .axelrod_second import (
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen, Weiner, Harrington,
MoreTidemanAndChieruzzi, Getzler)
MoreTidemanAndChieruzzi, Getzler, Leyvraz)
from .backstabber import BackStabber, DoubleCrosser
from .better_and_better import BetterAndBetter
from .bush_mosteller import BushMosteller
Expand Down Expand Up @@ -197,6 +197,7 @@
Kluepfel,
KnowledgeableWorseAndWorse,
LevelPunisher,
Leyvraz,
LimitedRetaliate,
LimitedRetaliate2,
LimitedRetaliate3,
Expand Down
64 changes: 59 additions & 5 deletions axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,11 +1300,11 @@ def strategy(self, opponent: Player) -> Action:
if self.burned or random.random() > self.prob:
# Tit-for-Tat with probability 1-`prob`
return self.try_return(opponent.history[-1], inc_parity=True)
else:
# Otherwise Defect, Cooperate, Cooperate, and increase `prob`
self.prob += 0.05
self.more_coop, self.last_generous_n_turns_ago = 2, 1
return self.try_return(D, lower_flags=False)

# Otherwise Defect, Cooperate, Cooperate, and increase `prob`
self.prob += 0.05
self.more_coop, self.last_generous_n_turns_ago = 2, 1
return self.try_return(D, lower_flags=False)


class MoreTidemanAndChieruzzi(Player):
Expand Down Expand Up @@ -1454,3 +1454,57 @@ def strategy(self, opponent: Player) -> Action:
self.flack *= 0.5 # Defections have half-life of one round

return random_choice(1.0 - self.flack)


class Leyvraz(Player):
"""
Strategy submitted to Axelrod's second tournament by Fransois Leyvraz
(K68R) and came in eleventh in that tournament.
The strategy uses the opponent's last three moves to decide on an action
based on the following ordered rules.
1. If opponent Defected last two turns, then Defect with prob 75%.
2. If opponent Defected three turns ago, then Cooperate.
3. If opponent Defected two turns ago, then Defect.
4. If opponent Defected last turn, then Defect with prob 50%.
5. Otherwise (all Cooperations), then Cooperate.
Names:
- Leyvraz: [Axelrod1980b]_
"""

name = 'Leyvraz'
classifier = {
'memory_depth': 3,
'stochastic': True,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def __init__(self) -> None:
super().__init__()
self.prob_coop = {
(C, C, C): 1.0,
(C, C, D): 0.5, # Rule 4
(C, D, C): 0.0, # Rule 3
(C, D, D): 0.25, # Rule 1
(D, C, C): 1.0, # Rule 2
(D, C, D): 1.0, # Rule 2
(D, D, C): 1.0, # Rule 2
(D, D, D): 0.25 # Rule 1
}

def strategy(self, opponent: Player) -> Action:
recent_history = [C, C, C] # Default to C.
for go_back in range(1, 4):
if len(opponent.history) >= go_back:
recent_history[-go_back] = opponent.history[-go_back]

return random_choice(self.prob_coop[(recent_history[-3],
recent_history[-2],
recent_history[-1])])
30 changes: 30 additions & 0 deletions axelrod/tests/strategies/test_axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,33 @@ def test_strategy(self):
actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)]
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=4, attrs={"flack": 5./16.})


class TestLeyvraz(TestPlayer):
name = 'Leyvraz'
player = axelrod.Leyvraz
expected_classifier = {
'memory_depth': 3,
'stochastic': True,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
actions = [(C, C)] * 100
self.versus_test(axelrod.Cooperator(), expected_actions=actions)

actions = [(C, D), (C, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1)
actions = [(C, D), (D, D), (D, D), (C, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2)

actions = [(C, D), (C, C), (D, C), (C, D), (D, C), (D, D), (C, D), (D, C), (C, D)]
self.versus_test(axelrod.SuspiciousTitForTat(), expected_actions=actions, seed=1)

actions = [(C, C), (C, D), (D, C)] + [(D, D), (C, C)] * 3
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2)
actions = [(C, C), (C, D), (C, C)] + [(D, D), (C, C)] * 3
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3)
2 changes: 1 addition & 1 deletion docs/reference/overview_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ repository.
"K65R_", "Mark F Batell", "Not Implemented"
"K66R_", "Ray Mikkelson", "Not Implemented"
"K67R_", "Craig Feathers", ":class:`Tranquilizer <axelrod.strategies.axelrod_second.Tranquilizer>`"
"K68R_", "Fransois Leyvraz", "Not Implemented"
"K68R_", "Fransois Leyvraz", ":class:`Leyvraz <axelrod.strategies.axelrod_second.Leyvraz>`"
"K69R_", "Johann Joss", "Not Implemented"
"K70R_", "Robert Pebly", "Not Implemented"
"K71R_", "James E Hall", "Not Implemented"
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ strategies::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
80
81

Or, to find out how many strategies only use 1 turn worth of memory to
make a decision::
Expand All @@ -69,7 +69,7 @@ range of memory_depth values, we can use the 'min_memory_depth' and
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
56
57

We can also identify strategies that make use of particular properties of the
tournament. For example, here is the number of strategies that make use of the
Expand Down

0 comments on commit bc9cd5a

Please sign in to comment.