Skip to content

Commit bc9cd5a

Browse files
Merge pull request #1153 from gaffney2010/master
Implemented Leyvraz, K86R from Axelrod's second
2 parents 3776881 + 835764a commit bc9cd5a

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .axelrod_second import (
1111
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
1212
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen, Weiner, Harrington,
13-
MoreTidemanAndChieruzzi, Getzler)
13+
MoreTidemanAndChieruzzi, Getzler, Leyvraz)
1414
from .backstabber import BackStabber, DoubleCrosser
1515
from .better_and_better import BetterAndBetter
1616
from .bush_mosteller import BushMosteller
@@ -197,6 +197,7 @@
197197
Kluepfel,
198198
KnowledgeableWorseAndWorse,
199199
LevelPunisher,
200+
Leyvraz,
200201
LimitedRetaliate,
201202
LimitedRetaliate2,
202203
LimitedRetaliate3,

axelrod/strategies/axelrod_second.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,11 +1300,11 @@ def strategy(self, opponent: Player) -> Action:
13001300
if self.burned or random.random() > self.prob:
13011301
# Tit-for-Tat with probability 1-`prob`
13021302
return self.try_return(opponent.history[-1], inc_parity=True)
1303-
else:
1304-
# Otherwise Defect, Cooperate, Cooperate, and increase `prob`
1305-
self.prob += 0.05
1306-
self.more_coop, self.last_generous_n_turns_ago = 2, 1
1307-
return self.try_return(D, lower_flags=False)
1303+
1304+
# Otherwise Defect, Cooperate, Cooperate, and increase `prob`
1305+
self.prob += 0.05
1306+
self.more_coop, self.last_generous_n_turns_ago = 2, 1
1307+
return self.try_return(D, lower_flags=False)
13081308

13091309

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

14561456
return random_choice(1.0 - self.flack)
1457+
1458+
1459+
class Leyvraz(Player):
1460+
"""
1461+
Strategy submitted to Axelrod's second tournament by Fransois Leyvraz
1462+
(K68R) and came in eleventh in that tournament.
1463+
1464+
The strategy uses the opponent's last three moves to decide on an action
1465+
based on the following ordered rules.
1466+
1467+
1. If opponent Defected last two turns, then Defect with prob 75%.
1468+
2. If opponent Defected three turns ago, then Cooperate.
1469+
3. If opponent Defected two turns ago, then Defect.
1470+
4. If opponent Defected last turn, then Defect with prob 50%.
1471+
5. Otherwise (all Cooperations), then Cooperate.
1472+
1473+
Names:
1474+
1475+
- Leyvraz: [Axelrod1980b]_
1476+
"""
1477+
1478+
name = 'Leyvraz'
1479+
classifier = {
1480+
'memory_depth': 3,
1481+
'stochastic': True,
1482+
'makes_use_of': set(),
1483+
'long_run_time': False,
1484+
'inspects_source': False,
1485+
'manipulates_source': False,
1486+
'manipulates_state': False
1487+
}
1488+
1489+
def __init__(self) -> None:
1490+
super().__init__()
1491+
self.prob_coop = {
1492+
(C, C, C): 1.0,
1493+
(C, C, D): 0.5, # Rule 4
1494+
(C, D, C): 0.0, # Rule 3
1495+
(C, D, D): 0.25, # Rule 1
1496+
(D, C, C): 1.0, # Rule 2
1497+
(D, C, D): 1.0, # Rule 2
1498+
(D, D, C): 1.0, # Rule 2
1499+
(D, D, D): 0.25 # Rule 1
1500+
}
1501+
1502+
def strategy(self, opponent: Player) -> Action:
1503+
recent_history = [C, C, C] # Default to C.
1504+
for go_back in range(1, 4):
1505+
if len(opponent.history) >= go_back:
1506+
recent_history[-go_back] = opponent.history[-go_back]
1507+
1508+
return random_choice(self.prob_coop[(recent_history[-3],
1509+
recent_history[-2],
1510+
recent_history[-1])])

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,3 +930,33 @@ def test_strategy(self):
930930
actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)]
931931
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=4, attrs={"flack": 5./16.})
932932

933+
934+
class TestLeyvraz(TestPlayer):
935+
name = 'Leyvraz'
936+
player = axelrod.Leyvraz
937+
expected_classifier = {
938+
'memory_depth': 3,
939+
'stochastic': True,
940+
'makes_use_of': set(),
941+
'long_run_time': False,
942+
'inspects_source': False,
943+
'manipulates_source': False,
944+
'manipulates_state': False
945+
}
946+
947+
def test_strategy(self):
948+
actions = [(C, C)] * 100
949+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
950+
951+
actions = [(C, D), (C, D), (D, D), (D, D)]
952+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1)
953+
actions = [(C, D), (D, D), (D, D), (C, D)]
954+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2)
955+
956+
actions = [(C, D), (C, C), (D, C), (C, D), (D, C), (D, D), (C, D), (D, C), (C, D)]
957+
self.versus_test(axelrod.SuspiciousTitForTat(), expected_actions=actions, seed=1)
958+
959+
actions = [(C, C), (C, D), (D, C)] + [(D, D), (C, C)] * 3
960+
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2)
961+
actions = [(C, C), (C, D), (C, C)] + [(D, D), (C, C)] * 3
962+
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3)

docs/reference/overview_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ repository.
136136
"K65R_", "Mark F Batell", "Not Implemented"
137137
"K66R_", "Ray Mikkelson", "Not Implemented"
138138
"K67R_", "Craig Feathers", ":class:`Tranquilizer <axelrod.strategies.axelrod_second.Tranquilizer>`"
139-
"K68R_", "Fransois Leyvraz", "Not Implemented"
139+
"K68R_", "Fransois Leyvraz", ":class:`Leyvraz <axelrod.strategies.axelrod_second.Leyvraz>`"
140140
"K69R_", "Johann Joss", "Not Implemented"
141141
"K70R_", "Robert Pebly", "Not Implemented"
142142
"K71R_", "James E Hall", "Not Implemented"

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ strategies::
4747
... }
4848
>>> strategies = axl.filtered_strategies(filterset)
4949
>>> len(strategies)
50-
80
50+
81
5151

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

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

0 commit comments

Comments
 (0)