Skip to content

Add support for SOS1 constraints #984

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 5 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
11 changes: 11 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,17 @@ cdef extern from "scip/cons_sos1.h":
SCIP_CONS* cons,
SCIP_VAR* var)

int SCIPgetNVarsSOS1(SCIP* scip, SCIP_CONS* cons)

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

SCIP_Real* SCIPgetWeightsSOS1(SCIP* scip, SCIP_CONS* cons)

SCIP_RETCODE SCIPmakeSOS1Feasible(SCIP* scip,
SCIP_CONSHDLR* conshdlr,
SCIP_SOL* solution,
SCIP_Bool* changed,
SCIP_Bool* success)

cdef extern from "scip/cons_sos2.h":
SCIP_RETCODE SCIPcreateConsSOS2(SCIP* scip,
Expand Down
101 changes: 71 additions & 30 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,43 @@ cdef class Variable(Expr):
else:
mayround = SCIPvarMayRoundUp(self.scip_var)
return mayround

def varIsSOS1(self, Conshdlr conshdlr, Variable var):
"""
Returns whether variable is part of the SOS1 conflict graph

Parameters
----------
conshdlr : Conshdlr
SOS1 constraint handler
var : Variable
variable to check

Returns
-------
bool
True if variable is part of the SOS1 conflict graph, False otherwise

"""
return SCIPvarIsSOS1(conshdlr.scip_conshdlr, var.scip_var)

def varGetNodeSOS1(self, Conshdlr conshdlr, Variable var):
"""
Returns SOS1 index of variable or -1 if variable is not part of the SOS1 conflict graph

Parameters
----------
conshdlr : Conshdlr
SOS1 constraint handler
var : Variable
variable

Returns
-------
int

"""
return SCIPvarGetNodeSOS1(conshdlr.scip_conshdlr, var.scip_var)

class MatrixVariable(MatrixExpr):

Expand Down Expand Up @@ -6471,6 +6508,40 @@ cdef class Model:

return Constraint.create(scip_cons)

def getWeightsSOS1(self, Constraint cons):
"""
Retrieve the coefficients of an SOS1 constraint

Parameters
----------
cons : Constraint
SOS1 constraint to get the coefficients of

Returns
-------
dict of str to float

"""
cdef SCIP_VAR** vars
cdef SCIP_Longint* vals
cdef int nvars
cdef int i

constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(cons.scip_cons))).decode('UTF-8')
if not constype == 'SOS1':
raise Warning("weights not available for constraints of type ", constype)

nvars = SCIPgetNVarsSOS1(self._scip, cons.scip_cons)
vals = SCIPgetWeightsSOS1(self._scip, cons.scip_cons)
vars = SCIPgetVarsSOS1(self._scip, cons.scip_cons)

valsdict = {}
for i in range(nvars):
var_name = bytes(SCIPvarGetName(vars[i])).decode('utf-8')
valsdict[var_name] = vals[i]

return valsdict

def addConsSOS2(self, vars, weights=None, name="",
initial=True, separate=True, enforce=True, check=True,
propagate=True, local=False, dynamic=False,
Expand Down Expand Up @@ -7119,36 +7190,6 @@ cdef class Model:
PY_SCIP_CALL(SCIPaddCons(self._scip, cons.scip_cons))
Py_INCREF(cons)

def addVarSOS1(self, Constraint cons, Variable var, weight):
"""
Add variable to SOS1 constraint.

Parameters
----------
cons : Constraint
SOS1 constraint
var : Variable
new variable
weight : weight
weight of new variable

"""
PY_SCIP_CALL(SCIPaddVarSOS1(self._scip, cons.scip_cons, var.scip_var, weight))

def appendVarSOS1(self, Constraint cons, Variable var):
"""
Append variable to SOS1 constraint.

Parameters
----------
cons : Constraint
SOS1 constraint
var : Variable
variable to append

"""
PY_SCIP_CALL(SCIPappendVarSOS1(self._scip, cons.scip_cons, var.scip_var))

def addVarSOS2(self, Constraint cons, Variable var, weight):
"""
Add variable to SOS2 constraint.
Expand Down