passagemath-polyhedra 10.6.31rc3__cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
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.
Potentially problematic release.
This version of passagemath-polyhedra might be problematic. Click here for more details.
- passagemath_polyhedra-10.6.31rc3.dist-info/METADATA +367 -0
- passagemath_polyhedra-10.6.31rc3.dist-info/METADATA.bak +369 -0
- passagemath_polyhedra-10.6.31rc3.dist-info/RECORD +206 -0
- passagemath_polyhedra-10.6.31rc3.dist-info/WHEEL +6 -0
- passagemath_polyhedra-10.6.31rc3.dist-info/top_level.txt +2 -0
- passagemath_polyhedra.libs/libgmp-6e109695.so.10.5.0 +0 -0
- passagemath_polyhedra.libs/libgomp-e985bcbb.so.1.0.0 +0 -0
- sage/all__sagemath_polyhedra.py +50 -0
- sage/game_theory/all.py +8 -0
- sage/game_theory/catalog.py +6 -0
- sage/game_theory/catalog_normal_form_games.py +923 -0
- sage/game_theory/cooperative_game.py +844 -0
- sage/game_theory/matching_game.py +1181 -0
- sage/game_theory/normal_form_game.py +2697 -0
- sage/game_theory/parser.py +275 -0
- sage/geometry/all__sagemath_polyhedra.py +22 -0
- sage/geometry/cone.py +6940 -0
- sage/geometry/cone_catalog.py +847 -0
- sage/geometry/cone_critical_angles.py +1027 -0
- sage/geometry/convex_set.py +1119 -0
- sage/geometry/fan.py +3743 -0
- sage/geometry/fan_isomorphism.py +389 -0
- sage/geometry/fan_morphism.py +1884 -0
- sage/geometry/hasse_diagram.py +202 -0
- sage/geometry/hyperplane_arrangement/affine_subspace.py +390 -0
- sage/geometry/hyperplane_arrangement/all.py +1 -0
- sage/geometry/hyperplane_arrangement/arrangement.py +3895 -0
- sage/geometry/hyperplane_arrangement/check_freeness.py +145 -0
- sage/geometry/hyperplane_arrangement/hyperplane.py +773 -0
- sage/geometry/hyperplane_arrangement/library.py +825 -0
- sage/geometry/hyperplane_arrangement/ordered_arrangement.py +642 -0
- sage/geometry/hyperplane_arrangement/plot.py +520 -0
- sage/geometry/integral_points.py +35 -0
- sage/geometry/integral_points_generic_dense.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/integral_points_generic_dense.pyx +7 -0
- sage/geometry/lattice_polytope.py +5894 -0
- sage/geometry/linear_expression.py +773 -0
- sage/geometry/newton_polygon.py +767 -0
- sage/geometry/point_collection.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/point_collection.pyx +1008 -0
- sage/geometry/polyhedral_complex.py +2616 -0
- sage/geometry/polyhedron/all.py +8 -0
- sage/geometry/polyhedron/backend_cdd.py +460 -0
- sage/geometry/polyhedron/backend_cdd_rdf.py +231 -0
- sage/geometry/polyhedron/backend_field.py +347 -0
- sage/geometry/polyhedron/backend_normaliz.py +2503 -0
- sage/geometry/polyhedron/backend_number_field.py +168 -0
- sage/geometry/polyhedron/backend_polymake.py +765 -0
- sage/geometry/polyhedron/backend_ppl.py +582 -0
- sage/geometry/polyhedron/base.py +1206 -0
- sage/geometry/polyhedron/base0.py +1444 -0
- sage/geometry/polyhedron/base1.py +886 -0
- sage/geometry/polyhedron/base2.py +812 -0
- sage/geometry/polyhedron/base3.py +1845 -0
- sage/geometry/polyhedron/base4.py +1262 -0
- sage/geometry/polyhedron/base5.py +2700 -0
- sage/geometry/polyhedron/base6.py +1741 -0
- sage/geometry/polyhedron/base7.py +997 -0
- sage/geometry/polyhedron/base_QQ.py +1258 -0
- sage/geometry/polyhedron/base_RDF.py +98 -0
- sage/geometry/polyhedron/base_ZZ.py +934 -0
- sage/geometry/polyhedron/base_mutable.py +215 -0
- sage/geometry/polyhedron/base_number_field.py +122 -0
- sage/geometry/polyhedron/cdd_file_format.py +155 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/all.py +1 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/base.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/base.pxd +76 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/base.pyx +3859 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/combinatorial_face.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/combinatorial_face.pxd +39 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/combinatorial_face.pyx +1038 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/conversions.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/conversions.pxd +9 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/conversions.pyx +501 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_data_structure.pxd +207 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_iterator.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_iterator.pxd +102 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_iterator.pyx +2274 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_list_data_structure.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_list_data_structure.pxd +370 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/face_list_data_structure.pyx +84 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/list_of_faces.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/list_of_faces.pxd +31 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/list_of_faces.pyx +587 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/polyhedron_face_lattice.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/polyhedron_face_lattice.pxd +52 -0
- sage/geometry/polyhedron/combinatorial_polyhedron/polyhedron_face_lattice.pyx +560 -0
- sage/geometry/polyhedron/constructor.py +773 -0
- sage/geometry/polyhedron/double_description.py +753 -0
- sage/geometry/polyhedron/double_description_inhomogeneous.py +564 -0
- sage/geometry/polyhedron/face.py +1060 -0
- sage/geometry/polyhedron/generating_function.py +1810 -0
- sage/geometry/polyhedron/lattice_euclidean_group_element.py +178 -0
- sage/geometry/polyhedron/library.py +3502 -0
- sage/geometry/polyhedron/misc.py +121 -0
- sage/geometry/polyhedron/modules/all.py +1 -0
- sage/geometry/polyhedron/modules/formal_polyhedra_module.py +155 -0
- sage/geometry/polyhedron/palp_database.py +447 -0
- sage/geometry/polyhedron/parent.py +1279 -0
- sage/geometry/polyhedron/plot.py +1986 -0
- sage/geometry/polyhedron/ppl_lattice_polygon.py +556 -0
- sage/geometry/polyhedron/ppl_lattice_polytope.py +1257 -0
- sage/geometry/polyhedron/representation.py +1723 -0
- sage/geometry/pseudolines.py +515 -0
- sage/geometry/relative_interior.py +445 -0
- sage/geometry/toric_plotter.py +1103 -0
- sage/geometry/triangulation/all.py +2 -0
- sage/geometry/triangulation/base.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/geometry/triangulation/base.pyx +963 -0
- sage/geometry/triangulation/data.h +147 -0
- sage/geometry/triangulation/data.pxd +4 -0
- sage/geometry/triangulation/element.py +914 -0
- sage/geometry/triangulation/functions.h +10 -0
- sage/geometry/triangulation/functions.pxd +4 -0
- sage/geometry/triangulation/point_configuration.py +2256 -0
- sage/geometry/triangulation/triangulations.h +49 -0
- sage/geometry/triangulation/triangulations.pxd +7 -0
- sage/geometry/voronoi_diagram.py +319 -0
- sage/interfaces/all__sagemath_polyhedra.py +1 -0
- sage/interfaces/polymake.py +2028 -0
- sage/numerical/all.py +13 -0
- sage/numerical/all__sagemath_polyhedra.py +11 -0
- sage/numerical/backends/all.py +1 -0
- sage/numerical/backends/all__sagemath_polyhedra.py +1 -0
- sage/numerical/backends/cvxopt_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/cvxopt_backend.pyx +1006 -0
- sage/numerical/backends/cvxopt_backend_test.py +19 -0
- sage/numerical/backends/cvxopt_sdp_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/cvxopt_sdp_backend.pyx +382 -0
- sage/numerical/backends/cvxpy_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/cvxpy_backend.pxd +41 -0
- sage/numerical/backends/cvxpy_backend.pyx +934 -0
- sage/numerical/backends/cvxpy_backend_test.py +13 -0
- sage/numerical/backends/generic_backend_test.py +24 -0
- sage/numerical/backends/interactivelp_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/interactivelp_backend.pxd +36 -0
- sage/numerical/backends/interactivelp_backend.pyx +1231 -0
- sage/numerical/backends/interactivelp_backend_test.py +12 -0
- sage/numerical/backends/logging_backend.py +391 -0
- sage/numerical/backends/matrix_sdp_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/matrix_sdp_backend.pxd +15 -0
- sage/numerical/backends/matrix_sdp_backend.pyx +478 -0
- sage/numerical/backends/ppl_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/ppl_backend.pyx +1126 -0
- sage/numerical/backends/ppl_backend_test.py +13 -0
- sage/numerical/backends/scip_backend.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/backends/scip_backend.pxd +22 -0
- sage/numerical/backends/scip_backend.pyx +1289 -0
- sage/numerical/backends/scip_backend_test.py +13 -0
- sage/numerical/interactive_simplex_method.py +5338 -0
- sage/numerical/knapsack.py +665 -0
- sage/numerical/linear_functions.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/linear_functions.pxd +31 -0
- sage/numerical/linear_functions.pyx +1648 -0
- sage/numerical/linear_tensor.py +470 -0
- sage/numerical/linear_tensor_constraints.py +448 -0
- sage/numerical/linear_tensor_element.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/linear_tensor_element.pxd +6 -0
- sage/numerical/linear_tensor_element.pyx +459 -0
- sage/numerical/mip.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/mip.pxd +40 -0
- sage/numerical/mip.pyx +3667 -0
- sage/numerical/sdp.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/numerical/sdp.pxd +39 -0
- sage/numerical/sdp.pyx +1433 -0
- sage/rings/all__sagemath_polyhedra.py +3 -0
- sage/rings/polynomial/all__sagemath_polyhedra.py +10 -0
- sage/rings/polynomial/omega.py +982 -0
- sage/schemes/all__sagemath_polyhedra.py +2 -0
- sage/schemes/toric/all.py +10 -0
- sage/schemes/toric/chow_group.py +1248 -0
- sage/schemes/toric/divisor.py +2082 -0
- sage/schemes/toric/divisor_class.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/schemes/toric/divisor_class.pyx +322 -0
- sage/schemes/toric/fano_variety.py +1606 -0
- sage/schemes/toric/homset.py +650 -0
- sage/schemes/toric/ideal.py +451 -0
- sage/schemes/toric/library.py +1322 -0
- sage/schemes/toric/morphism.py +1958 -0
- sage/schemes/toric/points.py +1032 -0
- sage/schemes/toric/sheaf/all.py +1 -0
- sage/schemes/toric/sheaf/constructor.py +302 -0
- sage/schemes/toric/sheaf/klyachko.py +921 -0
- sage/schemes/toric/toric_subscheme.py +905 -0
- sage/schemes/toric/variety.py +3460 -0
- sage/schemes/toric/weierstrass.py +1078 -0
- sage/schemes/toric/weierstrass_covering.py +457 -0
- sage/schemes/toric/weierstrass_higher.py +288 -0
- sage_wheels/share/reflexive_polytopes/Full2d/zzdb.info +10 -0
- sage_wheels/share/reflexive_polytopes/Full2d/zzdb.v03 +0 -0
- sage_wheels/share/reflexive_polytopes/Full2d/zzdb.v04 +0 -0
- sage_wheels/share/reflexive_polytopes/Full2d/zzdb.v05 +1 -0
- sage_wheels/share/reflexive_polytopes/Full2d/zzdb.v06 +1 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.info +22 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v04 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v05 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v06 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v07 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v08 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v09 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v10 +0 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v11 +1 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v12 +1 -0
- sage_wheels/share/reflexive_polytopes/Full3d/zzdb.v13 +1 -0
- sage_wheels/share/reflexive_polytopes/reflexive_polytopes_2d +80 -0
- sage_wheels/share/reflexive_polytopes/reflexive_polytopes_3d +37977 -0
|
@@ -0,0 +1,2697 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-polyhedra
|
|
2
|
+
r"""
|
|
3
|
+
Normal form games with N players.
|
|
4
|
+
|
|
5
|
+
This module implements a class for normal form games (strategic form games)
|
|
6
|
+
[NN2007]_. At present the following algorithms are implemented to
|
|
7
|
+
compute equilibria of these games:
|
|
8
|
+
|
|
9
|
+
* ``'enumeration'`` - An implementation of the support enumeration
|
|
10
|
+
algorithm built in Sage.
|
|
11
|
+
|
|
12
|
+
* ``'LCP'`` - An interface with the 'gambit' solver's implementation
|
|
13
|
+
of the Lemke-Howson algorithm.
|
|
14
|
+
|
|
15
|
+
* ``'lp'`` - A built-in Sage implementation (with a gambit alternative)
|
|
16
|
+
of a zero-sum game solver using linear programming. See
|
|
17
|
+
:class:`MixedIntegerLinearProgram` for more on MILP solvers in Sage.
|
|
18
|
+
|
|
19
|
+
* ``'lrs'`` - A solver interfacing with the 'lrslib' library.
|
|
20
|
+
|
|
21
|
+
The architecture for the class is based on the gambit architecture to
|
|
22
|
+
ensure an easy transition between gambit and Sage. At present the
|
|
23
|
+
algorithms for the computation of equilibria only solve 2 player games.
|
|
24
|
+
|
|
25
|
+
A very simple and well known example of normal form game is referred
|
|
26
|
+
to as the 'Battle of the Sexes' in which two players Amy and Bob
|
|
27
|
+
are modeled. Amy prefers to play video games and Bob prefers to
|
|
28
|
+
watch a movie. They both however want to spend their evening together.
|
|
29
|
+
This can be modeled using the following two matrices:
|
|
30
|
+
|
|
31
|
+
.. MATH::
|
|
32
|
+
|
|
33
|
+
A = \begin{pmatrix}
|
|
34
|
+
3&1\\
|
|
35
|
+
0&2\\
|
|
36
|
+
\end{pmatrix}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
B = \begin{pmatrix}
|
|
40
|
+
2&1\\
|
|
41
|
+
0&3\\
|
|
42
|
+
\end{pmatrix}
|
|
43
|
+
|
|
44
|
+
Matrix `A` represents the utilities of Amy and matrix `B` represents the
|
|
45
|
+
utility of Bob. The choices of Amy correspond to the rows of the matrices:
|
|
46
|
+
|
|
47
|
+
* The first row corresponds to video games.
|
|
48
|
+
|
|
49
|
+
* The second row corresponds to movies.
|
|
50
|
+
|
|
51
|
+
Similarly Bob's choices are represented by the columns:
|
|
52
|
+
|
|
53
|
+
* The first column corresponds to video games.
|
|
54
|
+
|
|
55
|
+
* The second column corresponds to movies.
|
|
56
|
+
|
|
57
|
+
Thus, if both Amy and Bob choose to play video games: Amy receives a
|
|
58
|
+
utility of 3 and Bob a utility of 2. If Amy is indeed going to stick
|
|
59
|
+
with video games Bob has no incentive to deviate (and vice versa).
|
|
60
|
+
|
|
61
|
+
This situation repeats itself if both Amy and Bob choose to watch a movie:
|
|
62
|
+
neither has an incentive to deviate.
|
|
63
|
+
|
|
64
|
+
This loosely described situation is referred to as a Nash Equilibrium.
|
|
65
|
+
We can use Sage to find them, and more importantly, see if there is any
|
|
66
|
+
other situation where Amy and Bob have no reason to change their choice
|
|
67
|
+
of action:
|
|
68
|
+
|
|
69
|
+
Here is how we create the game in Sage::
|
|
70
|
+
|
|
71
|
+
sage: A = matrix([[3, 1], [0, 2]])
|
|
72
|
+
sage: B = matrix([[2, 1], [0, 3]])
|
|
73
|
+
sage: battle_of_the_sexes = NormalFormGame([A, B])
|
|
74
|
+
sage: battle_of_the_sexes
|
|
75
|
+
Normal Form Game with the following utilities: {(0, 0): [3, 2],
|
|
76
|
+
(0, 1): [1, 1], (1, 0): [0, 0], (1, 1): [2, 3]}
|
|
77
|
+
|
|
78
|
+
To obtain the Nash equilibria we run the ``obtain_nash()`` method. In the
|
|
79
|
+
first few examples, we will use the 'support enumeration' algorithm.
|
|
80
|
+
A discussion about the different algorithms will be given later::
|
|
81
|
+
|
|
82
|
+
sage: battle_of_the_sexes.obtain_nash(algorithm='enumeration')
|
|
83
|
+
[[(0, 1), (0, 1)], [(3/4, 1/4), (1/4, 3/4)], [(1, 0), (1, 0)]]
|
|
84
|
+
|
|
85
|
+
If we look a bit closer at our output we see that a list of three
|
|
86
|
+
pairs of tuples have been returned. Each of these correspond to a
|
|
87
|
+
Nash Equilibrium, represented as a probability distribution over the
|
|
88
|
+
available strategies:
|
|
89
|
+
|
|
90
|
+
* `[(1, 0), (1, 0)]` corresponds to the first player only
|
|
91
|
+
playing their first strategy and the second player also only playing
|
|
92
|
+
their first strategy. In other words Amy and Bob both play video games.
|
|
93
|
+
|
|
94
|
+
* `[(0, 1), (0, 1)]` corresponds to the first player only
|
|
95
|
+
playing their second strategy and the second player also only playing
|
|
96
|
+
their second strategy. In other words Amy and Bob both watch movies.
|
|
97
|
+
|
|
98
|
+
* `[(3/4, 1/4), (1/4, 3/4)]` corresponds to players `mixing` their
|
|
99
|
+
strategies. Amy plays video games 75% of the time and Bob watches
|
|
100
|
+
movies 75% of the time. At this equilibrium point Amy and Bob will
|
|
101
|
+
only ever do the same activity `3/8` of the time.
|
|
102
|
+
|
|
103
|
+
We can use Sage to compute the expected utility for any mixed strategy
|
|
104
|
+
pair `(\sigma_1, \sigma_2)`. The payoff to player 1 is given by the
|
|
105
|
+
vector/matrix multiplication:
|
|
106
|
+
|
|
107
|
+
.. MATH::
|
|
108
|
+
|
|
109
|
+
\sigma_1 A \sigma_2
|
|
110
|
+
|
|
111
|
+
The payoff to player 2 is given by:
|
|
112
|
+
|
|
113
|
+
.. MATH::
|
|
114
|
+
|
|
115
|
+
\sigma_1 B \sigma_2
|
|
116
|
+
|
|
117
|
+
To compute this in Sage we have::
|
|
118
|
+
|
|
119
|
+
sage: for ne in battle_of_the_sexes.obtain_nash(algorithm='enumeration'):
|
|
120
|
+
....: print("Utility for {}: ".format(ne))
|
|
121
|
+
....: print("{} {}".format(vector(ne[0]) * A * vector(ne[1]), vector(ne[0]) * B * vector(ne[1])))
|
|
122
|
+
Utility for [(0, 1), (0, 1)]:
|
|
123
|
+
2 3
|
|
124
|
+
Utility for [(3/4, 1/4), (1/4, 3/4)]:
|
|
125
|
+
3/2 3/2
|
|
126
|
+
Utility for [(1, 0), (1, 0)]:
|
|
127
|
+
3 2
|
|
128
|
+
|
|
129
|
+
Allowing players to play mixed strategies ensures that there will always
|
|
130
|
+
be a Nash Equilibrium for a normal form game. This result is called Nash's
|
|
131
|
+
Theorem ([Nas1950]_).
|
|
132
|
+
|
|
133
|
+
Let us consider the game called 'matching pennies' where two players each
|
|
134
|
+
present a coin with either HEADS or TAILS showing. If the coins show the
|
|
135
|
+
same side then player 1 wins, otherwise player 2 wins:
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
.. MATH::
|
|
139
|
+
|
|
140
|
+
A = \begin{pmatrix}
|
|
141
|
+
1&-1\\
|
|
142
|
+
-1&1\\
|
|
143
|
+
\end{pmatrix}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
B = \begin{pmatrix}
|
|
147
|
+
-1&1\\
|
|
148
|
+
1&-1\\
|
|
149
|
+
\end{pmatrix}
|
|
150
|
+
|
|
151
|
+
It should be relatively straightforward to observe, that there is no
|
|
152
|
+
situation, where both players always do the same thing, and have no
|
|
153
|
+
incentive to deviate.
|
|
154
|
+
|
|
155
|
+
We can plot the utility of player 1 when player 2 is playing a mixed
|
|
156
|
+
strategy `\sigma_2 = (y, 1-y)` (so that the utility to player 1 for
|
|
157
|
+
playing strategy number `i` is given by the matrix/vector multiplication
|
|
158
|
+
`(Ay)_i`, ie element in position `i` of the matrix/vector multiplication
|
|
159
|
+
`Ay`) ::
|
|
160
|
+
|
|
161
|
+
sage: y = var('y') # needs sage.symbolic
|
|
162
|
+
sage: A = matrix([[1, -1], [-1, 1]])
|
|
163
|
+
sage: p = plot((A * vector([y, 1 - y]))[0], y, 0, 1, color='blue', # needs sage.symbolic
|
|
164
|
+
....: legend_label='$u_1(r_1, (y, 1-y))$', axes_labels=['$y$', ''])
|
|
165
|
+
sage: p += plot((A * vector([y, 1 - y]))[1], y, 0, 1, color='red', # needs sage.symbolic
|
|
166
|
+
....: legend_label='$u_1(r_2, (y, 1-y))$'); p
|
|
167
|
+
Graphics object consisting of 2 graphics primitives
|
|
168
|
+
|
|
169
|
+
We see that the only point at which player 1 is indifferent amongst
|
|
170
|
+
the available strategies is when `y = 1/2`.
|
|
171
|
+
|
|
172
|
+
If we compute the Nash equilibria we see that this corresponds to a point
|
|
173
|
+
at which both players are indifferent::
|
|
174
|
+
|
|
175
|
+
sage: A = matrix([[1, -1], [-1, 1]])
|
|
176
|
+
sage: B = matrix([[-1, 1], [1, -1]])
|
|
177
|
+
sage: matching_pennies = NormalFormGame([A, B])
|
|
178
|
+
sage: matching_pennies.obtain_nash(algorithm='enumeration')
|
|
179
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
180
|
+
|
|
181
|
+
The utilities to both players at this Nash equilibrium
|
|
182
|
+
is easily computed::
|
|
183
|
+
|
|
184
|
+
sage: [vector([1/2, 1/2]) * M * vector([1/2, 1/2])
|
|
185
|
+
....: for M in matching_pennies.payoff_matrices()]
|
|
186
|
+
[0, 0]
|
|
187
|
+
|
|
188
|
+
Note that the above uses the ``payoff_matrices`` method
|
|
189
|
+
which returns the payoff matrices for a 2 player game::
|
|
190
|
+
|
|
191
|
+
sage: matching_pennies.payoff_matrices()
|
|
192
|
+
(
|
|
193
|
+
[ 1 -1] [-1 1]
|
|
194
|
+
[-1 1], [ 1 -1]
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
One can also input a single matrix and then a zero sum game is constructed.
|
|
198
|
+
Here is an instance of `Rock-Paper-Scissors-Lizard-Spock
|
|
199
|
+
<http://www.samkass.com/theories/RPSSL.html>`_::
|
|
200
|
+
|
|
201
|
+
sage: A = matrix([[0, -1, 1, 1, -1],
|
|
202
|
+
....: [1, 0, -1, -1, 1],
|
|
203
|
+
....: [-1, 1, 0, 1 , -1],
|
|
204
|
+
....: [-1, 1, -1, 0, 1],
|
|
205
|
+
....: [1, -1, 1, -1, 0]])
|
|
206
|
+
sage: g = NormalFormGame([A])
|
|
207
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
208
|
+
[[(1/5, 1/5, 1/5, 1/5, 1/5), (1/5, 1/5, 1/5, 1/5, 1/5)]]
|
|
209
|
+
|
|
210
|
+
We can also study games where players aim to minimize their utility.
|
|
211
|
+
Here is the Prisoner's Dilemma (where players are aiming to reduce
|
|
212
|
+
time spent in prison)::
|
|
213
|
+
|
|
214
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
215
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
216
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
217
|
+
sage: prisoners_dilemma.obtain_nash(algorithm='enumeration', maximization=False)
|
|
218
|
+
[[(0, 1), (0, 1)]]
|
|
219
|
+
|
|
220
|
+
When obtaining Nash equilibrium the following algorithms are
|
|
221
|
+
currently available:
|
|
222
|
+
|
|
223
|
+
* ``'lp'``: A solver for constant sum 2 player games using linear
|
|
224
|
+
programming. This constructs a
|
|
225
|
+
:mod:`MixedIntegerLinearProgram <sage.numerical.MILP>` using the
|
|
226
|
+
solver which was passed in with ``solver`` to solve the linear
|
|
227
|
+
programming representation of the game. See
|
|
228
|
+
:class:`MixedIntegerLinearProgram` for more on MILP solvers in Sage.
|
|
229
|
+
|
|
230
|
+
* ``'lrs'``: Reverse search vertex enumeration for 2 player games. This
|
|
231
|
+
algorithm uses the optional 'lrslib' package. To install it, type
|
|
232
|
+
``sage -i lrslib`` in the shell. For more information, see [Av2000]_.
|
|
233
|
+
|
|
234
|
+
* ``'LCP'``: Linear complementarity program algorithm for 2 player games.
|
|
235
|
+
This algorithm uses the open source game theory package:
|
|
236
|
+
`Gambit <http://gambit.sourceforge.net/>`_ [Gambit]_. At present this is
|
|
237
|
+
the only gambit algorithm available in sage but further development will
|
|
238
|
+
hope to implement more algorithms
|
|
239
|
+
(in particular for games with more than 2 players).
|
|
240
|
+
|
|
241
|
+
* ``'enumeration'``: Support enumeration for 2 player games. This
|
|
242
|
+
algorithm is hard coded in Sage and checks through all potential
|
|
243
|
+
supports of a strategy. Supports of a given size with a conditionally
|
|
244
|
+
dominated strategy are ignored. Note: this is not the preferred
|
|
245
|
+
algorithm. The algorithm implemented is a combination of a basic
|
|
246
|
+
algorithm described in [NN2007]_ and a pruning component described
|
|
247
|
+
in [SLB2008]_.
|
|
248
|
+
|
|
249
|
+
Below we show how the these algorithms are called::
|
|
250
|
+
|
|
251
|
+
sage: matching_pennies.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
252
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
253
|
+
sage: matching_pennies.obtain_nash(algorithm='LCP') # optional - gambit
|
|
254
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
255
|
+
sage: matching_pennies.obtain_nash(algorithm='lp', solver='PPL')
|
|
256
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
257
|
+
sage: matching_pennies.obtain_nash(algorithm='lp', solver='gambit') # optional - gambit
|
|
258
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
259
|
+
sage: matching_pennies.obtain_nash(algorithm='enumeration')
|
|
260
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
261
|
+
|
|
262
|
+
Note that if no algorithm argument is passed then the default will be
|
|
263
|
+
selected according to the following order (if the corresponding package is
|
|
264
|
+
installed):
|
|
265
|
+
|
|
266
|
+
1. ``'lp'`` (if the game is constant-sum; uses the solver chosen by Sage)
|
|
267
|
+
2. ``'lrs'`` (requires 'lrslib')
|
|
268
|
+
3. ``'enumeration'``
|
|
269
|
+
|
|
270
|
+
Here is a game being constructed using gambit syntax (note that a
|
|
271
|
+
``NormalFormGame`` object acts like a dictionary with pure strategy tuples as
|
|
272
|
+
keys and payoffs as their values)::
|
|
273
|
+
|
|
274
|
+
sage: f = NormalFormGame()
|
|
275
|
+
sage: f.add_player(2) # Adding first player with 2 strategies
|
|
276
|
+
sage: f.add_player(2) # Adding second player with 2 strategies
|
|
277
|
+
sage: f[0,0][0] = 1
|
|
278
|
+
sage: f[0,0][1] = 3
|
|
279
|
+
sage: f[0,1][0] = 2
|
|
280
|
+
sage: f[0,1][1] = 3
|
|
281
|
+
sage: f[1,0][0] = 3
|
|
282
|
+
sage: f[1,0][1] = 1
|
|
283
|
+
sage: f[1,1][0] = 4
|
|
284
|
+
sage: f[1,1][1] = 4
|
|
285
|
+
sage: f
|
|
286
|
+
Normal Form Game with the following utilities: {(0, 0): [1, 3],
|
|
287
|
+
(0, 1): [2, 3], (1, 0): [3, 1], (1, 1): [4, 4]}
|
|
288
|
+
|
|
289
|
+
Once this game is constructed we can view the payoff matrices and solve the
|
|
290
|
+
game::
|
|
291
|
+
|
|
292
|
+
sage: f.payoff_matrices()
|
|
293
|
+
(
|
|
294
|
+
[1 2] [3 3]
|
|
295
|
+
[3 4], [1 4]
|
|
296
|
+
)
|
|
297
|
+
sage: f.obtain_nash(algorithm='enumeration')
|
|
298
|
+
[[(0, 1), (0, 1)]]
|
|
299
|
+
|
|
300
|
+
We can add an extra strategy to the first player::
|
|
301
|
+
|
|
302
|
+
sage: f.add_strategy(0)
|
|
303
|
+
sage: f
|
|
304
|
+
Normal Form Game with the following utilities: {(0, 0): [1, 3],
|
|
305
|
+
(0, 1): [2, 3],
|
|
306
|
+
(1, 0): [3, 1],
|
|
307
|
+
(1, 1): [4, 4],
|
|
308
|
+
(2, 0): [False, False],
|
|
309
|
+
(2, 1): [False, False]}
|
|
310
|
+
|
|
311
|
+
If we do this and try and obtain the Nash equilibrium or view the payoff
|
|
312
|
+
matrices(without specifying the utilities), an error is returned::
|
|
313
|
+
|
|
314
|
+
sage: f.obtain_nash()
|
|
315
|
+
Traceback (most recent call last):
|
|
316
|
+
...
|
|
317
|
+
ValueError: utilities have not been populated
|
|
318
|
+
sage: f.payoff_matrices()
|
|
319
|
+
Traceback (most recent call last):
|
|
320
|
+
...
|
|
321
|
+
ValueError: utilities have not been populated
|
|
322
|
+
|
|
323
|
+
Here we populate the missing utilities::
|
|
324
|
+
|
|
325
|
+
sage: f[2, 1] = [5, 3]
|
|
326
|
+
sage: f[2, 0] = [2, 1]
|
|
327
|
+
sage: f.payoff_matrices()
|
|
328
|
+
(
|
|
329
|
+
[1 2] [3 3]
|
|
330
|
+
[3 4] [1 4]
|
|
331
|
+
[2 5], [1 3]
|
|
332
|
+
)
|
|
333
|
+
sage: f.obtain_nash()
|
|
334
|
+
[[(0, 0, 1), (0, 1)]]
|
|
335
|
+
|
|
336
|
+
We can use the same syntax as above to create games with
|
|
337
|
+
more than 2 players::
|
|
338
|
+
|
|
339
|
+
sage: threegame = NormalFormGame()
|
|
340
|
+
sage: threegame.add_player(2) # Adding first player with 2 strategies
|
|
341
|
+
sage: threegame.add_player(2) # Adding second player with 2 strategies
|
|
342
|
+
sage: threegame.add_player(2) # Adding third player with 2 strategies
|
|
343
|
+
sage: threegame[0, 0, 0][0] = 3
|
|
344
|
+
sage: threegame[0, 0, 0][1] = 1
|
|
345
|
+
sage: threegame[0, 0, 0][2] = 4
|
|
346
|
+
sage: threegame[0, 0, 1][0] = 1
|
|
347
|
+
sage: threegame[0, 0, 1][1] = 5
|
|
348
|
+
sage: threegame[0, 0, 1][2] = 9
|
|
349
|
+
sage: threegame[0, 1, 0][0] = 2
|
|
350
|
+
sage: threegame[0, 1, 0][1] = 6
|
|
351
|
+
sage: threegame[0, 1, 0][2] = 5
|
|
352
|
+
sage: threegame[0, 1, 1][0] = 3
|
|
353
|
+
sage: threegame[0, 1, 1][1] = 5
|
|
354
|
+
sage: threegame[0, 1, 1][2] = 8
|
|
355
|
+
sage: threegame[1, 0, 0][0] = 9
|
|
356
|
+
sage: threegame[1, 0, 0][1] = 7
|
|
357
|
+
sage: threegame[1, 0, 0][2] = 9
|
|
358
|
+
sage: threegame[1, 0, 1][0] = 3
|
|
359
|
+
sage: threegame[1, 0, 1][1] = 2
|
|
360
|
+
sage: threegame[1, 0, 1][2] = 3
|
|
361
|
+
sage: threegame[1, 1, 0][0] = 8
|
|
362
|
+
sage: threegame[1, 1, 0][1] = 4
|
|
363
|
+
sage: threegame[1, 1, 0][2] = 6
|
|
364
|
+
sage: threegame[1, 1, 1][0] = 2
|
|
365
|
+
sage: threegame[1, 1, 1][1] = 6
|
|
366
|
+
sage: threegame[1, 1, 1][2] = 4
|
|
367
|
+
sage: threegame
|
|
368
|
+
Normal Form Game with the following utilities: {(0, 0, 0): [3, 1, 4],
|
|
369
|
+
(0, 0, 1): [1, 5, 9],
|
|
370
|
+
(0, 1, 0): [2, 6, 5],
|
|
371
|
+
(0, 1, 1): [3, 5, 8],
|
|
372
|
+
(1, 0, 0): [9, 7, 9],
|
|
373
|
+
(1, 0, 1): [3, 2, 3],
|
|
374
|
+
(1, 1, 0): [8, 4, 6],
|
|
375
|
+
(1, 1, 1): [2, 6, 4]}
|
|
376
|
+
|
|
377
|
+
The above requires a lot of input that could be simplified if there is
|
|
378
|
+
another data structure with our utilities and/or a structure to the
|
|
379
|
+
utilities. The following example creates a game with a relatively strange
|
|
380
|
+
utility function::
|
|
381
|
+
|
|
382
|
+
sage: def utility(strategy_triplet, player):
|
|
383
|
+
....: return sum(strategy_triplet) * player
|
|
384
|
+
sage: threegame = NormalFormGame()
|
|
385
|
+
sage: threegame.add_player(2) # Adding first player with 2 strategies
|
|
386
|
+
sage: threegame.add_player(2) # Adding second player with 2 strategies
|
|
387
|
+
sage: threegame.add_player(2) # Adding third player with 2 strategies
|
|
388
|
+
sage: for i, j, k in [(i, j, k) for i in [0,1] for j in [0,1] for k in [0,1]]:
|
|
389
|
+
....: for p in range(3):
|
|
390
|
+
....: threegame[i, j, k][p] = utility([i, j, k], p)
|
|
391
|
+
sage: threegame
|
|
392
|
+
Normal Form Game with the following utilities: {(0, 0, 0): [0, 0, 0],
|
|
393
|
+
(0, 0, 1): [0, 1, 2],
|
|
394
|
+
(0, 1, 0): [0, 1, 2],
|
|
395
|
+
(0, 1, 1): [0, 2, 4],
|
|
396
|
+
(1, 0, 0): [0, 1, 2],
|
|
397
|
+
(1, 0, 1): [0, 2, 4],
|
|
398
|
+
(1, 1, 0): [0, 2, 4],
|
|
399
|
+
(1, 1, 1): [0, 3, 6]}
|
|
400
|
+
|
|
401
|
+
At present no algorithm has been implemented in Sage for games with
|
|
402
|
+
more than 2 players::
|
|
403
|
+
|
|
404
|
+
sage: threegame.obtain_nash()
|
|
405
|
+
Traceback (most recent call last):
|
|
406
|
+
...
|
|
407
|
+
NotImplementedError: Nash equilibrium for games with more than 2 players
|
|
408
|
+
have not been implemented yet. Please see the gambit website
|
|
409
|
+
(http://gambit.sourceforge.net/) that has a variety of available algorithms
|
|
410
|
+
|
|
411
|
+
There are however a variety of such algorithms available in gambit,
|
|
412
|
+
further compatibility between Sage and gambit is actively being developed:
|
|
413
|
+
https://github.com/tturocy/gambit/tree/sage_integration.
|
|
414
|
+
|
|
415
|
+
It can be shown that linear scaling of the payoff matrices conserves the
|
|
416
|
+
equilibrium values::
|
|
417
|
+
|
|
418
|
+
sage: A = matrix([[2, 1], [1, 2.5]])
|
|
419
|
+
sage: B = matrix([[-1, 3], [2, 1]])
|
|
420
|
+
sage: g = NormalFormGame([A, B])
|
|
421
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
422
|
+
[[(1/5, 4/5), (3/5, 2/5)]]
|
|
423
|
+
sage: g.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
424
|
+
[[(1/5, 4/5), (3/5, 2/5)]]
|
|
425
|
+
sage: A = 2 * A
|
|
426
|
+
sage: g = NormalFormGame([A, B])
|
|
427
|
+
sage: g.obtain_nash(algorithm='LCP') # optional - gambit
|
|
428
|
+
[[(0.2, 0.8), (0.6, 0.4)]]
|
|
429
|
+
|
|
430
|
+
It is also possible to generate a Normal form game from a gambit Game::
|
|
431
|
+
|
|
432
|
+
sage: # optional - gambit
|
|
433
|
+
sage: from gambit import Game
|
|
434
|
+
sage: gambitgame= Game.new_table([2, 2])
|
|
435
|
+
sage: gambitgame[int(0), int(0)][int(0)] = int(8)
|
|
436
|
+
sage: gambitgame[int(0), int(0)][int(1)] = int(8)
|
|
437
|
+
sage: gambitgame[int(0), int(1)][int(0)] = int(2)
|
|
438
|
+
sage: gambitgame[int(0), int(1)][int(1)] = int(10)
|
|
439
|
+
sage: gambitgame[int(1), int(0)][int(0)] = int(10)
|
|
440
|
+
sage: gambitgame[int(1), int(0)][int(1)] = int(2)
|
|
441
|
+
sage: gambitgame[int(1), int(1)][int(0)] = int(5)
|
|
442
|
+
sage: gambitgame[int(1), int(1)][int(1)] = int(5)
|
|
443
|
+
sage: g = NormalFormGame(gambitgame); g
|
|
444
|
+
Normal Form Game with the following utilities: {(0, 0): [8.0, 8.0],
|
|
445
|
+
(0, 1): [2.0, 10.0],
|
|
446
|
+
(1, 0): [10.0, 2.0],
|
|
447
|
+
(1, 1): [5.0, 5.0]}
|
|
448
|
+
|
|
449
|
+
For more information on using Gambit in Sage see: :mod:`Using Gambit in
|
|
450
|
+
Sage<sage.game_theory.gambit_docs>`. This includes how to access Gambit
|
|
451
|
+
directly using the version of iPython shipped with Sage and an explanation
|
|
452
|
+
as to why the ``int`` calls are needed to handle the Sage preparser.
|
|
453
|
+
|
|
454
|
+
Here is a slightly longer game that would take too long to solve with
|
|
455
|
+
``'enumeration'``. Consider the following:
|
|
456
|
+
|
|
457
|
+
An airline loses two suitcases belonging to two different travelers. Both
|
|
458
|
+
suitcases happen to be identical and contain identical antiques. An
|
|
459
|
+
airline manager tasked to settle the claims of both travelers explains
|
|
460
|
+
that the airline is liable for a maximum of 10 per suitcase, and in order
|
|
461
|
+
to determine an honest appraised value of the antiques the manager
|
|
462
|
+
separates both travelers so they can't confer, and asks them to write down
|
|
463
|
+
the amount of their value at no less than 2 and no larger than 10. He
|
|
464
|
+
also tells them that if both write down the same number, he will treat
|
|
465
|
+
that number as the true dollar value of both suitcases and reimburse both
|
|
466
|
+
travelers that amount.
|
|
467
|
+
|
|
468
|
+
However, if one writes down a smaller number than the other, this smaller
|
|
469
|
+
number will be taken as the true dollar value, and both travelers will
|
|
470
|
+
receive that amount along with a bonus/malus: 2 extra will be paid to the
|
|
471
|
+
traveler who wrote down the lower value and a 2 deduction will be taken
|
|
472
|
+
from the person who wrote down the higher amount. The challenge is: what
|
|
473
|
+
strategy should both travelers follow to decide the value they should
|
|
474
|
+
write down?
|
|
475
|
+
|
|
476
|
+
In the following we create the game (with a max value of 10) and solve it::
|
|
477
|
+
|
|
478
|
+
sage: K = 10 # Modifying this value lets us play with games of any size
|
|
479
|
+
sage: A = matrix([[min(i,j) + 2 * sign(j-i) for j in range(K, 1, -1)]
|
|
480
|
+
....: for i in range(K, 1, -1)])
|
|
481
|
+
sage: B = matrix([[min(i,j) + 2 * sign(i-j) for j in range(K, 1, -1)]
|
|
482
|
+
....: for i in range(K, 1, -1)])
|
|
483
|
+
sage: g = NormalFormGame([A, B])
|
|
484
|
+
sage: g.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
485
|
+
[[(0, 0, 0, 0, 0, 0, 0, 0, 1), (0, 0, 0, 0, 0, 0, 0, 0, 1)]]
|
|
486
|
+
sage: g.obtain_nash(algorithm='LCP') # optional - gambit
|
|
487
|
+
[[(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
|
|
488
|
+
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0)]]
|
|
489
|
+
|
|
490
|
+
The output is a pair of vectors (as before) showing the Nash equilibrium.
|
|
491
|
+
In particular it here shows that out of the 10 possible strategies both
|
|
492
|
+
players should choose the last. Recall that the above considers a reduced
|
|
493
|
+
version of the game where individuals can claim integer values from 10
|
|
494
|
+
to 2. The equilibrium strategy is thus for both players to state that
|
|
495
|
+
the value of their suitcase is 2.
|
|
496
|
+
|
|
497
|
+
Several standard Normal Form Games have also been implemented.
|
|
498
|
+
For more information on how to access these, see:
|
|
499
|
+
:mod:`Game Theory Catalog<sage.game_theory.catalog>`.
|
|
500
|
+
Included is information on the situation each Game models.
|
|
501
|
+
For example::
|
|
502
|
+
|
|
503
|
+
sage: g = game_theory.normal_form_games.PrisonersDilemma()
|
|
504
|
+
sage: g
|
|
505
|
+
Prisoners dilemma - Normal Form Game with the following utilities: ...
|
|
506
|
+
sage: d = {(0, 1): [-5, 0], (1, 0): [0, -5],
|
|
507
|
+
....: (0, 0): [-2, -2], (1, 1): [-4, -4]}
|
|
508
|
+
sage: g == d
|
|
509
|
+
True
|
|
510
|
+
sage: g.obtain_nash()
|
|
511
|
+
[[(0, 1), (0, 1)]]
|
|
512
|
+
|
|
513
|
+
We can easily obtain the best response for a player to a given strategy. In
|
|
514
|
+
this example we obtain the best responses for Player 1, when Player 2 uses two
|
|
515
|
+
different strategies::
|
|
516
|
+
|
|
517
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
518
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
519
|
+
sage: g = NormalFormGame([A, B])
|
|
520
|
+
sage: g.best_responses((1/2, 1/2), player=0)
|
|
521
|
+
[0, 1, 2]
|
|
522
|
+
sage: g.best_responses((3/4, 1/4), player=0)
|
|
523
|
+
[0]
|
|
524
|
+
|
|
525
|
+
Here we do the same for player 2::
|
|
526
|
+
|
|
527
|
+
sage: g.best_responses((4/5, 1/5, 0), player=1)
|
|
528
|
+
[0, 1]
|
|
529
|
+
|
|
530
|
+
We see that for the game `Rock-Paper-Scissors-Lizard-Spock
|
|
531
|
+
<http://www.samkass.com/theories/RPSSL.html>`_ any pure strategy has two best
|
|
532
|
+
responses::
|
|
533
|
+
|
|
534
|
+
sage: g = game_theory.normal_form_games.RPSLS()
|
|
535
|
+
sage: A, B = g.payoff_matrices()
|
|
536
|
+
sage: A, B
|
|
537
|
+
(
|
|
538
|
+
[ 0 -1 1 1 -1] [ 0 1 -1 -1 1]
|
|
539
|
+
[ 1 0 -1 -1 1] [-1 0 1 1 -1]
|
|
540
|
+
[-1 1 0 1 -1] [ 1 -1 0 -1 1]
|
|
541
|
+
[-1 1 -1 0 1] [ 1 -1 1 0 -1]
|
|
542
|
+
[ 1 -1 1 -1 0], [-1 1 -1 1 0]
|
|
543
|
+
)
|
|
544
|
+
sage: g.best_responses((1, 0, 0, 0, 0), player=0)
|
|
545
|
+
[1, 4]
|
|
546
|
+
sage: g.best_responses((0, 1, 0, 0, 0), player=0)
|
|
547
|
+
[2, 3]
|
|
548
|
+
sage: g.best_responses((0, 0, 1, 0, 0), player=0)
|
|
549
|
+
[0, 4]
|
|
550
|
+
sage: g.best_responses((0, 0, 0, 1, 0), player=0)
|
|
551
|
+
[0, 2]
|
|
552
|
+
sage: g.best_responses((0, 0, 0, 0, 1), player=0)
|
|
553
|
+
[1, 3]
|
|
554
|
+
sage: g.best_responses((1, 0, 0, 0, 0), player=1)
|
|
555
|
+
[1, 4]
|
|
556
|
+
sage: g.best_responses((0, 1, 0, 0, 0), player=1)
|
|
557
|
+
[2, 3]
|
|
558
|
+
sage: g.best_responses((0, 0, 1, 0, 0), player=1)
|
|
559
|
+
[0, 4]
|
|
560
|
+
sage: g.best_responses((0, 0, 0, 1, 0), player=1)
|
|
561
|
+
[0, 2]
|
|
562
|
+
sage: g.best_responses((0, 0, 0, 0, 1), player=1)
|
|
563
|
+
[1, 3]
|
|
564
|
+
|
|
565
|
+
Note that degenerate games can cause problems for most algorithms.
|
|
566
|
+
The following example in fact has an infinite quantity of equilibria which
|
|
567
|
+
is evidenced by the various algorithms returning different solutions::
|
|
568
|
+
|
|
569
|
+
sage: A = matrix([[3,3],[2,5],[0,6]])
|
|
570
|
+
sage: B = matrix([[3,3],[2,6],[3,1]])
|
|
571
|
+
sage: degenerate_game = NormalFormGame([A,B])
|
|
572
|
+
sage: degenerate_game.obtain_nash(algorithm='lrs') # random, optional - lrslib
|
|
573
|
+
[[(0, 1/3, 2/3), (1/3, 2/3)], [(1, 0, 0), (1/2, 3)], [(1, 0, 0), (1, 3)]]
|
|
574
|
+
sage: degenerate_game.obtain_nash(algorithm='LCP') # optional - gambit
|
|
575
|
+
[[(0.0, 0.3333333333, 0.6666666667), (0.3333333333, 0.6666666667)],
|
|
576
|
+
[(1.0, -0.0, 0.0), (0.6666666667, 0.3333333333)],
|
|
577
|
+
[(1.0, 0.0, 0.0), (1.0, 0.0)]]
|
|
578
|
+
sage: degenerate_game.obtain_nash(algorithm='enumeration')
|
|
579
|
+
[[(0, 1/3, 2/3), (1/3, 2/3)], [(1, 0, 0), (1, 0)]]
|
|
580
|
+
|
|
581
|
+
We can check the cause of this by using ``is_degenerate()``::
|
|
582
|
+
|
|
583
|
+
sage: degenerate_game.is_degenerate()
|
|
584
|
+
True
|
|
585
|
+
|
|
586
|
+
Note the 'negative' `-0.0` output by gambit. This is due to the numerical
|
|
587
|
+
nature of the algorithm used.
|
|
588
|
+
|
|
589
|
+
Here is an example with the trivial game where all payoffs are 0::
|
|
590
|
+
|
|
591
|
+
sage: g = NormalFormGame()
|
|
592
|
+
sage: g.add_player(3) # Adding first player with 3 strategies
|
|
593
|
+
sage: g.add_player(3) # Adding second player with 3 strategies
|
|
594
|
+
sage: for key in g:
|
|
595
|
+
....: g[key] = [0, 0]
|
|
596
|
+
sage: g.payoff_matrices()
|
|
597
|
+
(
|
|
598
|
+
[0 0 0] [0 0 0]
|
|
599
|
+
[0 0 0] [0 0 0]
|
|
600
|
+
[0 0 0], [0 0 0]
|
|
601
|
+
)
|
|
602
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
603
|
+
[[(0, 0, 1), (0, 0, 1)], [(0, 0, 1), (0, 1, 0)], [(0, 0, 1), (1, 0, 0)],
|
|
604
|
+
[(0, 1, 0), (0, 0, 1)], [(0, 1, 0), (0, 1, 0)], [(0, 1, 0), (1, 0, 0)],
|
|
605
|
+
[(1, 0, 0), (0, 0, 1)], [(1, 0, 0), (0, 1, 0)], [(1, 0, 0), (1, 0, 0)]]
|
|
606
|
+
|
|
607
|
+
A good description of degenerate games can be found in [NN2007]_.
|
|
608
|
+
|
|
609
|
+
REFERENCES:
|
|
610
|
+
|
|
611
|
+
- [Nas1950]_
|
|
612
|
+
|
|
613
|
+
- [NN2007]_
|
|
614
|
+
|
|
615
|
+
- [Av2000]_
|
|
616
|
+
|
|
617
|
+
- [Gambit]_
|
|
618
|
+
|
|
619
|
+
- [SLB2008]_
|
|
620
|
+
|
|
621
|
+
AUTHORS:
|
|
622
|
+
|
|
623
|
+
- James Campbell and Vince Knight (06-2014): Original version
|
|
624
|
+
|
|
625
|
+
- Tobenna P. Igwe: Constant-sum game solvers
|
|
626
|
+
"""
|
|
627
|
+
|
|
628
|
+
# ****************************************************************************
|
|
629
|
+
# Copyright (C) 2014 James Campbell james.campbell@tanti.org.uk
|
|
630
|
+
#
|
|
631
|
+
# This program is free software: you can redistribute it and/or modify
|
|
632
|
+
# it under the terms of the GNU General Public License as published by
|
|
633
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
634
|
+
# (at your option) any later version.
|
|
635
|
+
# https://www.gnu.org/licenses/
|
|
636
|
+
# ****************************************************************************
|
|
637
|
+
|
|
638
|
+
from collections.abc import MutableMapping
|
|
639
|
+
from itertools import product
|
|
640
|
+
from .parser import Parser
|
|
641
|
+
from sage.misc.latex import latex
|
|
642
|
+
from sage.combinat.subset import powerset
|
|
643
|
+
from sage.rings.rational_field import QQ
|
|
644
|
+
from sage.structure.sage_object import SageObject
|
|
645
|
+
from sage.matrix.constructor import matrix
|
|
646
|
+
from sage.matrix.constructor import vector
|
|
647
|
+
from sage.misc.temporary_file import tmp_filename
|
|
648
|
+
from sage.numerical.mip import MixedIntegerLinearProgram
|
|
649
|
+
from sage.cpython.string import bytes_to_str
|
|
650
|
+
|
|
651
|
+
try:
|
|
652
|
+
from gambit import Game
|
|
653
|
+
from gambit.nash import ExternalLPSolver, ExternalLCPSolver
|
|
654
|
+
except ImportError:
|
|
655
|
+
Game = None
|
|
656
|
+
ExternalLPSolver = None
|
|
657
|
+
ExternalLCPSolver = None
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
class NormalFormGame(SageObject, MutableMapping):
|
|
661
|
+
r"""
|
|
662
|
+
An object representing a Normal Form Game. Primarily used to compute the
|
|
663
|
+
Nash Equilibria.
|
|
664
|
+
|
|
665
|
+
INPUT:
|
|
666
|
+
|
|
667
|
+
- ``generator`` -- can be a list of 2 matrices, a single matrix or left
|
|
668
|
+
blank
|
|
669
|
+
"""
|
|
670
|
+
|
|
671
|
+
def __init__(self, generator=None):
|
|
672
|
+
r"""
|
|
673
|
+
Initialize a Normal Form game and checks the inputs.
|
|
674
|
+
|
|
675
|
+
EXAMPLES:
|
|
676
|
+
|
|
677
|
+
Can have games with more than 2 players::
|
|
678
|
+
|
|
679
|
+
sage: threegame = NormalFormGame()
|
|
680
|
+
sage: threegame.add_player(2) # Adding first player with 2 strategies
|
|
681
|
+
sage: threegame.add_player(2) # Adding second player with 2 strategies
|
|
682
|
+
sage: threegame.add_player(2) # Adding third player with 2 strategies
|
|
683
|
+
sage: threegame[0, 0, 0][0] = 3
|
|
684
|
+
sage: threegame[0, 0, 0][1] = 1
|
|
685
|
+
sage: threegame[0, 0, 0][2] = 4
|
|
686
|
+
sage: threegame[0, 0, 1][0] = 1
|
|
687
|
+
sage: threegame[0, 0, 1][1] = 5
|
|
688
|
+
sage: threegame[0, 0, 1][2] = 9
|
|
689
|
+
sage: threegame[0, 1, 0][0] = 2
|
|
690
|
+
sage: threegame[0, 1, 0][1] = 6
|
|
691
|
+
sage: threegame[0, 1, 0][2] = 5
|
|
692
|
+
sage: threegame[0, 1, 1][0] = 3
|
|
693
|
+
sage: threegame[0, 1, 1][1] = 5
|
|
694
|
+
sage: threegame[0, 1, 1][2] = 8
|
|
695
|
+
sage: threegame[1, 0, 0][0] = 9
|
|
696
|
+
sage: threegame[1, 0, 0][1] = 7
|
|
697
|
+
sage: threegame[1, 0, 0][2] = 9
|
|
698
|
+
sage: threegame[1, 0, 1][0] = 3
|
|
699
|
+
sage: threegame[1, 0, 1][1] = 2
|
|
700
|
+
sage: threegame[1, 0, 1][2] = 3
|
|
701
|
+
sage: threegame[1, 1, 0][0] = 8
|
|
702
|
+
sage: threegame[1, 1, 0][1] = 4
|
|
703
|
+
sage: threegame[1, 1, 0][2] = 6
|
|
704
|
+
sage: threegame[1, 1, 1][0] = 2
|
|
705
|
+
sage: threegame[1, 1, 1][1] = 6
|
|
706
|
+
sage: threegame[1, 1, 1][2] = 4
|
|
707
|
+
sage: threegame.obtain_nash()
|
|
708
|
+
Traceback (most recent call last):
|
|
709
|
+
...
|
|
710
|
+
NotImplementedError: Nash equilibrium for games with more than
|
|
711
|
+
2 players have not been implemented yet. Please see the gambit
|
|
712
|
+
website (http://gambit.sourceforge.net/) that has a variety of
|
|
713
|
+
available algorithms
|
|
714
|
+
|
|
715
|
+
Can initialise a game from a gambit game object::
|
|
716
|
+
|
|
717
|
+
sage: # optional - gambit
|
|
718
|
+
sage: from gambit import Game
|
|
719
|
+
sage: gambitgame= Game.new_table([2, 2])
|
|
720
|
+
sage: gambitgame[int(0), int(0)][int(0)] = int(5)
|
|
721
|
+
sage: gambitgame[int(0), int(0)][int(1)] = int(8)
|
|
722
|
+
sage: gambitgame[int(0), int(1)][int(0)] = int(2)
|
|
723
|
+
sage: gambitgame[int(0), int(1)][int(1)] = int(11)
|
|
724
|
+
sage: gambitgame[int(1), int(0)][int(0)] = int(10)
|
|
725
|
+
sage: gambitgame[int(1), int(0)][int(1)] = int(7)
|
|
726
|
+
sage: gambitgame[int(1), int(1)][int(0)] = int(5)
|
|
727
|
+
sage: gambitgame[int(1), int(1)][int(1)] = int(5)
|
|
728
|
+
sage: g = NormalFormGame(gambitgame); g
|
|
729
|
+
Normal Form Game with the following utilities: {(0, 0): [5.0, 8.0],
|
|
730
|
+
(0, 1): [2.0, 11.0],
|
|
731
|
+
(1, 0): [10.0, 7.0],
|
|
732
|
+
(1, 1): [5.0, 5.0]}
|
|
733
|
+
|
|
734
|
+
TESTS:
|
|
735
|
+
|
|
736
|
+
Raise error if matrices aren't the same size::
|
|
737
|
+
|
|
738
|
+
sage: p1 = matrix([[1, 2], [3, 4]])
|
|
739
|
+
sage: p2 = matrix([[3, 3], [1, 4], [6, 6]])
|
|
740
|
+
sage: error = NormalFormGame([p1, p2])
|
|
741
|
+
Traceback (most recent call last):
|
|
742
|
+
...
|
|
743
|
+
ValueError: matrices must be the same size
|
|
744
|
+
|
|
745
|
+
Note that when initializing, a single argument must be passed::
|
|
746
|
+
|
|
747
|
+
sage: p1 = matrix([[1, 2], [3, 4]])
|
|
748
|
+
sage: p2 = matrix([[3, 3], [1, 4], [6, 6]])
|
|
749
|
+
sage: error = NormalFormGame(p1, p2)
|
|
750
|
+
Traceback (most recent call last):
|
|
751
|
+
...
|
|
752
|
+
TypeError: ...__init__() takes from 1 to 2 positional arguments but 3 were given
|
|
753
|
+
|
|
754
|
+
When initiating, argument passed must be a list or nothing::
|
|
755
|
+
|
|
756
|
+
sage: error = NormalFormGame({4:6, 6:9})
|
|
757
|
+
Traceback (most recent call last):
|
|
758
|
+
...
|
|
759
|
+
TypeError: Generator function must be a list, gambit game or nothing
|
|
760
|
+
|
|
761
|
+
When passing nothing, the utilities then need to be entered manually::
|
|
762
|
+
|
|
763
|
+
sage: game = NormalFormGame()
|
|
764
|
+
sage: game
|
|
765
|
+
Normal Form Game with the following utilities: {}
|
|
766
|
+
"""
|
|
767
|
+
self.players = []
|
|
768
|
+
self.utilities = {}
|
|
769
|
+
matrices = []
|
|
770
|
+
if generator is not None:
|
|
771
|
+
if type(generator) is not list and type(generator) is not Game:
|
|
772
|
+
raise TypeError("Generator function must be a list, gambit game or nothing")
|
|
773
|
+
|
|
774
|
+
if type(generator) is list:
|
|
775
|
+
if len(generator) == 1:
|
|
776
|
+
generator.append(-generator[-1])
|
|
777
|
+
matrices = generator
|
|
778
|
+
if matrices[0].dimensions() != matrices[1].dimensions():
|
|
779
|
+
raise ValueError("matrices must be the same size")
|
|
780
|
+
self._two_matrix_game(matrices)
|
|
781
|
+
elif type(generator) is Game:
|
|
782
|
+
game = generator
|
|
783
|
+
self._gambit_game(game)
|
|
784
|
+
|
|
785
|
+
def __delitem__(self, key):
|
|
786
|
+
r"""
|
|
787
|
+
This method is one of a collection that aims to make a game
|
|
788
|
+
instance behave like a dictionary which can be used if a game
|
|
789
|
+
is to be generated without using a matrix.
|
|
790
|
+
|
|
791
|
+
Here we set up deleting an element of the utilities dictionary::
|
|
792
|
+
|
|
793
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
794
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
795
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
796
|
+
sage: prisoners_dilemma
|
|
797
|
+
Normal Form Game with the following utilities: {(0, 0): [2, 2],
|
|
798
|
+
(0, 1): [5, 0], (1, 0): [0, 5], (1, 1): [4, 4]}
|
|
799
|
+
sage: del(prisoners_dilemma[(0,1)])
|
|
800
|
+
sage: prisoners_dilemma
|
|
801
|
+
Normal Form Game with the following utilities: {(0, 0): [2, 2],
|
|
802
|
+
(1, 0): [0, 5], (1, 1): [4, 4]}
|
|
803
|
+
"""
|
|
804
|
+
self.utilities.pop(key, None)
|
|
805
|
+
|
|
806
|
+
def __getitem__(self, key):
|
|
807
|
+
r"""
|
|
808
|
+
This method is one of a collection that aims to make a game
|
|
809
|
+
instance behave like a dictionary which can be used if a game
|
|
810
|
+
is to be generated without using a matrix.
|
|
811
|
+
|
|
812
|
+
Here we allow for querying a key::
|
|
813
|
+
|
|
814
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
815
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
816
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
817
|
+
sage: prisoners_dilemma[(0, 1)]
|
|
818
|
+
[5, 0]
|
|
819
|
+
sage: del(prisoners_dilemma[(0,1)])
|
|
820
|
+
sage: prisoners_dilemma[(0, 1)]
|
|
821
|
+
Traceback (most recent call last):
|
|
822
|
+
...
|
|
823
|
+
KeyError: (0, 1)
|
|
824
|
+
"""
|
|
825
|
+
|
|
826
|
+
return self.utilities[key]
|
|
827
|
+
|
|
828
|
+
def __iter__(self):
|
|
829
|
+
r"""
|
|
830
|
+
This method is one of a collection that aims to make a game
|
|
831
|
+
instance behave like a dictionary which can be used if a game
|
|
832
|
+
is to be generated without using a matrix.
|
|
833
|
+
|
|
834
|
+
Here we allow for iteration over the game to correspond to
|
|
835
|
+
iteration over keys of the utility dictionary::
|
|
836
|
+
|
|
837
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
838
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
839
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
840
|
+
sage: for key, value in sorted(prisoners_dilemma.items()):
|
|
841
|
+
....: print("The strategy pair {} gives utilities {}".format(key, value))
|
|
842
|
+
The strategy pair (0, 0) gives utilities [2, 2]
|
|
843
|
+
The strategy pair (0, 1) gives utilities [5, 0]
|
|
844
|
+
The strategy pair (1, 0) gives utilities [0, 5]
|
|
845
|
+
The strategy pair (1, 1) gives utilities [4, 4]
|
|
846
|
+
"""
|
|
847
|
+
return iter(self.utilities)
|
|
848
|
+
|
|
849
|
+
def __setitem__(self, key, value):
|
|
850
|
+
r"""
|
|
851
|
+
This method is one of a collection that aims to make a game
|
|
852
|
+
instance behave like a dictionary which can be used if a game
|
|
853
|
+
is to be generated without using a matrix.
|
|
854
|
+
|
|
855
|
+
Here we set up setting the value of a key::
|
|
856
|
+
|
|
857
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
858
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
859
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
860
|
+
sage: del(prisoners_dilemma[(0,1)])
|
|
861
|
+
sage: prisoners_dilemma[(0,1)] = [5,6]
|
|
862
|
+
sage: prisoners_dilemma.payoff_matrices()
|
|
863
|
+
(
|
|
864
|
+
[2 5] [2 6]
|
|
865
|
+
[0 4], [5 4]
|
|
866
|
+
)
|
|
867
|
+
|
|
868
|
+
We can use the dictionary-like interface to overwrite a strategy
|
|
869
|
+
profile::
|
|
870
|
+
|
|
871
|
+
sage: prisoners_dilemma[(0,1)] = [-3,-30]
|
|
872
|
+
sage: prisoners_dilemma.payoff_matrices()
|
|
873
|
+
(
|
|
874
|
+
[ 2 -3] [ 2 -30]
|
|
875
|
+
[ 0 4], [ 5 4]
|
|
876
|
+
)
|
|
877
|
+
"""
|
|
878
|
+
self.utilities[key] = value
|
|
879
|
+
|
|
880
|
+
def __len__(self):
|
|
881
|
+
r"""
|
|
882
|
+
Return the length of the game to be the length of the utilities.
|
|
883
|
+
|
|
884
|
+
EXAMPLES::
|
|
885
|
+
|
|
886
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
887
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
888
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
889
|
+
sage: len(prisoners_dilemma)
|
|
890
|
+
4
|
|
891
|
+
"""
|
|
892
|
+
return len(self.utilities)
|
|
893
|
+
|
|
894
|
+
def _repr_(self) -> str:
|
|
895
|
+
r"""
|
|
896
|
+
Return the strategy_profiles of the game.
|
|
897
|
+
|
|
898
|
+
EXAMPLES:
|
|
899
|
+
|
|
900
|
+
Basic description of the game shown when calling the game instance::
|
|
901
|
+
|
|
902
|
+
sage: p1 = matrix([[1, 2], [3, 4]])
|
|
903
|
+
sage: p2 = matrix([[3, 3], [1, 4]])
|
|
904
|
+
sage: g = NormalFormGame([p1, p2])
|
|
905
|
+
sage: g
|
|
906
|
+
Normal Form Game with the following utilities: {(0, 0): [1, 3],
|
|
907
|
+
(0, 1): [2, 3], (1, 0): [3, 1], (1, 1): [4, 4]}
|
|
908
|
+
"""
|
|
909
|
+
from pprint import pformat
|
|
910
|
+
base_str = "Normal Form Game with the following utilities: {}"
|
|
911
|
+
return base_str.format(pformat(self.utilities))
|
|
912
|
+
|
|
913
|
+
def _latex_(self) -> str:
|
|
914
|
+
r"""
|
|
915
|
+
Return the LaTeX code representing the ``NormalFormGame``.
|
|
916
|
+
|
|
917
|
+
EXAMPLES:
|
|
918
|
+
|
|
919
|
+
LaTeX method shows the two payoff matrices for a two player game::
|
|
920
|
+
|
|
921
|
+
sage: A = matrix([[-1, -2], [-12, 2]])
|
|
922
|
+
sage: B = matrix([[1, 0], [1, -1]])
|
|
923
|
+
sage: g = NormalFormGame([A, B])
|
|
924
|
+
sage: latex(g)
|
|
925
|
+
\left(\left(\begin{array}{rr}
|
|
926
|
+
-1 & -2 \\
|
|
927
|
+
-12 & 2
|
|
928
|
+
\end{array}\right), \left(\begin{array}{rr}
|
|
929
|
+
1 & 0 \\
|
|
930
|
+
1 & -1
|
|
931
|
+
\end{array}\right)\right)
|
|
932
|
+
|
|
933
|
+
LaTeX method shows nothing interesting for games with more players::
|
|
934
|
+
|
|
935
|
+
sage: g = NormalFormGame()
|
|
936
|
+
sage: g.add_player(2) # Adding first player with 2 strategies
|
|
937
|
+
sage: g.add_player(2) # Adding second player with 2 strategies
|
|
938
|
+
sage: g.add_player(2) # Creating a game with three players
|
|
939
|
+
sage: latex(g)
|
|
940
|
+
\begin{array}{l}
|
|
941
|
+
\text{\texttt{Normal{ }Form{ }Game{ }with{ }the{ }...
|
|
942
|
+
...
|
|
943
|
+
\end{array}
|
|
944
|
+
"""
|
|
945
|
+
if len(self.players) == 2:
|
|
946
|
+
M1, M2 = self.payoff_matrices()
|
|
947
|
+
return r"\left(%s, %s\right)" % (M1._latex_(), M2._latex_())
|
|
948
|
+
return latex(str(self))
|
|
949
|
+
|
|
950
|
+
def _two_matrix_game(self, matrices):
|
|
951
|
+
r"""
|
|
952
|
+
Populate ``self.utilities`` with the values from 2 matrices.
|
|
953
|
+
|
|
954
|
+
EXAMPLES:
|
|
955
|
+
|
|
956
|
+
A small example game::
|
|
957
|
+
|
|
958
|
+
sage: A = matrix([[1, 0], [-2, 3]])
|
|
959
|
+
sage: B = matrix([[3, 2], [-1, 0]])
|
|
960
|
+
sage: two_game = NormalFormGame()
|
|
961
|
+
sage: two_game._two_matrix_game([A, B])
|
|
962
|
+
"""
|
|
963
|
+
self.players = []
|
|
964
|
+
self.utilities = {}
|
|
965
|
+
self.add_player(matrices[0].dimensions()[0])
|
|
966
|
+
self.add_player(matrices[1].dimensions()[1])
|
|
967
|
+
for strategy_profile in self.utilities:
|
|
968
|
+
self.utilities[strategy_profile] = [matrices[0][strategy_profile],
|
|
969
|
+
matrices[1][strategy_profile]]
|
|
970
|
+
|
|
971
|
+
def _gambit_game(self, game):
|
|
972
|
+
r"""
|
|
973
|
+
Create a ``NormalFormGame`` object from a Gambit game.
|
|
974
|
+
|
|
975
|
+
TESTS::
|
|
976
|
+
|
|
977
|
+
sage: # optional - gambit
|
|
978
|
+
sage: from gambit import Game
|
|
979
|
+
sage: testgame = Game.new_table([2, 2])
|
|
980
|
+
sage: testgame[int(0), int(0)][int(0)] = int(8)
|
|
981
|
+
sage: testgame[int(0), int(0)][int(1)] = int(8)
|
|
982
|
+
sage: testgame[int(0), int(1)][int(0)] = int(2)
|
|
983
|
+
sage: testgame[int(0), int(1)][int(1)] = int(10)
|
|
984
|
+
sage: testgame[int(1), int(0)][int(0)] = int(10)
|
|
985
|
+
sage: testgame[int(1), int(0)][int(1)] = int(2)
|
|
986
|
+
sage: testgame[int(1), int(1)][int(0)] = int(5)
|
|
987
|
+
sage: testgame[int(1), int(1)][int(1)] = int(5)
|
|
988
|
+
sage: g = NormalFormGame()
|
|
989
|
+
sage: g._gambit_game(testgame); g
|
|
990
|
+
Normal Form Game with the following utilities: {(0, 0): [8.0, 8.0],
|
|
991
|
+
(0, 1): [2.0, 10.0],
|
|
992
|
+
(1, 0): [10.0, 2.0],
|
|
993
|
+
(1, 1): [5.0, 5.0]}
|
|
994
|
+
"""
|
|
995
|
+
self.players = []
|
|
996
|
+
self.utilities = {}
|
|
997
|
+
for player in game.players:
|
|
998
|
+
num_strategies = len(player.strategies)
|
|
999
|
+
self.add_player(num_strategies)
|
|
1000
|
+
for strategy_profile in self.utilities:
|
|
1001
|
+
utility_vector = [float(game[strategy_profile][i]) for i in range(len(self.players))]
|
|
1002
|
+
self.utilities[strategy_profile] = utility_vector
|
|
1003
|
+
|
|
1004
|
+
def _gambit_(self, as_integer=False, maximization=True):
|
|
1005
|
+
r"""
|
|
1006
|
+
Create a Gambit game from a ``NormalFormGame`` object.
|
|
1007
|
+
|
|
1008
|
+
INPUT:
|
|
1009
|
+
|
|
1010
|
+
- ``as_integer`` -- boolean; whether the gambit representation
|
|
1011
|
+
should have the payoffs represented as integers or decimals
|
|
1012
|
+
|
|
1013
|
+
- ``maximization`` -- boolean; whether a player is trying to
|
|
1014
|
+
maximize their utility or minimize it
|
|
1015
|
+
|
|
1016
|
+
TESTS::
|
|
1017
|
+
|
|
1018
|
+
sage: # optional - gambit
|
|
1019
|
+
sage: from gambit import Game
|
|
1020
|
+
sage: A = matrix([[2, 1], [1, 2.5]])
|
|
1021
|
+
sage: g = NormalFormGame([A])
|
|
1022
|
+
sage: gg = g._gambit_(); gg
|
|
1023
|
+
NFG 1 R "" { "1" "2" }
|
|
1024
|
+
<BLANKLINE>
|
|
1025
|
+
{ { "1" "2" }
|
|
1026
|
+
{ "1" "2" }
|
|
1027
|
+
}
|
|
1028
|
+
""
|
|
1029
|
+
<BLANKLINE>
|
|
1030
|
+
{
|
|
1031
|
+
{ "" 2, -2 }
|
|
1032
|
+
{ "" 1, -1 }
|
|
1033
|
+
{ "" 1, -1 }
|
|
1034
|
+
{ "" 2.5, -2.5 }
|
|
1035
|
+
}
|
|
1036
|
+
1 2 3 4
|
|
1037
|
+
<BLANKLINE>
|
|
1038
|
+
sage: gg = g._gambit_(as_integer=True); gg
|
|
1039
|
+
NFG 1 R "" { "1" "2" }
|
|
1040
|
+
<BLANKLINE>
|
|
1041
|
+
{ { "1" "2" }
|
|
1042
|
+
{ "1" "2" }
|
|
1043
|
+
}
|
|
1044
|
+
""
|
|
1045
|
+
<BLANKLINE>
|
|
1046
|
+
{
|
|
1047
|
+
{ "" 2, -2 }
|
|
1048
|
+
{ "" 1, -1 }
|
|
1049
|
+
{ "" 1, -1 }
|
|
1050
|
+
{ "" 2, -2 }
|
|
1051
|
+
}
|
|
1052
|
+
1 2 3 4
|
|
1053
|
+
<BLANKLINE>
|
|
1054
|
+
|
|
1055
|
+
::
|
|
1056
|
+
|
|
1057
|
+
sage: # optional - gambit
|
|
1058
|
+
sage: A = matrix([[2, 1], [1, 2.5]])
|
|
1059
|
+
sage: B = matrix([[3, 2], [5.5, 4]])
|
|
1060
|
+
sage: g = NormalFormGame([A, B])
|
|
1061
|
+
sage: gg = g._gambit_(); gg
|
|
1062
|
+
NFG 1 R "" { "1" "2" }
|
|
1063
|
+
<BLANKLINE>
|
|
1064
|
+
{ { "1" "2" }
|
|
1065
|
+
{ "1" "2" }
|
|
1066
|
+
}
|
|
1067
|
+
""
|
|
1068
|
+
<BLANKLINE>
|
|
1069
|
+
{
|
|
1070
|
+
{ "" 2, 3 }
|
|
1071
|
+
{ "" 1, 5.5 }
|
|
1072
|
+
{ "" 1, 2 }
|
|
1073
|
+
{ "" 2.5, 4 }
|
|
1074
|
+
}
|
|
1075
|
+
1 2 3 4
|
|
1076
|
+
<BLANKLINE>
|
|
1077
|
+
sage: gg = g._gambit_(as_integer = True); gg
|
|
1078
|
+
NFG 1 R "" { "1" "2" }
|
|
1079
|
+
<BLANKLINE>
|
|
1080
|
+
{ { "1" "2" }
|
|
1081
|
+
{ "1" "2" }
|
|
1082
|
+
}
|
|
1083
|
+
""
|
|
1084
|
+
<BLANKLINE>
|
|
1085
|
+
{
|
|
1086
|
+
{ "" 2, 3 }
|
|
1087
|
+
{ "" 1, 5 }
|
|
1088
|
+
{ "" 1, 2 }
|
|
1089
|
+
{ "" 2, 4 }
|
|
1090
|
+
}
|
|
1091
|
+
1 2 3 4
|
|
1092
|
+
<BLANKLINE>
|
|
1093
|
+
|
|
1094
|
+
::
|
|
1095
|
+
|
|
1096
|
+
sage: # optional - gambit
|
|
1097
|
+
sage: threegame = NormalFormGame()
|
|
1098
|
+
sage: threegame.add_player(2)
|
|
1099
|
+
sage: threegame.add_player(2)
|
|
1100
|
+
sage: threegame.add_player(2)
|
|
1101
|
+
sage: threegame[0, 0, 0][0] = 3
|
|
1102
|
+
sage: threegame[0, 0, 0][1] = 1
|
|
1103
|
+
sage: threegame[0, 0, 0][2] = 4
|
|
1104
|
+
sage: threegame[0, 0, 1][0] = 1
|
|
1105
|
+
sage: threegame[0, 0, 1][1] = 5
|
|
1106
|
+
sage: threegame[0, 0, 1][2] = 9
|
|
1107
|
+
sage: threegame[0, 1, 0][0] = 2
|
|
1108
|
+
sage: threegame[0, 1, 0][1] = 6
|
|
1109
|
+
sage: threegame[0, 1, 0][2] = 5
|
|
1110
|
+
sage: threegame[0, 1, 1][0] = 3
|
|
1111
|
+
sage: threegame[0, 1, 1][1] = 5
|
|
1112
|
+
sage: threegame[0, 1, 1][2] = 8
|
|
1113
|
+
sage: threegame[1, 0, 0][0] = 9
|
|
1114
|
+
sage: threegame[1, 0, 0][1] = 7
|
|
1115
|
+
sage: threegame[1, 0, 0][2] = 9
|
|
1116
|
+
sage: threegame[1, 0, 1][0] = 3
|
|
1117
|
+
sage: threegame[1, 0, 1][1] = 2
|
|
1118
|
+
sage: threegame[1, 0, 1][2] = 3
|
|
1119
|
+
sage: threegame[1, 1, 0][0] = 8
|
|
1120
|
+
sage: threegame[1, 1, 0][1] = 4
|
|
1121
|
+
sage: threegame[1, 1, 0][2] = 6
|
|
1122
|
+
sage: threegame[1, 1, 1][0] = 2
|
|
1123
|
+
sage: threegame[1, 1, 1][1] = 6
|
|
1124
|
+
sage: threegame[1, 1, 1][2] = 4
|
|
1125
|
+
sage: threegame._gambit_(as_integer = True)
|
|
1126
|
+
NFG 1 R "" { "1" "2" "3" }
|
|
1127
|
+
<BLANKLINE>
|
|
1128
|
+
{ { "1" "2" }
|
|
1129
|
+
{ "1" "2" }
|
|
1130
|
+
{ "1" "2" }
|
|
1131
|
+
}
|
|
1132
|
+
""
|
|
1133
|
+
<BLANKLINE>
|
|
1134
|
+
{
|
|
1135
|
+
{ "" 3, 1, 4 }
|
|
1136
|
+
{ "" 9, 7, 9 }
|
|
1137
|
+
{ "" 2, 6, 5 }
|
|
1138
|
+
{ "" 8, 4, 6 }
|
|
1139
|
+
{ "" 1, 5, 9 }
|
|
1140
|
+
{ "" 3, 2, 3 }
|
|
1141
|
+
{ "" 3, 5, 8 }
|
|
1142
|
+
{ "" 2, 6, 4 }
|
|
1143
|
+
}
|
|
1144
|
+
1 2 3 4 5 6 7 8
|
|
1145
|
+
<BLANKLINE>
|
|
1146
|
+
"""
|
|
1147
|
+
from decimal import Decimal
|
|
1148
|
+
strategy_sizes = [p.num_strategies for p in self.players]
|
|
1149
|
+
g = Game.new_table(strategy_sizes)
|
|
1150
|
+
|
|
1151
|
+
sgn = 1
|
|
1152
|
+
if not maximization:
|
|
1153
|
+
sgn = -1
|
|
1154
|
+
|
|
1155
|
+
players = len(strategy_sizes)
|
|
1156
|
+
|
|
1157
|
+
for strategy_profile in self.utilities:
|
|
1158
|
+
for i in range(players):
|
|
1159
|
+
if as_integer:
|
|
1160
|
+
g[strategy_profile][i] = sgn * int(self.utilities[strategy_profile][i])
|
|
1161
|
+
else:
|
|
1162
|
+
g[strategy_profile][i] = sgn * Decimal(float(self.utilities[strategy_profile][i]))
|
|
1163
|
+
return g
|
|
1164
|
+
|
|
1165
|
+
def is_constant_sum(self):
|
|
1166
|
+
r"""
|
|
1167
|
+
Check if the game is constant sum.
|
|
1168
|
+
|
|
1169
|
+
EXAMPLES::
|
|
1170
|
+
|
|
1171
|
+
sage: A = matrix([[2, 1], [1, 2.5]])
|
|
1172
|
+
sage: g = NormalFormGame([A])
|
|
1173
|
+
sage: g.is_constant_sum()
|
|
1174
|
+
True
|
|
1175
|
+
sage: g = NormalFormGame([A, A])
|
|
1176
|
+
sage: g.is_constant_sum()
|
|
1177
|
+
False
|
|
1178
|
+
sage: A = matrix([[1, 1], [1, 1]])
|
|
1179
|
+
sage: g = NormalFormGame([A, A])
|
|
1180
|
+
sage: g.is_constant_sum()
|
|
1181
|
+
True
|
|
1182
|
+
sage: A = matrix([[1, 1, 2], [1, 1, -1], [1, -1, 1]])
|
|
1183
|
+
sage: B = matrix([[2, 2, 1], [2, 2, 4], [2, 4, 2]])
|
|
1184
|
+
sage: g = NormalFormGame([A, B])
|
|
1185
|
+
sage: g.is_constant_sum()
|
|
1186
|
+
True
|
|
1187
|
+
sage: A = matrix([[1, 1, 2], [1, 1, -1], [1, -1, 1]])
|
|
1188
|
+
sage: B = matrix([[2, 2, 1], [2, 2.1, 4], [2, 4, 2]])
|
|
1189
|
+
sage: g = NormalFormGame([A, B])
|
|
1190
|
+
sage: g.is_constant_sum()
|
|
1191
|
+
False
|
|
1192
|
+
"""
|
|
1193
|
+
import sys
|
|
1194
|
+
if len(self.players) > 2:
|
|
1195
|
+
return False
|
|
1196
|
+
m1, m2 = self.payoff_matrices()
|
|
1197
|
+
c = m1 + m2
|
|
1198
|
+
t = c[0, 0]
|
|
1199
|
+
|
|
1200
|
+
for row in c:
|
|
1201
|
+
for i in row:
|
|
1202
|
+
if abs(t - i) > sys.float_info.epsilon:
|
|
1203
|
+
return False
|
|
1204
|
+
|
|
1205
|
+
return True
|
|
1206
|
+
|
|
1207
|
+
def payoff_matrices(self):
|
|
1208
|
+
r"""
|
|
1209
|
+
Return 2 matrices representing the payoffs for each player.
|
|
1210
|
+
|
|
1211
|
+
EXAMPLES::
|
|
1212
|
+
|
|
1213
|
+
sage: p1 = matrix([[1, 2], [3, 4]])
|
|
1214
|
+
sage: p2 = matrix([[3, 3], [1, 4]])
|
|
1215
|
+
sage: g = NormalFormGame([p1, p2])
|
|
1216
|
+
sage: g.payoff_matrices()
|
|
1217
|
+
(
|
|
1218
|
+
[1 2] [3 3]
|
|
1219
|
+
[3 4], [1 4]
|
|
1220
|
+
)
|
|
1221
|
+
|
|
1222
|
+
If we create a game with 3 players we will not be able to
|
|
1223
|
+
obtain payoff matrices::
|
|
1224
|
+
|
|
1225
|
+
sage: g = NormalFormGame()
|
|
1226
|
+
sage: g.add_player(2) # adding first player with 2 strategies
|
|
1227
|
+
sage: g.add_player(2) # adding second player with 2 strategies
|
|
1228
|
+
sage: g.add_player(2) # adding third player with 2 strategies
|
|
1229
|
+
sage: g.payoff_matrices()
|
|
1230
|
+
Traceback (most recent call last):
|
|
1231
|
+
...
|
|
1232
|
+
ValueError: Only available for 2 player games
|
|
1233
|
+
|
|
1234
|
+
If we do create a two player game but it is not complete
|
|
1235
|
+
then an error is also raised::
|
|
1236
|
+
|
|
1237
|
+
sage: g = NormalFormGame()
|
|
1238
|
+
sage: g.add_player(1) # Adding first player with 1 strategy
|
|
1239
|
+
sage: g.add_player(1) # Adding second player with 1 strategy
|
|
1240
|
+
sage: g.payoff_matrices()
|
|
1241
|
+
Traceback (most recent call last):
|
|
1242
|
+
...
|
|
1243
|
+
ValueError: utilities have not been populated
|
|
1244
|
+
|
|
1245
|
+
The above creates a 2 player game where each player has
|
|
1246
|
+
a single strategy. Here we populate the strategies and
|
|
1247
|
+
can then view the payoff matrices::
|
|
1248
|
+
|
|
1249
|
+
sage: g[0, 0] = [1,2]
|
|
1250
|
+
sage: g.payoff_matrices()
|
|
1251
|
+
([1], [2])
|
|
1252
|
+
"""
|
|
1253
|
+
if len(self.players) != 2:
|
|
1254
|
+
raise ValueError("Only available for 2 player games")
|
|
1255
|
+
|
|
1256
|
+
if not self._is_complete():
|
|
1257
|
+
raise ValueError("utilities have not been populated")
|
|
1258
|
+
|
|
1259
|
+
m1 = matrix(QQ, self.players[0].num_strategies, self.players[1].num_strategies)
|
|
1260
|
+
m2 = matrix(QQ, self.players[0].num_strategies, self.players[1].num_strategies)
|
|
1261
|
+
for strategy_profile in self.utilities:
|
|
1262
|
+
m1[strategy_profile] = self[strategy_profile][0]
|
|
1263
|
+
m2[strategy_profile] = self[strategy_profile][1]
|
|
1264
|
+
return m1, m2
|
|
1265
|
+
|
|
1266
|
+
def add_player(self, num_strategies):
|
|
1267
|
+
r"""
|
|
1268
|
+
Add a player to a NormalFormGame.
|
|
1269
|
+
|
|
1270
|
+
INPUT:
|
|
1271
|
+
|
|
1272
|
+
- ``num_strategies`` -- the number of strategies the player should have
|
|
1273
|
+
|
|
1274
|
+
EXAMPLES::
|
|
1275
|
+
|
|
1276
|
+
sage: g = NormalFormGame()
|
|
1277
|
+
sage: g.add_player(2) # Adding first player with 2 strategies
|
|
1278
|
+
sage: g.add_player(1) # Adding second player with 1 strategy
|
|
1279
|
+
sage: g.add_player(1) # Adding third player with 1 strategy
|
|
1280
|
+
sage: g
|
|
1281
|
+
Normal Form Game with the following utilities:
|
|
1282
|
+
{(0, 0, 0): [False, False, False],
|
|
1283
|
+
(1, 0, 0): [False, False, False]}
|
|
1284
|
+
"""
|
|
1285
|
+
self.players.append(_Player(num_strategies))
|
|
1286
|
+
self._generate_utilities(True)
|
|
1287
|
+
|
|
1288
|
+
def _generate_utilities(self, replacement):
|
|
1289
|
+
r"""
|
|
1290
|
+
Create all the required keys for ``self.utilities``.
|
|
1291
|
+
|
|
1292
|
+
This is used when generating players and/or adding strategies.
|
|
1293
|
+
|
|
1294
|
+
INPUT:
|
|
1295
|
+
|
|
1296
|
+
- ``replacement`` -- boolean value of whether previously created
|
|
1297
|
+
profiles should be replaced or not
|
|
1298
|
+
|
|
1299
|
+
TESTS::
|
|
1300
|
+
|
|
1301
|
+
sage: from sage.game_theory.normal_form_game import _Player
|
|
1302
|
+
sage: g = NormalFormGame()
|
|
1303
|
+
sage: g.players.append(_Player(2))
|
|
1304
|
+
sage: g.players.append(_Player(2))
|
|
1305
|
+
sage: g
|
|
1306
|
+
Normal Form Game with the following utilities: {}
|
|
1307
|
+
|
|
1308
|
+
sage: g._generate_utilities(True)
|
|
1309
|
+
sage: g
|
|
1310
|
+
Normal Form Game with the following utilities: {(0, 0): [False, False],
|
|
1311
|
+
(0, 1): [False, False],
|
|
1312
|
+
(1, 0): [False, False],
|
|
1313
|
+
(1, 1): [False, False]}
|
|
1314
|
+
|
|
1315
|
+
sage: g[(0,1)] = [2, 3]
|
|
1316
|
+
sage: g.add_strategy(1)
|
|
1317
|
+
sage: g._generate_utilities(False)
|
|
1318
|
+
sage: g
|
|
1319
|
+
Normal Form Game with the following utilities: {(0, 0): [False, False],
|
|
1320
|
+
(0, 1): [2, 3],
|
|
1321
|
+
(0, 2): [False, False],
|
|
1322
|
+
(1, 0): [False, False],
|
|
1323
|
+
(1, 1): [False, False],
|
|
1324
|
+
(1, 2): [False, False]}
|
|
1325
|
+
|
|
1326
|
+
sage: g._generate_utilities(True)
|
|
1327
|
+
sage: g
|
|
1328
|
+
Normal Form Game with the following utilities: {(0, 0): [False, False],
|
|
1329
|
+
(0, 1): [False, False],
|
|
1330
|
+
(0, 2): [False, False],
|
|
1331
|
+
(1, 0): [False, False],
|
|
1332
|
+
(1, 1): [False, False],
|
|
1333
|
+
(1, 2): [False, False]}
|
|
1334
|
+
"""
|
|
1335
|
+
strategy_sizes = [range(p.num_strategies) for p in self.players]
|
|
1336
|
+
if replacement is True:
|
|
1337
|
+
self.utilities = {}
|
|
1338
|
+
for profile in product(*strategy_sizes):
|
|
1339
|
+
if profile not in self.utilities.keys():
|
|
1340
|
+
self.utilities[profile] = [False] * len(self.players)
|
|
1341
|
+
|
|
1342
|
+
def add_strategy(self, player):
|
|
1343
|
+
r"""
|
|
1344
|
+
Add a strategy to a player, will not affect already completed
|
|
1345
|
+
strategy profiles.
|
|
1346
|
+
|
|
1347
|
+
INPUT:
|
|
1348
|
+
|
|
1349
|
+
- ``player`` -- the index of the player
|
|
1350
|
+
|
|
1351
|
+
EXAMPLES:
|
|
1352
|
+
|
|
1353
|
+
A simple example::
|
|
1354
|
+
|
|
1355
|
+
sage: s = matrix([[1, 0], [-2, 3]])
|
|
1356
|
+
sage: t = matrix([[3, 2], [-1, 0]])
|
|
1357
|
+
sage: example = NormalFormGame([s, t])
|
|
1358
|
+
sage: example
|
|
1359
|
+
Normal Form Game with the following utilities: {(0, 0): [1, 3],
|
|
1360
|
+
(0, 1): [0, 2], (1, 0): [-2, -1], (1, 1): [3, 0]}
|
|
1361
|
+
sage: example.add_strategy(0)
|
|
1362
|
+
sage: example
|
|
1363
|
+
Normal Form Game with the following utilities: {(0, 0): [1, 3],
|
|
1364
|
+
(0, 1): [0, 2],
|
|
1365
|
+
(1, 0): [-2, -1],
|
|
1366
|
+
(1, 1): [3, 0],
|
|
1367
|
+
(2, 0): [False, False],
|
|
1368
|
+
(2, 1): [False, False]}
|
|
1369
|
+
"""
|
|
1370
|
+
self.players[player].add_strategy()
|
|
1371
|
+
self._generate_utilities(False)
|
|
1372
|
+
|
|
1373
|
+
def _is_complete(self):
|
|
1374
|
+
r"""
|
|
1375
|
+
Check if ``utilities`` has been completed and return a
|
|
1376
|
+
boolean.
|
|
1377
|
+
|
|
1378
|
+
EXAMPLES:
|
|
1379
|
+
|
|
1380
|
+
A simple example::
|
|
1381
|
+
|
|
1382
|
+
sage: s = matrix([[1, 0], [-2, 3]])
|
|
1383
|
+
sage: t = matrix([[3, 2], [-1, 0]])
|
|
1384
|
+
sage: example = NormalFormGame([s, t])
|
|
1385
|
+
sage: example.add_strategy(0)
|
|
1386
|
+
sage: example._is_complete()
|
|
1387
|
+
False
|
|
1388
|
+
"""
|
|
1389
|
+
results = (all(not isinstance(i, bool) for i in profile)
|
|
1390
|
+
for profile in self.utilities.values())
|
|
1391
|
+
return all(results)
|
|
1392
|
+
|
|
1393
|
+
def obtain_nash(self, algorithm=False, maximization=True, solver=None):
|
|
1394
|
+
r"""
|
|
1395
|
+
A function to return the Nash equilibrium for the game.
|
|
1396
|
+
Optional arguments can be used to specify the algorithm used.
|
|
1397
|
+
If no algorithm is passed then an attempt is made to use the most
|
|
1398
|
+
appropriate algorithm.
|
|
1399
|
+
|
|
1400
|
+
INPUT:
|
|
1401
|
+
|
|
1402
|
+
- ``algorithm`` -- the following algorithms should be available through
|
|
1403
|
+
this function:
|
|
1404
|
+
|
|
1405
|
+
* ``'lrs'`` -- this algorithm is only suited for 2 player games.
|
|
1406
|
+
See the lrs web site (http://cgm.cs.mcgill.ca/~avis/C/lrs.html).
|
|
1407
|
+
|
|
1408
|
+
* ``'LCP'`` -- this algorithm is only suited for 2 player games.
|
|
1409
|
+
See the gambit web site (http://gambit.sourceforge.net/).
|
|
1410
|
+
|
|
1411
|
+
* ``'lp'`` -- this algorithm is only suited for 2 player
|
|
1412
|
+
constant sum games. Uses MILP solver determined by the
|
|
1413
|
+
``solver`` argument.
|
|
1414
|
+
|
|
1415
|
+
* ``'enumeration'`` -- this is a very inefficient
|
|
1416
|
+
algorithm (in essence a brute force approach).
|
|
1417
|
+
|
|
1418
|
+
1. For each k in 1...min(size of strategy sets)
|
|
1419
|
+
2. For each I,J supports of size k
|
|
1420
|
+
3. Prune: check if supports are dominated
|
|
1421
|
+
4. Solve indifference conditions and check that have Nash Equilibrium.
|
|
1422
|
+
|
|
1423
|
+
Solving the indifference conditions is done by building the
|
|
1424
|
+
corresponding linear system. If `\rho_1, \rho_2` are the
|
|
1425
|
+
supports player 1 and 2 respectively. Then, indifference implies:
|
|
1426
|
+
|
|
1427
|
+
.. MATH::
|
|
1428
|
+
|
|
1429
|
+
u_1(s_1,\rho_2) = u_1(s_2, \rho_2)
|
|
1430
|
+
|
|
1431
|
+
for all `s_1, s_2` in the support of `\rho_1`. This corresponds to:
|
|
1432
|
+
|
|
1433
|
+
.. MATH::
|
|
1434
|
+
|
|
1435
|
+
\sum_{j\in S(\rho_2)}A_{s_1,j}{\rho_2}_j = \sum_{j\in S(\rho_2)}A_{s_2,j}{\rho_2}_j
|
|
1436
|
+
|
|
1437
|
+
for all `s_1, s_2` in the support of `\rho_1` where `A` is the payoff
|
|
1438
|
+
matrix of player 1. Equivalently we can consider consecutive rows of
|
|
1439
|
+
`A` (instead of all pairs of strategies). Thus the corresponding
|
|
1440
|
+
linear system can be written as:
|
|
1441
|
+
|
|
1442
|
+
.. MATH::
|
|
1443
|
+
|
|
1444
|
+
\left(\sum_{j \in S(\rho_2)}A_{i,j} - A_{i+1,j}\right){\rho_2}_j
|
|
1445
|
+
|
|
1446
|
+
for all `1\leq i \leq |S(\rho_1)|` (where `A` has been modified to only
|
|
1447
|
+
contain the rows corresponding to `S(\rho_1)`). We also require all
|
|
1448
|
+
elements of `\rho_2` to sum to 1:
|
|
1449
|
+
|
|
1450
|
+
.. MATH::
|
|
1451
|
+
|
|
1452
|
+
\sum_{j\in S(\rho_1)}{\rho_2}_j = 1
|
|
1453
|
+
|
|
1454
|
+
- ``maximization`` -- boolean (default: ``True``); whether a player is
|
|
1455
|
+
trying to maximize their utility or minimize it:
|
|
1456
|
+
|
|
1457
|
+
* When set to ``True`` it is assumed that players aim to
|
|
1458
|
+
maximise their utility.
|
|
1459
|
+
|
|
1460
|
+
* When set to ``False`` it is assumed that players aim to
|
|
1461
|
+
minimise their utility.
|
|
1462
|
+
|
|
1463
|
+
- ``solver`` -- (optional) see :class:`MixedIntegerLinearProgram`
|
|
1464
|
+
for more information on the MILP solvers in Sage, may also
|
|
1465
|
+
be ``'gambit'`` to use the MILP solver included with the gambit
|
|
1466
|
+
library. Note that ``None`` means to use the default Sage LP solver,
|
|
1467
|
+
normally GLPK.
|
|
1468
|
+
|
|
1469
|
+
EXAMPLES:
|
|
1470
|
+
|
|
1471
|
+
A game with 1 equilibrium when ``maximization`` is ``True`` and 3 when
|
|
1472
|
+
``maximization`` is ``False``::
|
|
1473
|
+
|
|
1474
|
+
sage: A = matrix([[10, 500, 44],
|
|
1475
|
+
....: [15, 10, 105],
|
|
1476
|
+
....: [19, 204, 55],
|
|
1477
|
+
....: [20, 200, 590]])
|
|
1478
|
+
sage: B = matrix([[2, 1, 2],
|
|
1479
|
+
....: [0, 5, 6],
|
|
1480
|
+
....: [3, 4, 1],
|
|
1481
|
+
....: [4, 1, 20]])
|
|
1482
|
+
sage: g=NormalFormGame([A, B])
|
|
1483
|
+
sage: g.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
1484
|
+
[[(0, 0, 0, 1), (0, 0, 1)]]
|
|
1485
|
+
sage: g.obtain_nash(algorithm='lrs', maximization=False) # optional - lrslib
|
|
1486
|
+
[[(2/3, 1/12, 1/4, 0), (6333/8045, 247/8045, 293/1609)],
|
|
1487
|
+
[(3/4, 0, 1/4, 0), (0, 11/307, 296/307)],
|
|
1488
|
+
[(5/6, 1/6, 0, 0), (98/99, 1/99, 0)]]
|
|
1489
|
+
|
|
1490
|
+
This particular game has 3 Nash equilibria::
|
|
1491
|
+
|
|
1492
|
+
sage: A = matrix([[3,3],
|
|
1493
|
+
....: [2,5],
|
|
1494
|
+
....: [0,6]])
|
|
1495
|
+
sage: B = matrix([[3,2],
|
|
1496
|
+
....: [2,6],
|
|
1497
|
+
....: [3,1]])
|
|
1498
|
+
sage: g = NormalFormGame([A, B])
|
|
1499
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
1500
|
+
[[(0, 1/3, 2/3), (1/3, 2/3)],
|
|
1501
|
+
[(4/5, 1/5, 0), (2/3, 1/3)],
|
|
1502
|
+
[(1, 0, 0), (1, 0)]]
|
|
1503
|
+
|
|
1504
|
+
Here is a slightly larger game::
|
|
1505
|
+
|
|
1506
|
+
sage: A = matrix([[160, 205, 44],
|
|
1507
|
+
....: [175, 180, 45],
|
|
1508
|
+
....: [201, 204, 50],
|
|
1509
|
+
....: [120, 207, 49]])
|
|
1510
|
+
sage: B = matrix([[2, 2, 2],
|
|
1511
|
+
....: [1, 0, 0],
|
|
1512
|
+
....: [3, 4, 1],
|
|
1513
|
+
....: [4, 1, 2]])
|
|
1514
|
+
sage: g=NormalFormGame([A, B])
|
|
1515
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
1516
|
+
[[(0, 0, 3/4, 1/4), (1/28, 27/28, 0)]]
|
|
1517
|
+
sage: g.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
1518
|
+
[[(0, 0, 3/4, 1/4), (1/28, 27/28, 0)]]
|
|
1519
|
+
sage: g.obtain_nash(algorithm='LCP') # optional - gambit
|
|
1520
|
+
[[(0.0, 0.0, 0.75, 0.25), (0.0357142857, 0.9642857143, 0.0)]]
|
|
1521
|
+
|
|
1522
|
+
2 random matrices::
|
|
1523
|
+
|
|
1524
|
+
sage: player1 = matrix([[2, 8, -1, 1, 0],
|
|
1525
|
+
....: [1, 1, 2, 1, 80],
|
|
1526
|
+
....: [0, 2, 15, 0, -12],
|
|
1527
|
+
....: [-2, -2, 1, -20, -1],
|
|
1528
|
+
....: [1, -2, -1, -2, 1]])
|
|
1529
|
+
sage: player2 = matrix([[0, 8, 4, 2, -1],
|
|
1530
|
+
....: [6, 14, -5, 1, 0],
|
|
1531
|
+
....: [0, -2, -1, 8, -1],
|
|
1532
|
+
....: [1, -1, 3, -3, 2],
|
|
1533
|
+
....: [8, -4, 1, 1, -17]])
|
|
1534
|
+
sage: fivegame = NormalFormGame([player1, player2])
|
|
1535
|
+
sage: fivegame.obtain_nash(algorithm='enumeration')
|
|
1536
|
+
[[(1, 0, 0, 0, 0), (0, 1, 0, 0, 0)]]
|
|
1537
|
+
sage: fivegame.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
1538
|
+
[[(1, 0, 0, 0, 0), (0, 1, 0, 0, 0)]]
|
|
1539
|
+
sage: fivegame.obtain_nash(algorithm='LCP') # optional - gambit
|
|
1540
|
+
[[(1.0, 0.0, 0.0, 0.0, 0.0), (0.0, 1.0, 0.0, 0.0, 0.0)]]
|
|
1541
|
+
|
|
1542
|
+
Here are some examples of finding Nash equilibria for constant-sum games::
|
|
1543
|
+
|
|
1544
|
+
sage: A = matrix.identity(2)
|
|
1545
|
+
sage: cg = NormalFormGame([A])
|
|
1546
|
+
sage: cg.obtain_nash(algorithm='lp')
|
|
1547
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1548
|
+
sage: cg.obtain_nash(algorithm='lp', solver='Coin') # optional - sage_numerical_backends_coin
|
|
1549
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1550
|
+
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
|
|
1551
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
1552
|
+
sage: cg.obtain_nash(algorithm='lp', solver='gambit') # optional - gambit
|
|
1553
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1554
|
+
sage: A = matrix([[2, 1], [1, 3]])
|
|
1555
|
+
sage: cg = NormalFormGame([A])
|
|
1556
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='glpk')
|
|
1557
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]
|
|
1558
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1559
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='Coin') # optional - sage_numerical_backends_coin
|
|
1560
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - sage_numerical_backends_coin
|
|
1561
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1562
|
+
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
|
|
1563
|
+
[[(2/3, 1/3), (2/3, 1/3)]]
|
|
1564
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='gambit') # optional - gambit
|
|
1565
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - gambit
|
|
1566
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1567
|
+
sage: A = matrix([[1, 2, 1], [1, 1, 2], [2, 1, 1]])
|
|
1568
|
+
sage: B = matrix([[2, 1, 2], [2, 2, 1], [1, 2, 2]])
|
|
1569
|
+
sage: cg = NormalFormGame([A, B])
|
|
1570
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='glpk')
|
|
1571
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]
|
|
1572
|
+
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
|
|
1573
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='Coin') # optional - sage_numerical_backends_coin
|
|
1574
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - sage_numerical_backends_coin
|
|
1575
|
+
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
|
|
1576
|
+
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
|
|
1577
|
+
[[(1/3, 1/3, 1/3), (1/3, 1/3, 1/3)]]
|
|
1578
|
+
sage: ne = cg.obtain_nash(algorithm='lp', solver='gambit') # optional - gambit
|
|
1579
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - gambit
|
|
1580
|
+
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
|
|
1581
|
+
sage: A = matrix([[160, 205, 44],
|
|
1582
|
+
....: [175, 180, 45],
|
|
1583
|
+
....: [201, 204, 50],
|
|
1584
|
+
....: [120, 207, 49]])
|
|
1585
|
+
sage: cg = NormalFormGame([A])
|
|
1586
|
+
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
|
|
1587
|
+
[[(0, 0, 1, 0), (0, 0, 1)]]
|
|
1588
|
+
|
|
1589
|
+
Running the constant-sum solver on a game which is not a constant sum
|
|
1590
|
+
game generates a :exc:`ValueError`::
|
|
1591
|
+
|
|
1592
|
+
sage: cg = NormalFormGame([A, A])
|
|
1593
|
+
sage: cg.obtain_nash(algorithm='lp', solver='glpk')
|
|
1594
|
+
Traceback (most recent call last):
|
|
1595
|
+
...
|
|
1596
|
+
ValueError: Input game needs to be a two player constant sum game
|
|
1597
|
+
|
|
1598
|
+
Here is an example of a 3 by 2 game with 3 Nash equilibrium::
|
|
1599
|
+
|
|
1600
|
+
sage: A = matrix([[3,3],
|
|
1601
|
+
....: [2,5],
|
|
1602
|
+
....: [0,6]])
|
|
1603
|
+
sage: B = matrix([[3,2],
|
|
1604
|
+
....: [2,6],
|
|
1605
|
+
....: [3,1]])
|
|
1606
|
+
sage: g = NormalFormGame([A, B])
|
|
1607
|
+
sage: g.obtain_nash(algorithm='enumeration')
|
|
1608
|
+
[[(0, 1/3, 2/3), (1/3, 2/3)], [(4/5, 1/5, 0), (2/3, 1/3)], [(1, 0, 0), (1, 0)]]
|
|
1609
|
+
|
|
1610
|
+
Of the algorithms implemented, only ``'lrs'`` and ``'enumeration'``
|
|
1611
|
+
are guaranteed to find all Nash equilibria in a game. The solver for
|
|
1612
|
+
constant sum games only ever finds one Nash equilibrium. Although it
|
|
1613
|
+
is possible for the ``'LCP'`` solver to find all Nash equilibria
|
|
1614
|
+
in some instances, there are instances where it will not be able to
|
|
1615
|
+
find all Nash equilibria.::
|
|
1616
|
+
|
|
1617
|
+
sage: A = matrix(2, 2)
|
|
1618
|
+
sage: gg = NormalFormGame([A])
|
|
1619
|
+
sage: gg.obtain_nash(algorithm='enumeration')
|
|
1620
|
+
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
|
|
1621
|
+
sage: gg.obtain_nash(algorithm='lrs') # optional - lrs
|
|
1622
|
+
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
|
|
1623
|
+
sage: gg.obtain_nash(algorithm='lp', solver='glpk')
|
|
1624
|
+
[[(1.0, 0.0), (1.0, 0.0)]]
|
|
1625
|
+
sage: gg.obtain_nash(algorithm='LCP') # optional - gambit
|
|
1626
|
+
[[(1.0, 0.0), (1.0, 0.0)]]
|
|
1627
|
+
sage: gg.obtain_nash(algorithm='enumeration', maximization=False)
|
|
1628
|
+
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
|
|
1629
|
+
sage: gg.obtain_nash(algorithm='lrs', maximization=False) # optional - lrs
|
|
1630
|
+
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
|
|
1631
|
+
sage: gg.obtain_nash(algorithm='lp', solver='glpk', maximization=False)
|
|
1632
|
+
[[(1.0, 0.0), (1.0, 0.0)]]
|
|
1633
|
+
sage: gg.obtain_nash(algorithm='LCP', maximization=False) # optional - gambit
|
|
1634
|
+
[[(1.0, 0.0), (1.0, 0.0)]]
|
|
1635
|
+
|
|
1636
|
+
Note that outputs for all algorithms are as lists of lists of
|
|
1637
|
+
tuples and the equilibria have been sorted so that all algorithms give
|
|
1638
|
+
a comparable output (although ``'LCP'`` returns floats)::
|
|
1639
|
+
|
|
1640
|
+
sage: enumeration_eqs = g.obtain_nash(algorithm='enumeration')
|
|
1641
|
+
sage: [[type(s) for s in eq] for eq in enumeration_eqs]
|
|
1642
|
+
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
|
|
1643
|
+
sage: lrs_eqs = g.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
1644
|
+
sage: [[type(s) for s in eq] for eq in lrs_eqs] # optional - lrslib
|
|
1645
|
+
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
|
|
1646
|
+
sage: LCP_eqs = g.obtain_nash(algorithm='LCP') # optional - gambit
|
|
1647
|
+
sage: [[type(s) for s in eq] for eq in LCP_eqs] # optional - gambit
|
|
1648
|
+
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
|
|
1649
|
+
sage: enumeration_eqs == sorted(enumeration_eqs)
|
|
1650
|
+
True
|
|
1651
|
+
sage: lrs_eqs == sorted(lrs_eqs) # optional - lrslib
|
|
1652
|
+
True
|
|
1653
|
+
sage: LCP_eqs == sorted(LCP_eqs) # optional - gambit
|
|
1654
|
+
True
|
|
1655
|
+
sage: lrs_eqs == enumeration_eqs # optional - lrslib
|
|
1656
|
+
True
|
|
1657
|
+
sage: enumeration_eqs == LCP_eqs # optional - gambit
|
|
1658
|
+
False
|
|
1659
|
+
sage: [[[round(float(p), 6) for p in str] for str in eq] for eq in enumeration_eqs] == [[[round(float(p), 6) for p in str] for str in eq] for eq in LCP_eqs] # optional - gambit
|
|
1660
|
+
True
|
|
1661
|
+
|
|
1662
|
+
Also, not specifying a valid solver would lead to an error::
|
|
1663
|
+
|
|
1664
|
+
sage: A = matrix.identity(2)
|
|
1665
|
+
sage: g = NormalFormGame([A])
|
|
1666
|
+
sage: g.obtain_nash(algorithm='invalid')
|
|
1667
|
+
Traceback (most recent call last):
|
|
1668
|
+
...
|
|
1669
|
+
ValueError: 'algorithm' should be set to 'enumeration', 'LCP', 'lp' or 'lrs'
|
|
1670
|
+
sage: g.obtain_nash(algorithm='lp', solver='invalid')
|
|
1671
|
+
Traceback (most recent call last):
|
|
1672
|
+
...
|
|
1673
|
+
ValueError: 'solver' should be set to 'GLPK', ..., None
|
|
1674
|
+
(in which case the default one is used), or a callable.
|
|
1675
|
+
"""
|
|
1676
|
+
if len(self.players) > 2:
|
|
1677
|
+
raise NotImplementedError("Nash equilibrium for games with more "
|
|
1678
|
+
"than 2 players have not been "
|
|
1679
|
+
"implemented yet. Please see the gambit "
|
|
1680
|
+
"website (http://gambit.sourceforge.net/) that has a variety of "
|
|
1681
|
+
"available algorithms")
|
|
1682
|
+
|
|
1683
|
+
if not self._is_complete():
|
|
1684
|
+
raise ValueError("utilities have not been populated")
|
|
1685
|
+
|
|
1686
|
+
from sage.features.lrs import LrsNash
|
|
1687
|
+
if not algorithm:
|
|
1688
|
+
if self.is_constant_sum():
|
|
1689
|
+
algorithm = "lp"
|
|
1690
|
+
elif LrsNash().is_present():
|
|
1691
|
+
algorithm = "lrs"
|
|
1692
|
+
else:
|
|
1693
|
+
algorithm = "enumeration"
|
|
1694
|
+
|
|
1695
|
+
if algorithm == "lrs":
|
|
1696
|
+
LrsNash().require()
|
|
1697
|
+
return self._solve_lrs(maximization)
|
|
1698
|
+
|
|
1699
|
+
if algorithm == "LCP":
|
|
1700
|
+
if Game is None:
|
|
1701
|
+
raise RuntimeError("gambit not found") # should later become a FeatureNotFoundError
|
|
1702
|
+
return self._solve_LCP(maximization)
|
|
1703
|
+
|
|
1704
|
+
if algorithm.startswith('lp'):
|
|
1705
|
+
return self._solve_LP(solver=solver, maximization=maximization)
|
|
1706
|
+
|
|
1707
|
+
if algorithm == "enumeration":
|
|
1708
|
+
return self._solve_enumeration(maximization)
|
|
1709
|
+
|
|
1710
|
+
raise ValueError("'algorithm' should be set to 'enumeration', 'LCP', 'lp' or 'lrs'")
|
|
1711
|
+
|
|
1712
|
+
def _solve_lrs(self, maximization=True):
|
|
1713
|
+
r"""
|
|
1714
|
+
EXAMPLES:
|
|
1715
|
+
|
|
1716
|
+
A simple game::
|
|
1717
|
+
|
|
1718
|
+
sage: A = matrix([[1, 2], [3, 4]])
|
|
1719
|
+
sage: B = matrix([[3, 3], [1, 4]])
|
|
1720
|
+
sage: C = NormalFormGame([A, B])
|
|
1721
|
+
sage: C._solve_lrs() # optional - lrslib
|
|
1722
|
+
[[(0, 1), (0, 1)]]
|
|
1723
|
+
|
|
1724
|
+
2 random matrices::
|
|
1725
|
+
|
|
1726
|
+
sage: p1 = matrix([[-1, 4, 0, 2, 0],
|
|
1727
|
+
....: [-17, 246, -5, 1, -2],
|
|
1728
|
+
....: [0, 1, 1, -4, -4],
|
|
1729
|
+
....: [1, -3, 9, 6, -1],
|
|
1730
|
+
....: [2, 53, 0, -5, 0]])
|
|
1731
|
+
sage: p2 = matrix([[0, 1, 1, 3, 1],
|
|
1732
|
+
....: [3, 9, 44, -1, -1],
|
|
1733
|
+
....: [1, -4, -1, -3, 1],
|
|
1734
|
+
....: [1, 0, 0, 0, 0,],
|
|
1735
|
+
....: [1, -3, 1, 21, -2]])
|
|
1736
|
+
sage: biggame = NormalFormGame([p1, p2])
|
|
1737
|
+
sage: biggame._solve_lrs() # optional - lrslib
|
|
1738
|
+
[[(0, 0, 0, 20/21, 1/21), (11/12, 0, 0, 1/12, 0)]]
|
|
1739
|
+
|
|
1740
|
+
Another test::
|
|
1741
|
+
|
|
1742
|
+
sage: p1 = matrix([[-7, -5, 5],
|
|
1743
|
+
....: [5, 5, 3],
|
|
1744
|
+
....: [1, -6, 1]])
|
|
1745
|
+
sage: p2 = matrix([[-9, 7, 9],
|
|
1746
|
+
....: [6, -2, -3],
|
|
1747
|
+
....: [-4, 6, -10]])
|
|
1748
|
+
sage: biggame = NormalFormGame([p1, p2])
|
|
1749
|
+
sage: biggame._solve_lrs() # optional - lrslib
|
|
1750
|
+
[[(0, 1, 0), (1, 0, 0)],
|
|
1751
|
+
[(1/3, 2/3, 0), (0, 1/6, 5/6)],
|
|
1752
|
+
[(1/3, 2/3, 0), (1/7, 0, 6/7)],
|
|
1753
|
+
[(1, 0, 0), (0, 0, 1)]]
|
|
1754
|
+
"""
|
|
1755
|
+
from subprocess import PIPE, Popen
|
|
1756
|
+
m1, m2 = self.payoff_matrices()
|
|
1757
|
+
if maximization is False:
|
|
1758
|
+
m1 = - m1
|
|
1759
|
+
m2 = - m2
|
|
1760
|
+
|
|
1761
|
+
game_str = self._lrs_nash_format(m1, m2)
|
|
1762
|
+
game_name = tmp_filename()
|
|
1763
|
+
with open(game_name, 'w') as game_file:
|
|
1764
|
+
game_file.write(game_str)
|
|
1765
|
+
|
|
1766
|
+
from sage.features.lrs import LrsNash
|
|
1767
|
+
LrsNash().require()
|
|
1768
|
+
process = Popen([LrsNash().absolute_filename(), game_name],
|
|
1769
|
+
stdout=PIPE, stderr=PIPE)
|
|
1770
|
+
|
|
1771
|
+
lrs_output = [bytes_to_str(row) for row in process.stdout]
|
|
1772
|
+
process.terminate()
|
|
1773
|
+
|
|
1774
|
+
nasheq = Parser(lrs_output).format_lrs()
|
|
1775
|
+
return sorted(nasheq)
|
|
1776
|
+
|
|
1777
|
+
def _solve_LCP(self, maximization):
|
|
1778
|
+
r"""
|
|
1779
|
+
Solve a :class:`NormalFormGame` using Gambit's LCP algorithm.
|
|
1780
|
+
|
|
1781
|
+
EXAMPLES::
|
|
1782
|
+
|
|
1783
|
+
sage: a = matrix([[1, 0], [1, 4]])
|
|
1784
|
+
sage: b = matrix([[2, 3], [2, 4]])
|
|
1785
|
+
sage: c = NormalFormGame([a, b])
|
|
1786
|
+
sage: c._solve_LCP(maximization=True) # optional - gambit
|
|
1787
|
+
[[(0.0, 1.0), (0.0, 1.0)]]
|
|
1788
|
+
"""
|
|
1789
|
+
g = self._gambit_(maximization)
|
|
1790
|
+
output = ExternalLCPSolver().solve(g)
|
|
1791
|
+
nasheq = Parser(output).format_gambit(g)
|
|
1792
|
+
return sorted(nasheq)
|
|
1793
|
+
|
|
1794
|
+
def _solve_gambit_LP(self, maximization=True):
|
|
1795
|
+
r"""
|
|
1796
|
+
Solve a constant sum :class:`NormalFormGame` using Gambit's
|
|
1797
|
+
LP implementation.
|
|
1798
|
+
|
|
1799
|
+
EXAMPLES::
|
|
1800
|
+
|
|
1801
|
+
sage: A = matrix([[2, 1], [1, 2.5]])
|
|
1802
|
+
sage: g = NormalFormGame([A])
|
|
1803
|
+
sage: g._solve_gambit_LP() # optional - gambit
|
|
1804
|
+
[[(0.6, 0.4), (0.6, 0.4)]]
|
|
1805
|
+
sage: A = matrix.identity(2)
|
|
1806
|
+
sage: g = NormalFormGame([A])
|
|
1807
|
+
sage: g._solve_gambit_LP() # optional - gambit
|
|
1808
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1809
|
+
sage: g = NormalFormGame([A,A])
|
|
1810
|
+
sage: g._solve_gambit_LP() # optional - gambit
|
|
1811
|
+
Traceback (most recent call last):
|
|
1812
|
+
...
|
|
1813
|
+
RuntimeError: Method only valid for constant-sum games.
|
|
1814
|
+
"""
|
|
1815
|
+
if Game is None:
|
|
1816
|
+
raise NotImplementedError("gambit is not installed")
|
|
1817
|
+
g = self._gambit_(maximization=maximization)
|
|
1818
|
+
output = ExternalLPSolver().solve(g)
|
|
1819
|
+
nasheq = Parser(output).format_gambit(g)
|
|
1820
|
+
return sorted(nasheq)
|
|
1821
|
+
|
|
1822
|
+
def _solve_LP(self, solver='glpk', maximization=True):
|
|
1823
|
+
r"""
|
|
1824
|
+
Solve a constant sum :class:`NormalFormGame` using
|
|
1825
|
+
the specified LP solver.
|
|
1826
|
+
|
|
1827
|
+
INPUT:
|
|
1828
|
+
|
|
1829
|
+
- ``solver`` -- the solver to be used to solve the LP:
|
|
1830
|
+
|
|
1831
|
+
* ``'gambit'`` -- his uses the solver included within the gambit
|
|
1832
|
+
library to create and solve the LP
|
|
1833
|
+
|
|
1834
|
+
* for further possible values, see :class:`MixedIntegerLinearProgram`
|
|
1835
|
+
|
|
1836
|
+
EXAMPLES::
|
|
1837
|
+
|
|
1838
|
+
sage: A = matrix.identity(2)
|
|
1839
|
+
sage: g = NormalFormGame([A])
|
|
1840
|
+
sage: g._solve_LP()
|
|
1841
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1842
|
+
sage: g._solve_LP('gambit') # optional - gambit
|
|
1843
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1844
|
+
sage: g._solve_LP('Coin') # optional - sage_numerical_backends_coin
|
|
1845
|
+
[[(0.5, 0.5), (0.5, 0.5)]]
|
|
1846
|
+
sage: g._solve_LP('PPL')
|
|
1847
|
+
[[(1/2, 1/2), (1/2, 1/2)]]
|
|
1848
|
+
sage: A = matrix([[2, 1], [1, 3]])
|
|
1849
|
+
sage: g = NormalFormGame([A])
|
|
1850
|
+
sage: ne = g._solve_LP()
|
|
1851
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]
|
|
1852
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1853
|
+
sage: ne = g._solve_LP('gambit') # optional - gambit
|
|
1854
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - gambit
|
|
1855
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1856
|
+
sage: ne = g._solve_LP('Coin') # optional - sage_numerical_backends_coin
|
|
1857
|
+
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne] # optional - sage_numerical_backends_coin
|
|
1858
|
+
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
|
|
1859
|
+
sage: g._solve_LP('PPL')
|
|
1860
|
+
[[(2/3, 1/3), (2/3, 1/3)]]
|
|
1861
|
+
|
|
1862
|
+
An exception is raised if the input game is not constant sum::
|
|
1863
|
+
|
|
1864
|
+
sage: A = matrix.identity(2)
|
|
1865
|
+
sage: B = A.transpose()
|
|
1866
|
+
sage: g = NormalFormGame([A, B])
|
|
1867
|
+
sage: g._solve_LP()
|
|
1868
|
+
Traceback (most recent call last):
|
|
1869
|
+
...
|
|
1870
|
+
ValueError: Input game needs to be a two player constant sum game
|
|
1871
|
+
"""
|
|
1872
|
+
if not self.is_constant_sum():
|
|
1873
|
+
raise ValueError("Input game needs to be a two player constant sum game")
|
|
1874
|
+
if solver == 'gambit':
|
|
1875
|
+
return self._solve_gambit_LP(maximization)
|
|
1876
|
+
|
|
1877
|
+
sgn = 1
|
|
1878
|
+
if not maximization:
|
|
1879
|
+
sgn = -1
|
|
1880
|
+
|
|
1881
|
+
strategy_sizes = [p.num_strategies for p in self.players]
|
|
1882
|
+
|
|
1883
|
+
p = MixedIntegerLinearProgram(maximization=False, solver=solver)
|
|
1884
|
+
y = p.new_variable(nonnegative=True)
|
|
1885
|
+
v = p.new_variable(nonnegative=False)
|
|
1886
|
+
p.add_constraint(sgn * self.payoff_matrices()[0] * y - v[0] <= 0)
|
|
1887
|
+
p.add_constraint(matrix([[1] * strategy_sizes[1]]) * y == 1)
|
|
1888
|
+
p.set_objective(v[0])
|
|
1889
|
+
p.solve()
|
|
1890
|
+
y = tuple(p.get_values(y).values())
|
|
1891
|
+
|
|
1892
|
+
p = MixedIntegerLinearProgram(maximization=False, solver=solver)
|
|
1893
|
+
x = p.new_variable(nonnegative=True)
|
|
1894
|
+
u = p.new_variable(nonnegative=False)
|
|
1895
|
+
p.add_constraint(sgn * -self.payoff_matrices()[0].T * x - u[0] <= 0)
|
|
1896
|
+
p.add_constraint(matrix([[1] * strategy_sizes[0]]) * x == 1)
|
|
1897
|
+
p.set_objective(u[0])
|
|
1898
|
+
p.solve()
|
|
1899
|
+
x = tuple(p.get_values(x).values())
|
|
1900
|
+
return [[x, y]]
|
|
1901
|
+
|
|
1902
|
+
def _solve_enumeration(self, maximization=True):
|
|
1903
|
+
r"""
|
|
1904
|
+
Obtain the Nash equilibria using support enumeration.
|
|
1905
|
+
|
|
1906
|
+
Algorithm implemented here is Algorithm 3.4 of [NN2007]_
|
|
1907
|
+
with an aspect of pruning from [SLB2008]_.
|
|
1908
|
+
|
|
1909
|
+
1. For each k in 1...min(size of strategy sets)
|
|
1910
|
+
2. For each I,J supports of size k
|
|
1911
|
+
3. Prune: check if supports are dominated
|
|
1912
|
+
4. Solve indifference conditions and check that have Nash Equilibrium.
|
|
1913
|
+
|
|
1914
|
+
EXAMPLES:
|
|
1915
|
+
|
|
1916
|
+
A Game::
|
|
1917
|
+
|
|
1918
|
+
sage: A = matrix([[160, 205, 44],
|
|
1919
|
+
....: [175, 180, 45],
|
|
1920
|
+
....: [201, 204, 50],
|
|
1921
|
+
....: [120, 207, 49]])
|
|
1922
|
+
sage: B = matrix([[2, 2, 2],
|
|
1923
|
+
....: [1, 0, 0],
|
|
1924
|
+
....: [3, 4, 1],
|
|
1925
|
+
....: [4, 1, 2]])
|
|
1926
|
+
sage: g=NormalFormGame([A, B])
|
|
1927
|
+
sage: g._solve_enumeration()
|
|
1928
|
+
[[(0, 0, 3/4, 1/4), (1/28, 27/28, 0)]]
|
|
1929
|
+
|
|
1930
|
+
A game with 3 equilibria::
|
|
1931
|
+
|
|
1932
|
+
sage: A = matrix([[3,3],
|
|
1933
|
+
....: [2,5],
|
|
1934
|
+
....: [0,6]])
|
|
1935
|
+
sage: B = matrix([[3,2],
|
|
1936
|
+
....: [2,6],
|
|
1937
|
+
....: [3,1]])
|
|
1938
|
+
sage: g = NormalFormGame([A, B])
|
|
1939
|
+
sage: g._solve_enumeration(maximization=False)
|
|
1940
|
+
[[(1, 0, 0), (0, 1)]]
|
|
1941
|
+
|
|
1942
|
+
A simple example::
|
|
1943
|
+
|
|
1944
|
+
sage: s = matrix([[1, 0], [-2, 3]])
|
|
1945
|
+
sage: t = matrix([[3, 2], [-1, 0]])
|
|
1946
|
+
sage: example = NormalFormGame([s, t])
|
|
1947
|
+
sage: example._solve_enumeration()
|
|
1948
|
+
[[(0, 1), (0, 1)], [(1/2, 1/2), (1/2, 1/2)], [(1, 0), (1, 0)]]
|
|
1949
|
+
|
|
1950
|
+
Another::
|
|
1951
|
+
|
|
1952
|
+
sage: A = matrix([[0, 1, 7, 1],
|
|
1953
|
+
....: [2, 1, 3, 1],
|
|
1954
|
+
....: [3, 1, 3, 5],
|
|
1955
|
+
....: [6, 4, 2, 7]])
|
|
1956
|
+
sage: B = matrix([[3, 2, 8, 4],
|
|
1957
|
+
....: [6, 2, 0, 3],
|
|
1958
|
+
....: [1, 3, -1, 1],
|
|
1959
|
+
....: [3, 2, 1, 1]])
|
|
1960
|
+
sage: C = NormalFormGame([A, B])
|
|
1961
|
+
sage: C._solve_enumeration()
|
|
1962
|
+
[[(0, 0, 0, 1), (1, 0, 0, 0)],
|
|
1963
|
+
[(2/7, 0, 0, 5/7), (5/11, 0, 6/11, 0)],
|
|
1964
|
+
[(1, 0, 0, 0), (0, 0, 1, 0)]]
|
|
1965
|
+
|
|
1966
|
+
Again::
|
|
1967
|
+
|
|
1968
|
+
sage: X = matrix([[1, 4, 2],
|
|
1969
|
+
....: [4, 0, 3],
|
|
1970
|
+
....: [2, 3, 5]])
|
|
1971
|
+
sage: Y = matrix([[3, 9, 2],
|
|
1972
|
+
....: [0, 3, 1],
|
|
1973
|
+
....: [5, 4, 6]])
|
|
1974
|
+
sage: Z = NormalFormGame([X, Y])
|
|
1975
|
+
sage: Z._solve_enumeration()
|
|
1976
|
+
[[(0, 0, 1), (0, 0, 1)], [(2/9, 0, 7/9), (0, 3/4, 1/4)], [(1, 0, 0), (0, 1, 0)]]
|
|
1977
|
+
|
|
1978
|
+
TESTS:
|
|
1979
|
+
|
|
1980
|
+
Due to the nature of the linear equations solved in this algorithm
|
|
1981
|
+
some negative vectors can be returned. Here is a test that ensures
|
|
1982
|
+
this doesn't happen (the particular payoff matrices chosen give a
|
|
1983
|
+
linear system that would have negative valued vectors as solution)::
|
|
1984
|
+
|
|
1985
|
+
sage: a = matrix([[-13, 59],
|
|
1986
|
+
....: [27, 86]])
|
|
1987
|
+
sage: b = matrix([[14, 6],
|
|
1988
|
+
....: [58, -14]])
|
|
1989
|
+
sage: c = NormalFormGame([a, b])
|
|
1990
|
+
sage: c._solve_enumeration()
|
|
1991
|
+
[[(0, 1), (1, 0)]]
|
|
1992
|
+
|
|
1993
|
+
Testing against an error in ``_is_NE``. Note that 1 equilibrium is
|
|
1994
|
+
missing: ``[(2/3, 1/3), (0, 1)]``, however this equilibrium has
|
|
1995
|
+
supports of different sizes. This only occurs in degenerate games
|
|
1996
|
+
and is not supported in the `enumeration` algorithm::
|
|
1997
|
+
|
|
1998
|
+
sage: N = NormalFormGame([matrix(2,[0,-1,-2,-1]),matrix(2,[1,0,0,2])])
|
|
1999
|
+
sage: N._solve_enumeration()
|
|
2000
|
+
[[(0, 1), (0, 1)], [(1, 0), (1, 0)]]
|
|
2001
|
+
|
|
2002
|
+
In this instance the `lrs` algorithm is able to find all
|
|
2003
|
+
three equilibria::
|
|
2004
|
+
|
|
2005
|
+
sage: N = NormalFormGame([matrix(2,[0,-1,-2,-1]),matrix(2,[1,0,0,2])])
|
|
2006
|
+
sage: N.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
2007
|
+
[[(0, 1), (0, 1)], [(2/3, 1/3), (0, 1)], [(1, 0), (1, 0)]]
|
|
2008
|
+
|
|
2009
|
+
Here is another::
|
|
2010
|
+
|
|
2011
|
+
sage: N = NormalFormGame([matrix(2,[7,-8,-4,-8,7,0]),matrix(2,[-9,-1,-8,3,2,3])])
|
|
2012
|
+
sage: N._solve_enumeration()
|
|
2013
|
+
[[(0, 1), (0, 0, 1)]]
|
|
2014
|
+
"""
|
|
2015
|
+
|
|
2016
|
+
M1, M2 = self.payoff_matrices()
|
|
2017
|
+
if maximization is False:
|
|
2018
|
+
M1 = -M1
|
|
2019
|
+
M2 = -M2
|
|
2020
|
+
|
|
2021
|
+
potential_supports = [[tuple(support) for support in
|
|
2022
|
+
powerset(range(player.num_strategies))]
|
|
2023
|
+
for player in self.players]
|
|
2024
|
+
|
|
2025
|
+
potential_support_pairs = (pair for pair in product(*potential_supports) if len(pair[0]) == len(pair[1]))
|
|
2026
|
+
|
|
2027
|
+
equilibria = []
|
|
2028
|
+
for pair in potential_support_pairs:
|
|
2029
|
+
# Check if any supports are dominated for row player
|
|
2030
|
+
if (self._row_cond_dominance(pair[0], pair[1], M1)
|
|
2031
|
+
# Check if any supports are dominated for col player
|
|
2032
|
+
and self._row_cond_dominance(pair[1], pair[0], M2.transpose())):
|
|
2033
|
+
a = self._solve_indifference(pair[0], pair[1], M2)
|
|
2034
|
+
b = self._solve_indifference(pair[1], pair[0], M1.transpose())
|
|
2035
|
+
if a and b and self._is_NE(a, b, pair[0], pair[1], M1, M2):
|
|
2036
|
+
equilibria.append([tuple(a), tuple(b)])
|
|
2037
|
+
|
|
2038
|
+
return sorted(equilibria)
|
|
2039
|
+
|
|
2040
|
+
def _row_cond_dominance(self, p1_sup, p2_sup, matrix):
|
|
2041
|
+
r"""
|
|
2042
|
+
Check if any row strategies of a sub matrix defined
|
|
2043
|
+
by a given pair of supports are conditionally dominated.
|
|
2044
|
+
Return ``False`` if a row is conditionally dominated.
|
|
2045
|
+
|
|
2046
|
+
TESTS:
|
|
2047
|
+
|
|
2048
|
+
A matrix that depending on the support for the column player
|
|
2049
|
+
has a dominated row::
|
|
2050
|
+
|
|
2051
|
+
sage: g = NormalFormGame()
|
|
2052
|
+
sage: A = matrix([[1, 1, 5], [2, 2, 0]])
|
|
2053
|
+
sage: g._row_cond_dominance((0, 1), (0, 1), A)
|
|
2054
|
+
False
|
|
2055
|
+
|
|
2056
|
+
or does not have a dominated row::
|
|
2057
|
+
|
|
2058
|
+
sage: g._row_cond_dominance((0, 1), (0, 2), A)
|
|
2059
|
+
True
|
|
2060
|
+
"""
|
|
2061
|
+
subm = matrix.matrix_from_rows_and_columns(list(p1_sup), list(p2_sup))
|
|
2062
|
+
nbr_rows = subm.nrows()
|
|
2063
|
+
nbr_cols = subm.ncols()
|
|
2064
|
+
for s in range(nbr_rows):
|
|
2065
|
+
strategy = subm.rows()[s]
|
|
2066
|
+
for r in range(s, nbr_rows):
|
|
2067
|
+
row = subm.rows()[r]
|
|
2068
|
+
if strategy != row:
|
|
2069
|
+
if all(strategy[i] < row[i] for i in range(nbr_cols)):
|
|
2070
|
+
return False
|
|
2071
|
+
if all(row[i] < strategy[i] for i in range(nbr_cols)):
|
|
2072
|
+
return False
|
|
2073
|
+
return True
|
|
2074
|
+
|
|
2075
|
+
def _solve_indifference(self, support1, support2, M):
|
|
2076
|
+
r"""
|
|
2077
|
+
For support1, returns the strategy with support: support2 that makes the
|
|
2078
|
+
column player indifferent for the utilities given by M.
|
|
2079
|
+
|
|
2080
|
+
This is done by building the corresponding linear system.
|
|
2081
|
+
If `\rho_1, \rho_2` are the supports of player 1 and 2 respectively.
|
|
2082
|
+
Then, indifference for player 1 implies:
|
|
2083
|
+
|
|
2084
|
+
.. MATH::
|
|
2085
|
+
|
|
2086
|
+
u_1(s_1,\rho_2) = u_1(s_2, \rho_2)
|
|
2087
|
+
|
|
2088
|
+
for all `s_1, s_2` in the support of `\rho_1`. This corresponds to:
|
|
2089
|
+
|
|
2090
|
+
.. MATH::
|
|
2091
|
+
|
|
2092
|
+
\sum_{j\in S(\rho_2)}A_{s_1,j}{\rho_2}_j =
|
|
2093
|
+
\sum_{j\in S(\rho_2)}A_{s_2,j}{\rho_2}_j
|
|
2094
|
+
|
|
2095
|
+
for all `s_1, s_2` in the support of `\rho_1` where `A` is the payoff
|
|
2096
|
+
matrix of player 1. Equivalently we can consider consecutive rows of
|
|
2097
|
+
`A` (instead of all pairs of strategies). Thus the corresponding
|
|
2098
|
+
linear system can be written as:
|
|
2099
|
+
|
|
2100
|
+
.. MATH::
|
|
2101
|
+
|
|
2102
|
+
\left(\sum_{j \in S(\rho_2)}^{A_{i,j} - A_{i+1,j}\right){\rho_2}_j
|
|
2103
|
+
|
|
2104
|
+
for all `1\leq i \leq |S(\rho_1)|` (where `A` has been modified to only
|
|
2105
|
+
contain the row corresponding to `S(\rho_1)`). We also require all
|
|
2106
|
+
elements of `\rho_2` to sum to 1:
|
|
2107
|
+
|
|
2108
|
+
.. MATH::
|
|
2109
|
+
|
|
2110
|
+
\sum_{j\in S(\rho_1)}{\rho_2}_j = 1.
|
|
2111
|
+
|
|
2112
|
+
TESTS:
|
|
2113
|
+
|
|
2114
|
+
Find the indifference vector for a support pair that has
|
|
2115
|
+
no dominated strategies::
|
|
2116
|
+
|
|
2117
|
+
sage: A = matrix([[1, 1, 5], [2, 2, 0]])
|
|
2118
|
+
sage: g = NormalFormGame([A])
|
|
2119
|
+
sage: g._solve_indifference((0, 1), (0, 2), A)
|
|
2120
|
+
(1/3, 2/3)
|
|
2121
|
+
sage: g._solve_indifference((0, 2), (0, 1), -A.transpose())
|
|
2122
|
+
(5/6, 0, 1/6)
|
|
2123
|
+
|
|
2124
|
+
When a support pair has a dominated strategy there is no
|
|
2125
|
+
solution to the indifference equation::
|
|
2126
|
+
|
|
2127
|
+
sage: g._solve_indifference((0, 1), (0, 1), -A.transpose())
|
|
2128
|
+
<BLANKLINE>
|
|
2129
|
+
|
|
2130
|
+
Particular case of a game with 1 strategy for each for each player::
|
|
2131
|
+
|
|
2132
|
+
sage: A = matrix([[10]])
|
|
2133
|
+
sage: g = NormalFormGame([A])
|
|
2134
|
+
sage: g._solve_indifference((0,), (0,), -A.transpose())
|
|
2135
|
+
(1)
|
|
2136
|
+
"""
|
|
2137
|
+
linearsystem = matrix(QQ, len(support2) + 1, M.nrows())
|
|
2138
|
+
|
|
2139
|
+
# Build linear system for player 1
|
|
2140
|
+
for strategy1 in support1:
|
|
2141
|
+
# Checking particular case of supports of pure strategies
|
|
2142
|
+
if len(support2) == 1:
|
|
2143
|
+
for strategy2 in range(M.ncols()):
|
|
2144
|
+
if M[strategy1][support2[0]] < \
|
|
2145
|
+
M[strategy1][strategy2]:
|
|
2146
|
+
return False
|
|
2147
|
+
else:
|
|
2148
|
+
for strategy_pair2 in range(len(support2)):
|
|
2149
|
+
# Coefficients of linear system that ensure indifference
|
|
2150
|
+
# between two consecutive strategies of the support
|
|
2151
|
+
linearsystem[strategy_pair2, strategy1] = \
|
|
2152
|
+
M[strategy1][support2[strategy_pair2]] -\
|
|
2153
|
+
M[strategy1][support2[strategy_pair2 - 1]]
|
|
2154
|
+
# Coefficients of linear system that ensure the vector is
|
|
2155
|
+
# a probability vector. ie. sum to 1
|
|
2156
|
+
linearsystem[-1, strategy1] = 1
|
|
2157
|
+
# Create rhs of linear systems
|
|
2158
|
+
linearsystem_rhs = vector([0 for i in range(len(support2))] + [1])
|
|
2159
|
+
|
|
2160
|
+
# Solve both linear systems
|
|
2161
|
+
try:
|
|
2162
|
+
result = linearsystem.solve_right(linearsystem_rhs)
|
|
2163
|
+
except ValueError:
|
|
2164
|
+
return None
|
|
2165
|
+
|
|
2166
|
+
return result
|
|
2167
|
+
|
|
2168
|
+
def _is_NE(self, a, b, p1_support, p2_support, M1, M2):
|
|
2169
|
+
r"""
|
|
2170
|
+
For vectors that obey indifference for a given support pair,
|
|
2171
|
+
checks if it corresponds to a Nash equilibria (support is obeyed and
|
|
2172
|
+
no negative values, also that no player has incentive to deviate
|
|
2173
|
+
out of supports).
|
|
2174
|
+
|
|
2175
|
+
TESTS::
|
|
2176
|
+
|
|
2177
|
+
sage: X = matrix([[1, 4, 2],
|
|
2178
|
+
....: [4, 0, 3],
|
|
2179
|
+
....: [2, 3, 5]])
|
|
2180
|
+
sage: Y = matrix([[3, 9, 2],
|
|
2181
|
+
....: [0, 3, 1],
|
|
2182
|
+
....: [5, 4, 6]])
|
|
2183
|
+
sage: Z = NormalFormGame([X, Y])
|
|
2184
|
+
sage: Z._is_NE([0, 1/4, 3/4], [3/5, 2/5, 0], (1, 2,), (0, 1,), X, Y)
|
|
2185
|
+
False
|
|
2186
|
+
|
|
2187
|
+
sage: Z._is_NE([2/9, 0, 7/9], [0, 3/4, 1/4], (0, 2), (1, 2), X, Y)
|
|
2188
|
+
True
|
|
2189
|
+
|
|
2190
|
+
Checking pure strategies are not forgotten::
|
|
2191
|
+
|
|
2192
|
+
sage: A = matrix(2, [0, -1, -2, -1])
|
|
2193
|
+
sage: B = matrix(2, [1, 0, 0, 2])
|
|
2194
|
+
sage: N = NormalFormGame([A, B])
|
|
2195
|
+
sage: N._is_NE([1, 0], [1, 0], (0,), (0,), A, B)
|
|
2196
|
+
True
|
|
2197
|
+
sage: N._is_NE([0, 1], [0, 1], (1,), (1,), A, B)
|
|
2198
|
+
True
|
|
2199
|
+
sage: N._is_NE([1, 0], [0, 1], (0,), (1,), A, B)
|
|
2200
|
+
False
|
|
2201
|
+
sage: N._is_NE([0, 1], [1, 0], (1,), (0,), A, B)
|
|
2202
|
+
False
|
|
2203
|
+
|
|
2204
|
+
sage: A = matrix(3, [-7, -5, 5, 5, 5, 3, 1, -6, 1])
|
|
2205
|
+
sage: B = matrix(3, [-9, 7, 9, 6, -2, -3, -4, 6, -10])
|
|
2206
|
+
sage: N = NormalFormGame([A, B])
|
|
2207
|
+
sage: N._is_NE([1, 0, 0], [0, 0, 1], (0,), (2,), A, B)
|
|
2208
|
+
True
|
|
2209
|
+
sage: N._is_NE([0, 1, 0], [1, 0, 0], (1,), (0,), A, B)
|
|
2210
|
+
True
|
|
2211
|
+
sage: N._is_NE([0, 1, 0], [0, 1, 0], (1,), (1,), A, B)
|
|
2212
|
+
False
|
|
2213
|
+
sage: N._is_NE([0, 0, 1], [0, 1, 0], (2,), (1,), A, B)
|
|
2214
|
+
False
|
|
2215
|
+
sage: N._is_NE([0, 0, 1], [0, 0, 1], (2,), (2,), A, B)
|
|
2216
|
+
False
|
|
2217
|
+
"""
|
|
2218
|
+
# Check that supports are obeyed
|
|
2219
|
+
if not (all(a[i] > 0 for i in p1_support) and
|
|
2220
|
+
all(b[j] > 0 for j in p2_support) and
|
|
2221
|
+
all(a[i] == 0 for i in range(len(a))
|
|
2222
|
+
if i not in p1_support) and
|
|
2223
|
+
all(b[j] == 0 for j in range(len(b))
|
|
2224
|
+
if j not in p2_support)):
|
|
2225
|
+
return False
|
|
2226
|
+
|
|
2227
|
+
# Check that have pair of best responses
|
|
2228
|
+
|
|
2229
|
+
p1_payoffs = [sum(v * row[i] for i, v in enumerate(b))
|
|
2230
|
+
for row in M1.rows()]
|
|
2231
|
+
p2_payoffs = [sum(v * col[j] for j, v in enumerate(a))
|
|
2232
|
+
for col in M2.columns()]
|
|
2233
|
+
|
|
2234
|
+
# if p1_payoffs.index(max(p1_payoffs)) not in p1_support:
|
|
2235
|
+
if not any(i in p1_support for i, x in enumerate(p1_payoffs)
|
|
2236
|
+
if x == max(p1_payoffs)):
|
|
2237
|
+
return False
|
|
2238
|
+
if not any(i in p2_support for i, x in enumerate(p2_payoffs)
|
|
2239
|
+
if x == max(p2_payoffs)):
|
|
2240
|
+
return False
|
|
2241
|
+
|
|
2242
|
+
return True
|
|
2243
|
+
|
|
2244
|
+
def _lrs_nash_format(self, m1, m2):
|
|
2245
|
+
r"""
|
|
2246
|
+
Create the input format for ``lrsnash``, version 6.1 or newer.
|
|
2247
|
+
|
|
2248
|
+
EXAMPLES:
|
|
2249
|
+
|
|
2250
|
+
An example from the ``lrsnash`` manual in the old and the format::
|
|
2251
|
+
|
|
2252
|
+
sage: A = matrix([[0, 6], [2, 5], [3, 3]])
|
|
2253
|
+
sage: B = matrix([[1, 0], [0, 2], [4, 3]])
|
|
2254
|
+
sage: C = NormalFormGame([A, B])
|
|
2255
|
+
sage: print(C._lrs_nash_format(A, B))
|
|
2256
|
+
3 2
|
|
2257
|
+
<BLANKLINE>
|
|
2258
|
+
0 6
|
|
2259
|
+
2 5
|
|
2260
|
+
3 3
|
|
2261
|
+
<BLANKLINE>
|
|
2262
|
+
1 0
|
|
2263
|
+
0 2
|
|
2264
|
+
4 3
|
|
2265
|
+
<BLANKLINE>
|
|
2266
|
+
|
|
2267
|
+
.. NOTE::
|
|
2268
|
+
|
|
2269
|
+
The former legacy format has been removed in :issue:`39464`.
|
|
2270
|
+
"""
|
|
2271
|
+
from sage.geometry.polyhedron.misc import _to_space_separated_string
|
|
2272
|
+
m = self.players[0].num_strategies
|
|
2273
|
+
n = self.players[1].num_strategies
|
|
2274
|
+
s = f'{m} {n}\n\n'
|
|
2275
|
+
s += '\n'.join(_to_space_separated_string(r) for r in m1.rows())
|
|
2276
|
+
s += '\n\n'
|
|
2277
|
+
s += '\n'.join(_to_space_separated_string(r) for r in m2.rows())
|
|
2278
|
+
s += '\n'
|
|
2279
|
+
return s
|
|
2280
|
+
|
|
2281
|
+
def is_degenerate(self, certificate=False) -> bool:
|
|
2282
|
+
"""
|
|
2283
|
+
A function to check whether the game is degenerate or not.
|
|
2284
|
+
|
|
2285
|
+
Will return a boolean.
|
|
2286
|
+
|
|
2287
|
+
A two-player game is called nondegenerate if no mixed strategy of
|
|
2288
|
+
support size `k` has more than `k` pure best responses [NN2007]_. In a
|
|
2289
|
+
degenerate game, this definition is violated, for example if there
|
|
2290
|
+
is a pure strategy that has two pure best responses.
|
|
2291
|
+
|
|
2292
|
+
The implementation here transforms the search over mixed strategies to a
|
|
2293
|
+
search over supports which is a discrete search. A full explanation of
|
|
2294
|
+
this is given in [CK2015]_. This problem is known to be NP-Hard
|
|
2295
|
+
[Du2009]_. Another possible implementation is via best response
|
|
2296
|
+
polytopes, see :issue:`18958`.
|
|
2297
|
+
|
|
2298
|
+
The game Rock-Paper-Scissors is an example of a non-degenerate game,::
|
|
2299
|
+
|
|
2300
|
+
sage: g = game_theory.normal_form_games.RPS()
|
|
2301
|
+
sage: g.is_degenerate()
|
|
2302
|
+
False
|
|
2303
|
+
|
|
2304
|
+
whereas `Rock-Paper-Scissors-Lizard-Spock
|
|
2305
|
+
<http://www.samkass.com/theories/RPSSL.html>`_ is degenerate because
|
|
2306
|
+
for every pure strategy there are two best responses.::
|
|
2307
|
+
|
|
2308
|
+
sage: g = game_theory.normal_form_games.RPSLS()
|
|
2309
|
+
sage: g.is_degenerate()
|
|
2310
|
+
True
|
|
2311
|
+
|
|
2312
|
+
EXAMPLES:
|
|
2313
|
+
|
|
2314
|
+
Here is an example of a degenerate game given in [DGRB2010]_::
|
|
2315
|
+
|
|
2316
|
+
sage: A = matrix([[3, 3], [2, 5], [0, 6]])
|
|
2317
|
+
sage: B = matrix([[3, 3], [2, 6], [3, 1]])
|
|
2318
|
+
sage: degenerate_game = NormalFormGame([A,B])
|
|
2319
|
+
sage: degenerate_game.is_degenerate()
|
|
2320
|
+
True
|
|
2321
|
+
|
|
2322
|
+
Here is an example of a degenerate game given in [NN2007]_::
|
|
2323
|
+
|
|
2324
|
+
sage: A = matrix([[0, 6], [2, 5], [3, 3]])
|
|
2325
|
+
sage: B = matrix([[1, 0], [0, 2], [4, 4]])
|
|
2326
|
+
sage: d_game = NormalFormGame([A, B])
|
|
2327
|
+
sage: d_game.is_degenerate()
|
|
2328
|
+
True
|
|
2329
|
+
|
|
2330
|
+
Here are some other examples of degenerate games::
|
|
2331
|
+
|
|
2332
|
+
sage: M = matrix([[2, 1], [1, 1]])
|
|
2333
|
+
sage: N = matrix([[1, 1], [1, 2]])
|
|
2334
|
+
sage: game = NormalFormGame([M, N])
|
|
2335
|
+
sage: game.is_degenerate()
|
|
2336
|
+
True
|
|
2337
|
+
|
|
2338
|
+
If more information is required, it may be useful to use
|
|
2339
|
+
``certificate=True``. This will return a boolean of whether the game is
|
|
2340
|
+
degenerate or not, and if True; a tuple containing the strategy where
|
|
2341
|
+
degeneracy was found and the player it belongs to. ``0`` is the row
|
|
2342
|
+
player and ``1`` is the column player.::
|
|
2343
|
+
|
|
2344
|
+
sage: M = matrix([[2, 1], [1, 1]])
|
|
2345
|
+
sage: N = matrix([[1, 1], [1, 2]])
|
|
2346
|
+
sage: g = NormalFormGame([M, N])
|
|
2347
|
+
sage: test, certificate = g.is_degenerate(certificate=True)
|
|
2348
|
+
sage: test, certificate
|
|
2349
|
+
(True, ((1, 0), 0))
|
|
2350
|
+
|
|
2351
|
+
Using the output, we see that the opponent has more best responses than
|
|
2352
|
+
the size of the support of the strategy in question ``(1, 0)``. (We
|
|
2353
|
+
specify the player as ``(player + 1) % 2`` to ensure that we have the
|
|
2354
|
+
opponent's index.)::
|
|
2355
|
+
|
|
2356
|
+
sage: g.best_responses(certificate[0], (certificate[1] + 1) % 2)
|
|
2357
|
+
[0, 1]
|
|
2358
|
+
|
|
2359
|
+
Another example with a mixed strategy causing degeneracy.::
|
|
2360
|
+
|
|
2361
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2362
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2363
|
+
sage: g = NormalFormGame([A, B])
|
|
2364
|
+
sage: test, certificate = g.is_degenerate(certificate=True)
|
|
2365
|
+
sage: test, certificate
|
|
2366
|
+
(True, ((1/2, 1/2), 1))
|
|
2367
|
+
|
|
2368
|
+
Again, we see that the opponent has more best responses than the size of
|
|
2369
|
+
the support of the strategy in question ``(1/2, 1/2)``.::
|
|
2370
|
+
|
|
2371
|
+
sage: g.best_responses(certificate[0], (certificate[1] + 1) % 2)
|
|
2372
|
+
[0, 1, 2]
|
|
2373
|
+
|
|
2374
|
+
Sometimes, the different algorithms for obtaining nash_equilibria don't
|
|
2375
|
+
agree with each other. This can happen when games are degenerate::
|
|
2376
|
+
|
|
2377
|
+
sage: a = matrix([[-75, 18, 45, 33],
|
|
2378
|
+
....: [42, -8, -77, -18],
|
|
2379
|
+
....: [83, 18, 11, 40],
|
|
2380
|
+
....: [-10, -38, 76, -9]])
|
|
2381
|
+
sage: b = matrix([[62, 64, 87, 51],
|
|
2382
|
+
....: [-41, -27, -69, 52],
|
|
2383
|
+
....: [-17, 25, -97, -82],
|
|
2384
|
+
....: [30, 31, -1, 50]])
|
|
2385
|
+
sage: d_game = NormalFormGame([a, b])
|
|
2386
|
+
sage: d_game.obtain_nash(algorithm='lrs') # optional - lrslib
|
|
2387
|
+
[[(0, 0, 1, 0), (0, 1, 0, 0)],
|
|
2388
|
+
[(17/29, 0, 0, 12/29), (0, 0, 42/73, 31/73)],
|
|
2389
|
+
[(122/145, 0, 23/145, 0), (0, 1, 0, 0)]]
|
|
2390
|
+
sage: d_game.obtain_nash(algorithm='LCP') # optional - gambit
|
|
2391
|
+
[[(0.5862068966, 0.0, 0.0, 0.4137931034),
|
|
2392
|
+
(0.0, 0.0, 0.5753424658, 0.4246575342)]]
|
|
2393
|
+
sage: d_game.obtain_nash(algorithm='enumeration')
|
|
2394
|
+
[[(0, 0, 1, 0), (0, 1, 0, 0)], [(17/29, 0, 0, 12/29), (0, 0, 42/73, 31/73)]]
|
|
2395
|
+
sage: d_game.is_degenerate()
|
|
2396
|
+
True
|
|
2397
|
+
|
|
2398
|
+
TESTS::
|
|
2399
|
+
|
|
2400
|
+
sage: g = NormalFormGame()
|
|
2401
|
+
sage: g.add_player(3) # Adding first player with 3 strategies
|
|
2402
|
+
sage: g.add_player(3) # Adding second player with 3 strategies
|
|
2403
|
+
sage: for key in g:
|
|
2404
|
+
....: g[key] = [0, 0]
|
|
2405
|
+
sage: g.is_degenerate()
|
|
2406
|
+
True
|
|
2407
|
+
|
|
2408
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2409
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2410
|
+
sage: g = NormalFormGame([A, B])
|
|
2411
|
+
sage: g.is_degenerate()
|
|
2412
|
+
True
|
|
2413
|
+
|
|
2414
|
+
sage: A = matrix([[1, -1], [-1, 1]])
|
|
2415
|
+
sage: B = matrix([[-1, 1], [1, -1]])
|
|
2416
|
+
sage: matching_pennies = NormalFormGame([A, B])
|
|
2417
|
+
sage: matching_pennies.is_degenerate()
|
|
2418
|
+
False
|
|
2419
|
+
|
|
2420
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
2421
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
2422
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
2423
|
+
sage: prisoners_dilemma.is_degenerate()
|
|
2424
|
+
False
|
|
2425
|
+
|
|
2426
|
+
sage: g = NormalFormGame()
|
|
2427
|
+
sage: g.add_player(2)
|
|
2428
|
+
sage: g.add_player(2)
|
|
2429
|
+
sage: g.add_player(2)
|
|
2430
|
+
sage: g.is_degenerate()
|
|
2431
|
+
Traceback (most recent call last):
|
|
2432
|
+
...
|
|
2433
|
+
NotImplementedError: Tests for Degeneracy is not yet implemented for
|
|
2434
|
+
games with more than two players.
|
|
2435
|
+
"""
|
|
2436
|
+
if len(self.players) > 2:
|
|
2437
|
+
raise NotImplementedError("Tests for Degeneracy is not yet "
|
|
2438
|
+
"implemented for games with more than "
|
|
2439
|
+
"two players.")
|
|
2440
|
+
|
|
2441
|
+
d = self._is_degenerate_pure(certificate)
|
|
2442
|
+
if d:
|
|
2443
|
+
return d
|
|
2444
|
+
|
|
2445
|
+
M1, M2 = self.payoff_matrices()
|
|
2446
|
+
potential_supports = [[tuple(support) for support in
|
|
2447
|
+
powerset(range(player.num_strategies))]
|
|
2448
|
+
for player in self.players]
|
|
2449
|
+
|
|
2450
|
+
# filter out all supports that are pure or empty
|
|
2451
|
+
potential_supports = [[i for i in k if len(i) > 1]
|
|
2452
|
+
for k in potential_supports]
|
|
2453
|
+
|
|
2454
|
+
potential_support_pairs = [pair for pair in
|
|
2455
|
+
product(*potential_supports) if
|
|
2456
|
+
len(pair[0]) != len(pair[1])]
|
|
2457
|
+
|
|
2458
|
+
# Sort so that solve small linear systems first
|
|
2459
|
+
potential_support_pairs.sort(key=lambda x: sum([len(k) for k in x]))
|
|
2460
|
+
|
|
2461
|
+
for pair in potential_support_pairs:
|
|
2462
|
+
if len(pair[0]) < len(pair[1]):
|
|
2463
|
+
strat = self._solve_indifference(pair[0], pair[1], M2)
|
|
2464
|
+
if strat and len(self.best_responses(strat, player=0)) > len(pair[0]):
|
|
2465
|
+
if certificate:
|
|
2466
|
+
return True, (strat, 0)
|
|
2467
|
+
else:
|
|
2468
|
+
return True
|
|
2469
|
+
elif len(pair[1]) < len(pair[0]):
|
|
2470
|
+
strat = self._solve_indifference(pair[1], pair[0], M1.transpose())
|
|
2471
|
+
if strat and len(self.best_responses(strat, player=0)) > len(pair[1]):
|
|
2472
|
+
if certificate:
|
|
2473
|
+
return True, (strat, 1)
|
|
2474
|
+
else:
|
|
2475
|
+
return True
|
|
2476
|
+
|
|
2477
|
+
if certificate:
|
|
2478
|
+
return False, ()
|
|
2479
|
+
else:
|
|
2480
|
+
return False
|
|
2481
|
+
|
|
2482
|
+
def best_responses(self, strategy, player):
|
|
2483
|
+
"""
|
|
2484
|
+
For a given strategy for a player and the index of the opponent,
|
|
2485
|
+
computes the payoff for the opponent and returns a list of the indices
|
|
2486
|
+
of the best responses. Only implemented for two player games
|
|
2487
|
+
|
|
2488
|
+
INPUT:
|
|
2489
|
+
|
|
2490
|
+
- ``strategy`` -- a probability distribution vector
|
|
2491
|
+
|
|
2492
|
+
- ``player`` -- the index of the opponent, ``0`` for the row player,
|
|
2493
|
+
``1`` for the column player
|
|
2494
|
+
|
|
2495
|
+
EXAMPLES::
|
|
2496
|
+
|
|
2497
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2498
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2499
|
+
sage: g = NormalFormGame([A, B])
|
|
2500
|
+
|
|
2501
|
+
Now we can obtain the best responses for Player 1, when Player 2 uses
|
|
2502
|
+
different strategies::
|
|
2503
|
+
|
|
2504
|
+
sage: g.best_responses((1/2, 1/2), player=0)
|
|
2505
|
+
[0, 1, 2]
|
|
2506
|
+
sage: g.best_responses((3/4, 1/4), player=0)
|
|
2507
|
+
[0]
|
|
2508
|
+
|
|
2509
|
+
To get the best responses for Player 2 we pass the argument :code:`player=1`::
|
|
2510
|
+
|
|
2511
|
+
sage: g.best_responses((4/5, 1/5, 0), player=1)
|
|
2512
|
+
[0, 1]
|
|
2513
|
+
|
|
2514
|
+
sage: A = matrix([[1, 0], [0, 1], [0, 0]])
|
|
2515
|
+
sage: B = matrix([[1, 0], [0, 1], [0.7, 0.8]])
|
|
2516
|
+
sage: g = NormalFormGame([A, B])
|
|
2517
|
+
sage: g.best_responses((0, 1, 0), player=1)
|
|
2518
|
+
[1]
|
|
2519
|
+
|
|
2520
|
+
sage: A = matrix([[3,3],[2,5],[0,6]])
|
|
2521
|
+
sage: B = matrix([[3,3],[2,6],[3,1]])
|
|
2522
|
+
sage: degenerate_game = NormalFormGame([A,B])
|
|
2523
|
+
sage: degenerate_game.best_responses((1, 0, 0), player=1)
|
|
2524
|
+
[0, 1]
|
|
2525
|
+
|
|
2526
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2527
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2528
|
+
sage: g = NormalFormGame([A, B])
|
|
2529
|
+
sage: g.best_responses((1/3, 1/3, 1/3), player=1)
|
|
2530
|
+
[1]
|
|
2531
|
+
|
|
2532
|
+
Note that this has only been implemented for 2 player games::
|
|
2533
|
+
|
|
2534
|
+
sage: g = NormalFormGame()
|
|
2535
|
+
sage: g.add_player(2) # adding first player with 2 strategies
|
|
2536
|
+
sage: g.add_player(2) # adding second player with 2 strategies
|
|
2537
|
+
sage: g.add_player(2) # adding third player with 2 strategies
|
|
2538
|
+
sage: g.best_responses((1/2, 1/2), player=2)
|
|
2539
|
+
Traceback (most recent call last):
|
|
2540
|
+
...
|
|
2541
|
+
ValueError: Only available for 2 player games
|
|
2542
|
+
|
|
2543
|
+
If the strategy is not of the correct dimension for the given player
|
|
2544
|
+
then an error is returned::
|
|
2545
|
+
|
|
2546
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2547
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2548
|
+
sage: g = NormalFormGame([A, B])
|
|
2549
|
+
sage: g.best_responses((1/2, 1/2), player=1)
|
|
2550
|
+
Traceback (most recent call last):
|
|
2551
|
+
...
|
|
2552
|
+
ValueError: Strategy is not of correct dimension
|
|
2553
|
+
|
|
2554
|
+
sage: g.best_responses((1/3, 1/3, 1/3), player=0)
|
|
2555
|
+
Traceback (most recent call last):
|
|
2556
|
+
...
|
|
2557
|
+
ValueError: Strategy is not of correct dimension
|
|
2558
|
+
|
|
2559
|
+
If the strategy is not a true probability vector then an error is
|
|
2560
|
+
passed::
|
|
2561
|
+
|
|
2562
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2563
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2564
|
+
sage: g = NormalFormGame([A, B])
|
|
2565
|
+
sage: g.best_responses((1/3, 1/2, 0), player=1)
|
|
2566
|
+
Traceback (most recent call last):
|
|
2567
|
+
...
|
|
2568
|
+
ValueError: Strategy is not a probability distribution vector
|
|
2569
|
+
|
|
2570
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2571
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2572
|
+
sage: g = NormalFormGame([A, B])
|
|
2573
|
+
sage: g.best_responses((3/2, -1/2), player=0)
|
|
2574
|
+
Traceback (most recent call last):
|
|
2575
|
+
...
|
|
2576
|
+
ValueError: Strategy is not a probability distribution vector
|
|
2577
|
+
|
|
2578
|
+
If the player specified is not `0` or `1`, an error is raised::
|
|
2579
|
+
|
|
2580
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2581
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2582
|
+
sage: g = NormalFormGame([A, B])
|
|
2583
|
+
sage: g.best_responses((1/2, 1/2), player='Player1')
|
|
2584
|
+
Traceback (most recent call last):
|
|
2585
|
+
...
|
|
2586
|
+
ValueError: Player1 is not an index of the opponent, must be 0 or 1
|
|
2587
|
+
"""
|
|
2588
|
+
if len(self.players) != 2:
|
|
2589
|
+
raise ValueError('Only available for 2 player games')
|
|
2590
|
+
|
|
2591
|
+
if player != 0 and player != 1:
|
|
2592
|
+
raise ValueError('%s is not an index of the opponent, must be 0 or 1' % player)
|
|
2593
|
+
|
|
2594
|
+
strategy = vector(strategy)
|
|
2595
|
+
|
|
2596
|
+
if sum(strategy) != 1 or min(strategy) < 0:
|
|
2597
|
+
raise ValueError('Strategy is not a probability distribution vector')
|
|
2598
|
+
|
|
2599
|
+
if player == 0:
|
|
2600
|
+
payoff_matrix = self.payoff_matrices()[0]
|
|
2601
|
+
elif player == 1:
|
|
2602
|
+
payoff_matrix = self.payoff_matrices()[1].transpose()
|
|
2603
|
+
|
|
2604
|
+
if len(strategy) != payoff_matrix.dimensions()[1]:
|
|
2605
|
+
raise ValueError('Strategy is not of correct dimension')
|
|
2606
|
+
|
|
2607
|
+
payoffs = list(payoff_matrix * strategy)
|
|
2608
|
+
indices = [i for i, j in enumerate(payoffs) if j == max(payoffs)]
|
|
2609
|
+
|
|
2610
|
+
return indices
|
|
2611
|
+
|
|
2612
|
+
def _is_degenerate_pure(self, certificate=False):
|
|
2613
|
+
"""
|
|
2614
|
+
Check whether a game is degenerate in pure strategies.
|
|
2615
|
+
|
|
2616
|
+
TESTS::
|
|
2617
|
+
|
|
2618
|
+
sage: A = matrix([[3,3],[2,5],[0,6]])
|
|
2619
|
+
sage: B = matrix([[3,3],[2,6],[3,1]])
|
|
2620
|
+
sage: degenerate_game = NormalFormGame([A,B])
|
|
2621
|
+
sage: degenerate_game._is_degenerate_pure()
|
|
2622
|
+
True
|
|
2623
|
+
|
|
2624
|
+
sage: A = matrix([[1, 0], [0, 1], [0, 0]])
|
|
2625
|
+
sage: B = matrix([[1, 0], [0, 1], [0.7, 0.8]])
|
|
2626
|
+
sage: g = NormalFormGame([A, B])
|
|
2627
|
+
sage: g._is_degenerate_pure()
|
|
2628
|
+
False
|
|
2629
|
+
|
|
2630
|
+
sage: A = matrix([[2, 5], [0, 4]])
|
|
2631
|
+
sage: B = matrix([[2, 0], [5, 4]])
|
|
2632
|
+
sage: prisoners_dilemma = NormalFormGame([A, B])
|
|
2633
|
+
sage: prisoners_dilemma._is_degenerate_pure()
|
|
2634
|
+
False
|
|
2635
|
+
|
|
2636
|
+
sage: A = matrix([[0, -1, 1, 1, -1],
|
|
2637
|
+
....: [1, 0, -1, -1, 1],
|
|
2638
|
+
....: [-1, 1, 0, 1 , -1],
|
|
2639
|
+
....: [-1, 1, -1, 0, 1],
|
|
2640
|
+
....: [1, -1, 1, -1, 0]])
|
|
2641
|
+
sage: g = NormalFormGame([A])
|
|
2642
|
+
sage: g._is_degenerate_pure()
|
|
2643
|
+
True
|
|
2644
|
+
|
|
2645
|
+
Whilst this game is not degenerate in pure strategies, it is
|
|
2646
|
+
actually degenerate, but only in mixed strategies::
|
|
2647
|
+
|
|
2648
|
+
sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
|
|
2649
|
+
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
|
|
2650
|
+
sage: g = NormalFormGame([A, B])
|
|
2651
|
+
sage: g._is_degenerate_pure()
|
|
2652
|
+
False
|
|
2653
|
+
"""
|
|
2654
|
+
M1, M2 = self.payoff_matrices()
|
|
2655
|
+
for i, row in enumerate(M2.rows()):
|
|
2656
|
+
if list(row).count(max(row)) > 1:
|
|
2657
|
+
if certificate:
|
|
2658
|
+
strat = [0 for k in range(M1.nrows())]
|
|
2659
|
+
strat[i] = 1
|
|
2660
|
+
return True, (tuple(strat), 0)
|
|
2661
|
+
else:
|
|
2662
|
+
return True
|
|
2663
|
+
|
|
2664
|
+
for j, col in enumerate(M1.columns()):
|
|
2665
|
+
if list(col).count(max(col)) > 1:
|
|
2666
|
+
if certificate:
|
|
2667
|
+
strat = [0 for k in range(M1.ncols())]
|
|
2668
|
+
strat[j] = 1
|
|
2669
|
+
return True, (tuple(strat), 1)
|
|
2670
|
+
else:
|
|
2671
|
+
return True
|
|
2672
|
+
return False
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
class _Player:
|
|
2676
|
+
def __init__(self, num_strategies):
|
|
2677
|
+
r"""
|
|
2678
|
+
TESTS::
|
|
2679
|
+
|
|
2680
|
+
sage: from sage.game_theory.normal_form_game import _Player
|
|
2681
|
+
sage: p = _Player(5)
|
|
2682
|
+
sage: p.num_strategies
|
|
2683
|
+
5
|
|
2684
|
+
"""
|
|
2685
|
+
self.num_strategies = num_strategies
|
|
2686
|
+
|
|
2687
|
+
def add_strategy(self):
|
|
2688
|
+
r"""
|
|
2689
|
+
TESTS::
|
|
2690
|
+
|
|
2691
|
+
sage: from sage.game_theory.normal_form_game import _Player
|
|
2692
|
+
sage: p = _Player(5)
|
|
2693
|
+
sage: p.add_strategy()
|
|
2694
|
+
sage: p.num_strategies
|
|
2695
|
+
6
|
|
2696
|
+
"""
|
|
2697
|
+
self.num_strategies += 1
|