Ejemplo n.º 1
0
    def setUpClass(cls):
        cls.test_key = (axl.TitForTat(), axl.Defector())
        cls.test_value = [(C, D), (D, D), (D, D)]
        save_path = pathlib.Path("test_outputs/test_cache_save.txt")
        cls.test_save_file = axl_filename(save_path)
        load_path = pathlib.Path("test_outputs/test_cache_load.txt")
        cls.test_load_file = axl_filename(load_path)
        test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]}
        cls.test_pickle = pickle.dumps(test_data_to_pickle)

        with open(cls.test_load_file, "wb") as f:
            f.write(cls.test_pickle)
Ejemplo n.º 2
0
    def test_analyse_cooperation_ratio(self):
        tf = TransitiveFingerprint(axl.TitForTat)
        path = pathlib.Path("test_outputs/test_fingerprint.csv")
        filename = axl_filename(path)
        with open(filename, "w") as f:
            f.write(
                """Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions
0,0,1,0,Player0,Player1,CCC
0,1,0,0,Player1,Player0,DDD
1,0,1,1,Player0,Player1,CCC
1,1,0,1,Player1,Player0,DDD
2,0,2,0,Player0,Player2,CCD
2,2,0,0,Player2,Player0,DDD
3,0,2,1,Player0,Player2,CCC
3,2,0,1,Player2,Player0,DDD
4,0,3,0,Player0,Player3,CCD
4,3,0,0,Player3,Player0,DDD
5,0,3,1,Player0,Player3,DCC
5,3,0,1,Player3,Player0,DDD
6,0,4,2,Player0,Player4,DDD
6,4,0,2,Player4,Player0,DDD
7,0,4,3,Player0,Player4,DDD
7,4,0,3,Player4,Player0,DDD""")
        data = tf.analyse_cooperation_ratio(filename)
        expected_data = np.array([[1, 1, 1], [1, 1, 1 / 2], [1 / 2, 1, 1 / 2],
                                  [0, 0, 0]])
        self.assertTrue(np.array_equal(data, expected_data))
Ejemplo n.º 3
0
    def setUpClass(cls):
        cls.game = axl.Game()
        cls.players = [s() for s in test_strategies]
        cls.test_name = "test"
        cls.test_repetitions = test_repetitions
        cls.test_turns = test_turns

        cls.expected_payoff = [
            [600, 600, 0, 600, 600],
            [600, 600, 199, 600, 600],
            [1000, 204, 200, 204, 204],
            [600, 600, 199, 600, 600],
            [600, 600, 199, 600, 600],
        ]

        cls.expected_cooperation = [
            [200, 200, 200, 200, 200],
            [200, 200, 1, 200, 200],
            [0, 0, 0, 0, 0],
            [200, 200, 1, 200, 200],
            [200, 200, 1, 200, 200],
        ]

        path = pathlib.Path("test_outputs/test_tournament.csv")
        cls.filename = axl_filename(path)
Ejemplo n.º 4
0
 def test_repeat_tournament_stochastic(self):
     """
     A test to check that tournament gives same results when setting seed.
     """
     files = []
     for _ in range(2):
         axl.seed(0)
         stochastic_players = [
             s() for s in axl.short_run_time_strategies
             if axl.Classifiers["stochastic"](s())
         ]
         tournament = axl.Tournament(
             name="test",
             players=stochastic_players,
             game=self.game,
             turns=2,
             repetitions=2,
         )
         path = pathlib.Path(
             "test_outputs/stochastic_tournament_{}.csv".format(_))
         files.append(axl_filename(path))
         tournament.play(progress_bar=False,
                         filename=files[-1],
                         build_results=False)
     self.assertTrue(filecmp.cmp(files[0], files[1]))
Ejemplo n.º 5
0
    def test_load_error_for_inccorect_format(self):
        path = pathlib.Path("test_outputs/test.cache")
        filename = axl_filename(path)
        with open(filename, "wb") as io:
            pickle.dump(range(5), io)

        with self.assertRaises(ValueError):
            self.cache.load(filename)
Ejemplo n.º 6
0
    def test_axl_filename(self):
        path = pathlib.Path("axelrod/strategies/titfortat.py")
        actual_fn = axl_filename(path)

        # First go from "unit" up to "tests", then up to "axelrod"
        dirname = os.path.dirname(__file__)
        expected_fn = os.path.join(dirname, "../../strategies/titfortat.py")

        self.assertTrue(os.path.samefile(actual_fn, expected_fn))
Ejemplo n.º 7
0
 def test_big_tournaments(self, tournament):
     """A test to check that tournament runs with a sample of non-cheating
     strategies."""
     path = pathlib.Path("test_outputs/test_tournament.csv")
     filename = axl_filename(path)
     self.assertIsNone(
         tournament.play(progress_bar=False,
                         filename=filename,
                         build_results=False))
