Esempio n. 1
0
    def test_strategy(self):
        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
        # coop and 60% otherwise, after first couple
        actions = [
            (C, C),
            (C, D),  # Views first three as the same.
            # A random gets used in each of the first two.
            (D, C),
            (D, D),
            (C, C),
            (C, D)
        ]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         seed=1)

        actions = [(C, C), (C, D), (C, C), (D, D), (D, C), (C, D)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         seed=2)

        actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, C)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         seed=3)

        # Now we have to test the detect-random logic, which doesn't pick up
        # until after 26 turns.  So we need a big sample.
        actions = [
            (C, D),
            (D, D),
            (D, D),
            (D, D),
            (D, D),
            (D, C),
            (C, C),
            (C, D),
            (C, C),
            (D, D),
            (D, C),
            (C, C),
            (C, D),
            (D, D),
            (C, D),
            (D, D),
            (D, C),
            (C, C),
            (D, C),
            (C, C),
            (C, D),
            (D, D),
            (D, C),
            (C, D),
            (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),
            (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)
        ]
        self.versus_test(axelrod.Random(0.5),
                         expected_actions=actions,
                         seed=10)
Esempio n. 2
0
 def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten(
     self
 ):
     against_cooperator = [(C, C)] * 3 + [(D, C)] * 20
     self.versus_test(axl.Cooperator(), expected_actions=against_cooperator)
Esempio n. 3
0
    def test_strategy(self):
        default_init_kwargs = {
            "discount_factor": .75,
            "promotion_threshold": 3,
            "violation_threshold": 4,
            "reject_threshold": 4,
            "tree_depth": 5,
        }

        # Test that DBS always cooperate against Cooperator.
        actions = [(C, C)] * 7
        self.versus_test(
            opponent=axelrod.Cooperator(),
            expected_actions=actions,
            init_kwargs=default_init_kwargs,
        )

        # Test if it correctly learns Alternator strategy.
        actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3
        self.versus_test(
            opponent=axelrod.Alternator(),
            expected_actions=actions,
            init_kwargs=default_init_kwargs,
        )

        # Check that algorithms take into account a change in opponent's
        # strategy.
        mock_actions = [C, C, C, D, D, D, D, D, D, D]
        exp_actions = [(C, C)] * 3 + [(C, D)] * 4 + [(D, D)] * 3
        self.versus_test(
            opponent=axelrod.MockPlayer(actions=mock_actions),
            expected_actions=exp_actions,
            init_kwargs=default_init_kwargs,
        )

        # Check that adaptation is faster if diminishing promotion_threshold.
        init_kwargs_2 = {
            "discount_factor": .75,
            "promotion_threshold": 2,
            "violation_threshold": 4,
            "reject_threshold": 4,
            "tree_depth": 5,
        }
        mock_actions = [C, C, C, D, D, D, D, D, D, D]
        exp_actions = [(C, C)] * 3 + [(C, D)] * 3 + [(D, D)] * 4
        self.versus_test(
            opponent=axelrod.MockPlayer(actions=mock_actions),
            expected_actions=exp_actions,
            init_kwargs=init_kwargs_2,
        )

        # Check that ShouldDemote mechanism works.
        # We play against Alternator for 12 turns to make the
        # algorithm learn Alternator's strategy, then at turn 13 we
        # change opponent to Defector, hence triggering ShouldDemote
        # mechanism. For this test we use violation_threshold=3
        init_kwargs_3 = {
            "discount_factor": .75,
            "promotion_threshold": 3,
            "violation_threshold": 3,
            "reject_threshold": 3,
            "tree_depth": 5,
        }
        exp_actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3
        exp_actions += [(D, D), (C, D)] * 3 + [(D, D)]
        mock_actions = [
            C, D, C, D, C, D, C, D, C, D, C, D, D, D, D, D, D, D, D
        ]
        self.versus_test(
            opponent=axelrod.MockPlayer(actions=mock_actions),
            expected_actions=exp_actions,
            init_kwargs=init_kwargs_3,
        )
Esempio n. 4
0
 def test_strategy(self):
     """Should cooperate against cooperators and defect against defectors."""
     self.versus_test(axelrod.Defector(), expected_actions=[(D, D)] * 5)
     self.versus_test(axelrod.Cooperator(), expected_actions=[(C, C)] * 5)
     self.versus_test(axelrod.Alternator(), expected_actions=[(C, C), (D, D)] * 5)
