Skip to content

Commit

Permalink
Fix locale fix (#843)
Browse files Browse the repository at this point in the history
* Fix overambitious locale

* Update CHANGELOG

* Add locale fix to write methods

* Write locale test

* Update CHANGELOG

* Fix path

---------

Co-authored-by: Mohammed Ghannam <ghannam@zib.de>
  • Loading branch information
Joao-Dionisio and mmghannam authored May 5, 2024
1 parent e750b29 commit 001f423
Show file tree
Hide file tree
Showing 4 changed files with 690 additions and 39 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

## Unreleased
### Added
- Expanded locale test
- Added methods for creating expression constraints without adding to problem
- Added methods for creating/adding/appending disjunction constraints
- Added check for pt_PT locale in test_model.py
- Added SCIPgetOrigConss and SCIPgetNOrigConss Cython bindings.
- Added transformed=False option to getConss, getNConss, and getNVars
### Fixed
- Fixed locale errors in reading
### Changed
### Removed

Expand All @@ -18,6 +20,7 @@
- Add SCIP function SCIPgetTreesizeEstimation and wrapper getTreesizeEstimation
- New test for model setLogFile
### Fixed
- Fixed locale fix
- Fixed model.setLogFile(None) error
- Add recipes sub-package
- Fixed "weakly-referenced object no longer exists" when calling dropEvent in test_customizedbenders
Expand Down
101 changes: 62 additions & 39 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1129,12 +1129,12 @@ cdef class Model:

def printVersion(self):
"""Print version, copyright information and compile mode"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

SCIPprintVersion(self._scip, NULL)

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def getProbName(self):
"""Retrieve problem name"""
Expand Down Expand Up @@ -1463,8 +1463,8 @@ cdef class Model:
:param genericnames: indicates whether the problem should be written with generic variable and constraint names (Default value = False)
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
Expand All @@ -1479,7 +1479,7 @@ cdef class Model:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))
print('wrote problem to file ' + str_absfile)

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

# Variable Functions

Expand Down Expand Up @@ -4495,13 +4495,13 @@ cdef class Model:
"""writes current LP to a file
:param filename: file name (Default value = "LP.lp")
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

absfile = str_conversion(abspath(filename))
PY_SCIP_CALL( SCIPwriteLP(self._scip, absfile) )

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def createSol(self, Heur heur = None):
"""Create a new primal solution.
Expand Down Expand Up @@ -4540,12 +4540,12 @@ cdef class Model:

def printBestSol(self, write_zeros=False):
"""Prints the best feasible primal solution."""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

PY_SCIP_CALL(SCIPprintBestSol(self._scip, NULL, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def printSol(self, Solution solution=None, write_zeros=False):
"""Print the given primal solution.
Expand All @@ -4555,15 +4555,15 @@ cdef class Model:
write_zeros -- include variables that are set to zero
"""

user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

if solution is None:
PY_SCIP_CALL(SCIPprintSol(self._scip, NULL, NULL, write_zeros))
else:
PY_SCIP_CALL(SCIPprintSol(self._scip, solution.sol, NULL, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeBestSol(self, filename="origprob.sol", write_zeros=False):
"""Write the best feasible primal solution to a file.
Expand All @@ -4573,16 +4573,16 @@ cdef class Model:
write_zeros -- include variables that are set to zero
"""

user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

# use this doubled opening pattern to ensure that IOErrors are
# triggered early and in Python not in C,Cython or SCIP.
with open(filename, "w") as f:
cfile = fdopen(f.fileno(), "w")
PY_SCIP_CALL(SCIPprintBestSol(self._scip, cfile, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeBestTransSol(self, filename="transprob.sol", write_zeros=False):
"""Write the best feasible primal solution for the transformed problem to a file.
Expand All @@ -4591,16 +4591,16 @@ cdef class Model:
filename -- name of the output file
write_zeros -- include variables that are set to zero
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

# use this double opening pattern to ensure that IOErrors are
# triggered early and in python not in C, Cython or SCIP.
with open(filename, "w") as f:
cfile = fdopen(f.fileno(), "w")
PY_SCIP_CALL(SCIPprintBestTransSol(self._scip, cfile, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeSol(self, Solution solution, filename="origprob.sol", write_zeros=False):
"""Write the given primal solution to a file.
Expand All @@ -4610,16 +4610,16 @@ cdef class Model:
filename -- name of the output file
write_zeros -- include variables that are set to zero
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

# use this doubled opening pattern to ensure that IOErrors are
# triggered early and in Python not in C,Cython or SCIP.
with open(filename, "w") as f:
cfile = fdopen(f.fileno(), "w")
PY_SCIP_CALL(SCIPprintSol(self._scip, solution.sol, cfile, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeTransSol(self, Solution solution, filename="transprob.sol", write_zeros=False):
"""Write the given transformed primal solution to a file.
Expand All @@ -4629,16 +4629,16 @@ cdef class Model:
filename -- name of the output file
write_zeros -- include variables that are set to zero
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

# use this doubled opening pattern to ensure that IOErrors are
# triggered early and in Python not in C,Cython or SCIP.
with open(filename, "w") as f:
cfile = fdopen(f.fileno(), "w")
PY_SCIP_CALL(SCIPprintTransSol(self._scip, solution.sol, cfile, write_zeros))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

# perhaps this should not be included as it implements duplicated functionality
# (as does it's namesake in SCIP)
Expand All @@ -4648,9 +4648,14 @@ cdef class Model:
Keyword arguments:
filename -- name of the input file
"""
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

absfile = str_conversion(abspath(filename))
PY_SCIP_CALL(SCIPreadSol(self._scip, absfile))

locale.setlocale(locale.LC_NUMERIC, user_locale)

def readSolFile(self, filename):
"""Reads a given solution file.
Expand All @@ -4668,7 +4673,14 @@ cdef class Model:
str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
solution = self.createSol()

user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

PY_SCIP_CALL(SCIPreadSolFile(self._scip, absfile, solution.sol, False, &partial, &error))

locale.setlocale(locale.LC_NUMERIC, user_locale)

if error:
raise Exception("SCIP: reading solution from file " + str_absfile + " failed!")

Expand Down Expand Up @@ -4896,12 +4908,12 @@ cdef class Model:
:param Variable var: variable
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

PY_SCIP_CALL(SCIPwriteVarName(self._scip, NULL, var.scip_var, False))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def getStage(self):
"""Retrieve current SCIP stage"""
Expand Down Expand Up @@ -5032,29 +5044,29 @@ cdef class Model:

def printStatistics(self):
"""Print statistics."""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

PY_SCIP_CALL(SCIPprintStatistics(self._scip, NULL))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeStatistics(self, filename="origprob.stats"):
"""Write statistics to a file.
Keyword arguments:
filename -- name of the output file
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

# use this doubled opening pattern to ensure that IOErrors are
# triggered early and in Python not in C,Cython or SCIP.
with open(filename, "w") as f:
cfile = fdopen(f.fileno(), "w")
PY_SCIP_CALL(SCIPprintStatistics(self._scip, cfile))

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def getNLPs(self):
"""gets total number of LPs solved so far"""
Expand Down Expand Up @@ -5241,7 +5253,13 @@ cdef class Model:
"""
absfile = str_conversion(abspath(file))

user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

PY_SCIP_CALL(SCIPreadParams(self._scip, absfile))

locale.setlocale(locale.LC_NUMERIC, user_locale)

def writeParams(self, filename='param.set', comments = True, onlychanged = True):
"""Write parameter settings to an external file.
Expand All @@ -5251,15 +5269,15 @@ cdef class Model:
:param onlychanged: write only modified parameters (Default value = True)
"""
user_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL, "C")
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
PY_SCIP_CALL(SCIPwriteParams(self._scip, absfile, comments, onlychanged))
print('wrote parameter settings to file ' + str_absfile)

locale.setlocale(locale.LC_ALL, user_locale)
locale.setlocale(locale.LC_NUMERIC,user_locale)

def resetParam(self, name):
"""Reset parameter setting to its default value
Expand Down Expand Up @@ -5290,13 +5308,18 @@ cdef class Model:
:param extension: specify file extension/type (Default value = None)
"""
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

absfile = str_conversion(abspath(filename))
if extension is None:
PY_SCIP_CALL(SCIPreadProb(self._scip, absfile, NULL))
else:
extension = str_conversion(extension)
PY_SCIP_CALL(SCIPreadProb(self._scip, absfile, extension))

locale.setlocale(locale.LC_NUMERIC, user_locale)

# Counting functions

def count(self):
Expand Down
Loading

0 comments on commit 001f423

Please sign in to comment.