Skip to content

Commit

Permalink
Merge pull request #926 from scipopt/add-printOrigProblem
Browse files Browse the repository at this point in the history
Allow writeProblem to write to stdout
  • Loading branch information
Opt-Mucca authored Dec 2, 2024
2 parents 6876af9 + 3b4eb05 commit d3e8ed0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Added
- Added printProblem to print problem to stdout
- Added stage checks to presolve, freereoptsolve, freetransform
- Added primal_dual_evolution recipe and a plot recipe
### Fixed
Expand Down
61 changes: 46 additions & 15 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,32 @@ cdef class Model:
if not onlyroot:
self.setIntParam("propagating/maxrounds", 0)

def printProblem(self, ext='.cip', trans=False, genericnames=False):
"""
Write current model/problem to standard output.
Parameters
----------
ext : str, optional
the extension to be used (Default value = '.cip').
Should have an extension corresponding to one of the readable file formats,
described in https://www.scipopt.org/doc/html/group__FILEREADERS.php.
trans : bool, optional
indicates whether the transformed problem is written to file (Default value = False)
genericnames : bool, optional
indicates whether the problem should be written with generic variable
and constraint names (Default value = False)
"""
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))

locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeProblem(self, filename='model.cip', trans=False, genericnames=False, verbose=True):
"""
Write current model/problem to a file.
Expand All @@ -2911,22 +2937,27 @@ cdef class Model:
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
fn, ext = splitext(absfile)

if len(ext) == 0:
ext = str_conversion('.cip')
fn = fn + ext
ext = ext[1:]

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
if filename:
str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
fn, ext = splitext(absfile)
if len(ext) == 0:
ext = str_conversion('.cip')
fn = fn + ext
ext = ext[1:]

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))

if verbose:
print('wrote problem to file ' + str_absfile)
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))

if verbose:
print('wrote problem to file ' + str_absfile)
if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))

locale.setlocale(locale.LC_NUMERIC,user_locale)

Expand Down
1 change: 1 addition & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_model():

s.writeProblem('model')
s.writeProblem('model.lp')
s.printProblem()

s.freeProb()
s = Model()
Expand Down

0 comments on commit d3e8ed0

Please sign in to comment.