Esempio n. 5
0
    def test_strategy(self):
        actions = [(C, C)] * 4 + [(D, C)] * 10
        self.versus_test(opponent=axl.Cooperator(), expected_actions=actions)

        actions = [(C, D)] * 14
        self.versus_test(opponent=axl.Defector(), expected_actions=actions)
Esempio n. 6
0
    def test_strategy(self):
        actions = [(C, C)] * 5
        self.versus_test(axl.Cooperator(), expected_actions=actions)

        actions = [(C, D), (D, D), (D, D)]
        self.versus_test(axl.Defector(), expected_actions=actions)
 def test_cooperator(self):
     tft = axelrod.Cooperator()
     # It always makes sense to defect here.
     self.assertEqual(look_ahead(self.inspector, tft, self.game, 1), D)
     self.assertEqual(look_ahead(self.inspector, tft, self.game, 2), D)
     self.assertEqual(look_ahead(self.inspector, tft, self.game, 5), D)
Esempio n. 8
0
 def test_birth_in_bd(self):
     players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat()
     mp = MoranProcess(players, mode="bd")
     axelrod.seed(1)
     self.assertEqual(mp.birth(0), 0)
Esempio n. 9
0
 def test_next(self):
     players = axelrod.Cooperator(), axelrod.Defector()
     mp = MoranProcess(players)
     self.assertIsInstance(next(mp), MoranProcess)
Esempio n. 10
0
    def test_strategy(self):
        # TitForTat test_strategy
        init_kwargs = {'N': 1, 'M': 1}
        actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        actions = [(C, C), (C, C), (C, C), (C, C), (C, C)]
        self.versus_test(axelrod.Cooperator(),
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
        self.versus_test(axelrod.Defector(),
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         match_attributes={"length": float("inf")},
                         init_kwargs=init_kwargs)
        actions = [(C, D), (D, D), (D, C), (C, C), (C, D)]
        self.versus_test(axelrod.Random(),
                         expected_actions=actions,
                         seed=0,
                         init_kwargs=init_kwargs)
        actions = [(C, C), (C, D), (D, D), (D, C)]
        self.versus_test(axelrod.Random(),
                         expected_actions=actions,
                         seed=1,
                         init_kwargs=init_kwargs)
        opponent = axelrod.MockPlayer(actions=[C, D])
        actions = [(C, C), (C, D), (D, C), (C, D)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D])
        actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         init_kwargs=init_kwargs)

        # TitFor2Tats test_strategy
        init_kwargs = {'N': 1, 'M': 2}
        opponent = axelrod.MockPlayer(actions=[D, D, D, C, C])
        actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         init_kwargs=init_kwargs)

        # TwoTitsForTat test_strategy
        init_kwargs = {'N': 2, 'M': 1}
        opponent = axelrod.MockPlayer(actions=[D, C, C, D, C])
        actions = [(C, D), (D, C), (D, C), (C, D), (D, C)]
        self.versus_test(opponent,
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        actions = [(C, C), (C, C)]
        self.versus_test(opponent=axelrod.Cooperator(),
                         expected_actions=actions,
                         init_kwargs=init_kwargs)
        actions = [(C, D), (D, D), (D, D)]
        self.versus_test(opponent=axelrod.Defector(),
                         expected_actions=actions,
                         init_kwargs=init_kwargs)

        # Cooperator test_strategy
        actions = [(C, C)] + [(C, D), (C, C)] * 9
        self.versus_test(opponent=axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs={
                             'N': 0,
                             'M': 1
                         })
        self.versus_test(opponent=axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs={
                             'N': 0,
                             'M': 5
                         })
        self.versus_test(opponent=axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs={
                             'N': 0,
                             'M': 0
                         })

        # Defector test_strategy
        actions = [(D, C)] + [(D, D), (D, C)] * 9
        self.versus_test(opponent=axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs={
                             'N': 1,
                             'M': 0
                         })
        self.versus_test(opponent=axelrod.Alternator(),
                         expected_actions=actions,
                         init_kwargs={
                             'N': 5,
                             'M': 0
                         })

        # Default init args
        actions = [(C, C), (C, D), (C, D), (D, C), (D, C), (D, D), (C, C)]
        opponent = axelrod.MockPlayer(actions=[acts[1] for acts in actions])
        self.versus_test(opponent=opponent, expected_actions=actions)
 def test_rounds(self):
     self.versus_test(axelrod.Fortress4(), axelrod.Cooperator(),
                      [D, D, D, D, D, D], [C] * 6)
Esempio n. 12
0
 def test_not_revised(self):
     # Test not revised
     player = self.player(revised=False)
     opponent = axelrod.Cooperator()
     match = axelrod.Match((player, opponent), turns=2)
     self.assertEqual(match.play(), [(D, C), (D, C)])
 def __init__(self, id, env=None, attr_dim=3, strat=axl.Cooperator()):
     Agent.__init__(self, id)
     self.vector = self.__getRandomUnitVector(attr_dim)
     self.strat = strat
     self.score = 0
     self.env = env
Esempio n. 14
0
    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)
