Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gladstein strategy #1110

Merged
merged 10 commits into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .axelrod_first import (
Davis, RevisedDowning, Feld, Grofman, Nydegger, Joss, Shubik, Tullock,
UnnamedStrategy, SteinAndRapoport)
from .axelrod_second import Champion, Eatherley, Tester
from .axelrod_second import Champion, Eatherley, Tester, Gladstein
from .backstabber import BackStabber, DoubleCrosser
from .better_and_better import BetterAndBetter
from .calculator import Calculator
Expand Down Expand Up @@ -156,6 +156,7 @@
GellerCooperator,
GellerDefector,
GeneralSoftGrudger,
Gladstein,
GoByMajority,
GoByMajority10,
GoByMajority20,
Expand Down
62 changes: 61 additions & 1 deletion axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def strategy(self, opponent: Player) -> Action:
defection_prop = opponent.defections / len(opponent.history)
if opponent.history[-1] == D:
r = random.random()
if defection_prop > max(0.4, r):
if defection_prop >= max(0.4, r):
return D
return C

Expand Down Expand Up @@ -147,3 +147,63 @@ def strategy(self, opponent: Player) -> Action:
def reset(self):
super().reset()
self.is_TFT = False


class Gladstein(Player):
"""
Submitted to Axelrod's second tournament by David Gladstein.

This strategy is also known as Tester and is based on the reverse
engineering of the Fortran strategies from Axelrod's second tournament.

This strategy is a TFT variant that defects on the first round in order to
test the opponent's response. If the opponent ever defects, the strategy
'apologizes' by cooperating and then plays TFT for the rest of the game.
Otherwise, it defects as much as possible subject to the constraint that
the ratio of its defections to moves remains under 0.5, not counting the
first defection.

Names:

- Gladstein: [Axelrod1980b]_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the name Tester here, please?

- Tester: [Axelrod1980b]_
"""

name = "Gladstein"
classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def __init__(self) -> None:
super().__init__()
# This strategy assumes the opponent is a patsy
self.patsy = True

def strategy(self, opponent: Player) -> Action:
# Defect on the first move
if not self.history:
return D
# Is the opponent a patsy?
if self.patsy:
# If the opponent defects, apologize and play TFT.
if opponent.history[-1] == D:
self.patsy = False
return C
# Cooperate as long as the cooperation ratio is below 0.5
cooperation_ratio = self.cooperations / len(self.history)
if cooperation_ratio >= 0.5:
return D
return C
else:
# Play TFT
return opponent.history[-1]

def reset(self):
super().reset()
self.patsy = True
40 changes: 38 additions & 2 deletions axelrod/tests/strategies/test_axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_strategy(self):
match_attributes={"length": 200}, seed=2)



class TestEatherley(TestPlayer):

name = "Eatherley"
Expand Down Expand Up @@ -103,7 +102,7 @@ class TestTester(TestPlayer):

def test_strategy(self):
# Alternate after 3rd round if opponent only cooperates
actions = [(D, C)] + [(C, C), (C, C)] + [(D, C), (C, C)] * 4
actions = [(D, C)] + [(C, C), (C, C)] + [(D, C), (C, C)] * 4
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
attrs={"is_TFT": False})

Expand All @@ -117,3 +116,40 @@ def test_strategy(self):
actions = [(D, C), (C, D), (C, C), (C, D), (D, D), (D, C), (C, C)]
self.versus_test(opponent, expected_actions=actions,
attrs={"is_TFT": True})


class TestGladstein(TestPlayer):

name = "Gladstein"
player = axelrod.Gladstein
expected_classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
# Cooperates and begins to play TFT when Alternator defects
actions = [(D, C), (C, D), (C, C), (C, D), (D, C)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
attrs={'patsy': False})

# Cooperation ratio will always be less than or equal to 0.5
actions = [(D, C), (C, C), (D, C), (C, C), (D, C)]
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
attrs={'patsy': True})

# Apologizes immediately and plays TFT
actions = [(D, D), (C, D), (D, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions,
attrs={'patsy': False})

# Ratio is 1/3 when MockPlayer defected for the first time.
opponent = axelrod.MockPlayer(actions=[C, C, C, D, D])
actions = [(D, C), (C, C), (D, C), (C, D), (C, D)]
self.versus_test(opponent, expected_actions=actions,
attrs={'patsy': False})
2 changes: 1 addition & 1 deletion axelrod/tests/strategies/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class TestMetaMajorityLongMemory(TestMetaPlayer):
}

def test_strategy(self):
actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
actions = [(C, C), (C, D), (C, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=0)

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/overview_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ repository.
"K58R_", "Glen Rowsam", "Not Implemented"
"K59R_", "Leslie Downing", "Not Implemented"
"K60R_", "Jim Graaskamp and Ken Katzen", "Not Implemented"
"K61R_", "Danny C Champion", "Not Implemented"
"K61R_", "Danny C Champion", ":class:`Champion <axelrod.strategies.axelrod_second.Champion>`"
"K62R_", "Howard R Hollander", "Not Implemented"
"K63R_", "George Duisman", "Not Implemented"
"K64R_", "Brian Yamachi", "Not Implemented"
Expand All @@ -147,7 +147,7 @@ repository.
"K74R_", "Edward Friedland", "Not Implemented"
"K74RXX_", "Edward Friedland", "Not Implemented"
"K75R_", "Paul D Harrington", "Not Implemented"
"K76R_", "David Gladstein", "Not Implemented"
"K76R_", "David Gladstein", ":class:`Gladstein <axelrod.strategies.axelrod_second.Gladstein>`"
"K77R_", "Scott Feld", "Not Implemented"
"K78R_", "Fred Mauk", "Not Implemented"
"K79R_", "Dennis Ambuehl and Kevin Hickey", Not Implemented
Expand Down