Skip to content

Commit 31e60df

Browse files
committed
Implemented GraaskampKatzen strategy from Axelrod's Second (k60r)
1 parent dd91f0a commit 31e60df

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
UnnamedStrategy, SteinAndRapoport, TidemanAndChieruzzi)
1010
from .axelrod_second import (
1111
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
12-
Kluepfel, Borufsen, Cave, WmAdams)
12+
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen)
1313
from .backstabber import BackStabber, DoubleCrosser
1414
from .better_and_better import BetterAndBetter
1515
from .bush_mosteller import BushMosteller
@@ -170,6 +170,7 @@
170170
GoByMajority40,
171171
GoByMajority5,
172172
Golden,
173+
GraaskampKatzen,
173174
Gradual,
174175
GradualKiller,
175176
Grofman,

axelrod/strategies/axelrod_second.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,65 @@ def strategy(self, opponent: Player) -> Action:
809809
if number_defects > 9 and opponent.history[-1] == D:
810810
return random_choice((0.5) ** (number_defects - 9))
811811
return C
812+
813+
class GraaskampKatzen(Player):
814+
"""
815+
Strategy submitted to Axelrod's second tournament by Jim Graaskamp and Ken
816+
Katzen (K60R), and came in sixth in that tournament.
817+
818+
Play Tit-for-Tat at first, and track own score. At select checkpoints,
819+
check for a high score. Switch to Default Mode if:
820+
821+
- On move 11, score < 23
822+
- On move 21, score < 53
823+
- On move 31, score < 83
824+
- On move 41, score < 113
825+
- On move 51, score < 143
826+
- On move 101, score < 293
827+
828+
Once in Defect Mode, defect forever.
829+
830+
Names:
831+
832+
- GraaskampKatzen: [Axelrod1980b]_
833+
"""
834+
835+
name = "GraaskampKatzen"
836+
classifier = {
837+
'memory_depth': float('inf'),
838+
'stochastic': False,
839+
'makes_use_of': set(['game']),
840+
'long_run_time': False,
841+
'inspects_source': False,
842+
'manipulates_source': False,
843+
'manipulates_state': False
844+
}
845+
846+
def __init__(self):
847+
super().__init__()
848+
self.own_score = 0
849+
self.mode = "Normal"
850+
851+
def update_score(self, opponent: Player):
852+
game = self.match_attributes["game"]
853+
last_round = (self.history[-1], opponent.history[-1])
854+
self.own_score += game.score(last_round)[0]
855+
856+
def strategy(self, opponent: Player) -> Action:
857+
if self.mode == "Defect": return D
858+
859+
turn = len(self.history) + 1
860+
if turn == 1: return C
861+
862+
self.update_score(opponent)
863+
864+
if turn == 11 and self.own_score < 23 or \
865+
turn == 21 and self.own_score < 53 or \
866+
turn == 31 and self.own_score < 83 or \
867+
turn == 41 and self.own_score < 113 or \
868+
turn == 51 and self.own_score < 143 or \
869+
turn == 101 and self.own_score < 293:
870+
self.mode = "Defect"
871+
return D
872+
873+
return opponent.history[-1] # Tit-for-Tat

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,39 @@ def test_strategy(self):
585585
(C, D), (D, D), (C, D), (D, D), (C, C)]
586586
actions += [(C, C)] * 99
587587
self.versus_test(changed_man, expected_actions=actions, seed=1)
588+
589+
class Test(TestPlayer):
590+
name = "GraaskampKatzen"
591+
player = axelrod.GraaskampKatzen
592+
expected_classifier = {
593+
'memory_depth': float('inf'),
594+
'stochastic': False,
595+
'makes_use_of': set(["game"]),
596+
'long_run_time': False,
597+
'inspects_source': False,
598+
'manipulates_source': False,
599+
'manipulates_state': False
600+
}
601+
602+
def test_strategy(self):
603+
actions = [(C, C)] * 100 # Cooperate forever
604+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
605+
606+
# GK does not great against
607+
opponent_actions = [C, D, D] * 100
608+
GK_Foil = axelrod.MockPlayer(actions=opponent_actions)
609+
actions = [(C, C), (C, D), (D, D)]
610+
actions += [(D, C), (C, D), (D, D)] * 2
611+
actions += [(D, C)]
612+
actions += [(D, D), (D, D), (D, C)] * 20 # Defect here on
613+
self.versus_test(GK_Foil, expected_actions=actions)
614+
615+
# Fail on second checkpoint
616+
opponent_actions = [C] * 10 + [C, D, D] * 100
617+
Delayed_GK_Foil = axelrod.MockPlayer(actions=opponent_actions)
618+
actions = [(C, C)] * 10
619+
actions += [(C, C), (C, D), (D, D)]
620+
actions += [(D, C), (C, D), (D, D)] * 2
621+
actions += [(D, C)]
622+
actions += [(D, D), (D, D), (D, C)] * 20 # Defect here on
623+
self.versus_test(Delayed_GK_Foil, expected_actions=actions)

docs/reference/overview_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ repository.
130130
"K57R_", "Rudy Nydegger", "Not Implemented"
131131
"K58R_", "Glen Rowsam", "Not Implemented"
132132
"K59R_", "Leslie Downing", "Not Implemented"
133-
"K60R_", "Jim Graaskamp and Ken Katzen", "Not Implemented"
133+
"K60R_", "Jim Graaskamp and Ken Katzen", ":class:`GraaskampKatzen <axelrod.strategies.axelrod_second.GraaskampKatzen>`"
134134
"K61R_", "Danny C Champion", ":class:`Champion <axelrod.strategies.axelrod_second.Champion>`"
135135
"K62R_", "Howard R Hollander", "Not Implemented"
136136
"K63R_", "George Duisman", "Not Implemented"

0 commit comments

Comments
 (0)