Ejemplo n.º 1
0
    def test_set_memory_depth(self):
        mem_depth_1 = axl.LookerUp(pattern="CC", parameters=Plays(1, 0, 0))
        self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_1), 1)

        mem_depth_3 = axl.LookerUp(pattern="C" * 16, parameters=Plays(1, 3, 0))
        self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_3), 3)

        mem_depth_inf = axl.LookerUp(pattern="CC", parameters=Plays(0, 0, 1))
        self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_inf), float("inf"))
Ejemplo n.º 2
0
    def test_pattern_and_params_init_only_happens_if_both_are_present(self):
        default = {Plays((), (D, ), ()): D, Plays((), (C, ), ()): C}
        pattern = "CC"
        parameters = Plays(self_plays=0, op_plays=1, op_openings=0)
        player1 = axelrod.LookerUp(pattern=pattern)
        player2 = axelrod.LookerUp(parameters=parameters)

        self.assertEqual(player1.lookup_dict, default)
        self.assertEqual(player2.lookup_dict, default)
Ejemplo n.º 3
0
    def test_init_raises_errors(self):
        mismatch_dict = {((C, ), (C, ), ()): C, ((D, D), (D, D), ()): C}
        with self.assertRaises(ValueError):
            axelrod.LookerUp(lookup_dict=mismatch_dict)

        incomplete_lookup_dict = {((C, ), (C, ), ()): C, ((D, ), (D, ), ()): C}
        with self.assertRaises(ValueError):
            axelrod.LookerUp(lookup_dict=incomplete_lookup_dict)

        too_short_pattern = "CC"
        with self.assertRaises(ValueError):
            axelrod.LookerUp(pattern=too_short_pattern, parameters=(3, 3, 3))
Ejemplo n.º 4
0
    def test_set_memory_depth(self):
        mem_depth_1 = axelrod.LookerUp(pattern="CC", parameters=Plays(1, 0, 0))
        self.assertEqual(mem_depth_1.classifier["memory_depth"], 1)

        mem_depth_3 = axelrod.LookerUp(pattern="C" * 16,
                                       parameters=Plays(1, 3, 0))
        self.assertEqual(mem_depth_3.classifier["memory_depth"], 3)

        mem_depth_inf = axelrod.LookerUp(pattern="CC",
                                         parameters=Plays(0, 0, 1))
        self.assertEqual(mem_depth_inf.classifier["memory_depth"],
                         float("inf"))
Ejemplo n.º 5
0
    def test_set_memory_depth(self):
        mem_depth_1 = axelrod.LookerUp(pattern='CC', parameters=Plays(1, 0, 0))
        self.assertEqual(mem_depth_1.classifier['memory_depth'], 1)

        mem_depth_3 = axelrod.LookerUp(pattern='C' * 16,
                                       parameters=Plays(1, 3, 0))
        self.assertEqual(mem_depth_3.classifier['memory_depth'], 3)

        mem_depth_inf = axelrod.LookerUp(pattern='CC',
                                         parameters=Plays(0, 0, 1))
        self.assertEqual(mem_depth_inf.classifier['memory_depth'],
                         float('inf'))
Ejemplo n.º 6
0
    def test_starting_move(self):
        """A lookup table that always repeats the opponent's first move."""

        first_move_table = {
            # If opponent starts by cooperating:
            (C, C, D): C,
            (C, D, D): C,
            (C, C, C): C,
            (C, D, C): C,
            # If opponent starts by defecting:
            (D, C, D): D,
            (D, D, D): D,
            (D, C, C): D,
            (D, D, C): D,
        }

        self.player = lambda: axelrod.LookerUp(first_move_table)

        # if the opponent started by cooperating, we should always cooperate
        self.responses_test([C], [C, C, C], [C, C, C])
        self.responses_test([C], [D, D, D], [C, C, C])
        self.responses_test([C], [C, C, C], [C, D, C])
        self.responses_test([C], [C, C, D], [C, D, C])

        # if the opponent started by defecting, we should always defect
        self.responses_test([D], [C, C, C], [D, C, C])
        self.responses_test([D], [D, D, D], [D, C, C])
        self.responses_test([D], [C, C, C], [D, D, C])
        self.responses_test([D], [C, C, D], [D, D, C])
Ejemplo n.º 7
0
def do_table(table):
    """
    Take a lookup table dict, construct a lambda factory for it, and return
    a tuple of the score and the table itself
    """
    fac = lambda: axelrod.LookerUp(lookup_table=table)
    return (score_for(fac), table)
Ejemplo n.º 8
0
 def test_initial_actions_makes_up_missing_actions_with_c(self):
     initial_acitons = (D, )
     table_depth_three = axelrod.LookerUp(
         initial_actions=initial_acitons,
         pattern="CCCCCCCC",
         parameters=Plays(3, 0, 0),
     )
     self.assertEqual(table_depth_three.initial_actions, (D, C, C))
Ejemplo n.º 9
0
 def test_pattern_and_params_init_can_still_use_regular_tuple(self):
     pattern = (C, C)
     parameters = (1, 0, 0)
     player = axelrod.LookerUp(pattern=pattern, parameters=parameters)
     expected_lookup_table = {
         Plays((C, ), (), ()): C,
         Plays((D, ), (), ()): C
     }
     self.assertEqual(player.lookup_dict, expected_lookup_table)