Ejemplo n.º 8
0
 def test_fingerprint_with_filename(self):
     path = pathlib.Path("test_outputs/test_fingerprint.csv")
     filename = axl_filename(path)
     strategy = axl.TitForTat()
     tf = TransitiveFingerprint(strategy)
     tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename)
     with open(filename, "r") as out:
         data = out.read()
         self.assertEqual(len(data.split("\n")), 102)
Ejemplo n.º 9
0
 def test_serial_fingerprint(self):
     strategy = axl.TitForTat()
     tf = TransitiveFingerprint(strategy)
     path = pathlib.Path("test_outputs/test_fingerprint.csv")
     tf.fingerprint(
         repetitions=1,
         progress_bar=False,
         filename=axl_filename(path),
     )
     self.assertEqual(tf.data.shape, (50, 50))
Ejemplo n.º 10
0
 def test_fingerprint_with_filename(self):
     path = pathlib.Path("test_outputs/test_fingerprint.csv")
     filename = axl_filename(path)
     af = AshlockFingerprint(axl.TitForTat)
     af.fingerprint(
         turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename
     )
     with open(filename, "r") as out:
         data = out.read()
         self.assertEqual(len(data.split("\n")), 20)
Ejemplo n.º 11
0
 def test_write_to_csv_with_results(self):
     tournament = axl.Tournament(
         name=self.test_name,
         players=self.players,
         game=self.game,
         turns=2,
         repetitions=2,
     )
     tournament.play(filename=self.filename, progress_bar=False)
     df = pd.read_csv(self.filename)
     path = pathlib.Path("test_outputs/expected_test_tournament.csv")
     expected_df = pd.read_csv(axl_filename(path))
     self.assertTrue(df.equals(expected_df))
Ejemplo n.º 12
0
    def test_temp_file_creation(self):

        RecordedMksTemp.reset_record()
        af = AshlockFingerprint(axl.TitForTat)
        path = pathlib.Path("test_outputs/test_fingerprint.csv")
        filename = axl_filename(path)

        self.assertEqual(RecordedMksTemp.record, [])

        # Temp file is created and destroyed.
        af.fingerprint(
            turns=1, repetitions=1, step=0.5, progress_bar=False, filename=None
        )

        self.assertEqual(len(RecordedMksTemp.record), 1)
        filename = RecordedMksTemp.record[0][1]
        self.assertIsInstance(filename, str)
        self.assertNotEqual(filename, "")
        self.assertFalse(os.path.isfile(filename))
Ejemplo n.º 13
0
    def setUpClass(cls):
        path = pathlib.Path("test_outputs/test_results.csv")
        cls.filename = axl_filename(path)

        cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
        cls.repetitions = 3
        cls.turns = 5

        cls.test_result_set = axl.ResultSet(cls.filename,
                                            cls.players,
                                            cls.repetitions,
                                            progress_bar=False)

        cls.test_result_set = axl.ResultSet(cls.filename,
                                            cls.players,
                                            cls.repetitions,
                                            progress_bar=False)
        cls.expected_boxplot_dataset = [
            [(17 / 5 + 9 / 5) / 2 for _ in range(3)],
            [(13 / 5 + 4 / 5) / 2 for _ in range(3)],
            [3 / 2 for _ in range(3)],
        ]
        cls.expected_boxplot_xticks_locations = [1, 2, 3, 4]
        cls.expected_boxplot_xticks_labels = [
            "Defector", "Tit For Tat", "Alternator"
        ]

        cls.expected_lengthplot_dataset = [
            [cls.turns for _ in range(3)],
            [cls.turns for _ in range(3)],
            [cls.turns for _ in range(3)],
        ]

        cls.expected_payoff_dataset = [
            [
                0,
                mean([9 / 5 for _ in range(3)]),
                mean([17 / 5 for _ in range(3)])
            ],
            [
                mean([4 / 5 for _ in range(3)]), 0,
                mean([13 / 5 for _ in range(3)])
            ],
            [
                mean([2 / 5 for _ in range(3)]),
                mean([13 / 5 for _ in range(3)]), 0
            ],
        ]
        cls.expected_winplot_dataset = (
            [[2, 2, 2], [0, 0, 0], [0, 0, 0]],
            ["Defector", "Tit For Tat", "Alternator"],
        )

        cls.expected_sdvplot_dataset = (
            [
                [3, 3, 3, 1, 1, 1, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, -1, -1, -1],
                [0, 0, 0, 0, 0, 0, -3, -3, -3],
            ],
            ["Defector", "Tit For Tat", "Alternator"],
        )