Exemplo n.º 1
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.test_edges = test_edges
Exemplo n.º 2
0
 def test_different_game(self):
     # Possible for Cooperator to become fixed when using a different game
     p1, p2 = axl.Cooperator(), axl.Defector()
     game = axl.Game(r=4, p=2, s=1, t=6)
     mp = MoranProcess((p1, p2), turns=5, game=game, seed=88)
     populations = mp.play()
     self.assertEqual(mp.winning_strategy_name, str(p2))
Exemplo n.º 3
0
 def test_property_score(self, r, p, s, t):
     """Use the hypothesis library to test score"""
     game = axelrod.Game(r, s, t, p)
     self.assertEqual(game.score((C, C)), (r, r))
     self.assertEqual(game.score((D, D)), (p, p))
     self.assertEqual(game.score((C, D)), (s, t))
     self.assertEqual(game.score((D, C)), (t, s))
Exemplo n.º 4
0
 def test_scores_with_different_game(self):
     game = axelrod.Game(p=-1, r=-1, s=-1, t=-1)
     rs = axelrod.ResultSet(self.players, self.interactions,
                            progress_bar=False, game=game)
     for player in rs.scores:
         for score in player:
             self.assertFalse(score > 0)
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def test_random_score(self, r, p, s, t):
     """Test score method with random scores using the hypothesis library."""
     game = axl.Game(r, s, t, p)
     self.assertEqual(game.score((C, C)), (r, r))
     self.assertEqual(game.score((D, D)), (p, p))
     self.assertEqual(game.score((C, D)), (s, t))
     self.assertEqual(game.score((D, C)), (t, s))
Exemplo n.º 7
0
 def setUpClass(cls):
     cls.game = axelrod.Game()
     cls.players = [s() for s in test_strategies]
     cls.test_name = "test"
     cls.test_repetitions = test_repetitions
     cls.test_prob_end = test_prob_end
     cls.test_edges = test_edges
Exemplo n.º 8
0
def generate_match_results(player,
                           opponent,
                           turns=200,
                           repetitions=100,
                           noise=None):
    """Generates match date between two players. Yields rows of the form
    [(C, D), (C, C), ...]."""
    # Make sure we have two distinct player objects
    if not noise:
        noise = 0
    if player == opponent:
        opponent = opponent.clone()
    # Set tournament_attributes
    tournament_attributes = {'length': turns, 'game': axelrod.Game()}
    player.tournament_attributes = tournament_attributes
    opponent.tournament_attributes = tournament_attributes

    # Run matches
    row = []
    for _ in range(repetitions):
        # Compute a new match
        row = []
        # Reset any accumulated history
        player.reset()
        opponent.reset()
        # Compute rounds and write to CSV
        for _ in range(turns):
            player.play(opponent, noise=noise)
        yield zip(player.history, opponent.history)
Exemplo n.º 9
0
def games(draw, prisoners_dilemma=True, max_value=100):
    """
    A hypothesis decorator to return a random game.

    Parameters
    ----------
    prisoners_dilemma : bool
        If set not True the R,P,S,T values will be uniformly random. True by
        default which ensures T > R > P > S and 2R > T + S.
    max_value : the maximal payoff value
    """

    if prisoners_dilemma:
        s_upper_bound = max_value - 4  # Ensures there is enough room
        s = draw(integers(max_value=s_upper_bound))

        t_lower_bound = s + 3  # Ensures there is enough room
        t = draw(integers(min_value=t_lower_bound, max_value=max_value))

        r_upper_bound = t - 1
        r_lower_bound = min(max(int((t + s) / 2), s) + 2, r_upper_bound)
        r = draw(integers(min_value=r_lower_bound, max_value=r_upper_bound))

        p_lower_bound = s + 1
        p_upper_bound = r - 1
        p = draw(integers(min_value=p_lower_bound, max_value=p_upper_bound))

    else:
        s = draw(integers(max_value=max_value))
        t = draw(integers(max_value=max_value))
        r = draw(integers(max_value=max_value))
        p = draw(integers(max_value=max_value))

    game = axl.Game(r=r, s=s, t=t, p=p)
    return game
