def test_fingerprint_interactions_titfortat(self): af = AshlockFingerprint(axl.TitForTat()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) # Tit-for-Tats will always cooperate if left to their own devices, # so interactions are invariant for any points where y is zero, # and the score should be maximum possible. # Player 1 is Point(0.0, 0.0). # Player 4 is Point(0.5, 0.0). # Player 7 is Point(1.0, 0.0). for iplayer in (1, 4, 7): for turns in af.interactions[(0, iplayer)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (C, C) for t in turns)) self.assertEqual(af.data[Point(0.0, 0.0)], 3.0) self.assertEqual(af.data[Point(0.5, 0.0)], 3.0) self.assertEqual(af.data[Point(1.0, 0.0)], 3.0) # Player 3 is Point(0.0, 1.0) which implies defection after the # first turn since Tit-for-Tat is playing, and a score of 0.8 # since we get zero on first turn and one point per turn later. for turns in af.interactions[(0, 3)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (D, D) for t in turns[1:])) self.assertAlmostEqual(af.data[Point(0.0, 1.0)], 0.8)
def test_fingerprint_interactions_cooperator(self): af = AshlockFingerprint(axl.Cooperator()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) # The keys are edges between players, values are repetitions. self.assertCountEqual(af.interactions.keys(), [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)]) self.assertEqual(len(af.interactions.values()), 9) # Each edge has 3 repetitions with 5 turns each. repetitions = af.interactions.values() self.assertTrue(all(len(rep) == 3 for rep in repetitions)) for iturn in range(3): self.assertTrue(all(len(rep[iturn]) == 5 for rep in repetitions)) # Interactions are invariant for any points where y is zero, and # the score should be maximum possible. # Player 1 is Point(0.0, 0.0). # Player 4 is Point(0.5, 0.0). # Player 7 is Point(1.0, 0.0). for iplayer in (1, 4, 7): for turns in af.interactions[(0, iplayer)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (C, C) for t in turns)) self.assertEqual(af.data[Point(0.0, 0.0)], 3.0) self.assertEqual(af.data[Point(0.5, 0.0)], 3.0) self.assertEqual(af.data[Point(1.0, 0.0)], 3.0) # Player 3 is Point(0.0, 1.0), which means constant defection # from the probe. But the Cooperator doesn't change and score is zero. for turns in af.interactions[(0, 3)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (C, D) for t in turns)) self.assertEqual(af.data[Point(0.0, 1.0)], 0.0)
def test_pair_fingerprints(self, strategy_pair): """ A test to check that we can fingerprint with any two given strategies or instances """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe) data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False) self.assertIsInstance(data, dict) af = AshlockFingerprint(strategy(), probe) data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False) self.assertIsInstance(data, dict) af = AshlockFingerprint(strategy, probe()) data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False) self.assertIsInstance(data, dict) af = AshlockFingerprint(strategy(), probe()) data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False) self.assertIsInstance(data, dict)
def test_plot_data(self): axl.seed(0) # Fingerprinting is a random process. af = AshlockFingerprint(axl.Cooperator()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) reshaped_data = np.array([[0.0, 0.0, 0.0], [2.0, 1.0, 2.0], [3.0, 3.0, 3.0]]) plotted_data = af.plot().gca().images[0].get_array() np.testing.assert_allclose(plotted_data, reshaped_data)
def test_fingerprint_with_filename(self): filename = "test_outputs/test_fingerprint.csv" 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)
def test_parallel_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) af.fingerprint( turns=10, repetitions=2, step=0.5, processes=2, progress_bar=False ) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(af.data.keys())) self.assertEqual(af.step, 0.5) self.assertEqual(edge_keys, self.edges_when_using_half_step) self.assertEqual(coord_keys, self.points_when_using_half_step)
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)
def test_fingeprint_explicit_probe(self): af = AshlockFingerprint(axl.TitForTat(), probe=axl.Random(p=0.1)) af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False) probes = af.spatial_tournament.players[1:] self.assertEqual(str(probes[0]), "Joss-Ann Random: 0.1: (0.0, 0.0)") # x + y < 1 self.assertEqual(str(probes[2]), "Dual Joss-Ann Random: 0.1: (1.0, 0.0)") # x + y = 1 self.assertEqual(str(probes[8]), "Dual Joss-Ann Random: 0.1: (0.0, 0.0)") # x + y > 1
def test_fingerprint_with_filename(self): filename = "test_outputs/test_fingerprint.csv" af = AshlockFingerprint(self.strategy, self.probe) 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")), 10)
def test_parallel_fingerprint(self): af = AshlockFingerprint(self.strategy, self.probe) af.fingerprint(turns=10, repetitions=2, step=0.5, processes=2, progress_bar=False) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(af.data.keys())) self.assertEqual(af.step, 0.5) self.assertEqual(edge_keys, self.expected_edges) self.assertEqual(coord_keys, self.expected_points)
def test_fingeprint_explicit_probe(self): af = AshlockFingerprint(axl.TitForTat(), probe=axl.Random(p=0.1)) af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False) probes = af.spatial_tournament.players[1:] self.assertEqual( str(probes[0]), "Joss-Ann Random: 0.1: (0.0, 0.0)" ) # x + y < 1 self.assertEqual( str(probes[2]), "Dual Joss-Ann Random: 0.1: (1.0, 0.0)" ) # x + y = 1 self.assertEqual( str(probes[8]), "Dual Joss-Ann Random: 0.1: (0.0, 0.0)" ) # x + y > 1
def test_in_memory_fingerprint(self): af = AshlockFingerprint(self.strategy, self.probe) af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False, in_memory=True) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(af.data.keys())) self.assertEqual(af.step, 0.5) self.assertEqual(af.spatial_tournament.interactions_dict, af.interactions) self.assertEqual(edge_keys, self.expected_edges) self.assertEqual(coord_keys, self.expected_points)
def test_plot(self): af = AshlockFingerprint(self.strategy, self.probe) af.fingerprint(turns=10, repetitions=2, step=0.25, progress_bar=False) p = af.plot() self.assertIsInstance(p, matplotlib.pyplot.Figure) q = af.plot(col_map='jet') self.assertIsInstance(q, matplotlib.pyplot.Figure) r = af.plot(interpolation='bicubic') self.assertIsInstance(r, matplotlib.pyplot.Figure) t = af.plot(title='Title') self.assertIsInstance(t, matplotlib.pyplot.Figure) u = af.plot(colorbar=False) self.assertIsInstance(u, matplotlib.pyplot.Figure) v = af.plot(labels=False) self.assertIsInstance(v, matplotlib.pyplot.Figure)
def test_plot_figure(self): af = AshlockFingerprint(axl.WinStayLoseShift, axl.TitForTat) af.fingerprint(turns=10, repetitions=2, step=0.25, progress_bar=False) p = af.plot() self.assertIsInstance(p, matplotlib.pyplot.Figure) q = af.plot(cmap="jet") self.assertIsInstance(q, matplotlib.pyplot.Figure) r = af.plot(interpolation="bicubic") self.assertIsInstance(r, matplotlib.pyplot.Figure) t = af.plot(title="Title") self.assertIsInstance(t, matplotlib.pyplot.Figure) u = af.plot(colorbar=False) self.assertIsInstance(u, matplotlib.pyplot.Figure) v = af.plot(labels=False) self.assertIsInstance(v, matplotlib.pyplot.Figure)
def test_progress_bar_fingerprint(self): af = AshlockFingerprint(self.strategy, self.probe) data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=True) self.assertEqual(sorted(data.keys()), self.expected_points)
def test_progress_bar_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=True) self.assertEqual(sorted(data.keys()), self.points_when_using_half_step)
def test_temp_file_creation(self): RecordedMksTemp.reset_record() af = AshlockFingerprint(axl.TitForTat) filename = "test_outputs/test_fingerprint.csv" 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))
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))
def test_temp_file_creation(self): RecordedMksTemp.reset_record() af = AshlockFingerprint(self.strategy, self.probe) filename = "test_outputs/test_fingerprint.csv" 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))
def test_fingerprint_player(self): af = AshlockFingerprint(axl.Cooperator()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) self.assertEqual(af.step, 0.5) self.assertEqual(af.points, self.points_when_using_half_step) self.assertEqual(af.spatial_tournament.turns, 5) self.assertEqual(af.spatial_tournament.repetitions, 3) self.assertEqual(af.spatial_tournament.edges, self.edges_when_using_half_step) # The first player is the fingerprinted one, the rest are probes. self.assertIsInstance(af.spatial_tournament.players[0], axl.Cooperator) self.assertEqual(len(af.spatial_tournament.players), 10) probes = af.spatial_tournament.players[1:] self.assertEqual(len(probes), len(af.points)) self.assertEqual(str(probes[0]), "Joss-Ann Tit For Tat: (0.0, 0.0)") # x + y < 1 self.assertEqual(str(probes[2]), "Dual Joss-Ann Tit For Tat: (1.0, 0.0)") # x + y = 1 self.assertEqual(str(probes[8]), "Dual Joss-Ann Tit For Tat: (0.0, 0.0)") # x + y > 1
def test_fingerprint_reproducibility(self, strategy_pair): """ A test to check that we can fingerprint with any two given strategies or instances """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) data2 = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) self.assertEqual(data, data2)
def test_fingerprint_interactions_cooperator(self): af = AshlockFingerprint(axl.Cooperator()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) # The keys are edges between players, values are repetitions. self.assertCountEqual( af.interactions.keys(), [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], ) self.assertEqual(len(af.interactions.values()), 9) # Each edge has 3 repetitions with 5 turns each. repetitions = af.interactions.values() self.assertTrue(all(len(rep) == 3 for rep in repetitions)) for iturn in range(3): self.assertTrue(all(len(rep[iturn]) == 5 for rep in repetitions)) # Interactions are invariant for any points where y is zero, and # the score should be maximum possible. # Player 1 is Point(0.0, 0.0). # Player 4 is Point(0.5, 0.0). # Player 7 is Point(1.0, 0.0). for iplayer in (1, 4, 7): for turns in af.interactions[(0, iplayer)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (C, C) for t in turns)) self.assertEqual(af.data[Point(0.0, 0.0)], 3.0) self.assertEqual(af.data[Point(0.5, 0.0)], 3.0) self.assertEqual(af.data[Point(1.0, 0.0)], 3.0) # Player 3 is Point(0.0, 1.0), which means constant defection # from the probe. But the Cooperator doesn't change and score is zero. for turns in af.interactions[(0, 3)]: self.assertEqual(len(turns), 5) self.assertTrue(all(t == (C, D) for t in turns)) self.assertEqual(af.data[Point(0.0, 1.0)], 0.0)
def test_fingerprint_player(self): af = AshlockFingerprint(axl.Cooperator()) af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False) self.assertEqual(af.step, 0.5) self.assertEqual(af.points, self.points_when_using_half_step) self.assertEqual(af.spatial_tournament.turns, 5) self.assertEqual(af.spatial_tournament.repetitions, 3) self.assertEqual(af.spatial_tournament.edges, self.edges_when_using_half_step) # The first player is the fingerprinted one, the rest are probes. self.assertIsInstance(af.spatial_tournament.players[0], axl.Cooperator) self.assertEqual(len(af.spatial_tournament.players), 10) probes = af.spatial_tournament.players[1:] self.assertEqual(len(probes), len(af.points)) self.assertEqual( str(probes[0]), "Joss-Ann Tit For Tat: (0.0, 0.0)" ) # x + y < 1 self.assertEqual( str(probes[2]), "Dual Joss-Ann Tit For Tat: (1.0, 0.0)" ) # x + y = 1 self.assertEqual( str(probes[8]), "Dual Joss-Ann Tit For Tat: (0.0, 0.0)" ) # x + y > 1