Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #33213: replace SAGE_TMP in glpk backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
orlitzky committed Feb 15, 2022
1 parent d0d9aea commit e44917e
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/sage/numerical/backends/glpk_backend.pyx
Expand Up @@ -1807,10 +1807,13 @@ cdef class GLPKBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: fnam = os.path.join(SAGE_TMP, "lp_problem.lp")
sage: p.write_lp(fnam)
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: fnam = os.path.join(d, "lp_problem.lp")
....: _ = p.write_lp(fnam)
....: len([(x,y) for x,y in enumerate(open(fnam,"r"))])
...
sage: len([(x,y) for x,y in enumerate(open(fnam,"r"))])
9 lines were written
9
"""
filename = str_to_bytes(filename, FS_ENCODING, 'surrogateescape')
Expand All @@ -1832,10 +1835,13 @@ cdef class GLPKBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: fnam = os.path.join(SAGE_TMP, "lp_problem.mps")
sage: p.write_mps(fnam, 2)
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: fnam = os.path.join(d, "lp_problem.mps")
....: _ = p.write_mps(fnam, 2)
....: len([(x,y) for x,y in enumerate(open(fnam,"r"))])
...
sage: len([(x,y) for x,y in enumerate(open(fnam,"r"))])
17 records were written
17
"""
filename = str_to_bytes(filename, FS_ENCODING, 'surrogateescape')
Expand Down Expand Up @@ -2471,11 +2477,13 @@ cdef class GLPKBackend(GenericBackend):
1
sage: p.solve()
0
sage: p.print_ranges() # this writes out "ranges.tmp"
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: fnam = os.path.join(d, "ranges.tmp")
....: p.print_ranges(fnam)
....: for ll in open(fnam, "r"):
....: if ll != '': print(ll)
...
sage: fnam = os.path.join(SAGE_TMP, "ranges.tmp")
sage: for ll in open(fnam, "r"):
....: if ll != '': print(ll)
GLPK ... - SENSITIVITY ANALYSIS REPORT Page 1
Problem:
Objective: 7.5 (MAXimum)
Expand All @@ -2496,25 +2504,20 @@ cdef class GLPKBackend(GenericBackend):
. +Inf 1.50000 +Inf +Inf
End of report
"""

from sage.misc.misc import SAGE_TMP

if filename is None:
fname = SAGE_TMP + "/ranges.tmp"
else:
fname = filename

import os, tempfile
f = tempfile.NamedTemporaryFile(delete=False); f.close()
res = glp_print_ranges(self.lp, 0, 0, 0,
str_to_bytes(fname, FS_ENCODING,
str_to_bytes(f.name, FS_ENCODING,
'surrogateescape'))

if filename is None:
if res == 0:
with open(fname) as f:
for line in f:
if filename is not None:
os.rename(f.name, filename)
else:
if res == 0: # success
with open(f.name) as fh:
for line in fh:
print(line, end=" ")
print("\n")

os.remove(f.name)
return res

cpdef double get_row_dual(self, int variable):
Expand Down

0 comments on commit e44917e

Please sign in to comment.