def test_init_from_resulsetfromfile(self): tmp_file = tempfile.NamedTemporaryFile(mode='w', delete=False) players = [ axelrod.Cooperator(), axelrod.TitForTat(), axelrod.Defector() ] tournament = axelrod.Tournament(players=players, turns=2, repetitions=2) tournament.play(filename=tmp_file.name, progress_bar=False) tmp_file.close() rs = axelrod.ResultSet(tmp_file.name, players, 2, progress_bar=False) plot = axelrod.Plot(rs) self.assertEqual(plot.result_set, rs)
def test_ecosystem(self): if matplotlib_installed: eco = axelrod.Ecosystem(self.test_result_set) eco.reproduce(100) plot = axelrod.Plot(self.test_result_set) fig = plot.stackplot(eco) self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) fig = plot.stackplot(eco, title="dummy title") self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) fig = plot.stackplot(eco, logscale=False) self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) else: self.skipTest('matplotlib not installed')
def test_stackplot_with_passed_axes(self): # Test that can plot on a given matplotlib axes eco = axelrod.Ecosystem(self.test_result_set) eco.reproduce(100) plot = axelrod.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) self.assertEqual(axarr[0, 1].get_xlim(), (0, 1)) plot.stackplot(eco, ax=axarr[0, 1]) self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1)) # Plot on another axes with a title plot.stackplot(eco, title="dummy title", ax=axarr[1, 0]) self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1)) self.assertEqual(axarr[1, 0].get_title(), "dummy title")
def playWithHete(TD, DP, MC): ccPercent = [[0], [0], [0]] ccCnt = 0 players = [TD, DP] match = axl.Match(players, turns=10000) result = match.play() # print result for i in range(1, 10000): if result[i] == ('C', 'C'): ccCnt += 1 ccPercent[0].append(ccCnt / i) ccCnt = 0 players = [TD, MC] match = axl.Match(players, turns=10000) result = match.play() # print result for i in range(1, 10000): if result[i] == ('C', 'C'): ccCnt += 1 ccPercent[1].append(ccCnt / i) ccCnt = 0 players = [DP, MC] match = axl.Match(players, turns=10000) result = match.play() # print result for i in range(1, 10000): if result[i] == ('C', 'C'): ccCnt += 1 ccPercent[2].append(ccCnt / i) print ccPercent[0] print len(ccPercent[0]) print ccPercent[1] print len(ccPercent[1]) print ccPercent[2] print len(ccPercent[2]) drawCCPercent(ccPercent, False) # heat graph players = [TD, DP, MC] tournament = axl.Tournament(players, turns=10000) results = tournament.play() plot = axl.Plot(results) p = plot.payoff() p.savefig("heat graph")
def test_payoff_with_passed_axes(self): plot = axelrod.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) self.assertEqual(axarr[0, 1].get_xlim(), (0, 1)) plot.payoff(ax=axarr[0, 1]) self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1)) # Ensure color bar draw at same location as boxplot color_bar_bbox = fig.axes[-1].get_position().get_points() payoff_bbox_coord = fig.axes[1].get_position().get_points() self.assertEqual(color_bar_bbox[1, 1], payoff_bbox_coord[1, 1], msg="Color bar is not in correct location.") # Plot on another axes with a title plot.payoff(title="dummy title", ax=axarr[1, 0]) self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1)) self.assertEqual(axarr[1, 0].get_xlabel(), "dummy title")
def playWithFixed(TD, DP, MC): C = axl.Cooperator() D = axl.Defector() TFT = axl.TitForTat() BUL = axl.Bully() PAV = Pavlov() APAV = axl.APavlov2011() for learner in [TD,DP,MC]: players = [C, D, TFT, BUL, PAV, APAV, learner] tournament = axl.Tournament(players, turns=10000) results = tournament.play() title = learner.name + " VS Fixed Strategy Players" plot = axl.Plot(results) p = plot.boxplot(title) p.savefig(learner.name)
def tournament(self): """ Runs a tournament between EXP_RTL and all non-longrunning and non-cheating strategies in the Axelrod database. Strategies with very high computational cost and/or try to "cheat", eg by modifying their own or their opponents source code, have been excluded. The results of the tournament are stored to file, along with a series of plots that visualize the results. """ #Uncomment code below to run tournament with random seed: #axl.seed(0) players = [self] + [ strategy() for strategy in axl.strategies if strategy.classifier["long_run_time"] == False ] tournament = axl.Tournament(players, turns=200, repetitions=5) results = tournament.play() # Write results to file with open("results.txt", "a", encoding="UTF-8") as resultsfile: for player_rank in results.ranked_names: resultsfile.write(f"{player_rank}\n") # Write more comprehensive tournament summary to CSV file results.write_summary("tournament_summary.csv") # Create visual representations of the tournament results with plots plot = axl.Plot(results) """ All plots are saved to file, including boxplot, winplot, payoff matrix, and more. """ plot.save_all_plots("tournament_visualization")
def test_boxplot_dataset(self): plot = axelrod.Plot(self.test_result_set) self.assertSequenceEqual( plot._boxplot_dataset, self.expected_boxplot_dataset)
def test_payoff(self): if matplotlib_installed: plot = axelrod.Plot(self.test_result_set) self.assertIsInstance(plot.payoff(), matplotlib.pyplot.Figure) else: self.skipTest('matplotlib not installed')
def test_init(self): plot = axelrod.Plot(self.test_result_set) self.assertEqual(plot.result_set, self.test_result_set) self.assertEqual(matplotlib_installed, plot.matplotlib_installed)
def test_payoff(self): plot = axl.Plot(self.test_result_set) fig = plot.payoff() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig)
def test_payoff_with_title(self): plot = axl.Plot(self.test_result_set) fig = plot.payoff(title="dummy title") self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig)
def test_winplot_title(self): plot = axelrod.Plot(self.test_result_set) self.assertEqual(plot._winplot_title, self.expected_winplot_title)
def test_payoff_dataset(self): plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._payoff_dataset, self.expected_payoff_dataset)
def test_boxplot_with_title(self): plot = axl.Plot(self.test_result_set) fig = plot.boxplot(title="title") self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig)
def test_sdvplot_dataset(self): plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._sdv_plot_dataset, self.expected_sdvplot_dataset)
def obtain_assets(results, filename, turns, strategies_name="strategies", tournament_type="std", assets_dir="./assets", lengthplot=False): """ From the results of a tournament: obtain the various plots and the summary data set Parameters ---------- results: axelrod.resultset instance strategies_name: string, eg: "ordinary_strategies" tournament_type: string, eg: "std" assets_dir: string [default: "./assets"] lengthplot: boolean [default: False], whether or not to plot the length of the matches (only needed for the probabilistic ending matches) """ if not os.path.exists(assets_dir): os.makedirs(assets_dir) total = 6 + int(lengthplot) pbar = tqdm.tqdm(total=total, desc="Obtaining plots") file_path_root = "{}/{}_{}".format(assets_dir, strategies_name, tournament_type) plot = axl.Plot(results) f = plot.boxplot(title=label("Payoff", results, turns)) f.savefig("{}_boxplot.png".format(file_path_root)) f.savefig("{}_boxplot.svg".format(file_path_root)) pbar.update() f = plot.payoff(title=label("Payoff", results, turns)) f.savefig("{}_payoff.png".format(file_path_root)) f.savefig("{}_payoff.svg".format(file_path_root)) pbar.update() f = plot.winplot(title=label("Wins", results, turns)) f.savefig("{}_winplot.png".format(file_path_root)) f.savefig("{}_winplot.svg".format(file_path_root)) pbar.update() f = plot.sdvplot(title=label("Payoff differences", results, turns)) f.savefig("{}_sdvplot.png".format(file_path_root)) f.savefig("{}_sdvplot.svg".format(file_path_root)) pbar.update() f = plot.pdplot(title=label("Payoff differences", results, turns)) f.savefig("{}_pdplot.png".format(file_path_root)) f.savefig("{}_pdplot.svg".format(file_path_root)) pbar.update() eco = axl.Ecosystem(results) eco.reproduce(1000) f = plot.stackplot(eco, title=label("Eco", results, turns)) f.savefig("{}_reproduce.png".format(file_path_root)) f.savefig("{}_reproduce.svg".format(file_path_root)) pbar.update() if lengthplot is True: f = plot.lengthplot(title=label("Length of matches", results, turns)) f.savefig("{}_lengthplot.png".format(file_path_root)) f.savefig("{}_lengthplot.svg".format(file_path_root)) pbar.update() return plot
multAxel = axelrod.EvolveAxel(3, evolveCode, 'MULT') strategies.append(multAxel) singAxel = axelrod.EvolveAxel(3, singEvolveCode, 'SING') strategies.append(singAxel) strategies.append( axelrod.LearnerAxel(memory_depth=2, exploreProb=0.1, learnerType=2)) tournament = axelrod.Tournament(strategies) results = tournament.play() print results.scores name = results.players scor = results.normalised_scores for i in range(len(scor)): print name[i], numpy.mean(scor[i]) plot = axelrod.Plot(results) p = plot.boxplot() p.show() raw_input('Next') p = plot.winplot() p.show() raw_input('Next') p = plot.payoff() p.show() raw_input('Next')
def test_boxplot_title(self): plot = axelrod.Plot(self.test_result_set) self.assertEqual(plot.boxplot_title(), self.expected_boxplot_title)
def test_init(self): result_set = axelrod.ResultSet(('Player1', 'Player2', 'Player3'), 5, 2) self.assertRaises(AttributeError, axelrod.Plot, result_set) plot = axelrod.Plot(self.test_result_set) self.assertEqual(plot.result_set, self.test_result_set) self.assertEqual(matplotlib_installed, plot.matplotlib_installed)
import axelrod as axl from axelrod.strategies import Cooperator, Defector, TitForTat, ForgivingTitForTat from axelrod_agent import SMA2CPlayer import json # players = [Cooperator, Defector, TitForTat, ForgivingTitForTat, axl.] # players = (Cooperator(), Defector(), SMA2CPlayer('outputs/1592207053-sma2c-70000')) # tournament = axl.Tournament(players) # results = tournament.play() # print(results) axl.seed(10) players = [s() for s in axl.strategies if s in axl.axelrod_first_strategies] players.append( SMA2CPlayer('outputs/1592601088-sma2c-noseed-10000-smaller-mid-half')) game = axl.Game(r=2, s=-1, t=3, p=0) t = axl.Tournament(players, turns=25, repetitions=100, game=game) res = t.play(filename='tournament-results.csv') print(res.ranked_names) res.write_summary('summary-3.csv') p = axl.Plot(res)
def test_boxplot_xticks_labels(self): plot = axelrod.Plot(self.test_result_set) self.assertEqual( plot._boxplot_xticks_labels, self.expected_boxplot_xticks_labels)
def test_lengthplot_dataset(self): plot = axl.Plot(self.test_result_set) self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset)
def test_init(self): plot = axl.Plot(self.test_result_set) self.assertEqual(plot.result_set, self.test_result_set)
def test_lengthplot(self): plot = axelrod.Plot(self.test_result_set) fig = plot.lengthplot() self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig)
j = random.random() #Let Mr Hyde and Dr Jykle compete #If Dr Jykle beats Mr Hyde cooperate else defect if h - j >= 0: return C else: return D hj = DrJekyllMrHyde() #Create instance of class #players = [s() for s in axl.short_run_time_strategies] # Create players players = [ hj, axl.Defector(), axl.TitForTat(), axl.Thumper(), axl.Punisher(), axl.AdaptiveTitForTat(), axl.FirstByDavis(), axl.Appeaser() ] #players.append(hj) tournament = Tournament(players) # Create a tournament results = tournament.play() # Play the tournament results.write_summary('SummaryMileStone3.csv') # Write results to csv file plot = axl.Plot(results) p = plot.boxplot() #Plot results p.show()
def test_boxplot_xticks_locations(self): plot = axl.Plot(self.test_result_set) self.assertEqual(plot._boxplot_xticks_locations, self.expected_boxplot_xticks_locations)