Skip to content

Commit d3e8ed0

Browse files
authored
Merge pull request #926 from scipopt/add-printOrigProblem
Allow writeProblem to write to stdout
2 parents 6876af9 + 3b4eb05 commit d3e8ed0

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
### Added
5+
- Added printProblem to print problem to stdout
56
- Added stage checks to presolve, freereoptsolve, freetransform
67
- Added primal_dual_evolution recipe and a plot recipe
78
### Fixed

src/pyscipopt/scip.pxi

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,32 @@ cdef class Model:
28892889
if not onlyroot:
28902890
self.setIntParam("propagating/maxrounds", 0)
28912891

2892+
def printProblem(self, ext='.cip', trans=False, genericnames=False):
2893+
"""
2894+
Write current model/problem to standard output.
2895+
2896+
Parameters
2897+
----------
2898+
ext : str, optional
2899+
the extension to be used (Default value = '.cip').
2900+
Should have an extension corresponding to one of the readable file formats,
2901+
described in https://www.scipopt.org/doc/html/group__FILEREADERS.php.
2902+
trans : bool, optional
2903+
indicates whether the transformed problem is written to file (Default value = False)
2904+
genericnames : bool, optional
2905+
indicates whether the problem should be written with generic variable
2906+
and constraint names (Default value = False)
2907+
"""
2908+
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
2909+
locale.setlocale(locale.LC_NUMERIC, "C")
2910+
2911+
if trans:
2912+
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))
2913+
else:
2914+
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))
2915+
2916+
locale.setlocale(locale.LC_NUMERIC,user_locale)
2917+
28922918
def writeProblem(self, filename='model.cip', trans=False, genericnames=False, verbose=True):
28932919
"""
28942920
Write current model/problem to a file.
@@ -2911,22 +2937,27 @@ cdef class Model:
29112937
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
29122938
locale.setlocale(locale.LC_NUMERIC, "C")
29132939

2914-
str_absfile = abspath(filename)
2915-
absfile = str_conversion(str_absfile)
2916-
fn, ext = splitext(absfile)
2917-
2918-
if len(ext) == 0:
2919-
ext = str_conversion('.cip')
2920-
fn = fn + ext
2921-
ext = ext[1:]
2922-
2923-
if trans:
2924-
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
2940+
if filename:
2941+
str_absfile = abspath(filename)
2942+
absfile = str_conversion(str_absfile)
2943+
fn, ext = splitext(absfile)
2944+
if len(ext) == 0:
2945+
ext = str_conversion('.cip')
2946+
fn = fn + ext
2947+
ext = ext[1:]
2948+
2949+
if trans:
2950+
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
2951+
else:
2952+
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))
2953+
2954+
if verbose:
2955+
print('wrote problem to file ' + str_absfile)
29252956
else:
2926-
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))
2927-
2928-
if verbose:
2929-
print('wrote problem to file ' + str_absfile)
2957+
if trans:
2958+
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))
2959+
else:
2960+
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))
29302961

29312962
locale.setlocale(locale.LC_NUMERIC,user_locale)
29322963

tests/test_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def test_model():
6767

6868
s.writeProblem('model')
6969
s.writeProblem('model.lp')
70+
s.printProblem()
7071

7172
s.freeProb()
7273
s = Model()

0 commit comments

Comments
 (0)