### Christmas gifts, evolution and Python [#PyConNa](https://twitter.com/hashtag/PyConNa?src=hash) [@drvinceknight](https://twitter.com/drvinceknight)
playing rock paper
                    scissors
tweet about christmas
Prisoners dilemma

T>R>P>S

Prisoners dilemma

5>3>1>0

Christmas happens every year

  • Alternator: give gifts one year and not give gifts the next.
  • TitForTat: do whatever the other does the previous year.

>>> import axelrod as axl
>>> alex, camille = axl.Alternator(), axl.TitForTat()
>>> match = axl.Match([alex, camille], 10)
>>> _ = match.play()
>>> print(match.sparklines(c_symbol='πŸ˜€', d_symbol='🎁'))
πŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€πŸŽ
πŸ˜€πŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€πŸŽπŸ˜€
					
  1. Robert Axelrod
  2. 1980a: 14+1 strategies
  3. 1980b: 64+1 strategies
  1. Cooperator
  2. Defector
  3. Alternator
  4. TitForTat
  5. TwoTitForTat
  6. Grudger

>>> family = [axl.Cooperator(),
...           axl.Defector(),
...           axl.Alternator(),
...           axl.TitForTat(),
...           axl.TwoTitsForTat(),
...           axl.Grudger()]
>>> christmas = axl.Tournament(family, turns=50, repetitions=1)
>>> results = christmas.play()
>>> results.scores
[[525], [562], [417], [622], [646], [646]]
					

What about at a population level?


>>> evo = axl.Ecosystem(results)
>>> evo.reproduce(100)
>>> plot = axl.Plot(results)
>>> plot.stackplot(evo)
					
Evolutionary stackplot
Evolutionary stackplot with large value of temptation

T=500


>>> def check_if_end_pop_cooperates(r=3, p=1, s=0, t=5,
...                                 digits=5, family=family,
...                                 turns=10000):
...    """Returns a boolean and the last population vector"""
...    game = axl.Game(r=r, p=p, s=s, t=t)
...    christmas = axl.Tournament(family, turns=50,
...                               repetitions=1, game=game)
...    results = christmas.play()
...    evo = axl.Ecosystem(results)
...    evo.reproduce(turns)
...    last_pop = [round(pop, digits) for pop in evo.population_sizes[-1]]
...    return last_pop[1] == last_pop[2] == 0, last_pop
					
Evolutionary stackplot with large value of temptation
Evolutionary stackplot with large value of temptation

TLDR: The world we see is probably because it feels really good to give.

vknight.org/unpeudemath/

The Axelrod library

{[github.com](https://github.com/Axelrod-Python/Axelrod), [gitter.im](https://gitter.im/Axelrod-Python/Axelrod)}/Axelrod-Python/Axelrod

class TitForTat(Player):
    """A player starts by cooperating and then mimics previous move by opponent."""

    name = 'Tit For Tat'
    classifier = {
        'memory_depth': 1,  # Four-Vector = (1.,0.,1.,0.)
        'stochastic': False,
        'inspects_source': False,
        'manipulates_source': False,
        'manipulates_state': False
    }

    @staticmethod
    def strategy(opponent):
        return 'D' if opponent.history[-1:] == ['D'] else 'C'
					

class TestTitForTat(TestPlayer):

    name = "Tit For Tat"
    player = axelrod.TitForTat
    expected_classifier = {
        'memory_depth': 1,
        'stochastic': False,
        'inspects_source': False,
        'manipulates_source': False,
        'manipulates_state': False
    }

    def test_strategy(self):
        """Starts by cooperating."""
        self.first_play_test(C)

    def test_effect_of_strategy(self):
        """Repeats last action of opponent history."""
        self.markov_test([C, D, C, D])
        self.responses_test([C] * 4, [C, C, C, C], [C])
        self.responses_test([C] * 5, [C, C, C, C, D], [D])
					

axelrod-tournament.readthedocs.org

`pip install axelrod` {[github.com](https://github.com/Axelrod-Python/Axelrod), [gitter.im](https://gitter.im/Axelrod-Python/Axelrod)}/Axelrod-Python/Axelrod [Blog posts by TC](http://www.thomascampbell.me.uk/category/axelrod.html) [demo.ipynb](https://github.com/drvinceknight/Talks/blob/gh-pages/2015-09-20-Tit-for-tat-and-the-axelrod-library/demo.ipynb)