项目作者: Socrats

项目描述 :
C++/Python Toolbox for Evolutionary Game Theory.
高级语言: Python
项目地址: git://github.com/Socrats/EGTTools.git
创建时间: 2020-02-21T16:14:28Z
项目社区:https://github.com/Socrats/EGTTools

开源协议:GNU General Public License v3.0

下载


EGTtools

Toolbox for Evolutionary Game Theory

PyPI version
Documentation Status
Build Join the chat at https://gitter.im/EGTTools/community
Binder
DOI

EGTtools provides a centralized repository with analytical and numerical methods to study/model game theoretical
problems under the Evolutionary Game Theory (EGT) framework.

This library is composed of two parts:

  • a set of analytical methods implemented in Python 3
  • a set of computational methods implemented in C++ with (Python 3 bindings)

The second typed is used in cases where the state space is too big to solve analytically, and thus require estimating
the model parameters through monte-carlo simulations. The C++ implementation provides optimized computational methods
that can run in parallel in a reasonable time, while Python bindings make the methods easily accecible to a larger range
of researchers.

Table of Contents

  1. Requirements
  2. Downloading sources
  3. Examples of usage
  4. Documentation
  5. Caveats
  6. Citing
  7. Licence
  8. Acknowledgements

Requirements

To be able to install EGTtools, you must have:

  • A recent version of Linux (only tested on Ubuntu), MacOSX (Mojave or above) or Windows
  • CMake version 3.17 or higher
  • C++ 17
  • Eigen 3.3.*
  • Boost 1.80.*
  • Python 3.7 or higher
  • If you want support for parallel operations you should install OpenMP
  • Ideally, you should also install OpenBLAS, which offers optimized implementations of
    linear algebra kernels for several processor architectures, and install numpy and scipy versions that use it.

Downloading sources

When cloning the repository you should also clone the submodules so that pybind11 is downloaded. You can do that by
running:

  1. git clone --recurse-submodules -j8 https://github.com/Socrats/EGTTools.git

Installation

With pip

You can install egttools directly from PyPi with:

  1. pip install egttools

Currently, only the Linux build supports OpenMP parallelization for numerical simulations. This should normally be ok
for most applications, since numerical simulations are heavy and should be run on High Power Computing (HPC) clusters
which normally run Linux distributions.

We are investigating how to provide support for OpenMP in both Windows and Mac. In the meantime, if you really want to
run numerical simulations on either of the two platforms, you should follow the compilation instructions below and try
to link OpenMP for your platform yourself. Please, if you manage to do so, open an issue or a pull request with your
solutions.

Note: For Apple M1 (arm64) you should install using pip install egttools --no-deps so that pip does not
install the dependencies of the package. You should then install these dependencies through a virtual environment
created with miniforge (see Caveats for more information on why
this is necessary). Once you have miniforge installed you can do the following (assuming that you are in the base
miniforge environment):

  1. conda create -n egtenv python=3.9
  2. conda activate egtenv
  3. conda install numpy
  4. conda install scipy
  5. conda install matplotlib
  6. conda install networkx
  7. conda install seaborn

Build from source

To build egttools from source follow the following steps.

To install all required packages run:

  1. python -m venv egttools-env
  2. source egttools-env/bin/activate
  3. pip install -r requirements.txt

Or with anaconda:

  1. conda env create -f environment.yml
  2. conda activate egttools-env

Also, to make your virtual environment visible to jupyter:

  1. conda install ipykernel # or pip install ipykernel
  2. python -m ipykernel install --user --name=egttools-env

You can build EGTtools in your virtual environment by running:

  1. pip install build
  2. cd <path>
  3. python -m build

Where <path> represents the path to the EGTtools folder. If you are running this while inside the EGTtools folder,
then <path> is simply ./.

Finally, you can install EGTtools in development mode, this will allow the installation to update with new
modifications to the package:

  1. python -m pip install -e <path>

If you don’t want development mode, you can skip the option -e.

Examples of usage

The Analytical example is a jupyter notebook which analyses analytically the
evolutionary dynamics in a (2-person, 2-actions, one-shot) Hawk-Dove game.

The Numerical example is a jupyter notebook which analyses
through numerical simulations the evolutionary dynamics in a (2-person, 2-actions, one-shot) Hawk-Dove game.

The Invasion example is a jupyter notebook calculates the fixation
probabilities and stationary distribution of a Normal Form Game with 5 strategies and then plots an invasion diagram.

The Plot 2 Simplex is a jupyter notebook that shows how to use EGTtools to plot the
evolutionary dynamics in a 2 Simplex (a triangle), both for infinite and finite populations.

You can also check all these notebooks and a bit more on
this tutorial repository

For example, assuming the following payoff matrix:

