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

Add getnorigconss #845

Merged
merged 7 commits into from
May 4, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- 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
### Changed
### Removed
Expand Down
4 changes: 4 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,10 @@ cdef extern from "scip/scip.h":
SCIP_RETCODE SCIPdelVar(SCIP* scip, SCIP_VAR* var, SCIP_Bool* deleted)
SCIP_RETCODE SCIPaddCons(SCIP* scip, SCIP_CONS* cons)
SCIP_RETCODE SCIPdelCons(SCIP* scip, SCIP_CONS* cons)
SCIP_CONS** SCIPgetOrigConss(SCIP* scip)
int SCIPgetNOrigConss(SCIP* scip)
SCIP_CONS* SCIPfindOrigCons(SCIP* scip, const char*)
SCIP_CONS* SCIPfindCons(SCIP* scip, const char*)
SCIP_RETCODE SCIPsetObjsense(SCIP* scip, SCIP_OBJSENSE objsense)
SCIP_OBJSENSE SCIPgetObjsense(SCIP* scip)
SCIP_RETCODE SCIPsetObjlimit(SCIP* scip, SCIP_Real objlimit)
Expand Down
38 changes: 25 additions & 13 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

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

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -1774,13 +1774,15 @@

return vars

def getNVars(self):
"""Retrieve number of variables in the problems"""
return SCIPgetNVars(self._scip)

def getNConss(self):
"""Retrieve the number of constraints."""
return SCIPgetNConss(self._scip)
def getNVars(self, transformed=True):
"""Retrieve number of variables in the problems.

:param transformed: get transformed variables instead of original (Default value = True)
"""
if transformed:
return SCIPgetNVars(self._scip)
else:
return SCIPgetNOrigVars(self._scip)

def getNIntVars(self):
"""gets number of integer active problem variables"""
Expand Down Expand Up @@ -3366,19 +3368,29 @@
"""sets the value of the given variable in the global relaxation solution"""
PY_SCIP_CALL(SCIPsetRelaxSolVal(self._scip, NULL, var.scip_var, val))

def getConss(self):
"""Retrieve all constraints."""
def getConss(self, transformed=True):
"""Retrieve all constraints.

:param transformed: get transformed variables instead of original (Default value = True)
"""
cdef SCIP_CONS** _conss
cdef int _nconss
conss = []

_conss = SCIPgetConss(self._scip)
_nconss = SCIPgetNConss(self._scip)
if transformed:
_conss = SCIPgetConss(self._scip)
_nconss = SCIPgetNConss(self._scip)
else:
_conss = SCIPgetOrigConss(self._scip)
_nconss = SCIPgetNOrigConss(self._scip)
return [Constraint.create(_conss[i]) for i in range(_nconss)]

def getNConss(self):
def getNConss(self, transformed=True):
"""Retrieve number of all constraints"""
return SCIPgetNConss(self._scip)
if transformed:
return SCIPgetNConss(self._scip)
else:
return SCIPgetNOrigConss(self._scip)

def delCons(self, Constraint cons):
"""Delete constraint from the model
Expand Down
17 changes: 16 additions & 1 deletion tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def test_cons_indicator_fail():
m.setSolVal(sol, binvar, 0)
assert m.checkSol(sol) # solution should be feasible


def test_addConsCardinality():
m = Model()
x = {}
Expand All @@ -145,6 +144,22 @@ def test_addConsCardinality():

assert m.isEQ(m.getVal(quicksum(x[i] for i in range(5))), 3)

def test_getOrigConss():
m = Model()
x = m.addVar("x", lb=0, ub=2, obj=-1)
y = m.addVar("y", lb=0, ub=4, obj=0)
z = m.addVar("z", lb=0, ub=5, obj=2)
m.addCons(x <= y + z)
m.addCons(x <= z + 100)
m.addCons(y >= -100)
m.addCons(x + y <= 1000)
m.addCons(2* x + 2 * y <= 1000)
m.addCons(x + y + z <= 7)
m.optimize()
assert len(m.getConss(transformed=False)) == m.getNConss(transformed=False)
assert m.getNConss(transformed=False) == 6
assert m.getNConss(transformed=True) < m.getNConss(transformed=False)


def test_printCons():
m = Model()
Expand Down
Loading