Skip to content

Commit f9aeafd

Browse files
committed
Add test for noise in spatial tournaments.
Testing both the init and also the play of the complete tournament.
1 parent b11c3e6 commit f9aeafd

File tree

1 file changed

+73
-36
lines changed

1 file changed

+73
-36
lines changed

axelrod/tests/unit/test_tournament.py

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -594,45 +594,82 @@ def test_init(self):
594594
self.assertTrue(tournament._with_morality)
595595
self.assertIsInstance(tournament._logger, logging.Logger)
596596
self.assertEqual(tournament.noise, 0.2)
597+
self.assertEqual(tournament.match_generator.noise, 0.2)
597598
anonymous_tournament = axelrod.Tournament(players=self.players)
598599
self.assertEqual(anonymous_tournament.name, 'axelrod')
599600

600-
@given(strategies=strategy_lists(strategies=deterministic_strategies,
601-
min_size=2, max_size=2),
602-
turns=integers(min_value=1, max_value=20))
603-
604-
def test_complete_tournament(self, strategies, turns):
605-
"""
606-
A test to check that a spatial tournament on the complete multigraph
607-
gives the same results as the round robin.
608-
"""
609-
610-
players = [s() for s in strategies]
611-
# edges
612-
edges=[]
613-
for i in range(0, len(players)) :
614-
for j in range(i, len(players)) :
615-
edges.append((i, j))
616-
# create a round robin tournament
617-
tournament = axelrod.Tournament(players, turns=turns)
618-
results = tournament.play()
619-
# create a complete spatial tournament
620-
spatial_tournament = axelrod.SpatialTournament(players, turns=turns,
621-
edges=edges)
622-
spatial_results = spatial_tournament.play()
623-
self.assertEqual(results.ranked_names, spatial_results.ranked_names)
624-
self.assertEqual(results.nplayers, spatial_results.nplayers)
625-
self.assertEqual(results.nrepetitions, spatial_results.nrepetitions)
626-
self.assertEqual(results.payoff_diffs_means, spatial_results.payoff_diffs_means)
627-
self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix)
628-
self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs)
629-
self.assertEqual(results.payoffs, spatial_results.payoffs)
630-
self.assertEqual(results.cooperating_rating, spatial_results.cooperating_rating)
631-
self.assertEqual(results.cooperation, spatial_results.cooperation)
632-
self.assertEqual(results.normalised_cooperation, spatial_results.normalised_cooperation)
633-
self.assertEqual(results.normalised_scores, spatial_results.normalised_scores)
634-
self.assertEqual(results.good_partner_matrix, spatial_results.good_partner_matrix)
635-
self.assertEqual(results.good_partner_rating, spatial_results.good_partner_rating)
601+
@given(strategies=strategy_lists(strategies=deterministic_strategies,
602+
min_size=2, max_size=2),
603+
turns=integers(min_value=1, max_value=20),
604+
repetitions=integers(min_value=1, max_value=5),
605+
noise=floats(min_value=0, max_value=1),
606+
seed=integers(min_value=0, max_value=4294967295))
607+
@settings(max_examples=50, timeout=0)
608+
def test_complete_tournament(self, strategies, turns, repetitions,
609+
noise, seed):
610+
"""
611+
A test to check that a spatial tournament on the complete multigraph
612+
gives the same results as the round robin.
613+
"""
614+
615+
players = [s() for s in strategies]
616+
# edges
617+
edges = []
618+
for i in range(0, len(players)):
619+
for j in range(i, len(players)):
620+
edges.append((i, j))
621+
622+
# create a round robin tournament
623+
tournament = axelrod.Tournament(players, repetitions=repetitions,
624+
turns=turns, noise=noise)
625+
# create a complete spatial tournament
626+
spatial_tournament = axelrod.SpatialTournament(players,
627+
repetitions=repetitions,
628+
turns=turns,
629+
noise=noise,
630+
edges=edges)
631+
632+
axelrod.seed(seed)
633+
results = tournament.play(progress_bar=False)
634+
axelrod.seed(seed)
635+
spatial_results = spatial_tournament.play(progress_bar=False)
636+
637+
self.assertEqual(results.ranked_names, spatial_results.ranked_names)
638+
self.assertEqual(results.nplayers, spatial_results.nplayers)
639+
self.assertEqual(results.nrepetitions, spatial_results.nrepetitions)
640+
self.assertEqual(results.payoff_diffs_means,
641+
spatial_results.payoff_diffs_means)
642+
self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix)
643+
self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs)
644+
self.assertEqual(results.payoffs, spatial_results.payoffs)
645+
self.assertEqual(results.cooperating_rating,
646+
spatial_results.cooperating_rating)
647+
self.assertEqual(results.cooperation, spatial_results.cooperation)
648+
self.assertEqual(results.normalised_cooperation,
649+
spatial_results.normalised_cooperation)
650+
self.assertEqual(results.normalised_scores,
651+
spatial_results.normalised_scores)
652+
self.assertEqual(results.good_partner_matrix,
653+
spatial_results.good_partner_matrix)
654+
self.assertEqual(results.good_partner_rating,
655+
spatial_results.good_partner_rating)
656+
657+
def test_particular_tournament(self):
658+
"""A test for a tournament that has caused failures during some bug
659+
fixing"""
660+
players = [axelrod.Cooperator(), axelrod.Defector(),
661+
axelrod.TitForTat(), axelrod.Grudger()]
662+
edges = [(0, 2), (0, 3), (1, 2), (1, 3)]
663+
tournament = axelrod.SpatialTournament(players, edges=edges)
664+
results = tournament.play(progress_bar=False)
665+
expected_ranked_names = ['Cooperator', 'Tit For Tat',
666+
'Grudger', 'Defector']
667+
self.assertEqual(results.ranked_names, expected_ranked_names)
668+
669+
# Check that this tournament runs with noise
670+
tournament = axelrod.SpatialTournament(players, edges=edges, noise=.5)
671+
results = tournament.play(progress_bar=False)
672+
self.assertIsInstance(results, axelrod.ResultSetFromFile)
636673

637674

638675
class TestProbEndingSpatialTournament(unittest.TestCase):

0 commit comments

Comments
 (0)