def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None, filename=None, in_memory=False, progress_bar=True): """Build and play the spatial tournament. Creates the probes and their edges then builds a spatial tournament. When the coordinates of the probe sum to more than 1, the dual of the probe is taken instead and then the Joss-Ann Transformer is applied. If the coordinates sum to less than 1 (or equal), then only the Joss-Ann is applied, a dual is not required. Parameters ---------- turns : integer, optional The number of turns per match repetitions : integer, optional The number of times the round robin should be repeated step : float, optional The separation between each Point. Smaller steps will produce more Points that will be closer together. processes : integer, optional The number of processes to be used for parallel processing progress_bar : bool Whether or not to create a progress bar which will be updated Returns ---------- self.data : dictionary A dictionary where the keys are coordinates of the form (x, y) and the values are the mean score for the corresponding interactions. """ if on_windows and (filename is None): in_memory = True elif filename is not None: outputfile = open(filename, 'w') filename = outputfile.name else: outputfile = NamedTemporaryFile(mode='w') filename = outputfile.name edges, tourn_players = self.construct_tournament_elements(step, progress_bar=progress_bar) self.step = step self.spatial_tournament = axl.SpatialTournament(tourn_players, turns=turns, repetitions=repetitions, edges=edges) self.spatial_tournament.play(build_results=False, filename=filename, processes=processes, in_memory=in_memory, progress_bar=progress_bar) if in_memory: self.interactions = self.spatial_tournament.interactions_dict else: self.interactions = read_interactions_from_file(filename, progress_bar=progress_bar) self.data = self.generate_data(self.interactions, self.points, edges) return self.data
def test_particular_tournament(self): """A test for a tournament that has caused failures during some bug fixing""" players = [axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat(), axelrod.Grudger()] edges = [(0, 2), (0, 3), (1, 2), (1, 3)] tournament = axelrod.SpatialTournament(players, edges=edges) results = tournament.play(progress_bar=False) expected_ranked_names = ['Cooperator', 'Tit For Tat', 'Grudger', 'Defector'] self.assertEqual(results.ranked_names, expected_ranked_names) # Check that this tournament runs with noise tournament = axelrod.SpatialTournament(players, edges=edges, noise=.5) results = tournament.play(progress_bar=False) self.assertIsInstance(results, axelrod.ResultSet)
def build_ashlock_tournament(strategies): """For a list of strategies, build the ashlock tournament but do not play. """ points = create_points(step=0.25) probes = create_probes(axl.TitForTat, points) edges = create_edges(strategies, points) tournament_players = strategies + probes spatial_tournament = axl.SpatialTournament(tournament_players, edges=edges) return spatial_tournament
def main(index, players=players, processes=None, seed=1, turns=200, repetitions=10000, noise=0): """ index of the player in question """ edges = [(index, j) for j, _ in enumerate(players)] assert (index, index) in edges assert len(edges) == len(players) if processes is None: processes = multiprocessing.cpu_count() prefix = "{}_{}_{}_{}".format(seed, int(100 * noise), repetitions, abbreviations[str(players[index])]) interactions_filename = "../data/cooperation_{}_interactions.csv".format( prefix) output_filename = "../data/cooperation_{}_array.gz".format(prefix) # Deleting the file if it exists try: os.remove(interactions_filename) except OSError: pass axl.seed(seed) # Setting a seed assert axl.__version__ == "2.9.0" print("Axelrod version", axl.__version__) print(len(players), "Players") print("Seed", seed) print("Repetitions", repetitions) print("Noise", noise) print("Player", players[index]) tournament = axl.SpatialTournament(players, edges=edges, turns=turns, repetitions=repetitions, noise=noise) tournament.play(filename=interactions_filename, processes=processes, build_results=False, progress_bar=False) matrix = obtain_cooperation_matrix(interactions_filename) np.savetxt(fname=output_filename, X=matrix, delimiter=",")
def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): """ A test to check that a spatial tournament on the complete multigraph gives the same results as the round robin. """ players = [s() for s in strategies] # edges edges = [] for i in range(0, len(players)): for j in range(i, len(players)): edges.append((i, j)) # create a round robin tournament tournament = axelrod.Tournament(players, repetitions=repetitions, turns=turns, noise=noise) # create a complete spatial tournament spatial_tournament = axelrod.SpatialTournament(players, repetitions=repetitions, turns=turns, noise=noise, edges=edges) axelrod.seed(seed) results = tournament.play(progress_bar=False) axelrod.seed(seed) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.ranked_names, spatial_results.ranked_names) self.assertEqual(results.nplayers, spatial_results.nplayers) self.assertEqual(results.repetitions, spatial_results.repetitions) self.assertEqual(results.payoff_diffs_means, spatial_results.payoff_diffs_means) self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix) self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs) self.assertEqual(results.payoffs, spatial_results.payoffs) self.assertEqual(results.cooperating_rating, spatial_results.cooperating_rating) self.assertEqual(results.cooperation, spatial_results.cooperation) self.assertEqual(results.normalised_cooperation, spatial_results.normalised_cooperation) self.assertEqual(results.normalised_scores, spatial_results.normalised_scores) self.assertEqual(results.good_partner_matrix, spatial_results.good_partner_matrix) self.assertEqual(results.good_partner_rating, spatial_results.good_partner_rating)
def test_generate_data(self): af = AshlockFingerprint(self.strategy, self.probe) edges, players = af.construct_tournament_elements(0.5) spatial_tournament = axl.SpatialTournament(players, turns=10, repetitions=2, edges=edges) results = spatial_tournament.play(progress_bar=False, keep_interactions=True) data = af.generate_data(results.interactions, self.expected_points, self.expected_edges) keys = sorted(list(data.keys())) values = [0 < score < 5 for score in data.values()] self.assertEqual(sorted(keys), self.expected_points) self.assertEqual(all(values), True)
def test_init(self): tournament = axelrod.SpatialTournament(name=self.test_name, players=self.players, game=self.game, turns=self.test_turns, edges=self.test_edges, noise=0.2) self.assertEqual(tournament.match_generator.edges, tournament.edges) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertEqual(tournament.game.score(('C', 'C')), (3, 3)) self.assertEqual(tournament.turns, 100) self.assertEqual(tournament.repetitions, 10) self.assertEqual(tournament.name, 'test') self.assertTrue(tournament._with_morality) self.assertIsInstance(tournament._logger, logging.Logger) self.assertEqual(tournament.noise, 0.2) self.assertEqual(tournament.match_generator.noise, 0.2) anonymous_tournament = axelrod.Tournament(players=self.players) self.assertEqual(anonymous_tournament.name, 'axelrod')