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_generate_data(self): interactions = { (0, 1): [[(C, C)], [(C, C)]], (0, 2): [[(C, C), (C, C)], [(C, D)]], (0, 3): [[(C, C), (D, C)]], (0, 4): [[(C, C), (D, C)], [(D, D)]], (0, 5): [[(C, D), (D, C)]], (0, 6): [[(C, D), (C, D)]], (0, 7): [[(C, D), (D, D)]], (0, 8): [[(D, D), (D, D)]], (0, 9): [[(D, C), (D, C)]], } expected = { Point(0.0, 0.0): 3.0, Point(0.0, 0.5): 1.5, Point(0.0, 1.0): 4.0, Point(0.5, 0.0): 2.5, Point(0.5, 0.5): 2.5, Point(0.5, 1.0): 0.0, Point(1.0, 0.0): 0.5, Point(1.0, 0.5): 1.0, Point(1.0, 1.0): 5.0, } data = generate_data(interactions, self.expected_points, self.expected_edges) self.assertEqual(data, expected)
def test_majority_fingerprint(self): axl.seed(0) # Fingerprinting is a random process. test_data = { Point(x=0.0, y=0.0): 3.000, Point(x=0.0, y=0.25): 1.940, Point(x=0.0, y=0.5): 1.130, Point(x=0.0, y=0.75): 1.030, Point(x=0.0, y=1.0): 0.980, Point(x=0.25, y=0.0): 3.000, Point(x=0.25, y=0.25): 2.130, Point(x=0.25, y=0.5): 1.940, Point(x=0.25, y=0.75): 2.060, Point(x=0.25, y=1.0): 1.940, Point(x=0.5, y=0.0): 3.000, Point(x=0.5, y=0.25): 2.300, Point(x=0.5, y=0.5): 2.250, Point(x=0.5, y=0.75): 2.420, Point(x=0.5, y=1.0): 2.690, Point(x=0.75, y=0.0): 3.000, Point(x=0.75, y=0.25): 2.400, Point(x=0.75, y=0.5): 2.010, Point(x=0.75, y=0.75): 2.390, Point(x=0.75, y=1.0): 2.520, Point(x=1.0, y=0.0): 3.000, Point(x=1.0, y=0.25): 2.360, Point(x=1.0, y=0.5): 1.740, Point(x=1.0, y=0.75): 2.260, Point(x=1.0, y=1.0): 2.260, } af = axl.AshlockFingerprint(axl.GoByMajority, axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_tft_fingerprint(self): axl.seed(0) # Fingerprinting is a random process. test_data = { Point(x=0.0, y=0.0): 3.000, Point(x=0.0, y=0.25): 1.820, Point(x=0.0, y=0.5): 1.130, Point(x=0.0, y=0.75): 1.050, Point(x=0.0, y=1.0): 0.980, Point(x=0.25, y=0.0): 3.000, Point(x=0.25, y=0.25): 2.440, Point(x=0.25, y=0.5): 1.770, Point(x=0.25, y=0.75): 1.700, Point(x=0.25, y=1.0): 1.490, Point(x=0.5, y=0.0): 3.000, Point(x=0.5, y=0.25): 2.580, Point(x=0.5, y=0.5): 2.220, Point(x=0.5, y=0.75): 2.000, Point(x=0.5, y=1.0): 1.940, Point(x=0.75, y=0.0): 3.000, Point(x=0.75, y=0.25): 2.730, Point(x=0.75, y=0.5): 2.290, Point(x=0.75, y=0.75): 2.310, Point(x=0.75, y=1.0): 2.130, Point(x=1.0, y=0.0): 3.000, Point(x=1.0, y=0.25): 2.790, Point(x=1.0, y=0.5): 2.480, Point(x=1.0, y=0.75): 2.310, Point(x=1.0, y=1.0): 2.180, } af = axl.AshlockFingerprint(axl.TitForTat(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_wsls_fingerprint(self): axl.seed(0) # Fingerprinting is a random process. test_data = { Point(x=0.0, y=0.0): 3.000, Point(x=0.0, y=0.25): 1.710, Point(x=0.0, y=0.5): 1.440, Point(x=0.0, y=0.75): 1.080, Point(x=0.0, y=1.0): 0.500, Point(x=0.25, y=0.0): 3.000, Point(x=0.25, y=0.25): 2.280, Point(x=0.25, y=0.5): 1.670, Point(x=0.25, y=0.75): 1.490, Point(x=0.25, y=1.0): 0.770, Point(x=0.5, y=0.0): 3.000, Point(x=0.5, y=0.25): 2.740, Point(x=0.5, y=0.5): 2.240, Point(x=0.5, y=0.75): 1.730, Point(x=0.5, y=1.0): 1.000, Point(x=0.75, y=0.0): 3.000, Point(x=0.75, y=0.25): 3.520, Point(x=0.75, y=0.5): 2.830, Point(x=0.75, y=0.75): 1.750, Point(x=0.75, y=1.0): 1.250, Point(x=1.0, y=0.0): 3.000, Point(x=1.0, y=0.25): 4.440, Point(x=1.0, y=0.5): 4.410, Point(x=1.0, y=0.75): 4.440, Point(x=1.0, y=1.0): 1.300, } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_reshape_data(self): test_points = [ Point(x=0.0, y=0.0), Point(x=0.0, y=0.5), Point(x=0.0, y=1.0), Point(x=0.5, y=0.0), Point(x=0.5, y=0.5), Point(x=0.5, y=1.0), Point(x=1.0, y=0.0), Point(x=1.0, y=0.5), Point(x=1.0, y=1.0) ] test_data = { Point(x=0.0, y=0.0): 5, Point(x=0.0, y=0.5): 9, Point(x=0.0, y=1.0): 3, Point(x=0.5, y=0.0): 8, Point(x=0.5, y=0.5): 2, Point(x=0.5, y=1.0): 4, Point(x=1.0, y=0.0): 2, Point(x=1.0, y=0.5): 1, Point(x=1.0, y=1.0): 9 } test_shaped_data = [[3, 4, 9], [9, 2, 1], [5, 8, 2]] plotting_data = reshape_data(test_data, test_points, 3) for i in range(len(plotting_data)): self.assertEqual(list(plotting_data[i]), test_shaped_data[i])
def test_majority_fingerprint(self): axl.seed(0) # Fingerprinting is a random process test_data = { Point(x=0.25, y=1.0): 2.179, Point(x=0.25, y=0.5): 1.9, Point(x=0.75, y=0.5): 1.81, Point(x=0.25, y=0.25): 2.31, Point(x=0.0, y=0.75): 1.03, Point(x=0.75, y=1.0): 2.58, Point(x=0.5, y=0.25): 2.34, Point(x=0.0, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.4, Point(x=1.0, y=0.75): 2.0, Point(x=1.0, y=0.5): 1.74, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 2.219, Point(x=0.0, y=1.0): 0.98, Point(x=0.5, y=0.0): 3.0, Point(x=0.25, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.94, Point(x=0.0, y=0.5): 1.13, Point(x=0.5, y=0.75): 2.6, Point(x=0.5, y=0.5): 1.89, Point(x=0.75, y=0.75): 2.13, Point(x=0.5, y=1.0): 2.7, Point(x=0.75, y=0.0): 3.0, Point(x=0.25, y=0.75): 1.859, Point(x=1.0, y=1.0): 2.26 } af = axl.AshlockFingerprint(axl.GoByMajority, self.probe) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_tft_fingerprint(self): axl.seed(0) # Fingerprinting is a random process test_data = { Point(x=0.25, y=1.0): 1.63, Point(x=0.25, y=0.5): 1.92, Point(x=0.75, y=0.5): 2.33, Point(x=0.25, y=0.25): 2.31, Point(x=0.0, y=0.75): 1.05, Point(x=0.75, y=1.0): 2.07, Point(x=0.5, y=0.25): 2.6, Point(x=0.0, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.76, Point(x=1.0, y=0.75): 2.38, Point(x=1.0, y=0.5): 2.58, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 2.72, Point(x=0.0, y=1.0): 0.98, Point(x=0.5, y=0.0): 3.0, Point(x=0.25, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.82, Point(x=0.0, y=0.5): 1.13, Point(x=0.5, y=0.75): 1.93, Point(x=0.5, y=0.5): 2.46, Point(x=0.75, y=0.75): 2.3, Point(x=0.5, y=1.0): 1.91, Point(x=0.75, y=0.0): 3.0, Point(x=0.25, y=0.75): 1.62, Point(x=1.0, y=1.0): 2.18 } af = axl.AshlockFingerprint(axl.TitForTat, self.probe) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_wsls_fingerprint(self): axl.seed(0) # Fingerprinting is a random process test_data = { Point(x=0.25, y=1.0): 0.84, Point(x=0.25, y=0.5): 1.85, Point(x=0.75, y=0.5): 3.01, Point(x=0.25, y=0.25): 2.23, Point(x=0.0, y=0.75): 1.08, Point(x=0.75, y=1.0): 1.14, Point(x=0.5, y=0.25): 2.66, Point(x=0.0, y=0.0): 3.0, Point(x=0.75, y=0.25): 3.12, Point(x=1.0, y=0.75): 3.57, Point(x=1.0, y=0.5): 3.94, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 4.78, Point(x=0.0, y=1.0): 0.5, Point(x=0.5, y=0.0): 3.0, Point(x=0.25, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.71, Point(x=0.0, y=0.5): 1.44, Point(x=0.5, y=0.75): 1.62, Point(x=0.5, y=0.5): 2.77, Point(x=0.75, y=0.75): 2.27, Point(x=0.5, y=1.0): 1.02, Point(x=0.75, y=0.0): 3.0, Point(x=0.25, y=0.75): 1.27, Point(x=1.0, y=1.0): 1.3 } af = axl.AshlockFingerprint(self.strategy, self.probe) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_majority_fingerprint(self): test_data = { Point(x=0.0, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.6, Point(x=0.0, y=0.5): 1.01, Point(x=0.0, y=0.75): 1.04, Point(x=0.0, y=1.0): 0.98, Point(x=0.25, y=0.0): 3.0, Point(x=0.25, y=0.25): 2.12, Point(x=0.25, y=0.5): 1.81, Point(x=0.25, y=0.75): 2.06, Point(x=0.25, y=1.0): 1.86, Point(x=0.5, y=0.0): 3.0, Point(x=0.5, y=0.25): 2.4299999999999997, Point(x=0.5, y=0.5): 2.37, Point(x=0.5, y=0.75): 2.74, Point(x=0.5, y=1.0): 2.68, Point(x=0.75, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.4299999999999997, Point(x=0.75, y=0.5): 1.8399999999999999, Point(x=0.75, y=0.75): 2.34, Point(x=0.75, y=1.0): 2.46, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 2.12, Point(x=1.0, y=0.5): 1.8599999999999999, Point(x=1.0, y=0.75): 2.0300000000000002, Point(x=1.0, y=1.0): 2.26 } af = axl.AshlockFingerprint(axl.GoByMajority, axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_tft_fingerprint(self): test_data = { Point(x=0.0, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.21, Point(x=0.0, y=0.5): 1.01, Point(x=0.0, y=0.75): 1.04, Point(x=0.0, y=1.0): 0.98, Point(x=0.25, y=0.0): 3.0, Point(x=0.25, y=0.25): 2.01, Point(x=0.25, y=0.5): 2.05, Point(x=0.25, y=0.75): 1.71, Point(x=0.25, y=1.0): 1.52, Point(x=0.5, y=0.0): 3.0, Point(x=0.5, y=0.25): 2.6500000000000004, Point(x=0.5, y=0.5): 2.36, Point(x=0.5, y=0.75): 2.1100000000000003, Point(x=0.5, y=1.0): 1.8900000000000001, Point(x=0.75, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.57, Point(x=0.75, y=0.5): 2.51, Point(x=0.75, y=0.75): 2.13, Point(x=0.75, y=1.0): 2.12, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 2.68, Point(x=1.0, y=0.5): 2.51, Point(x=1.0, y=0.75): 2.41, Point(x=1.0, y=1.0): 2.18 } af = axl.AshlockFingerprint(axl.TitForTat(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_wsls_fingerprint(self): test_data = { Point(x=0.0, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.83, Point(x=0.0, y=0.5): 1.12, Point(x=0.0, y=0.75): 1.04, Point(x=0.0, y=1.0): 0.5, Point(x=0.25, y=0.0): 3.0, Point(x=0.25, y=0.25): 2.12, Point(x=0.25, y=0.5): 2.17, Point(x=0.25, y=0.75): 1.33, Point(x=0.25, y=1.0): 0.77, Point(x=0.5, y=0.0): 3.0, Point(x=0.5, y=0.25): 2.9299999999999997, Point(x=0.5, y=0.5): 2.3600000000000003, Point(x=0.5, y=0.75): 1.74, Point(x=0.5, y=1.0): 1.05, Point(x=0.75, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.52, Point(x=0.75, y=0.5): 2.79, Point(x=0.75, y=0.75): 2.41, Point(x=0.75, y=1.0): 1.2, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 4.86, Point(x=1.0, y=0.5): 4.36, Point(x=1.0, y=0.75): 4.05, Point(x=1.0, y=1.0): 1.3 } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)