Esempio n. 15
0
    def test_strategy(self):
        # Test TitForTat behavior in first 15 turns
        opponent = axelrod.Cooperator()
        actions = list([(C, C)]) * 15
        self.versus_test(opponent, expected_actions=actions)

        opponent = axelrod.Defector()
        actions = [(C, D)] + list([(D, D)]) * 14
        self.versus_test(opponent, expected_actions=actions)

        opponent = axelrod.Alternator()
        actions = [(C, C)] + [(C, D), (D, C)] * 7
        self.versus_test(opponent, expected_actions=actions)

        opponent_actions = [C, D, D, C, D, C, C, D, C, D, D, C, C, D, D]
        opponent = axelrod.MockPlayer(actions=opponent_actions)
        mem_actions = [C, C, D, D, C, D, C, C, D, C, D, D, C, C, D]
        actions = list(zip(mem_actions, opponent_actions))
        self.versus_test(opponent, expected_actions=actions)

        opponent = axelrod.Random()
        actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, C)]
        self.versus_test(opponent, expected_actions=actions, seed=0)

        # Test net-cooperation-score (NCS) based decisions in subsequent turns
        opponent = axelrod.Cooperator()
        actions = [(C, C)] * 15 + [(C, C)]
        self.versus_test(
            opponent,
            expected_actions=actions,
            seed=1,
            init_kwargs={"memory": [D] * 5 + [C] * 10},
        )

        opponent = axelrod.Cooperator()
        actions = [(C, C)] * 15 + [(C, C)]
        self.versus_test(
            opponent,
            expected_actions=actions,
            seed=1,
            init_kwargs={"memory": [D] * 4 + [C] * 11},
        )

        # Test alternative starting strategies
        opponent = axelrod.Cooperator()
        actions = list([(D, C)]) * 15
        self.versus_test(
            opponent,
            expected_actions=actions,
            init_kwargs={"start_strategy": axelrod.Defector},
        )

        opponent = axelrod.Cooperator()
        actions = list([(C, C)]) * 15
        self.versus_test(
            opponent,
            expected_actions=actions,
            init_kwargs={"start_strategy": axelrod.Cooperator},
        )

        opponent = axelrod.Cooperator()
        actions = [(C, C)] + list([(D, C), (C, C)]) * 7
        self.versus_test(
            opponent,
            expected_actions=actions,
            init_kwargs={"start_strategy": axelrod.Alternator},
        )

        opponent = axelrod.Defector()
        actions = [(C, D)] * 7 + [((D, D))]
        self.versus_test(
            opponent,
            expected_actions=actions,
            seed=4,
            init_kwargs={
                "memory": [C] * 12,
                "start_strategy": axelrod.Defector,
                "start_strategy_duration": 0,
            },
        )
Esempio n. 16
0
 def test_exit_condition(self):
     p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
     mp = MoranProcess((p1, p2))
     mp.play()
     self.assertEqual(len(mp), 1)
Esempio n. 17
0
 def test_rounds(self):
     self.versus_test(axelrod.EvolvedHMM5(), axelrod.Cooperator(), [C] * 5, [C] * 5)
Esempio n. 18
0
 def test_play_exception(self):
     p1, p2 = axelrod.Cooperator(), axelrod.Defector()
     mp = MoranProcess((p1, p2), mutation_rate=0.2)
     with self.assertRaises(ValueError):
         mp.play()
Esempio n. 19
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep  4 10:26:21 2019

