Skip to content

Commit

Permalink
Add getnorigconss (#845)
Browse files Browse the repository at this point in the history
* Add getnorigconss

* Add test for getorigconss

* Add an explicit multiplier

* Add transformed option to getconss. Fix test

* Update CHANGELOG

* Update docstring

---------

Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
  • Loading branch information
Opt-Mucca and Joao-Dionisio committed May 4, 2024
1 parent 60ec985 commit e750b29
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
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 @@ -1774,13 +1774,15 @@ cdef class Model:

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 @@ cdef class Model:
"""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

0 comments on commit e750b29

Please sign in to comment.