Exemplo n.º 10
0
 def test_default_scores(self):
     expected_scores = {
         (C, D): (0, 5),
         (D, C): (5, 0),
         (D, D): (1, 1),
         (C, C): (3, 3),
     }
     self.assertEqual(axl.Game().scores, expected_scores)
Exemplo n.º 11
0
 def test_serial_play_with_different_game(self):
     # Test that a non default game is passed to the result set
     game = axl.Game(p=-1, r=-1, s=-1, t=-1)
     tournament = axl.Tournament(
         name=self.test_name, players=self.players, game=game, turns=1, repetitions=1
     )
     results = tournament.play(progress_bar=False)
     self.assertLessEqual(np.max(results.scores), 0)
Exemplo n.º 12
0
 def test_scoring_with_alternate_game(self):
     """Tests that the alternate game is used in scoring."""
     player = axl.Adaptive()
     opponent = axl.Alternator()
     expected_actions = list(zip([C, C, C], [C, D, C]))
     attrs = {"scores": {C: 7, D: 0}}
     match_attributes = {"game": axl.Game(-3, 10, 10, 10)}
     self.versus_test(opponent, expected_actions, turns=3, attrs=attrs, seed=9,
                      match_attributes=match_attributes)
Exemplo n.º 13
0
def cumulative_scores(h1, h2):
    """Computes the cumulative scores of each player."""
    ss1, ss2 = [], []
    game = axl.Game()
    for p1, p2 in zip(h1, h2):
        s1, s2 = game.score((p1, p2))
        ss1.append(s1)
        ss2.append(s2)
    return np.cumsum(ss1), np.cumsum(ss2)
Exemplo n.º 14
0
 def test_scoring(self):
     player = axl.Adaptive()
     opponent = axl.Cooperator()
     player.play(opponent)
     player.play(opponent)
     self.assertEqual(3, player.scores[C])
     game = axl.Game(-3, 10, 10, 10)
     player.set_match_attributes(game=game)
     player.play(opponent)
     self.assertEqual(0, player.scores[C])
Exemplo n.º 15
0
 def test_serial_play_with_different_game(self):
     # Test that a non default game is passed to the result set
     game = axelrod.Game(p=-1, r=-1, s=-1, t=-1)
     tournament = axelrod.Tournament(name=self.test_name,
                                     players=self.players,
                                     game=game,
                                     turns=1,
                                     repetitions=1)
     results = tournament.play(progress_bar=False)
     self.assertEqual(results.game.RPST(), (-1, -1, -1, -1))
Exemplo n.º 16
0
def score_single(me, other, iterations=200):
    """
    Return the average score per turn for a player in a single match against
    an opponent.
     """
    g = axelrod.Game()
    for _ in range(iterations):
        me.play(other)
    return sum([g.score(pair)[0]
                for pair in zip(me.history, other.history)]) / iterations
Exemplo n.º 17
0
 def test_property_init(self, r, p, s, t):
     """Use the hypothesis library to test init"""
     expected_scores = {
         (C, D): (s, t),
         (D, C): (t, s),
         (D, D): (p, p),
         (C, C): (r, r)
     }
     game = axelrod.Game(r, s, t, p)
     self.assertEqual(game.scores, expected_scores)
Exemplo n.º 18
0
 def test_random_init(self, r, p, s, t):
     """Test init with random scores using the hypothesis library."""
     expected_scores = {
         (C, D): (s, t),
         (D, C): (t, s),
         (D, D): (p, p),
         (C, C): (r, r),
     }
     game = axl.Game(r, s, t, p)
     self.assertEqual(game.scores, expected_scores)
Exemplo n.º 19
0
    def setUpClass(cls):
        cls.game = axelrod.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]]
Exemplo n.º 20
0
    def setUpClass(cls):
        cls.game = axelrod.Game()
        cls.players = [
            axelrod.Cooperator(),
            axelrod.TitForTat(),
            axelrod.Defector(),
            axelrod.Grudger(),
            axelrod.GoByMajority()]
        cls.player_names = [str(p) for p in cls.players]
        cls.test_name = 'test'
        cls.test_repetitions = 5

        cls.expected_outcome = [
            ('Cooperator', [180, 180, 180, 180, 180]),
            ('Defector', [172, 172, 172, 172, 172]),
            ('Grudger', [199, 199, 199, 199, 199]),
            ('Soft Go By Majority', [199, 199, 199, 199, 199]),
            ('Tit For Tat', [199, 199, 199, 199, 199])]
        cls.expected_outcome.sort()
