Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow writeProblem to write to stdout #926

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -272,7 +272,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Integration-test (3.8)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Windows-test (3.8)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Integration-test (3.9)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Windows-test (3.9)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Integration-test (3.10)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Windows-test (3.10)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Integration-test (3.11)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Windows-test (3.11)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Integration-test (3.12)

SCIP: unspecified error!

Check failure on line 275 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / Windows-test (3.12)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -2889,6 +2889,32 @@
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 @@
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
Loading