A=\begin{pmatrix} -0.5 & 2 \\ 0 & 0 \end{pmatrix}

You can plot the gradient of selection in a finite population of (Z=100) individuals and assuming and intensity of
selection \beta=1 in the following way:

  1. import numpy as np
  2. from egttools.analytical import PairwiseComparison
  3. from egttools.games import Matrix2PlayerGameHolder
  4. beta = 1;
  5. Z = 100;
  6. nb_strategies = 2;
  7. A = np.array([[-0.5, 2.], [0., 0.]])
  8. pop_states = np.arange(0, Z + 1, 1)
  9. game = Matrix2PlayerGameHolder(nb_strategies, payoff_matrix=A)
  10. # Instantiate evolver and calculate gradient
  11. evolver = PairwiseComparison(population_size=Z, game=game)
  12. gradients = np.array([evolver.calculate_gradient_of_selection(beta, np.array([x, Z - x])) for x in range(Z + 1)])

Afterwards, you can plot the results with:

  1. from egttools.plotting import plot_gradients
  2. plot_gradients(gradients, figsize=(4, 4), fig_title="Hawk-Dove game stochastic dynamics",
  3. marker_facecolor='white',
  4. xlabel="frequency of hawks (k/Z)", marker="o", marker_size=20, marker_plot_freq=2)

Gradient of selection

And you can plot the stationary distribution for a mutation
rate \mu=1eˆ{-3} with:

  1. import matplotlib.pyplot as plt
  2. from egttools.utils import calculate_stationary_distribution
  3. transitions = evolver.calculate_transition_matrix(beta, mu=1e-3)
  4. stationary_with_mu = calculate_stationary_distribution(transitions.transpose())
  5. fig, ax = plt.subplots(figsize=(5, 4))
  6. fig.patch.set_facecolor('white')
  7. lines = ax.plot(np.arange(0, Z + 1) / Z, stationary_with_mu)
  8. plt.setp(lines, linewidth=2.0)
  9. ax.set_ylabel('stationary distribution', size=16)
  10. ax.set_xlabel('$k/Z$', size=16)
  11. ax.set_xlim(0, 1)
  12. plt.show()

Stationary distribution

We can obtain the same results through numerical simulations. The error will depend on how many independent simulations
you perform and for how long you let the simulation run. While a future implementation will offer an adaptive method to
vary these parameters depending on the variations between the estimated distributions, for the moment it is important
that you let the simulation run for enough generations after it has achieved a steady state. Here is a comparison
between analytical and numerical results:

  1. from egttools.numerical import PairwiseComparisonNumerical
  2. from egttools.games import NormalFormGame
  3. # Instantiate the game
  4. game = NormalFormGame(1, A)
  5. numerical_evolver = PairwiseComparisonNumerical(Z, game, 1000000)
  6. # We do this for different betas
  7. betas = np.logspace(-4, 1, 50)
  8. stationary_points = []
  9. # numerical simulations
  10. for i in range(len(betas)):
  11. stationary_points.append(numerical_evolver.stationary_distribution(30, int(1e6), int(1e3),
  12. betas[i], 1e-3))
  13. stationary_points = np.asarray(stationary_points)
  14. # Now we estimate the probability of Cooperation for each possible state
  15. state_frequencies = np.arange(0, Z + 1) / Z
  16. coop_level = np.dot(state_frequencies, stationary_points.T)

