Skip to content

Support for AND-constraints #980

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -3,6 +3,7 @@
## Unreleased
### Added
- Added support for knapsack constraints
- More support for AND-Constraints
### Fixed
### Changed
### Removed
Expand Down
10 changes: 10 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,16 @@ cdef extern from "scip/cons_and.h":
SCIP_Bool removable,
SCIP_Bool stickingatnode)

int SCIPgetNVarsAnd(SCIP* scip, SCIP_CONS* cons)

SCIP_VAR** SCIPgetVarsAnd(SCIP* scip, SCIP_CONS* cons)

SCIP_VAR* SCIPgetResultantAnd(SCIP* scip, SCIP_CONS* cons)

SCIP_Bool SCIPisAndConsSorted(SCIP* scip, SCIP_CONS* cons)

SCIP_RETCODE SCIPsortAndCons(SCIP* scip, SCIP_CONS* cons)

cdef extern from "scip/cons_or.h":
SCIP_RETCODE SCIPcreateConsOr(SCIP* scip,
SCIP_CONS** cons,
Expand Down
123 changes: 123 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 301 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 @@ -5778,6 +5778,129 @@

return vars

def getNVarsAnd(self, Constraint and_cons):
"""
Gets number of variables in and constraint.

Parameters
----------
and_cons : Constraint
AND constraint to get the number of variables from.

Returns
-------
int

"""
cdef int nvars
cdef SCIP_Bool success

return SCIPgetNVarsAnd(self._scip, and_cons.scip_cons)

def getVarsAnd(self, Constraint and_cons):
"""
Gets variables in AND constraint.

Parameters
----------
and_cons : Constraint
AND Constraint to get the variables from.

Returns
-------
list of Variable

"""
cdef SCIP_VAR** _vars
Copy link
Preview

Copilot AI May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allocated memory for _vars via malloc is immediately overwritten by the call to SCIPgetVarsAnd, which may lead to a memory leak. Consider removing the malloc call and directly assigning the return value from SCIPgetVarsAnd.

Copilot uses AI. Check for mistakes.

cdef int nvars
cdef SCIP_Bool success
cdef int i

constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(and_cons.scip_cons))).decode('UTF-8')
assert(constype == 'and', "The constraint handler %s does not have this functionality." % constype)

nvars = SCIPgetNVarsAnd(self._scip, and_cons.scip_cons)
_vars = <SCIP_VAR**> malloc(nvars * sizeof(SCIP_VAR*))
_vars = SCIPgetVarsAnd(self._scip, and_cons.scip_cons)

vars = []
for i in range(nvars):
ptr = <size_t>(_vars[i])
# check whether the corresponding variable exists already
if ptr in self._modelvars:
vars.append(self._modelvars[ptr])
else:
# create a new variable
var = Variable.create(_vars[i])
assert var.ptr() == ptr
self._modelvars[ptr] = var
vars.append(var)

return vars

def getResultantAnd(self, Constraint and_cons):
"""
Gets the resultant variable in And constraint.

Parameters
----------
and_cons : Constraint
Constraint to get the resultant variable from.

Returns
-------
Variable

"""
cdef SCIP_VAR* _resultant
cdef SCIP_Bool success

_resultant = SCIPgetResultantAnd(self._scip, and_cons.scip_cons)

ptr = <size_t>(_resultant)
# check whether the corresponding variable exists already
if ptr not in self._modelvars:
# create a new variable
resultant = Variable.create(_resultant)
assert resultant.ptr() == ptr
self._modelvars[ptr] = resultant
else:
resultant = self._modelvars[ptr]

return resultant

def isAndConsSorted(self, Constraint and_cons):
"""
Returns if the variables of the AND-constraint are sorted with respect to their indices.

Parameters
----------
and_cons : Constraint
Constraint to check.

Returns
-------
bool

"""
cdef SCIP_Bool success

return SCIPisAndConsSorted(self._scip, and_cons.scip_cons)

def sortAndCons(self, Constraint and_cons):
"""
Sorts the variables of the AND-constraint with respect to their indices.

Parameters
----------
and_cons : Constraint
Constraint to sort.

"""
cdef SCIP_Bool success

PY_SCIP_CALL(SCIPsortAndCons(self._scip, and_cons.scip_cons))

def printCons(self, Constraint constraint):
"""
Print the constraint
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ def test_cons_logical():
assert m.isEQ(m.getVal(result1), 1)
assert m.isEQ(m.getVal(result2), 0)

def test_cons_and():
m = Model()
x1 = m.addVar(vtype="B")
x2 = m.addVar(vtype="B")
result = m.addVar(vtype="B")

and_cons = m.addConsAnd([x1, x2], result)

assert m.getNVarsAnd(and_cons) == 2
assert m.getVarsAnd(and_cons) == [x1, x2]
resultant_var = m.getResultantAnd(and_cons)
assert resultant_var is result
m.optimize()

m.sortAndCons(and_cons)
assert m.isAndConsSorted(and_cons)

def test_SOScons():
m = Model()
x = {}
Expand Down