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

Fix PEP8 errors in Tit For Tat #750

Merged
merged 4 commits into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .eggs/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This directory contains eggs that were downloaded by setuptools to build, test, and run plug-ins.
Copy link
Member

Choose a reason for hiding this comment

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

Should we not just gitignore this directory?

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't notice that creep in there. I'll sort it.

Copy link
Member Author

Choose a reason for hiding this comment

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

hmmm..... our .gitignore looks a little light. Perhaps another PR coming shortly......


This directory caches those eggs to prevent repeated downloads.

However, it is safe to delete this directory.

13 changes: 8 additions & 5 deletions axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class SuspiciousTitForTat(Player):

name = "Suspicious Tit For Tat"
classifier = {
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
Expand Down Expand Up @@ -267,7 +267,7 @@ class OmegaTFT(Player):
"""OmegaTFT modifies Tit For Tat in two ways:
- checks for deadlock loops of alternating rounds of (C, D) and (D, C),
and attempting to break them
- uses a more sophisticated retaliation mechanism that is noise tolerant.
- uses a more sophisticated retaliation mechanism that is noise tolerant

Names:

Expand Down Expand Up @@ -315,9 +315,10 @@ def strategy(self, opponent):
# If the opponent's move changed, increase the counter
if opponent.history[-2] != opponent.history[-1]:
self.randomness_counter += 1
# If the opponent's last move differed from mine, increase the counter
# If the opponent's last move differed from mine,
# increase the counter
if self.history[-1] == opponent.history[-1]:
self.randomness_counter+= 1
self.randomness_counter += 1
# Compare counts to thresholds
# If randomness_counter exceeds Y, Defect for the remainder
if self.randomness_counter >= 8:
Expand Down Expand Up @@ -479,6 +480,7 @@ def strategy(self, opponent):
# Otherwise cooperate
return C


class AdaptiveTitForTat(Player):
"""ATFT - Adaptive Tit For Tat (Basic Model)

Expand Down Expand Up @@ -559,7 +561,8 @@ def __repr__(self):
class SpitefulTitForTat(Player):
"""
A player starts by cooperating and then mimics the previous action of the
opponent until opponent defects twice in a row, at which point player always defects
opponent until opponent defects twice in a row, at which point player
always defects

Names:

Expand Down
191 changes: 132 additions & 59 deletions axelrod/tests/unit/test_titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def test_strategy(self):
self.first_play_test(C)

def test_effect_of_strategy(self):
"""Will try defecting after two turns of cooperation, but will stop if punished."""
"""Will try defecting after two turns of cooperation, but will stop
if punished."""
self.responses_test([C, C], [C, C], [D])
self.responses_test([C, C, D, D], [C, C, C, D], [C])

Expand All @@ -139,7 +140,7 @@ class TestSuspiciousTitForTat(TestPlayer):
name = 'Suspicious Tit For Tat'
player = axelrod.SuspiciousTitForTat
expected_classifier = {
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'stochastic': False,
'makes_use_of': set(),
'inspects_source': False,
Expand All @@ -152,7 +153,8 @@ def test_strategy(self):
self.first_play_test(D)

def test_effect_of_strategy(self):
"""Plays like TFT after the first move, repeating the opponents last move."""
"""Plays like TFT after the first move, repeating the opponents last
move."""
self.markov_test([C, D, C, D])


Expand All @@ -161,7 +163,7 @@ class TestAntiTitForTat(TestPlayer):
name = 'Anti Tit For Tat'
player = axelrod.AntiTitForTat
expected_classifier = {
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.)
'stochastic': False,
'makes_use_of': set(),
'inspects_source': False,
Expand All @@ -183,7 +185,7 @@ class TestHardTitForTat(TestPlayer):
name = "Hard Tit For Tat"
player = axelrod.HardTitForTat
expected_classifier = {
'memory_depth': 3, # Four-Vector = (1.,0.,1.,0.)
'memory_depth': 3, # Four-Vector = (1.,0.,1.,0.)
'stochastic': False,
'makes_use_of': set(),
'inspects_source': False,
Expand Down Expand Up @@ -268,16 +270,20 @@ def test_reset(self):

class TestOmegaTFTvsSTFT(TestHeadsUp):
def test_rounds(self):
outcomes = zip()
self.versus_test(axelrod.OmegaTFT(), axelrod.SuspiciousTitForTat(),
[C, D, C, D, C, C, C, C, C],
[D, C, D, C, D, C, C, C, C])
self.versus_test(
axelrod.OmegaTFT(), axelrod.SuspiciousTitForTat(),
[C, D, C, D, C, C, C, C, C],
[D, C, D, C, D, C, C, C, C]
)


class TestOmegaTFTvsAlternator(TestHeadsUp):
def test_rounds(self):
self.versus_test(axelrod.OmegaTFT(), axelrod.Alternator(),
[C, C, D, C, D, C, C, C, D, C, C, C, D, D, D, D, D, D],
[C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D])
self.versus_test(
axelrod.OmegaTFT(), axelrod.Alternator(),
[C, C, D, C, D, C, C, C, D, C, C, C, D, D, D, D, D, D],
[C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D, C, D]
)


class TestGradual(TestPlayer):
Expand All @@ -300,38 +306,96 @@ def test_strategy(self):
def test_effect_of_strategy(self):
"""Punishes defection with a growing number of defections and calms
the opponent with two Cooperations in a row"""
self.responses_test([C], [C], [C], attrs={"calming": False,
"punishing": False, "punishment_count": 0,
"punishment_limit": 0})
self.responses_test([C], [D], [D], attrs={"calming": False,
"punishing": True, "punishment_count": 1,
"punishment_limit": 1})
self.responses_test([C, D], [D, C], [C], attrs={"calming": True,
"punishing": False, "punishment_count": 0,
"punishment_limit": 1})
self.responses_test([C, D, C], [D, C, D], [C], attrs={"calming": False,
"punishing": False, "punishment_count": 0,
"punishment_limit": 1})
self.responses_test([C, D, C, C], [D, C, D, C], [C],
attrs={"calming": False, "punishing": False,
"punishment_count": 0, "punishment_limit": 1})
self.responses_test([C, D, C, D, C], [D, C, D, C, C], [C],
attrs={"calming": False, "punishing": False,
"punishment_count": 0, "punishment_limit": 1})
self.responses_test([C, D, C, D, C, C], [D, C, D, C, C, D], [D],
attrs={"calming": False, "punishing": True,
"punishment_count": 1, "punishment_limit": 2})
self.responses_test([C, D, C, D, D, C, D], [D, C, D, C, C, D, C], [D],
attrs={"calming": False, "punishing": True,
"punishment_count": 2, "punishment_limit": 2})
self.responses_test([C, D, C, D, D, C, D, D],
[D, C, D, C, C, D, C, C], [C],
attrs={"calming": True, "punishing": False,
"punishment_count": 0, "punishment_limit": 2})
self.responses_test([C, D, C, D, D, C, D, D, C],
[D, C, D, C, C, D, C, C, C], [C],
attrs={"calming": False, "punishing": False,
"punishment_count": 0, "punishment_limit": 2})
self.responses_test(
[C], [C], [C],
attrs={
"calming": False,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 0
}
)
self.responses_test(
[C], [D], [D],
attrs={
"calming": False,
"punishing": True,
"punishment_count": 1,
"punishment_limit": 1
}
)
self.responses_test(
[C, D], [D, C], [C],
attrs={
"calming": True,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 1
}
)
self.responses_test(
[C, D, C], [D, C, D], [C],
attrs={
"calming": False,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 1
}
)
self.responses_test(
[C, D, C, C], [D, C, D, C], [C],
attrs={
"calming": False,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 1
}
)
self.responses_test(
[C, D, C, D, C], [D, C, D, C, C], [C],
attrs={
"calming": False,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 1
}
)
self.responses_test(
[C, D, C, D, C, C], [D, C, D, C, C, D], [D],
attrs={
"calming": False,
"punishing": True,
"punishment_count": 1,
"punishment_limit": 2
}
)
self.responses_test(
[C, D, C, D, D, C, D], [D, C, D, C, C, D, C], [D],
attrs={
"calming": False,
"punishing": True,
"punishment_count": 2,
"punishment_limit": 2
}
)
self.responses_test(
[C, D, C, D, D, C, D, D], [D, C, D, C, C, D, C, C], [C],
attrs={
"calming": True,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 2
}
)
self.responses_test(
[C, D, C, D, D, C, D, D, C], [D, C, D, C, C, D, C, C, C], [C],
attrs={
"calming": False,
"punishing": False,
"punishment_count": 0,
"punishment_limit": 2
}
)

def test_reset_cleans_all(self):
p = axelrod.Gradual()
Expand Down Expand Up @@ -432,13 +496,13 @@ def test_strategy_with_noise(self):
self.assertEqual(opponent.history, [C, D, D, D])
self.assertFalse(ctft.contrite)


def test_reset_cleans_all(self):
p = self.player()
p.contrite = True
p.reset()
self.assertFalse(p.contrite)


class TestSlowTitForTwoTats(TestPlayer):

name = "Slow Tit For Two Tats"
Expand All @@ -457,10 +521,12 @@ def test_strategy(self):
self.first_play_test(C)

def test_effect_of_strategy(self):
"""If opponent plays the same move twice, repeats last action of opponent history."""
self.responses_test([C]*2, [C, C], [C])
self.responses_test([C]*3, [C, D, C], [C])
self.responses_test([C]*3, [C, D, D], [D])
"""If opponent plays the same move twice, repeats last action of
opponent history."""
self.responses_test([C] * 2, [C, C], [C])
self.responses_test([C] * 3, [C, D, C], [C])
self.responses_test([C] * 3, [C, D, D], [D])


class TestAdaptiveTitForTat(TestPlayer):

Expand All @@ -474,20 +540,20 @@ class TestAdaptiveTitForTat(TestPlayer):
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
"""Start by cooperating."""
self.first_play_test(C)

def test_effect_of_strategy(self):

self.markov_test(['C', 'D', 'C', 'D'])

p1, p2 = self.player(), self.player()
p1.play(p2)
p1.play(p2)
self.assertEqual(p2.world, 0.75)

def test_world_rate_reset(self):
p1, p2 = self.player(), self.player()
p1.play(p2)
Expand All @@ -496,7 +562,7 @@ def test_world_rate_reset(self):
self.assertEqual(p2.world, 0.5)
self.assertEqual(p2.rate, 0.5)


class TestSpitefulTitForTat(TestPlayer):
name = "Spiteful Tit For Tat"
player = axelrod.SpitefulTitForTat
Expand All @@ -514,11 +580,18 @@ def test_strategy(self):
self.first_play_test(C)

def test_effect_of_strategy(self):
"""Repeats last action of opponent history until 2 consecutive defections, then always defects"""
"""Repeats last action of opponent history until 2 consecutive
defections, then always defects"""
self.markov_test([C, D, C, D])
self.responses_test([C] * 4, [C, C, C, C], [C], attrs={"retaliating": False})
self.responses_test([C] * 5, [C, C, C, C, D], [D], attrs={"retaliating": False})
self.responses_test([C] * 5, [C, C, D, D, C], [D], attrs={"retaliating": True})
self.responses_test(
[C] * 4, [C, C, C, C], [C], attrs={"retaliating": False}
)
self.responses_test(
[C] * 5, [C, C, C, C, D], [D], attrs={"retaliating": False}
)
self.responses_test(
[C] * 5, [C, C, D, D, C], [D], attrs={"retaliating": True}
)

def test_reset_retaliating(self):
player = self.player()
Expand Down