jlgametheory 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025, QuantEcon
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,110 @@
1
+ Metadata-Version: 2.4
2
+ Name: jlgametheory
3
+ Version: 0.1.0
4
+ Summary: Python interface to GameTheory.jl
5
+ Author: QuantEcon
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: License :: OSI Approved :: BSD License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Topic :: Scientific/Engineering
17
+ License-File: LICENSE
18
+ Requires-Dist: numpy>=1.22
19
+ Requires-Dist: quantecon>=0.8
20
+ Requires-Dist: juliacall>=0.9.20
21
+ Requires-Dist: sphinx>=8.0 ; extra == "docs"
22
+ Requires-Dist: sphinx-copybutton ; extra == "docs"
23
+ Requires-Dist: pytest>=7 ; extra == "test"
24
+ Requires-Dist: pytest-cov>=4 ; extra == "test"
25
+ Project-URL: Homepage, https://github.com/QuantEcon/jlgametheory
26
+ Project-URL: Repository, https://github.com/QuantEcon/jlgametheory
27
+ Provides-Extra: docs
28
+ Provides-Extra: test
29
+
30
+ # jlgametheory
31
+
32
+ [![Build Status](https://github.com/QuantEcon/jlgametheory/actions/workflows/ci.yml/badge.svg)](https://github.com/QuantEcon/jlgametheory/actions/workflows/ci.yml)
33
+ [![Coverage Status](https://coveralls.io/repos/QuantEcon/jlgametheory/badge.svg)](https://coveralls.io/github/QuantEcon/jlgametheory)
34
+ [![Documentation (latest)](https://img.shields.io/badge/docs-latest-blue.svg)](https://quantecon.github.io/jlgametheory/latest/)
35
+
36
+ Python interface to GameTheory.jl
37
+
38
+ `jlgametheory` is a Python package that allows passing
39
+ a `NormalFormGame` instance from
40
+ [`QuantEcon.py`](https://github.com/QuantEcon/QuantEcon.py) to
41
+ [`GameTheory.jl`](https://github.com/QuantEcon/GameTheory.jl) functions
42
+ via [`JuliaCall`](https://github.com/JuliaPy/PythonCall.jl).
43
+
44
+ ## Implemented functions
45
+
46
+ * [`lrsnash`](https://quantecon.github.io/jlgametheory/latest/_autosummary/jlgametheory.lrsnash.html):
47
+ Compute in exact arithmetic all extreme mixed-action Nash equilibria of a 2-player normal form game with integer payoffs.
48
+ * [`hc_solve`](https://quantecon.github.io/jlgametheory/latest/_autosummary/jlgametheory.hc_solve.html):
49
+ Compute all isolated mixed-action Nash equilibria of an N-player normal form game.
50
+
51
+ ## Example usage
52
+
53
+ ```python
54
+ import quantecon.game_theory as gt
55
+ import jlgametheory as jgt
56
+ ```
57
+
58
+ ### lrsnash
59
+
60
+ `lrsnash` calls the Nash equilibrium computation routine in [lrslib](http://cgm.cs.mcgill.ca/~avis/C/lrs.html)
61
+ (through its Julia wrapper [LRSLib.jl](https://github.com/JuliaPolyhedra/LRSLib.jl)):
62
+
63
+ ```python
64
+ bimatrix = [[(3, 3), (3, 2)],
65
+ [(2, 2), (5, 6)],
66
+ [(0, 3), (6, 1)]]
67
+ g = gt.NormalFormGame(bimatrix)
68
+ jgt.lrsnash(g)
69
+ ```
70
+
71
+ ```
72
+ [(array([Fraction(4, 5), Fraction(1, 5), Fraction(0, 1)], dtype=object),
73
+ array([Fraction(2, 3), Fraction(1, 3)], dtype=object)),
74
+ (array([Fraction(0, 1), Fraction(1, 3), Fraction(2, 3)], dtype=object),
75
+ array([Fraction(1, 3), Fraction(2, 3)], dtype=object)),
76
+ (array([Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object),
77
+ array([Fraction(1, 1), Fraction(0, 1)], dtype=object))]
78
+ ```
79
+
80
+ ### hc_solve
81
+
82
+ `hc_solve` computes all isolated Nash equilibria of an N-player game by using
83
+ [HomotopyContinuation.jl](https://github.com/JuliaHomotopyContinuation/HomotopyContinuation.jl):
84
+
85
+ ```python
86
+ g = gt.NormalFormGame((2, 2, 2))
87
+ g[0, 0, 0] = 9, 8, 12
88
+ g[1, 1, 0] = 9, 8, 2
89
+ g[0, 1, 1] = 3, 4, 6
90
+ g[1, 0, 1] = 3, 4, 4
91
+ jgt.hc_solve(g)
92
+ ```
93
+
94
+ ```
95
+ [(array([0., 1.]), array([0., 1.]), array([1., 0.])),
96
+ (array([0.5, 0.5]), array([0.5, 0.5]), array([1.000e+00, 2.351e-38])),
97
+ (array([1., 0.]), array([0., 1.]), array([-1.881e-37, 1.000e+00])),
98
+ (array([0.25, 0.75]), array([0.5, 0.5]), array([0.333, 0.667])),
99
+ (array([0.25, 0.75]), array([1.000e+00, 1.345e-43]), array([0.25, 0.75])),
100
+ (array([0., 1.]), array([0.333, 0.667]), array([0.333, 0.667])),
101
+ (array([1., 0.]), array([ 1.00e+00, -5.74e-42]), array([1., 0.])),
102
+ (array([0., 1.]), array([1., 0.]), array([2.374e-66, 1.000e+00])),
103
+ (array([0.5, 0.5]), array([0.333, 0.667]), array([0.25, 0.75]))]
104
+ ```
105
+
106
+ ## Tutorials
107
+
108
+ * [Tools for Game Theory in QuantEcon.py](https://nbviewer.jupyter.org/github/QuantEcon/game-theory-notebooks/blob/main/game_theory_py.ipynb)
109
+ * [Tools for Game Theory in GameTheory.jl](https://nbviewer.jupyter.org/github/QuantEcon/game-theory-notebooks/blob/main/game_theory_jl.ipynb)
110
+
@@ -0,0 +1,80 @@
1
+ # jlgametheory
2
+
3
+ [![Build Status](https://github.com/QuantEcon/jlgametheory/actions/workflows/ci.yml/badge.svg)](https://github.com/QuantEcon/jlgametheory/actions/workflows/ci.yml)
4
+ [![Coverage Status](https://coveralls.io/repos/QuantEcon/jlgametheory/badge.svg)](https://coveralls.io/github/QuantEcon/jlgametheory)
5
+ [![Documentation (latest)](https://img.shields.io/badge/docs-latest-blue.svg)](https://quantecon.github.io/jlgametheory/latest/)
6
+
7
+ Python interface to GameTheory.jl
8
+
9
+ `jlgametheory` is a Python package that allows passing
10
+ a `NormalFormGame` instance from
11
+ [`QuantEcon.py`](https://github.com/QuantEcon/QuantEcon.py) to
12
+ [`GameTheory.jl`](https://github.com/QuantEcon/GameTheory.jl) functions
13
+ via [`JuliaCall`](https://github.com/JuliaPy/PythonCall.jl).
14
+
15
+ ## Implemented functions
16
+
17
+ * [`lrsnash`](https://quantecon.github.io/jlgametheory/latest/_autosummary/jlgametheory.lrsnash.html):
18
+ Compute in exact arithmetic all extreme mixed-action Nash equilibria of a 2-player normal form game with integer payoffs.
19
+ * [`hc_solve`](https://quantecon.github.io/jlgametheory/latest/_autosummary/jlgametheory.hc_solve.html):
20
+ Compute all isolated mixed-action Nash equilibria of an N-player normal form game.
21
+
22
+ ## Example usage
23
+
24
+ ```python
25
+ import quantecon.game_theory as gt
26
+ import jlgametheory as jgt
27
+ ```
28
+
29
+ ### lrsnash
30
+
31
+ `lrsnash` calls the Nash equilibrium computation routine in [lrslib](http://cgm.cs.mcgill.ca/~avis/C/lrs.html)
32
+ (through its Julia wrapper [LRSLib.jl](https://github.com/JuliaPolyhedra/LRSLib.jl)):
33
+
34
+ ```python
35
+ bimatrix = [[(3, 3), (3, 2)],
36
+ [(2, 2), (5, 6)],
37
+ [(0, 3), (6, 1)]]
38
+ g = gt.NormalFormGame(bimatrix)
39
+ jgt.lrsnash(g)
40
+ ```
41
+
42
+ ```
43
+ [(array([Fraction(4, 5), Fraction(1, 5), Fraction(0, 1)], dtype=object),
44
+ array([Fraction(2, 3), Fraction(1, 3)], dtype=object)),
45
+ (array([Fraction(0, 1), Fraction(1, 3), Fraction(2, 3)], dtype=object),
46
+ array([Fraction(1, 3), Fraction(2, 3)], dtype=object)),
47
+ (array([Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object),
48
+ array([Fraction(1, 1), Fraction(0, 1)], dtype=object))]
49
+ ```
50
+
51
+ ### hc_solve
52
+
53
+ `hc_solve` computes all isolated Nash equilibria of an N-player game by using
54
+ [HomotopyContinuation.jl](https://github.com/JuliaHomotopyContinuation/HomotopyContinuation.jl):
55
+
56
+ ```python
57
+ g = gt.NormalFormGame((2, 2, 2))
58
+ g[0, 0, 0] = 9, 8, 12
59
+ g[1, 1, 0] = 9, 8, 2
60
+ g[0, 1, 1] = 3, 4, 6
61
+ g[1, 0, 1] = 3, 4, 4
62
+ jgt.hc_solve(g)
63
+ ```
64
+
65
+ ```
66
+ [(array([0., 1.]), array([0., 1.]), array([1., 0.])),
67
+ (array([0.5, 0.5]), array([0.5, 0.5]), array([1.000e+00, 2.351e-38])),
68
+ (array([1., 0.]), array([0., 1.]), array([-1.881e-37, 1.000e+00])),
69
+ (array([0.25, 0.75]), array([0.5, 0.5]), array([0.333, 0.667])),
70
+ (array([0.25, 0.75]), array([1.000e+00, 1.345e-43]), array([0.25, 0.75])),
71
+ (array([0., 1.]), array([0.333, 0.667]), array([0.333, 0.667])),
72
+ (array([1., 0.]), array([ 1.00e+00, -5.74e-42]), array([1., 0.])),
73
+ (array([0., 1.]), array([1., 0.]), array([2.374e-66, 1.000e+00])),
74
+ (array([0.5, 0.5]), array([0.333, 0.667]), array([0.25, 0.75]))]
75
+ ```
76
+
77
+ ## Tutorials
78
+
79
+ * [Tools for Game Theory in QuantEcon.py](https://nbviewer.jupyter.org/github/QuantEcon/game-theory-notebooks/blob/main/game_theory_py.ipynb)
80
+ * [Tools for Game Theory in GameTheory.jl](https://nbviewer.jupyter.org/github/QuantEcon/game-theory-notebooks/blob/main/game_theory_jl.ipynb)
@@ -0,0 +1,22 @@
1
+ """
2
+ jlgametheory: Python interface to GameTheory.jl
3
+
4
+ """
5
+ from juliacall import Main as jl
6
+ from importlib.metadata import PackageNotFoundError, version as _version
7
+
8
+ jl.seval("using GameTheory")
9
+ GameTheory = jl.GameTheory
10
+
11
+ from .jlgametheory import (
12
+ lrsnash, hc_solve
13
+ )
14
+
15
+ try:
16
+ __version__ = _version("jlgametheory")
17
+ except PackageNotFoundError: # pragma: no cover
18
+ __version__ = "0+unknown"
19
+
20
+ __all__ = [
21
+ "lrsnash", "hc_solve"
22
+ ]
@@ -0,0 +1,163 @@
1
+ import numpy as np
2
+ from . import GameTheory
3
+
4
+
5
+ def to_jl_nfg(g):
6
+ g_jl = GameTheory.NormalFormGame(
7
+ *(GameTheory.Player(player.payoff_array) for player in g.players)
8
+ )
9
+ return g_jl
10
+
11
+
12
+ def _to_py_ne(NE_jl):
13
+ return tuple(x.to_numpy() for x in NE_jl)
14
+
15
+
16
+ def _to_py_nes(NEs_jl):
17
+ return [_to_py_ne(NE) for NE in NEs_jl]
18
+
19
+
20
+ def lrsnash(g):
21
+ """
22
+ Compute in exact arithmetic all extreme mixed-action Nash equilibria
23
+ of a 2-player normal form game with integer payoffs. This function
24
+ calls the Nash equilibrium computation routine in `lrslib` (through
25
+ its Julia wrapper `LRSLib.jl`) which is based on the "lexicographic
26
+ reverse search" vertex enumeration algorithm [1]_.
27
+
28
+ Parameters
29
+ ----------
30
+ g : NormalFormGame
31
+ 2-player NormalFormGame instance with integer payoffs.
32
+
33
+ Returns
34
+ -------
35
+ NEs : list(tuple(ndarray(object, ndim=1)))
36
+ List containing tuples of Nash equilibrium mixed actions, where
37
+ the values are represented by `fractions.Fraction`.
38
+
39
+ Examples
40
+ --------
41
+ A degenerate game example:
42
+
43
+ >>> import quantecon.game_theory as gt
44
+ >>> import jlgametheory as jgt
45
+ >>> from pprint import pprint
46
+ >>> bimatrix = [[(3, 3), (3, 3)],
47
+ ... [(2, 2), (5, 6)],
48
+ ... [(0, 3), (6, 1)]]
49
+ >>> g = gt.NormalFormGame(bimatrix)
50
+ >>> NEs = jgt.lrsnash(g)
51
+ >>> pprint(NEs)
52
+ [(array([Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object),
53
+ array([Fraction(1, 1), Fraction(0, 1)], dtype=object)),
54
+ (array([Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object),
55
+ array([Fraction(2, 3), Fraction(1, 3)], dtype=object)),
56
+ (array([Fraction(0, 1), Fraction(1, 3), Fraction(2, 3)], dtype=object),
57
+ array([Fraction(1, 3), Fraction(2, 3)], dtype=object))]
58
+
59
+ The set of Nash equilibria of this degenerate game consists of an
60
+ isolated equilibrium, the third output, and a non-singleton
61
+ equilibrium component, the extreme points of which are given by the
62
+ first two outputs.
63
+
64
+ References
65
+ ----------
66
+ .. [1] D. Avis, G. Rosenberg, R. Savani, and B. von Stengel,
67
+ "Enumeration of Nash Equilibria for Two-Player Games," Economic
68
+ Theory (2010), 9-37.
69
+
70
+ """
71
+ try:
72
+ N = g.N
73
+ except AttributeError:
74
+ raise TypeError('input must be a 2-player NormalFormGame')
75
+ if N != 2:
76
+ raise NotImplementedError('Implemented only for 2-player games')
77
+ if not np.issubdtype(g.dtype, np.integer):
78
+ raise NotImplementedError(
79
+ 'Implemented only for games with integer payoffs'
80
+ )
81
+
82
+ NEs_jl = GameTheory.lrsnash(to_jl_nfg(g))
83
+ return _to_py_nes(NEs_jl)
84
+
85
+
86
+ def hc_solve(g, ntofind=float('inf'), **options):
87
+ """
88
+ Compute all isolated mixed-action Nash equilibria of an N-player
89
+ normal form game.
90
+
91
+ This function solves a system of polynomial equations arising from
92
+ the nonlinear complementarity problem representation of Nash
93
+ equilibrium, by using `HomotopyContinuation.jl`.
94
+
95
+ Parameters
96
+ ----------
97
+ g : NormalFormGame
98
+ N-player NormalFormGame instance.
99
+
100
+ ntofind : scalar, optional(default=float('inf'))
101
+ Number of Nash equilibria to find.
102
+
103
+ options :
104
+ Optional keyword arguments to pass to `HomotopyContinuation.solve`.
105
+ For example, the option `seed` can set the random seed used
106
+ during the computations. See the `documentation
107
+ <https://www.juliahomotopycontinuation.org/HomotopyContinuation.jl/stable/solve/>`_
108
+ for `HomotopyContinuation.solve` for details.
109
+
110
+ Returns
111
+ -------
112
+ NEs : list(tuple(ndarray(float, ndim=1)))
113
+ List containing tuples of Nash equilibrium mixed actions.
114
+
115
+ Examples
116
+ --------
117
+ Consider the 3-player 2-action game with 9 Nash equilibria in
118
+ McKelvey and McLennan (1996) "Computation of Equilibria in Finite
119
+ Games":
120
+
121
+ >>> import quantecon.game_theory as gt
122
+ >>> import jlgametheory as jgt
123
+ >>> from pprint import pprint
124
+ >>> import numpy as np
125
+ >>> np.set_printoptions(precision=3) # Reduce the digits printed
126
+ >>> g = gt.NormalFormGame((2, 2, 2))
127
+ >>> g[0, 0, 0] = 9, 8, 12
128
+ >>> g[1, 1, 0] = 9, 8, 2
129
+ >>> g[0, 1, 1] = 3, 4, 6
130
+ >>> g[1, 0, 1] = 3, 4, 4
131
+ >>> print(g)
132
+ 3-player NormalFormGame with payoff profile array:
133
+ [[[[ 9., 8., 12.], [ 0., 0., 0.]],
134
+ [[ 0., 0., 0.], [ 3., 4., 6.]]],
135
+ <BLANKLINE>
136
+ [[[ 0., 0., 0.], [ 3., 4., 4.]],
137
+ [[ 9., 8., 2.], [ 0., 0., 0.]]]]
138
+ >>> NEs = jgt.hc_solve(g, show_progress=False)
139
+ >>> len(NEs)
140
+ 9
141
+ >>> pprint(NEs)
142
+ [(array([0., 1.]), array([0., 1.]), array([1., 0.])),
143
+ (array([0.5, 0.5]), array([0.5, 0.5]), array([1.000e+00, 2.351e-38])),
144
+ (array([1., 0.]), array([0., 1.]), array([-1.881e-37, 1.000e+00])),
145
+ (array([0.25, 0.75]), array([0.5, 0.5]), array([0.333, 0.667])),
146
+ (array([0.25, 0.75]), array([1.000e+00, 1.345e-43]), array([0.25, 0.75])),
147
+ (array([0., 1.]), array([0.333, 0.667]), array([0.333, 0.667])),
148
+ (array([1., 0.]), array([ 1.00e+00, -5.74e-42]), array([1., 0.])),
149
+ (array([0., 1.]), array([1., 0.]), array([2.374e-66, 1.000e+00])),
150
+ (array([0.5, 0.5]), array([0.333, 0.667]), array([0.25, 0.75]))]
151
+ >>> all(g.is_nash(NE) for NE in NEs)
152
+ True
153
+
154
+ """
155
+ try:
156
+ N = g.N
157
+ except AttributeError:
158
+ raise TypeError('g must be a NormalFormGame')
159
+ if N < 2:
160
+ raise NotImplementedError('Not implemented for 1-player games')
161
+
162
+ NEs_jl = GameTheory.hc_solve(to_jl_nfg(g), ntofind=ntofind, **options)
163
+ return _to_py_nes(NEs_jl)
@@ -0,0 +1,9 @@
1
+ {
2
+ "julia": "^1.6",
3
+ "packages": {
4
+ "GameTheory": {
5
+ "uuid": "64a4ffa8-f47c-4a47-8dad-aee7aadc3b51",
6
+ "version": "0.3.6"
7
+ }
8
+ }
9
+ }
@@ -0,0 +1,46 @@
1
+ [build-system]
2
+ requires = ["flit_core >=3.2,<4"]
3
+ build-backend = "flit_core.buildapi"
4
+
5
+ [project]
6
+ name = "jlgametheory"
7
+ version = "0.1.0"
8
+ description = "Python interface to GameTheory.jl"
9
+ readme = "README.md"
10
+ license = {file = "LICENSE"}
11
+ authors = [
12
+ {name = "QuantEcon"}
13
+ ]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Science/Research",
17
+ "License :: OSI Approved :: BSD License",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Topic :: Scientific/Engineering",
24
+ ]
25
+ requires-python = ">=3.10"
26
+ dependencies = [
27
+ "numpy>=1.22",
28
+ "quantecon>=0.8",
29
+ "juliacall>=0.9.20",
30
+ ]
31
+
32
+ [project.optional-dependencies]
33
+ # pip install jlgametheory[test]
34
+ test = ["pytest>=7", "pytest-cov>=4"]
35
+ # pip install jlgametheory[docs]
36
+ docs = [
37
+ "sphinx>=8.0",
38
+ "sphinx-copybutton",
39
+ ]
40
+
41
+ [project.urls]
42
+ Homepage = "https://github.com/QuantEcon/jlgametheory"
43
+ Repository = "https://github.com/QuantEcon/jlgametheory"
44
+
45
+ [tool.coverage.run]
46
+ omit = ["tests/test_*.py"]