예제 #1
0
def obtain_assets(results,
                  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)
    """

    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))
    f.savefig("{}_boxplot.svg".format(file_path_root))
    pbar.update()

    f = plot.payoff(title=label("Payoff", results))
    f.savefig("{}_payoff.svg".format(file_path_root))
    pbar.update()

    f = plot.winplot(title=label("Wins", results))
    f.savefig("{}_winplot.svg".format(file_path_root))
    pbar.update()

    f = plot.sdvplot(title=label("Payoff differences", results))
    f.savefig("{}_sdvplot.svg".format(file_path_root))
    pbar.update()

    f = plot.pdplot(title=label("Payoff differences", results))
    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))
    f.savefig("{}_reproduce.svg".format(file_path_root))
    pbar.update()

    if lengthplot is True:
        f = plot.lengthplot(title=label("Length of matches", results))
        f.savefig("{}_lengthplot.svg".format(file_path_root))
        pbar.update()
예제 #2
0
 def test_population_normalization(self):
     eco = axl.Ecosystem(self.res_cooperators, population=[70, 25, 3, 2])
     pops = eco.population_sizes
     self.assertEqual(eco.num_players, 4)
     self.assertEqual(len(pops), 1)
     self.assertEqual(len(pops[0]), 4)
     self.assertAlmostEqual(sum(pops[0]), 1.0)
     self.assertEqual(pops[0], [0.7, 0.25, 0.03, 0.02])
예제 #3
0
 def test_default_population_sizes(self):
     eco = axl.Ecosystem(self.res_cooperators)
     pops = eco.population_sizes
     self.assertEqual(eco.num_players, 4)
     self.assertEqual(len(pops), 1)
     self.assertEqual(len(pops[0]), 4)
     self.assertAlmostEqual(sum(pops[0]), 1.0)
     self.assertEqual(list(set(pops[0])), [0.25])
예제 #4
0
 def test_ecosystem(self):
     if matplotlib_installed:
         eco = axelrod.Ecosystem(self.test_result_set)
         eco.reproduce(100)
         plot = axelrod.Plot(self.test_result_set)
         self.assertIsInstance(plot.stackplot(eco.population_sizes),
                               matplotlib.pyplot.Figure)
     else:
         self.skipTest('matplotlib not installed')
예제 #5
0
 def test_cooperators_are_stable_over_time(self):
     eco = axl.Ecosystem(self.res_cooperators)
     eco.reproduce(100)
     pops = eco.population_sizes
     self.assertEqual(len(pops), 101)
     for p in pops:
         self.assertEqual(len(p), 4)
         self.assertEqual(sum(p), 1.0)
         self.assertEqual(list(set(p)), [0.25])
예제 #6
0
 def test_non_default_population_sizes(self):
     eco = axelrod.Ecosystem(self.res_cooperators,
                             population=[.7, .25, .03, .02])
     pops = eco.population_sizes
     self.assertEqual(eco.num_players, 4)
     self.assertEqual(len(pops), 1)
     self.assertEqual(len(pops[0]), 4)
     self.assertAlmostEqual(sum(pops[0]), 1.0)
     self.assertEqual(pops[0], [.7, .25, .03, .02])
예제 #7
0
    def test_init(self):
        """Are the populations created correctly?"""

        # By default create populations of equal size
        eco = axelrod.Ecosystem(self.res_cooperators)
        pops = eco.population_sizes
        self.assertEqual(eco.nplayers, 4)
        self.assertEqual(len(pops), 1)
        self.assertEqual(len(pops[0]), 4)
        self.assertAlmostEqual(sum(pops[0]), 1.0)
        self.assertEqual(list(set(pops[0])), [0.25])

        # Can pass list of initial population distributions
        eco = axelrod.Ecosystem(self.res_cooperators,
                                population=[.7, .25, .03, .02])
        pops = eco.population_sizes
        self.assertEqual(eco.nplayers, 4)
        self.assertEqual(len(pops), 1)
        self.assertEqual(len(pops[0]), 4)
        self.assertAlmostEqual(sum(pops[0]), 1.0)
        self.assertEqual(pops[0], [.7, .25, .03, .02])

        # Distribution will automatically normalise
        eco = axelrod.Ecosystem(self.res_cooperators,
                                population=[70, 25, 3, 2])
        pops = eco.population_sizes
        self.assertEqual(eco.nplayers, 4)
        self.assertEqual(len(pops), 1)
        self.assertEqual(len(pops[0]), 4)
        self.assertAlmostEqual(sum(pops[0]), 1.0)
        self.assertEqual(pops[0], [.7, .25, .03, .02])

        # If passed list is of incorrect size get error
        self.assertRaises(TypeError,
                          axelrod.Ecosystem,
                          self.res_cooperators,
                          population=[.7, .2, .03, .1, .1])

        # If passed list has negative values
        self.assertRaises(TypeError,
                          axelrod.Ecosystem,
                          self.res_cooperators,
                          population=[.7, -.2, .03, .2])
예제 #8
0
    def test_cooperators(self):
        """Are cooperators stable over time?"""

        eco = axelrod.Ecosystem(self.res_cooperators)
        eco.reproduce(100)
        pops = eco.population_sizes
        self.assertEqual(len(pops), 101)
        for p in pops:
            self.assertEqual(len(p), 4)
            self.assertEqual(sum(p), 1.0)
            self.assertEqual(list(set(p)), [0.25])
예제 #9
0
 def test_defector_wins_with_only_cooperators(self):
     eco = axl.Ecosystem(self.res_defector_wins)
     eco.reproduce(1000)
     pops = eco.population_sizes
     self.assertEqual(len(pops), 1001)
     for p in pops:
         self.assertEqual(len(p), 4)
         self.assertAlmostEqual(sum(p), 1.0)
     last = pops[-1]
     self.assertAlmostEqual(last[0], 0.0)
     self.assertAlmostEqual(last[1], 0.0)
     self.assertAlmostEqual(last[2], 0.0)
     self.assertAlmostEqual(last[3], 1.0)
예제 #10
0
 def test_ecosystem(self):
     if matplotlib_installed:
         eco = axelrod.Ecosystem(self.test_result_set)
         eco.reproduce(100)
         plot = axelrod.Plot(self.test_result_set)
         self.assertIsInstance(plot.stackplot(eco),
                               matplotlib.pyplot.Figure)
         self.assertIsInstance(plot.stackplot(eco, title="dummy title"),
                               matplotlib.pyplot.Figure)
         self.assertIsInstance(plot.stackplot(eco, logscale=False),
                               matplotlib.pyplot.Figure)
     else:
         self.skipTest('matplotlib not installed')
예제 #11
0
    def test_stackplot(self):
        eco = axl.Ecosystem(self.test_result_set)
        eco.reproduce(100)

        plot = axl.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)
예제 #12
0
    def test_defector_wins(self):
        """Does one defector win over time?"""

        eco = axelrod.Ecosystem(self.res_defector_wins)
        eco.reproduce(1000)
        pops = eco.population_sizes
        self.assertEqual(len(pops), 1001)
        for p in pops:
            self.assertEqual(len(p), 4)
            self.assertAlmostEqual(sum(p), 1.0)
        last = pops[-1]
        self.assertAlmostEqual(last[0], 0.0)
        self.assertAlmostEqual(last[1], 0.0)
        self.assertAlmostEqual(last[2], 0.0)
        self.assertAlmostEqual(last[3], 1.0)
예제 #13
0
    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")
예제 #14
0
    def test_stackplot(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:  # pragma: no cover
            self.skipTest('matplotlib not installed')
예제 #15
0
 def test_fitness(self):
     fitness = lambda p: 2 * p
     eco = axelrod.Ecosystem(self.res_cooperators, fitness=fitness)
     self.assertTrue(eco.fitness(10), 20)