@author: rajdua
"""

import axelrod as axl
# axl.seed(0)  # for reproducible example
players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
mp = axl.MoranProcess(players)
populations = mp.play()
mp.winning_strategy_name
len(mp)

import pprint
pprint.pprint(populations)
for row in mp.score_history:
    print([round(element, 1) for element in row])

import random
import matplotlib.pyplot as plt
# axl.seed(15)  # for reproducible example
players = [
    axl.Defector(),
    axl.Defector(),
    axl.Defector(),
    axl.Cooperator(),
    axl.Cooperator(),
    axl.Cooperator(),
Esempio n. 20
0
 def test_iter(self):
     p1, p2 = axelrod.Cooperator(), axelrod.Defector()
     mp = MoranProcess((p1, p2))
     self.assertEqual(mp.__iter__(), mp)
Esempio n. 21
0
 def test_strategy(self):
     p1 = axl.MirrorMindReader()
     p2 = axl.Cooperator()
     self.assertEqual(p1.strategy(p2), C)
Esempio n. 22
0
 def test_reset(self):
     player = self.player()
     opponent = axelrod.Cooperator()
     [player.play(opponent) for _ in range(10)]
     player.reset()
     self.assertEqual(player.opponent_class, None)
Esempio n. 23
0
 def test_strategy(self):
     P1 = axelrod.MirrorMindReader()
     P2 = axelrod.Cooperator()
     self.assertEqual(P1.strategy(P2), C)
Esempio n. 24
0
 def test_versus_cooperator(self):
     actions = [(C, C), (D, C)] * 5
     self.versus_test(axl.Cooperator(), expected_actions=actions)
Esempio n. 25
0
 def test_len(self, turns):
     p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
     match = axelrod.Match((p1, p2), turns)
     self.assertEqual(len(match), turns)
Esempio n. 26
0
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import axelrod as axl
players = (axl.Cooperator(), axl.Alternator())
match = axl.Match(players, 5)
match.play()

match = axl.Match(players=players, turns=5, noise=0.2)
match.play()

match2 = axl.Match(players, 25)
match2.play()
print(match2.sparklines(c_symbol='|', d_symbol=''))

print(match2.sparklines())

players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()]
tournament = axl.Tournament(players)
results = tournament.play()
results.ranked_names
plot = axl.Plot(results)
p = plot.boxplot()

summary = results.summarise()
import pprint
pprint.pprint(summary)
Esempio n. 27
0
import axelrod as axl
import csv
import json
import jsonpickle

# strats = [axl.Alternator(), axl.AntiTitForTat(), axl.Bully(), axl.Cooperator(),axl.Defector(), axl.SuspiciousTitForTat(), axl.TitForTat(), axl.Grudger(),axl.WinShiftLoseStay(), axl.WinStayLoseShift()]
# strats1 = [axl.Alternator(), axl.AntiTitForTat(), axl.Bully(), axl.Cooperator(),axl.Defector(), axl.SuspiciousTitForTat(), axl.TitForTat(), axl.Grudger(),axl.WinShiftLoseStay(), axl.WinStayLoseShift()]

strats1 = [
    axl.Cooperator(),
    axl.Defector(),
    axl.Grudger(),
    axl.TitForTat(),
    axl.WinStayLoseShift(),
    axl.StochasticCooperator(),
    axl.Bully(),
    axl.Grumpy(),
    axl.CollectiveStrategy(),
    axl.APavlov2011()
]
strats2 = [
    axl.Cooperator(),
    axl.Defector(),
    axl.Grudger(),
    axl.TitForTat(),
    axl.WinStayLoseShift(),
    axl.StochasticCooperator(),
    axl.Bully(),
    axl.Grumpy(),
    axl.CollectiveStrategy(),
    axl.APavlov2011()
Esempio n. 28
0
    def test_strategy(self):
        # start by cooperating
        self.first_play_test(C)

        default_init_kwargs = {
                'discount_factor':.75, 'promotion_threshold':3,
                'violation_threshold':4, 'reject_threshold':4, 
                'tree_depth':5
                }

        # test that DBS always cooperate against Cooperator
        actions = [(C, C)] * 7
        self.versus_test(
                opponent=axelrod.Cooperator(), 
                expected_actions=actions,
                init_kwargs = default_init_kwargs
                )

        # test if it correctly learns Alternator strategy
        actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3 
        self.versus_test(
                opponent=axelrod.Alternator(),
                expected_actions=actions,
                init_kwargs = default_init_kwargs
                )

        # check that algorithms take into account a change in 
        # opponent's strategy
        mock_actions = [C, C, C, D, D, D, D, D, D, D]
        exp_actions = [(C, C)] * 3 + [(C, D)] * 4 + [(D, D)] * 3 
        self.versus_test(
                opponent=axelrod.MockPlayer(actions=mock_actions),
                expected_actions=exp_actions, 
                init_kwargs=default_init_kwargs
                )

        # check that adaptation is faster if diminishing promotion_threshold
        init_kwargs_2 = {
                'discount_factor':.75, 'promotion_threshold':2, 
                'violation_threshold':4, 'reject_threshold':4,
                'tree_depth':5
                }
        mock_actions = [C, C, C, D, D, D, D, D, D, D]
        exp_actions = [(C, C)] * 3 + [(C, D)] * 3 + [(D, D)] * 4 
        self.versus_test(
                opponent=axelrod.MockPlayer(actions=mock_actions),
                expected_actions=exp_actions, 
                init_kwargs = init_kwargs_2
                )
        
        # check that ShouldDemote mecanism works.
        # We play against Alternator during 12 turns to make the 
        # algorithm learn Alternator's strategy, then at turn 13 we
        # change opponent to Defector, hence trigging ShouldDemote
        # mecanism
        # For this test we use violation_threshold=3
        init_kwargs_3 = {
                'discount_factor':.75, 'promotion_threshold':3, 
                'violation_threshold':3, 'reject_threshold':3,
                'tree_depth':5
                }
        exp_actions = [(C, C), (C, D)] * 3 + [(D, C), (C, D)] * 3 
        exp_actions += [(D, D), (C, D)] * 3 + [(D, D)]
        mock_actions = [C, D, C, D, C, D, C, D, C, D, C, D, D, D, D, D, D, D, D]
        self.versus_test(
                opponent=axelrod.MockPlayer(actions=mock_actions),
                expected_actions=exp_actions, 
                init_kwargs = init_kwargs_3
                )
Esempio n. 29
0
    def test_strategy(self):
        actions = [(C, C)] * 5
        self.versus_test(axelrod.Cooperator(), expected_actions=actions)

        actions = [(C, D)] + [(D, D)] * 4
        self.versus_test(axelrod.Defector(), expected_actions=actions)
Esempio n. 30
0
    def test_strategy(self):
        actions = [(C, C)] * 100
        self.versus_test(axelrod.Cooperator(), expected_actions=actions)

        actions = [(C, D)] + [(D, D)] * 8
        self.versus_test(axelrod.Defector(),
                         expected_actions=actions,
                         attrs={"score_to_beat_inc": 5})

        actions = [(C, D)] + [(D, D)] * 8
        # On tenth turn, try a fresh start
        actions += [(C, D), (C, D)] + [(D, D)] * 2
        self.versus_test(axelrod.Defector(),
                         expected_actions=actions,
                         attrs={"last_fresh_start": 11})

        actions = [(C, C), (C, D)]
        # Scores and score_to_beat variables are a turn behind
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         attrs={
                             "current_score": 3,
                             "opponent_score": 3,
                             "score_to_beat": 0,
                             "score_to_beat_inc": 0
                         })
        actions += [(D, C), (C, D)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         attrs={
                             "current_score": 8,
                             "opponent_score": 8,
                             "score_to_beat": 0,
                             "score_to_beat_inc": 5
                         })
        actions += [(D, C), (D, D)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         attrs={
                             "current_score": 13,
                             "opponent_score": 13,
                             "score_to_beat": 5,
                             "score_to_beat_inc": 10
                         })
        actions += [(D, C), (D, D)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         attrs={
                             "current_score": 19,
                             "opponent_score": 14,
                             "score_to_beat": 15,
                             "score_to_beat_inc": 15
                         })
        actions += [(D, C), (D, D)]
        self.versus_test(axelrod.Alternator(),
                         expected_actions=actions,
                         attrs={
                             "current_score": 25,
                             "opponent_score": 15,
                             "score_to_beat": 30,
                             "score_to_beat_inc": 20
                         })

        # Build an opponent who will cause us to consider a Fresh Start, but
        # will fail the binomial test.
        opponent_actions = [C] * 5 + [D] * 5
        C5D5_Player = axelrod.MockPlayer(actions=opponent_actions)
        actions = [(C, C)] * 5 + [(C, D)] + [(D, D)] * 3
        actions += [(D, D)]  # No Defection here means no Fresh Start.
        self.versus_test(C5D5_Player, expected_actions=actions)