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 0.5
        actions = [(D, C), (C, C), (C, C), (D, C), (C, 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), (C, C), (D, D), (C, D)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         attrs={'patsy': False})

        opponent = axelrod.AntiTitForTat()
        actions = [(D, C), (C, C), (C, D), (C, D), (D, D)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         attrs={'patsy': False})
Beispiel #2
0
 def test_affect_of_strategy(self):
     """Will do opposite of what opponent does."""
     P1 = axelrod.AntiTitForTat()
     P2 = axelrod.Player()
     P1.history = ['D']
     P2.history = ['C']
     self.assertEqual(P1.strategy(P2), 'D')
     P1.history.append('D')
     P2.history.append('D')
     self.assertEqual(P1.strategy(P2), 'C')
     P1.history.append('C')
     P2.history.append('D')
     self.assertEqual(P1.strategy(P2), 'C')
     P1.history.append('C')
     P2.history.append('C')
     self.assertEqual(P1.strategy(P2), 'D')
    def test_strategy(self):
        # Cooperate for the first two rounds
        actions = [(C, C), (C, C)]
        self.versus_test(axelrod.Cooperator(), expected_actions=actions)

        # Cooperate for the first two rounds, then play tit for tat for 3-7
        actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C)]
        self.versus_test(axelrod.Alternator(), expected_actions=actions)

        # Demonstrate MoreGrofman Logic
        # Own previous move was C, opponent defected less than 3 times in last 8
        moregrofman_actions = [C] * 7 + [C]
        opponent_actions = [C] * 6 + [D] * 2
        opponent = axelrod.MockPlayer(actions=opponent_actions)
        actions = list(zip(moregrofman_actions, opponent_actions))
        self.versus_test(opponent, expected_actions=actions)

        # Own previous move was C, opponent defected 3 or more times in last 8
        moregrofman_actions = ([C] * 3 + [D] * 3 + [C]) + [D]
        opponent_actions = ([C] * 2 + [D] * 3 + [C] * 2) + [D]
        opponent = axelrod.MockPlayer(actions=opponent_actions)
        actions = list(zip(moregrofman_actions, opponent_actions))
        self.versus_test(opponent, expected_actions=actions)

        # Own previous move was D, opponent defected once or less in last 8
        moregrofman_actions = ([C] * 6 + [D]) + [C]
        opponent_actions = ([C] * 5 + [D] * 1 + [C]) + [D]
        opponent = axelrod.MockPlayer(actions=opponent_actions)
        actions = list(zip(moregrofman_actions, opponent_actions))
        self.versus_test(opponent, expected_actions=actions)

        # Own previous move was D, opponent defected more than once in last 8
        moregrofman_actions = ([C] * 2 + [D] * 5) + [D]
        opponent_actions = ([D] * 7) + [D]
        opponent = axelrod.MockPlayer(actions=opponent_actions)
        actions = list(zip(moregrofman_actions, opponent_actions))
        self.versus_test(opponent, expected_actions=actions)

        # Test to make sure logic matches Fortran (discrepancy found 8/23/2017)
        opponent = axelrod.AntiTitForTat()
        # Actions come from a match run by Axelrod Fortran using Player('k86r')
        actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (D, D),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C),
                   (C, C)]
        self.versus_test(opponent, expected_actions=actions)

        # Test to match the Fortran implementation for 30 rounds
        opponent = axelrod.AntiTitForTat()
        actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (D, D),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C),
                   (C, C), (C, D), (C, D), (C, D), (C, D), (D, D), (D, C),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (C, C),
                   (C, D), (C, D)]
        self.versus_test(opponent, expected_actions=actions)

        # Test to match the Fortran implementation for 60 rounds
        opponent = axelrod.AntiTitForTat()
        actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (D, D),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (D, C),
                   (C, C), (C, D), (C, D), (C, D), (C, D), (D, D), (D, C),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (D, C), (C, C),
                   (C, D), (C, D), (C, D), (C, D), (D, D), (D, C), (D, C),
                   (D, C), (D, C), (D, C), (D, C), (D, C), (C, C), (C, D),
                   (C, D), (C, D), (C, D), (D, D), (D, C), (D, C), (D, C),
                   (D, C), (D, C), (D, C), (D, C), (C, C), (C, D), (C, D),
                   (C, D), (C, D), (D, D), (D, C)]
        self.versus_test(opponent, expected_actions=actions)
Beispiel #4
0
 def test_stochastic(self):
     self.assertFalse(axelrod.AntiTitForTat().stochastic)
Beispiel #5
0
 def test_representation(self):
     P1 = axelrod.AntiTitForTat()
     self.assertEqual(str(P1), "Anti Tit For Tat")
Beispiel #6
0
 def test_initial_strategy(self):
     """Starts by defecting"""
     P1 = axelrod.AntiTitForTat()
     P2 = axelrod.Player()
     self.assertEqual(P1.strategy(P2), 'D')