Nash equilibrium in Python

11.1k views Asked by At

Is there a Python library out there that solves for the Nash equilibrium of two-person zero-games? I know the solution can be written down in terms of linear constraints and, in theory, scipy should be able to optimize it. However, for two-person zero-games the solution is exact and unique, but some of the solvers fail to converge for certain problems.

Rather than listing any of the libraries on Linear programing on the Python website, I would like to know what library would be most effective in terms of ease of use and speed.

3

There are 3 answers

3
Rafe Kettler On BEST ANSWER

Raymond Hettinger wrote a recipe for solving zero-sum payoff matrices. It should serve your purposes alright.

As for a more general library for solving game theory, there's nothing specifically designed for that. But, like you said, scipy can tackle optimization problems like this. You might be able to do something with GarlicSim, which claims to be for "any kind of simulation: Physics, game theory..." but I've never used it before so I can't recommend it.

0
theresawalrus On

There is Gambit, which is a little difficult to set up, but has a python API.

1
user51902 On

I've just started putting together some game theory python code: http://drvinceknight.github.com/Gamepy/

There's code which:

  • solves matching games,
  • calculates shapley values in cooperative games,
  • runs agent based simulations to identify emergent behaviour in normal form games,
  • (clumsily - my python foo is still growing) uses the lrs library (written in C: http://cgm.cs.mcgill.ca/~avis/C/lrs.html) to calculate the solutions to normal form games (this is I believe what you want).

The code is all available on github and that site (the first link at the beginning of this answer) explains how the code works and gives user examples.

You might also want to check out 'Gambit' which I've never used.