Lastly, we plot the results:

  1. from sklearn.metrics import mean_squared_error
  2. mse = mean_squared_error(1 - coop_level_analytical, coop_level)
  3. # Finally, we plot and compare visually (and check how much error we get)
  4. fig, ax = plt.subplots(figsize=(7, 5))
  5. # ax.scatter(betas, coop_level, label="simulation")
  6. ax.scatter(betas, coop_level_analytical, marker='x', label="analytical")
  7. ax.scatter(betas, coop_level, marker='o', label="simulation")
  8. ax.text(0.01, 0.535, 'MSE = {0:.3e}'.format(mse), style='italic',
  9. bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
  10. ax.legend()
  11. ax.set_xlabel(r'$\beta$', fontsize=15)
  12. ax.set_ylabel('Cooperation level', fontsize=15)
  13. ax.set_xscale('log')
  14. plt.show()

Comparison numerical analytical

Finally, you may also visualize the result of independent simulations:

  1. init_states = np.random.randint(0, Z + 1, size=10, dtype=np.uint64)
  2. output = []
  3. for i in range(10):
  4. output.append(evolver.run(int(1e6), 1, 1e-3,
  5. [init_states[i], Z - init_states[i]]))
  6. # Plot each year's time series in its own facet
  7. fig, ax = plt.subplots(figsize=(5, 4))
  8. for run in output:
  9. ax.plot(run[:, 0] / Z, color='gray', linewidth=.1, alpha=0.6)
  10. ax.set_ylabel('k/Z')
  11. ax.set_xlabel('generation')
  12. ax.set_xscale('log')

Comparison numerical analytical

Plotting the dynamics in a 2 Simplex

EGTtools can also be used to visualize the evolutionary dynamics in a 2 Simplex. In the example bellow, we use the
egttools.plotting.plot_replicator_dynamics_in_simplex which calculates the gradients on a simplex given an initial
payoff matrix and returns a egttools.plotting.Simplex2D object which can be used to plot the 2 Simplex.

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from egttools.plotting import plot_replicator_dynamics_in_simplex
  4. payoffs = np.array([[1, 0, 0],
  5. [0, 2, 0],
  6. [0, 0, 3]])
  7. type_labels = ['A', 'B', 'C']
  8. fig, ax = plt.subplots(figsize=(10, 8))
  9. simplex, gradient_function, roots, roots_xy, stability = plot_replicator_dynamics_in_simplex(payoffs, ax=ax)
  10. plot = (simplex.add_axis(ax=ax)
  11. .draw_triangle()
  12. .draw_gradients(zorder=0)
  13. .add_colorbar()
  14. .add_vertex_labels(type_labels)
  15. .draw_stationary_points(roots_xy, stability)
  16. .draw_trajectory_from_roots(gradient_function,
  17. roots,
  18. stability,
  19. trajectory_length=15,
  20. linewidth=1,
  21. step=0.01,
  22. color='k', draw_arrow=True,
  23. arrowdirection='right',
  24. arrowsize=30, zorder=4, arrowstyle='fancy')
  25. .draw_scatter_shadow(gradient_function, 300, color='gray', marker='.', s=0.1, zorder=0)
  26. )
  27. ax.axis('off')
  28. ax.set_aspect('equal')
  29. plt.xlim((-.05, 1.05))
  30. plt.ylim((-.02, simplex.top_corner + 0.05))
  31. plt.show()

2 Simplex dynamics in infinite populations

The same can be done for finite populations, with the added possibility to plot the stationary distribution inside the
triangle (see simplex plotting
and simplified simplex plotting
for a more in depth examples).

Documentation

The analytical module contains classes and functions that you may use to
investigate the evolutionary dynamics in N-player games. For now only the replicator dynamics (for infinite populations)
and the Pairwise Comparison imitation process (for finite populations) are implemented.

When your state-space is too big (in finite populations), it might become computationally hard to solve the system
analytically. Thus, we provide an efficient numerical module written in C++ and compiled to
Python. You may use it to estimate the fixation probabilities and stationary distribution through Monte-Carlo
simulations, or perform individual runs of the Moran process.

You can find more information in the ReadTheDocs documentation.

Caveats

  1. On Apple M1 (arm64) you should install (for the moment) miniforge, create
    a conda environment using it, and install EGTtools from the conda environment.

  2. In MacOSX it is assumed that you have Homebrew installed.

  3. You should install libomp with homebrew brew install libomp if you want to have support for parallel operations (
    there is a big difference in computation time).

  4. You must have Eigen 3.3.* installed.

  5. You do not need any of the above if you install EGTtools through pip install egttools --no-deps. However,
    on Apple M1 (arm64) you still need to install the dependencies through miniforge, since only there you can find a
    scipy wheel that supports this architecture.

Citing

If you use EGTtools in your publications, please cite it in the following way with bibtex:

@article{Fernandez2023, author = {Fernández Domingos, Elias and Santos, Francisco C. and Lenaerts, Tom}, title = {EGTtools: Evolutionary game dynamics in Python}, journal = {iScience}, volume = {26}, number = {4}, pages = {106419}, year = {2023}, issn = {2589-0042}, doi = {https://doi.org/10.1016/j.isci.2023.106419} }

Or in text format:

  1. Fernández Domingos, E., Santos, F. C. & Lenaerts, T. EGTtools: Evolutionary game dynamics in Python. iScience 26, 106419 (2023).

And to cite the current version of EGTtools you can use:

@misc{Fernandez2020, author = {Fernández Domingos, Elias}, title = {EGTTools: Toolbox for Evolutionary Game Theory (0.1.12)}, year = {2022}, month = {Dec}, journal = {Zenodo}, doi = {10.5281/zenodo.7458631} }

Moreover, you may find our article at here00496-0.pdf).

Licence

Acknowledgements

  • Great parts of this project have been possible thanks to the help of
    Yannick Jadoul author of
    Parselmouth
    and Eugenio Bargiacchi author of AIToolBox.
    They are both great programmers and scientists, so it is always a good idea to check out their work.
  • EGTtools makes use of the amazing pybind11. library to provide a Python
    interface for optimized monte-carlo simulations written in C++.