Exemplo n.º 21
0
    def setUpClass(cls):
        cls.game = axelrod.Game()
        cls.players = [
            axelrod.Cooperator(),
            axelrod.TitForTat(),
            axelrod.Defector(),
            axelrod.Grudger(),
            axelrod.GoByMajority(),
        ]
        cls.player_names = [str(p) for p in cls.players]
        cls.test_name = "test"
        cls.test_repetitions = 3

        cls.expected_outcome = [
            ("Cooperator", [45, 45, 45]),
            ("Defector", [52, 52, 52]),
            ("Grudger", [49, 49, 49]),
            ("Soft Go By Majority", [49, 49, 49]),
            ("Tit For Tat", [49, 49, 49]),
        ]
        cls.expected_outcome.sort()
Exemplo n.º 22
0
    def setUpClass(cls):
        cls.game = axelrod.Game()
        cls.players = [
            axelrod.Cooperator(),
            axelrod.TitForTat(),
            axelrod.Defector(),
            axelrod.Grudger(),
            axelrod.GoByMajority()
        ]
        cls.test_name = 'test'
        cls.test_repetitions = 5

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

        cls.expected_cooperation = [[200, 200, 200, 200, 200],
                                    [200, 200, 1, 200, 200],
                                    [0.0, 0.0, 0.0, 0.0, 0.0],
                                    [200, 200, 1, 200, 200],
                                    [200, 200, 1, 200, 200]]
Exemplo n.º 23
0
    def test_strategy(self):
        R, P, S, T = axelrod.Game().RPST()

        player = self.player(l=R)
        axelrod.seed(0)
        match = axelrod.Match((player, axelrod.Alternator()), turns=200)
        match.play()

        player = self.player(l=P)
        axelrod.seed(0)
        match = axelrod.Match((player, axelrod.Alternator()), turns=200)
        match.play()

        player = self.player(s=1)
        axelrod.seed(0)
        match = axelrod.Match((player, axelrod.Alternator()), turns=200)
        match.play()

        l = 2
        s_min = - min((T - l) / (l - S), (l - S) / (T - l))
        player = self.player(s=s_min, l=2)
        axelrod.seed(0)
        match = axelrod.Match((player, axelrod.Alternator()), turns=200)
        match.play()
Exemplo n.º 24
0
 def setUpClass(cls):
     cls.game = axelrod.Game()
Exemplo n.º 25
0
 def test_wrong_class_equality(self):
     self.assertNotEqual(axl.Game(), "wrong class")
Exemplo n.º 26
0
 def test_default_RPST(self):
     expected_values = (3, 1, 0, 5)
     self.assertEqual(axl.Game().RPST(), expected_values)
Exemplo n.º 27
0
 def test_default_score(self):
     game = axl.Game()
     self.assertEqual(game.score((C, C)), (3, 3))
     self.assertEqual(game.score((D, D)), (1, 1))
     self.assertEqual(game.score((C, D)), (0, 5))
     self.assertEqual(game.score((D, C)), (5, 0))
Exemplo n.º 28
0
 def test_default_equality(self):
     self.assertEqual(axl.Game(), axl.Game())
Exemplo n.º 29
0
 def test_not_default_equality(self):
     self.assertEqual(axl.Game(1, 2, 3, 4), axl.Game(1, 2, 3, 4))
     self.assertNotEqual(axl.Game(1, 2, 3, 4), axl.Game(1, 2, 3, 5))
     self.assertNotEqual(axl.Game(1, 2, 3, 4), axl.Game())
Exemplo n.º 30
0
 def test_random_RPST(self, r, p, s, t):
     """Test RPST method with random scores using the hypothesis library."""
     game = axl.Game(r, s, t, p)
     self.assertEqual(game.RPST(), (r, p, s, t))