Skip to content

Commit e93f039

Browse files
author
Release Manager
committed
sagemathgh-39464: remove deprecated method in normal_form_game found using the scripts in sagemath#39262 ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: sagemath#39464 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 573c41f + 9d5736e commit e93f039

File tree

2 files changed

+17
-160
lines changed

2 files changed

+17
-160
lines changed

src/sage/game_theory/normal_form_game.py

+13-125
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def __len__(self):
890890
"""
891891
return len(self.utilities)
892892

893-
def _repr_(self):
893+
def _repr_(self) -> str:
894894
r"""
895895
Return the strategy_profiles of the game.
896896
@@ -909,7 +909,7 @@ def _repr_(self):
909909
base_str = "Normal Form Game with the following utilities: {}"
910910
return base_str.format(pformat(self.utilities))
911911

912-
def _latex_(self):
912+
def _latex_(self) -> str:
913913
r"""
914914
Return the LaTeX code representing the ``NormalFormGame``.
915915
@@ -1194,7 +1194,7 @@ def is_constant_sum(self):
11941194
return False
11951195
m1, m2 = self.payoff_matrices()
11961196
c = m1 + m2
1197-
t = c[0,0]
1197+
t = c[0, 0]
11981198

11991199
for row in c:
12001200
for i in row:
@@ -2021,7 +2021,7 @@ def _solve_enumeration(self, maximization=True):
20212021
powerset(range(player.num_strategies))]
20222022
for player in self.players]
20232023

2024-
potential_support_pairs = [pair for pair in product(*potential_supports) if len(pair[0]) == len(pair[1])]
2024+
potential_support_pairs = (pair for pair in product(*potential_supports) if len(pair[0]) == len(pair[1]))
20252025

20262026
equilibria = []
20272027
for pair in potential_support_pairs:
@@ -2230,7 +2230,7 @@ def _is_NE(self, a, b, p1_support, p2_support, M1, M2):
22302230
p2_payoffs = [sum(v * col[j] for j, v in enumerate(a))
22312231
for col in M2.columns()]
22322232

2233-
#if p1_payoffs.index(max(p1_payoffs)) not in p1_support:
2233+
# if p1_payoffs.index(max(p1_payoffs)) not in p1_support:
22342234
if not any(i in p1_support for i, x in enumerate(p1_payoffs)
22352235
if x == max(p1_payoffs)):
22362236
return False
@@ -2240,89 +2240,6 @@ def _is_NE(self, a, b, p1_support, p2_support, M1, M2):
22402240

22412241
return True
22422242

2243-
def _Hrepresentation(self, m1, m2):
2244-
r"""
2245-
Create the H-representation strings required to use ``lrsnash``.
2246-
2247-
Since lrslib 6.1, this format is referred to as "legacy format".
2248-
2249-
This method is deprecated.
2250-
2251-
EXAMPLES::
2252-
2253-
sage: A = matrix([[1, 2], [3, 4]])
2254-
sage: B = matrix([[3, 3], [1, 4]])
2255-
sage: C = NormalFormGame([A, B])
2256-
sage: print(C._Hrepresentation(A, B)[0])
2257-
doctest:warning...
2258-
DeprecationWarning: NormalFormGame._Hrepresentation is deprecated as it
2259-
creates the legacy input format. Use NormalFormGame._lrs_nash_format instead
2260-
See https://github.com/sagemath/sage/issues/27745 for details.
2261-
H-representation
2262-
linearity 1 5
2263-
begin
2264-
5 4 rational
2265-
0 1 0 0
2266-
0 0 1 0
2267-
0 -3 -1 1
2268-
0 -3 -4 1
2269-
-1 1 1 0
2270-
end
2271-
<BLANKLINE>
2272-
sage: print(C._Hrepresentation(A, B)[1])
2273-
H-representation
2274-
linearity 1 5
2275-
begin
2276-
5 4 rational
2277-
0 -1 -2 1
2278-
0 -3 -4 1
2279-
0 1 0 0
2280-
0 0 1 0
2281-
-1 1 1 0
2282-
end
2283-
<BLANKLINE>
2284-
"""
2285-
from sage.misc.superseded import deprecation
2286-
deprecation(27745,
2287-
"NormalFormGame._Hrepresentation is deprecated as it "
2288-
"creates the legacy input format. "
2289-
"Use NormalFormGame._lrs_nash_format instead")
2290-
2291-
from sage.geometry.polyhedron.misc import _to_space_separated_string
2292-
m = self.players[0].num_strategies
2293-
n = self.players[1].num_strategies
2294-
midentity = list(matrix.identity(m))
2295-
nidentity = list(matrix.identity(n))
2296-
2297-
s = 'H-representation\n'
2298-
s += 'linearity 1 ' + str(m + n + 1) + '\n'
2299-
s += 'begin\n'
2300-
s += str(m + n + 1) + ' ' + str(m + 2) + ' rational\n'
2301-
for f in list(midentity):
2302-
s += '0 ' + _to_space_separated_string(f) + ' 0 \n'
2303-
for e in list(m2.transpose()):
2304-
s += '0 ' + _to_space_separated_string(-e) + ' 1 \n'
2305-
s += '-1 '
2306-
for g in range(m):
2307-
s += '1 '
2308-
s += '0 \n'
2309-
s += 'end\n'
2310-
2311-
t = 'H-representation\n'
2312-
t += 'linearity 1 ' + str(m + n + 1) + '\n'
2313-
t += 'begin\n'
2314-
t += str(m + n + 1) + ' ' + str(n + 2) + ' rational\n'
2315-
for e in list(m1):
2316-
t += '0 ' + _to_space_separated_string(-e) + ' 1 \n'
2317-
for f in list(nidentity):
2318-
t += '0 ' + _to_space_separated_string(f) + ' 0 \n'
2319-
t += '-1 '
2320-
for g in range(n):
2321-
t += '1 '
2322-
t += '0 \n'
2323-
t += 'end\n'
2324-
return s, t
2325-
23262243
def _lrs_nash_format(self, m1, m2):
23272244
r"""
23282245
Create the input format for ``lrsnash``, version 6.1 or newer.
@@ -2346,53 +2263,24 @@ def _lrs_nash_format(self, m1, m2):
23462263
4 3
23472264
<BLANKLINE>
23482265
2349-
sage: legacy_format = C._Hrepresentation(A, B)
2350-
doctest:warning...
2351-
DeprecationWarning: NormalFormGame._Hrepresentation is deprecated as it
2352-
creates the legacy input format. Use NormalFormGame._lrs_nash_format instead
2353-
See https://github.com/sagemath/sage/issues/27745 for details.
2354-
sage: print('*game: player 1\n', legacy_format[0])
2355-
*game: player 1
2356-
H-representation
2357-
linearity 1 6
2358-
begin
2359-
6 5 rational
2360-
0 1 0 0 0
2361-
0 0 1 0 0
2362-
0 0 0 1 0
2363-
0 -1 0 -4 1
2364-
0 0 -2 -3 1
2365-
-1 1 1 1 0
2366-
end
2367-
<BLANKLINE>
2368-
sage: print('*game: player 2\n', legacy_format[1])
2369-
*game: player 2
2370-
H-representation
2371-
linearity 1 6
2372-
begin
2373-
6 4 rational
2374-
0 0 -6 1
2375-
0 -2 -5 1
2376-
0 -3 -3 1
2377-
0 1 0 0
2378-
0 0 1 0
2379-
-1 1 1 0
2380-
end
2266+
.. NOTE::
2267+
2268+
The former legacy format has been removed in :issue:`39464`.
23812269
"""
23822270
from sage.geometry.polyhedron.misc import _to_space_separated_string
23832271
m = self.players[0].num_strategies
23842272
n = self.players[1].num_strategies
23852273
s = f'{m} {n}\n\n'
2386-
for r in m1.rows():
2387-
s += _to_space_separated_string(r) + '\n'
2274+
s += '\n'.join(_to_space_separated_string(r) for r in m1.rows())
2275+
s += '\n\n'
2276+
s += '\n'.join(_to_space_separated_string(r) for r in m2.rows())
23882277
s += '\n'
2389-
for r in m2.rows():
2390-
s += _to_space_separated_string(r) + '\n'
23912278
return s
23922279

2393-
def is_degenerate(self, certificate=False):
2280+
def is_degenerate(self, certificate=False) -> bool:
23942281
"""
23952282
A function to check whether the game is degenerate or not.
2283+
23962284
Will return a boolean.
23972285
23982286
A two-player game is called nondegenerate if no mixed strategy of

