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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
- More support for AND-Constraints
- Added getLinearConsIndicator
- Added SCIP_LPPARAM, setIntParam, setRealParam, getIntParam, getRealParam, isOptimal, getObjVal, getRedcost for lpi
- Added isFeasPositive
Expand Down
10 changes: 10 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,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
118 changes: 118 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -5689,6 +5689,124 @@ cdef class Model:

return vars

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

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

Returns
-------
int

"""
cdef int nvars
cdef SCIP_Bool success

return SCIPgetConsNVarsAnd(self._scip, constraint.scip_cons)

def getConsVarsAnd(self, Constraint constraint):
"""
Gets variables in and constraint.

Parameters
----------
constraint : Constraint
Constraint to get the variables from.

Returns
-------
list of Variable

"""
cdef SCIP_VAR** _vars
cdef int nvars
cdef SCIP_Bool success
cdef int i

SCIPgetConsNVarsAnd(self._scip, constraint.scip_cons, &nvars, &success)
_vars = <SCIP_VAR**> malloc(nvars * sizeof(SCIP_VAR*))
_vars = SCIPgetConsVarsAnd(self._scip, constraint.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 constraint):
"""
Gets the resultant variable in And constraint.

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

Returns
-------
Variable

"""
cdef SCIP_VAR* _resultant
cdef SCIP_Bool success

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

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

return resultant

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

Parameters
----------
constraint : Constraint
Constraint to check.

Returns
-------
bool

"""
cdef SCIP_Bool success

return SCIPisAndConsSorted(self._scip, constraint.scip_cons)

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

Parameters
----------
constraint : Constraint
Constraint to sort.

"""
cdef SCIP_Bool success

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

def printCons(self, Constraint constraint):
"""
Print the constraint
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ 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.getNConsAnd(and_cons) == 1
vars = m.getVarsAnd(and_cons)
assert len(vars) == 2
assert vars[0] == x1
assert vars[1] == x2
resultant_var = m.getResultAnd(and_cons)
assert resultant_var == result
m.optimize()

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

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