Skip to content

Add support for OR-constraints #983

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 1 commit 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
- Added support for OR-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 @@ -1648,6 +1648,16 @@ cdef extern from "scip/cons_xor.h":
SCIP_Bool dynamic,
SCIP_Bool removable,
SCIP_Bool stickingatnode)

int SCIPgetNVarsOr(SCIP* scip,
SCIP_CONS* cons)

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

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

cdef extern from "scip/scip_cons.h":
SCIP_RETCODE SCIPprintCons(SCIP* scip,
SCIP_CONS* cons,
Expand Down
75 changes: 75 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -6039,6 +6039,81 @@ cdef class Model:

return pyCons

def getNVarsOr(self, Constraint constraint):
"""
Get number of variables in an OR-constraint.

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

Returns
-------
int

"""
cdef int nvars
return SCIPgetNVarsOr(self._scip, constraint.scip_cons)

def getVarsOr(self, Constraint constraint):
"""
Get variables in an OR-constraint.

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

Returns
-------
list of Variable
"""
cdef SCIP_VAR** _vars
cdef int nvars
cdef int i
cdef list vars = []
nvars = SCIPgetNVarsOr(self._scip, constraint.scip_cons)
_vars = <SCIP_VAR**> malloc(nvars * sizeof(SCIP_VAR*))
_vars = SCIPgetVarsOr(self._scip, constraint.scip_cons)
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)
free(_vars)
return vars

def getResultantOr(self, Constraint constraint):
"""
Get the resultant variable of an OR-constraint.

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

Returns
-------
Variable
"""
cdef SCIP_VAR* _resvar
_resvar = SCIPgetResultantOr(self._scip, constraint.scip_cons)
ptr = <size_t>(_resvar)
# check whether the corresponding variable exists already
if ptr not in self._modelvars:
# create a new variable
var = Variable.create(_resvar)
assert var.ptr() == ptr
self._modelvars[ptr] = var
return self._modelvars[ptr]

def addConsXor(self, vars, rhsvar, name="XORcons",
initial=True, separate=True, enforce=True, check=True,
propagate=True, local=False, modifiable=False, dynamic=False,
Expand Down