src/sage/game_theory/parser.py

+4-35
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, raw_string):
5151
"""
5252
self.raw_string = raw_string
5353

54-
def format_lrs(self, legacy_format=False):
54+
def format_lrs(self):
5555
r"""
5656
Parses the output of lrs so as to return vectors
5757
corresponding to equilibria.
@@ -137,45 +137,14 @@ def format_lrs(self, legacy_format=False):
137137
[(1/3, 2/3, 0), (1/7, 0, 6/7)],
138138
[(1, 0, 0), (0, 0, 1)]]
139139
140-
TESTS:
141-
142-
An example with the legacy format::
143-
144-
sage: from sage.cpython.string import bytes_to_str
145-
sage: from sage.game_theory.parser import Parser
146-
sage: from subprocess import Popen, PIPE
147-
sage: A = matrix([[1, 2], [3, 2]])
148-
sage: g = NormalFormGame([A])
149-
sage: game1_str, game2_str = g._Hrepresentation(A, -A)
150-
doctest:warning...
151-
DeprecationWarning: NormalFormGame._Hrepresentation is deprecated as it
152-
creates the legacy input format. Use NormalFormGame._lrs_nash_format instead
153-
See https://github.com/sagemath/sage/issues/27745 for details.
154-
sage: g1_name, g2_name = tmp_filename(), tmp_filename()
155-
sage: g1_file, g2_file = open(g1_name, 'w'), open(g2_name, 'w')
156-
sage: _ = g1_file.write(game1_str)
157-
sage: g1_file.close()
158-
sage: _ = g2_file.write(game2_str)
159-
sage: g2_file.close()
160-
sage: from sage.features.lrs import LrsNash
161-
sage: process = Popen([LrsNash().absolute_filename(), g1_name, g2_name], # optional - lrslib
162-
....: stdout=PIPE, stderr=PIPE)
163-
sage: lrs_output = [bytes_to_str(row) for row in process.stdout] # not tested, optional - lrslib
164-
sage: nasheq = Parser(lrs_output).format_lrs(legacy_format=True) # not tested, optional - lrslib
165-
sage: nasheq # not tested, optional - lrslib
166-
[[(1/2, 1/2), (0, 1)], [(0, 1), (0, 1)]]
140+
The former legacy format has been removed in :issue:`39464`.
167141
"""
168142
equilibria = []
169143
from sage.misc.sage_eval import sage_eval
170144
from itertools import groupby, dropwhile
171145
lines = iter(self.raw_string)
172-
if legacy_format:
173-
# Skip until the magic stars announce the beginning of the real output
174-
while not next(lines).startswith("*****"):
175-
pass
176-
else:
177-
# Skip comment lines starting with a single star
178-
lines = dropwhile(lambda line: line.startswith('*'), lines)
146+
# Skip comment lines starting with a single star
147+
lines = dropwhile(lambda line: line.startswith('*'), lines)
179148
for collection in [list(x[1]) for x in groupby(lines, lambda x: x == '\n')]:
180149
if collection[0].startswith('2'):
181150
s1 = tuple([sage_eval(k) for k in collection[-1].split()][1:-1])

0 commit comments

Comments
 (0)