class Test_write_winner(unittest.TestCase): """Test that the output of a given simulation is as expected""" global players players = [axl.Cooperator(), axl.Defector()] global match_outcomes players = [axl.Cooperator(), axl.Defector()] match_outcomes = {} counter = collections.Counter([(5, 0)]) pdf = Pdf(counter) match_outcomes[('Defector', 'Cooperator')] = pdf counter = collections.Counter([(0, 5)]) pdf = Pdf(counter) match_outcomes[('Cooperator', 'Defector')] = pdf counter = collections.Counter([(3, 3)]) pdf = Pdf(counter) match_outcomes[('Cooperator', 'Cooperator')] = pdf counter = collections.Counter([(1, 1)]) pdf = Pdf(counter) match_outcomes[('Defector', 'Defector')] = pdf temp_file = tempfile.NamedTemporaryFile() names_inv = {"Cooperator": 0, "Defector": 1} def test_write_winner(self): write_winner(self.temp_file.name, self.names_inv, 2, 0, 1, 10) df = pd.read_csv(self.temp_file.name, header=None) self.assertEqual(list(df.ix[:, 3]), [0, 10]) self.temp_file.close()
class TestApproximateMoranProcess(unittest.TestCase): """A suite of tests for the ApproximateMoranProcess""" players = [axelrod.Cooperator(), axelrod.Defector()] cached_outcomes = {} counter = Counter([(0, 5)]) pdf = Pdf(counter) cached_outcomes[("Cooperator", "Defector")] = pdf counter = Counter([(3, 3)]) pdf = Pdf(counter) cached_outcomes[("Cooperator", "Cooperator")] = pdf counter = Counter([(1, 1)]) pdf = Pdf(counter) cached_outcomes[("Defector", "Defector")] = pdf amp = ApproximateMoranProcess(players, cached_outcomes) def test_init(self): """Test the initialisation process""" self.assertEqual( set(self.amp.cached_outcomes.keys()), set( [ ("Cooperator", "Defector"), ("Cooperator", "Cooperator"), ("Defector", "Defector"), ] ), ) self.assertEqual(self.amp.players, self.players) self.assertEqual(self.amp.turns, 0) self.assertEqual(self.amp.noise, 0) def test_score_all(self): """Test the score_all function of the Moran process""" scores = self.amp.score_all() self.assertEqual(scores, [0, 5]) scores = self.amp.score_all() self.assertEqual(scores, [0, 5]) scores = self.amp.score_all() self.assertEqual(scores, [0, 5]) def test_getting_scores_from_cache(self): """Test that read of scores from cache works (independent of ordering of player names""" scores = self.amp._get_scores_from_cache(("Cooperator", "Defector")) self.assertEqual(scores, (0, 5)) scores = self.amp._get_scores_from_cache(("Defector", "Cooperator")) self.assertEqual(scores, (5, 0))
class TestPdf(unittest.TestCase): """A suite of tests for the Pdf class""" observations = [(C, D)] * 4 + [(C, C)] * 12 + \ [(D, C)] * 2 + [(D, D)] * 15 counter = Counter(observations) pdf = Pdf(counter) def test_init(self): self.assertEqual(set(self.pdf.sample_space), set(self.counter.keys())) self.assertEqual(set(self.pdf.counts), set([4, 12, 2, 15])) self.assertEqual(self.pdf.total, sum([4, 12, 2, 15])) self.assertAlmostEqual(sum(self.pdf.probability), 1) def test_sample(self): """Test that sample maps to correct domain""" all_samples = [] seed(0) for sample in range(100): all_samples.append(self.pdf.sample()) self.assertEqual(len(all_samples), 100) self.assertEqual(set(all_samples), set(self.observations)) def test_seed(self): """Test that numpy seeds the sample properly""" for s in range(10): seed(s) sample = self.pdf.sample() seed(s) self.assertEqual(sample, self.pdf.sample())
def test_seed(self): """Test that numpy seeds the sample properly""" for s in range(10): pdf1 = Pdf(self.counter, s) sample = pdf1.sample() pdf2 = Pdf(self.counter, s) self.assertEqual(sample, pdf2.sample())
count=True, outfilename=outfilename, n=n) if __name__ == "__main__": # match_outcomes and players are global # Run with `python moran.py <N> <n> <outcome_file> <filename>` try: match_outcomes_file = sys.argv[3] except IndexError: match_outcomes_file = "../data/outcomes.csv" match_outcomes = read_csv(match_outcomes_file) for k, v in match_outcomes.items(): match_outcomes[k] = Pdf(v) # players are global from players import selected_players players = selected_players() main() ######### # Tests # ######### class Test_output_players(unittest.TestCase): """Test the output players function""" def test_output(self):