Ejemplo n.º 10
0
 def test_lookup_table_init(self):
     lookup_table = {
         ((C, ), (D, ), ()): C,
         ((D, ), (D, ), ()): C,
         ((C, ), (C, ), ()): C,
         ((D, ), (C, ), ()): C,
     }
     player = axelrod.LookerUp(lookup_dict=lookup_table)
     self.assertEqual(player.lookup_dict, lookup_table)
     self.assertIsInstance(next(iter(player.lookup_dict)), Plays)
Ejemplo n.º 11
0
 def test_pattern_and_params_init_pattern_is_string(self):
     pattern = "CCCC"
     parameters = Plays(1, 1, 0)
     player = axl.LookerUp(pattern=pattern, parameters=parameters)
     expected_lookup_table = {
         Plays((C,), (D,), ()): C,
         Plays((D,), (D,), ()): C,
         Plays((C,), (C,), ()): C,
         Plays((D,), (C,), ()): C,
     }
     self.assertEqual(player.lookup_dict, expected_lookup_table)
Ejemplo n.º 12
0
 def test_pattern_and_params_init_pattern_is_tuple(self):
     pattern = (C, C, C, C)
     parameters = Plays(1, 1, 0)
     player = axelrod.LookerUp(pattern=pattern, parameters=parameters)
     expected_lookup_table = {
         Plays((C, ), (D, ), ()): C,
         Plays((D, ), (D, ), ()): C,
         Plays((C, ), (C, ), ()): C,
         Plays((D, ), (C, ), ()): C,
     }
     self.assertEqual(player.lookup_dict, expected_lookup_table)
Ejemplo n.º 13
0
    def test_zero_tables(self):
        """Test the corner case where n=0."""
        pattern = "CD"
        lookup_table_keys = create_lookup_table_keys(
            plays=0, op_plays=2, op_start_plays=0)

        lookup_table = dict(zip(lookup_table_keys, pattern))
        player = axelrod.LookerUp(lookup_table)
        self.assertEqual(player.plays, 0)
        opp = axelrod.Cooperator()
        # This shouldn't throw an exception.
        for _ in range(5):
            player.play(opp)
Ejemplo n.º 14
0
 def test_lookup_table_display(self):
     player = axelrod.LookerUp(pattern='CCCC',
                               parameters=Plays(self_plays=2,
                                                op_plays=0,
                                                op_openings=0))
     self.assertEqual(
         player.lookup_table_display(
             ('self_plays', 'op_plays', 'op_openings')),
         ("self_plays | op_plays  |op_openings\n" +
          "   C, C    ,           ,           : C,\n" +
          "   C, D    ,           ,           : C,\n" +
          "   D, C    ,           ,           : C,\n" +
          "   D, D    ,           ,           : C,\n"))
Ejemplo n.º 15
0
 def test_lookup_table_display(self):
     player = axl.LookerUp(
         pattern="CCCC", parameters=Plays(self_plays=2, op_plays=0, op_openings=0)
     )
     self.assertEqual(
         player.lookup_table_display(("self_plays", "op_plays", "op_openings")),
         (
             "self_plays | op_plays  |op_openings\n"
             + "   C, C    ,           ,           : C,\n"
             + "   C, D    ,           ,           : C,\n"
             + "   D, C    ,           ,           : C,\n"
             + "   D, D    ,           ,           : C,\n"
         ),
     )
Ejemplo n.º 16
0
    def test_lookup_table_init_supersedes_pattern_init(self):
        lookup_table = {
            ((C, ), (D, ), ()): D,
            ((D, ), (D, ), ()): D,
            ((C, ), (C, ), ()): D,
            ((D, ), (C, ), ()): D,
        }
        pattern = "CCCCCCCC"
        parameters = Plays(self_plays=1, op_plays=1, op_openings=1)
        player = axelrod.LookerUp(lookup_dict=lookup_table,
                                  pattern=pattern,
                                  parameters=parameters)

        self.assertEqual(player.lookup_dict, lookup_table)
Ejemplo n.º 17
0
 def test_defector_table(self):
     """
     Testing a lookup table that always defects if there is enough history.
     In order for the testing framework to be able to construct new player
     objects for the test, self.player needs to be callable with no
     arguments, thus we use a lambda expression which will call the
     constructor with the lookup table we want.
     """
     defector_table = {
         ('', C, D): D,
         ('', D, D): D,
         ('', C, C): D,
         ('', D, C): D,
     }
     self.player = lambda : axelrod.LookerUp(defector_table)
     self.responses_test([D], [C, C], [C, C])
     self.responses_test([D], [C, D], [D, C])
     self.responses_test([D], [D, D], [D, D])
Ejemplo n.º 18
0
 def test_initial_actions_set_to_max_table_depth(self):
     initial_actions = (D, D, D)
     table_depth_one = axelrod.LookerUp(initial_actions=initial_actions)
     self.assertEqual(table_depth_one.initial_actions, (D, ))