Two Population Hawk-Dove Example

The following code is an example of a simulation that runs a Hawk-Dove game under two-population discrete-time replicator dynamics.

If you have downloaded the source code, the script is examples/two_population_hd.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
""" An example simulation for a Hawk-Dove game using two-population discrete time replicator dynamics.

"""

from simulations.simulation_runner import SimulationRunner
from simulations.dynamics.npop_discrete_replicator import NPopDiscreteReplicatorDynamics
from simulations.base import listener
from simulations.base import once


def firstgen(this, gennum, thisgen, lastgen):
    """ Prints 'Working...' after the first generation step is complete

    """

    print >> this.out, 'Working...'


def initialset(this, firstpop):
    """ Prints out a notice for the first population being chosen

    """

    print >> this.out, 'Initial population selected.'


def stablestate(this, genct, thisgen, lastgen, firstgen):
    """ Prints a notice when a stable state is reached

    """

    print >> this.out, 'Stable state reached!'


def forcestop(this, genct, thisgen, lastgen, firstgen):
    """ Prints a notice when the simulation is force-stopped

    """

    print >> this.out, 'Force stopped!'


def generation(this, genct, thisgen, lastgen):
    """ Prints a notice that a generation is done.

    """

    print >> this.out, 'Generation {0} complete.'.format(genct)


def simdone(this, result):
    """ Prints a notice that one of the sims has finished.

    """

    print "Simulation {0} complete.".format(this.finished_count)


def alldone(this):
    """ Prints a notice when all simulations are done.

    """

    print "All done."


@listener('generation', generation)
@listener('force stop', forcestop)
@listener('stable state', stablestate)
@listener('initial set', initialset)
@once('generation', firstgen)
class HawkDoveSim(NPopDiscreteReplicatorDynamics):

    _payoffs = [[0, 4], [1, 2]]

    def __init__(self, *args, **kwdargs):
        super(HawkDoveSim, self).__init__(*args, **kwdargs)

    def _default_types(self):
        return [['H', 'D'], ['H', 'D']]

    def _interaction(self, me, profile):

        if me == 0 or me == 1:
            return self._payoffs[profile[me]][profile[1 - me]]
        else:
            raise ValueError("Profile index out of bounds")


@listener('result', simdone)
@listener('done', alldone)
class HDSimRunner(SimulationRunner):
    pass

if __name__ == '__main__':
    runner = HDSimRunner(HawkDoveSim)
    runner.go()
Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.