From 816e0bb780850c556154e0aa17376077757c54d6 Mon Sep 17 00:00:00 2001 From: "T.J. Gaffney" Date: Sat, 21 Sep 2019 22:03:33 -0700 Subject: [PATCH 1/2] Fix bug in Kluepfel strategy --- axelrod/strategies/axelrod_second.py | 6 ++--- .../tests/strategies/test_axelrod_second.py | 27 +++++++++---------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index 6722fe0d6..d5ad5a07d 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -536,11 +536,11 @@ def strategy(self, opponent: Player) -> Action: self.dd_counts += 1 else: if opponent.history[-1] == C: - self.dc_counts += 1 - else: self.cc_counts += 1 + else: + self.dc_counts += 1 - # Check for randomness + # Check for "randomness" if len(self.history) > 26: if self.cd_counts >= (self.cd_counts + self.dd_counts) / 2 - 0.75 * np.sqrt( self.cd_counts + self.dd_counts diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index ce8a85370..9c2296c84 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -546,7 +546,12 @@ class TestKluepfel(TestPlayer): } def test_strategy(self): - actions = [(C, C)] * 100 # Cooperate forever + actions = [(C, C)] * 27 # Cooperate at first + # After this point, the strategy will always detect false, becasue the + # two checks become: + # 0 >= 0, and + # cc_counts >= (cc_counts)/2 - 0.75*sqrt(cc_counts) + actions += [(D, C)] * 100 self.versus_test(axelrod.Cooperator(), expected_actions=actions) # Since never two in a row, will respond in kind with 70% if @@ -598,16 +603,12 @@ def test_strategy(self): (D, C), (C, C), (C, D), - # Success detect random opponent for remaining turns. - (D, D), - (D, D), - (D, D), - (D, C), - (D, D), - (D, C), - (D, D), + (C, D), (D, C), - (D, D), + (C, C), + (C, C), + (D, D), # At this point cc_counts=10, cd_counts=10, dc_counts=10, + # and dd_counts=5. Detect random and defect hereafter. (D, C), (D, C), (D, D), @@ -617,11 +618,7 @@ def test_strategy(self): (D, C), (D, C), (D, D), - (D, C), - (D, C), - (D, C), - (D, C), - (D, D), + (D, C) ] self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=10) From c3a91dbdacabbe3551d710d801ecb94220ad2523 Mon Sep 17 00:00:00 2001 From: "T.J. Gaffney" Date: Thu, 26 Sep 2019 01:09:38 -0700 Subject: [PATCH 2/2] Fix bug in Kluepfel strategy --- axelrod/strategies/axelrod_second.py | 4 +-- .../tests/strategies/test_axelrod_second.py | 27 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index d5ad5a07d..593f9e1f7 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -540,11 +540,11 @@ def strategy(self, opponent: Player) -> Action: else: self.dc_counts += 1 - # Check for "randomness" + # Check for randomness if len(self.history) > 26: if self.cd_counts >= (self.cd_counts + self.dd_counts) / 2 - 0.75 * np.sqrt( self.cd_counts + self.dd_counts - ) and self.cc_counts >= ( + ) and self.dc_counts >= ( self.dc_counts + self.cc_counts ) / 2 - 0.75 * np.sqrt( self.dc_counts + self.cc_counts diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index 9c2296c84..ce8a85370 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -546,12 +546,7 @@ class TestKluepfel(TestPlayer): } def test_strategy(self): - actions = [(C, C)] * 27 # Cooperate at first - # After this point, the strategy will always detect false, becasue the - # two checks become: - # 0 >= 0, and - # cc_counts >= (cc_counts)/2 - 0.75*sqrt(cc_counts) - actions += [(D, C)] * 100 + actions = [(C, C)] * 100 # Cooperate forever self.versus_test(axelrod.Cooperator(), expected_actions=actions) # Since never two in a row, will respond in kind with 70% if @@ -603,22 +598,30 @@ def test_strategy(self): (D, C), (C, C), (C, D), - (C, D), + # Success detect random opponent for remaining turns. + (D, D), + (D, D), + (D, D), (D, C), - (C, C), - (C, C), - (D, D), # At this point cc_counts=10, cd_counts=10, dc_counts=10, - # and dd_counts=5. Detect random and defect hereafter. + (D, D), (D, C), + (D, D), (D, C), (D, D), + (D, C), + (D, C), + (D, D), + (D, D), + (D, C), + (D, C), + (D, C), + (D, C), (D, D), (D, C), (D, C), (D, C), (D, C), (D, D), - (D, C) ] self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=10)