Skip to content

Commit 8beb978

Browse files
committed
commit relative to upstream master
1 parent 32caac7 commit 8beb978

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

src/pyscipopt/scip.pxd

+100
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ cdef extern from "scip/scip.h":
333333
ctypedef struct SCIP_CONS:
334334
pass
335335

336+
ctypedef struct SCIP_DECOMP:
337+
pass
338+
336339
ctypedef struct SCIP_ROW:
337340
pass
338341

@@ -1552,6 +1555,95 @@ cdef extern from "scip/scip_cons.h":
15521555
SCIP_CONS* cons,
15531556
FILE* file)
15541557

1558+
cdef extern from "scip/pub_dcmp.h":
1559+
SCIP_RETCODE SCIPdecompCreate(SCIP_DECOMP** decomp,
1560+
BMS_BLKMEM* blkmem,
1561+
int nblocks,
1562+
SCIP_Bool original,
1563+
SCIP_Bool benderslabels)
1564+
1565+
SCIP_Bool SCIPdecompIsOriginal(SCIP_DECOMP *decomp)
1566+
1567+
void SCIPdecompSetUseBendersLabels(SCIP_DECOMP *decomp, SCIP_Bool benderslabels)
1568+
1569+
SCIP_Bool SCIPdecompUseBendersLabels(SCIP_DECOMP *decomp)
1570+
1571+
int SCIPdecompGetNBlocks(SCIP_DECOMP *decomp)
1572+
1573+
SCIP_Real SCIPdecompGetAreaScore(SCIP_DECOMP *decomp)
1574+
1575+
SCIP_Real SCIPdecompGetModularity(SCIP_DECOMP *decomp)
1576+
1577+
SCIP_RETCODE SCIPdecompGetVarsSize(SCIP_DECOMP *decomp, int *varssize, int nblocks)
1578+
1579+
SCIP_RETCODE SCIPdecompGetConssSize(SCIP_DECOMP *decomp, int *consssize, int nblocks)
1580+
1581+
int SCIPdecompGetNBorderVars(SCIP_DECOMP *decomp)
1582+
1583+
int SCIPdecompGetNBorderConss(SCIP_DECOMP *decomp)
1584+
1585+
int SCIPdecompGetNBlockGraphEdges(SCIP_DECOMP *decomp)
1586+
1587+
int SCIPdecompGetNBlockGraphComponents(SCIP_DECOMP *decomp)
1588+
1589+
int SCIPdecompGetNBlockGraphArticulations(SCIP_DECOMP *decomp)
1590+
1591+
int SCIPdecompGetBlockGraphMaxDegree(SCIP_DECOMP *decomp)
1592+
1593+
int SCIPdecompGetBlockGraphMinDegree(SCIP_DECOMP *decomp)
1594+
1595+
SCIP_RETCODE SCIPdecompSetVarsLabels(SCIP_DECOMP *decomp, SCIP_VAR **vrs, int *labels, int nvars)
1596+
1597+
void SCIPdecompGetVarsLabels(SCIP_DECOMP *decomp, SCIP_VAR **vrs, int *labels, int nvars)
1598+
1599+
SCIP_RETCODE SCIPdecompSetConsLabels(SCIP_DECOMP *decomp, SCIP_CONS **conss, int *labels, int nconss)
1600+
1601+
void SCIPdecompGetConsLabels(SCIP_DECOMP *decomp, SCIP_CONS **conss, int *labels, int nconss)
1602+
1603+
SCIP_RETCODE SCIPdecompClear(SCIP_DECOMP *decomp, SCIP_Bool clearvarlabels, SCIP_Bool clearconslabels)
1604+
1605+
char* SCIPdecompPrintStats(SCIP_DECOMP *decomp, char *strbuf)
1606+
1607+
cdef extern from "scip/scip_dcmp.h":
1608+
SCIP_RETCODE SCIPcreateDecomp(SCIP* scip,
1609+
SCIP_DECOMP** decomp,
1610+
int nblocks,
1611+
SCIP_Bool original,
1612+
SCIP_Bool benderslabels)
1613+
1614+
SCIP_RETCODE SCIPaddDecomp(SCIP* scip, SCIP_DECOMP* decomp)
1615+
1616+
void SCIPfreeDecomp(SCIP* scip, SCIP_DECOMP** decomp)
1617+
1618+
void SCIPgetDecomps(SCIP* scip,
1619+
SCIP_DECOMP*** decomp,
1620+
int* ndecomps,
1621+
SCIP_Bool original)
1622+
1623+
SCIP_RETCODE SCIPhasConsOnlyLinkVars(SCIP* scip,
1624+
SCIP_DECOMP* decomp,
1625+
SCIP_CONS *cons,
1626+
SCIP_Bool* hasonlylinkvars)
1627+
1628+
SCIP_RETCODE SCIPcomputeDecompConsLabels(SCIP* scip,
1629+
SCIP_DECOMP* decomp,
1630+
SCIP_CONS** conss,
1631+
int nconss)
1632+
1633+
SCIP_RETCODE SCIPcomputeDecompVarsLabels(SCIP* scip,
1634+
SCIP_DECOMP* decomp,
1635+
SCIP_CONS** conss,
1636+
int nconss)
1637+
SCIP_RETCODE SCIPassignDecompLinkConss(SCIP* scip,
1638+
SCIP_DECOMP* decomp,
1639+
SCIP_CONS** conss,
1640+
int* nskipconss)
1641+
1642+
SCIP_RETCODE SCIPcomputeDecompStats(SCIP* scip,
1643+
SCIP_DECOMP *decomp,
1644+
SCIP_CONS** conss,
1645+
int nconss,
1646+
int* nskipconss)
15551647
cdef extern from "blockmemshell/memory.h":
15561648
void BMScheckEmptyMemory()
15571649
long long BMSgetMemoryUsed()
@@ -1916,6 +2008,14 @@ cdef class Constraint:
19162008
@staticmethod
19172009
cdef create(SCIP_CONS* scipcons)
19182010

2011+
cdef class Decomposition:
2012+
cdef SCIP_DECOMP* scip_decomp
2013+
2014+
cdef public object data
2015+
2016+
@staticmethod
2017+
cdef create(SCIP_DECOMP* scip_decomp)
2018+
19192019
cdef class Model:
19202020
cdef SCIP* _scip
19212021
cdef SCIP_Bool* _valid

src/pyscipopt/scip.pxi

+125
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,128 @@ cdef class Constraint:
975975
return (self.__class__ == other.__class__
976976
and self.scip_cons == (<Constraint>other).scip_cons)
977977

978+
cdef class Decomposition:
979+
"""Base class holding a pointer to SCIP decomposition"""
980+
981+
@staticmethod
982+
cdef create(SCIP_DECOMP* scip_decomp):
983+
if scip_decomp == NULL:
984+
raise Warning("cannot create Constraint with SCIP_CONS* == NULL")
985+
decomp = Decomposition()
986+
decomp.scip_decomp = scip_decomp
987+
return decomp
988+
989+
def isOriginal(self):
990+
return SCIPdecompIsOriginal(self.scip_decomp)
991+
992+
def getAreaScore(self):
993+
return SCIPdecompGetAreaScore(self.scip_decomp)
994+
995+
def getModularity(self):
996+
return SCIPdecompGetModularity(self.scip_decomp)
997+
998+
def getNBorderVars(self):
999+
return SCIPdecompGetNBorderVars(self.scip_decomp)
1000+
1001+
def getNBorderConss(self):
1002+
return SCIPdecompGetNBorderConss(self.scip_decomp)
1003+
1004+
def getNBlockGraphEdges(self):
1005+
return SCIPdecompGetNBlockGraphEdges(self.scip_decomp)
1006+
1007+
def getNBlockGraphComponents(self):
1008+
return SCIPdecompGetNBlockGraphComponents(self.scip_decomp)
1009+
1010+
def getNblockGraphArticulations(self):
1011+
return SCIPdecompGetNBlockGraphArticulations(self.scip_decomp)
1012+
1013+
def getBlockGraphMaxDegree(self):
1014+
return SCIPdecompGetBlockGraphMaxDegree(self.scip_decomp)
1015+
1016+
def getBlockGraphMinDegree(self):
1017+
return SCIPdecompGetBlockGraphMinDegree(self.scip_decomp)
1018+
1019+
def getConsLabels(self, conss):
1020+
"""get {cons: label} pair for python constraints conss"""
1021+
cdef int nconss = <int> len(conss)
1022+
cdef int* labels = <int *> malloc(nconss * sizeof(int))
1023+
cdef SCIP_CONS** scip_conss = <SCIP_CONS**> malloc(nconss * sizeof(SCIP_CONS*))
1024+
1025+
for i in range(nconss):
1026+
scip_conss[i] = (<Constraint>conss[i]).scip_cons
1027+
1028+
PY_SCIP_CALL(SCIPdecompGetConsLabels(self.scip_decomp, scip_conss, labels, nconss))
1029+
1030+
cons_labels = {}
1031+
for i in range(nconss):
1032+
cons_labels[conss[i]] = labels[i]
1033+
free(labels)
1034+
free(scip_conss)
1035+
return cons_labels
1036+
1037+
def setConsLabels(self, cons_labels):
1038+
""" applies labels to constraints in decomposition.
1039+
:param cons_labels dict of {constraint: label} pairs to be applied
1040+
"""
1041+
cons_labels = cons_labels.items()
1042+
1043+
cdef int nconss = len(cons_labels)
1044+
cdef SCIP_CONS** scip_conss = <SCIP_CONS**> malloc(nconss* sizeof(SCIP_CONS*))
1045+
cdef int* labels = <int*> malloc(nconss * sizeof(int))
1046+
1047+
for i in range(nconss):
1048+
scip_conss[i] = (<Constraint>cons_labels[i][0]).scip_cons
1049+
labels[i] = cons_labels[i][1]
1050+
1051+
PY_SCIP_CALL(SCIPdecompSetConsLabels(self.scip_decomp, scip_conss, labels, nconss))
1052+
1053+
free(scip_conss)
1054+
free(labels)
1055+
1056+
def getVarsLabels(self, vrs):
1057+
"""get {var: label} pairs for python variables vrs"""
1058+
1059+
cdef int nvars = <int> len(vrs)
1060+
cdef int* labels = <int*> malloc(nvars * sizeof(int))
1061+
cdef SCIP_VAR** scip_vars = <SCIP_VAR**> malloc(nvars * sizeof(SCIP_VAR*))
1062+
1063+
for i in range(nvars):
1064+
scip_vars[i] = (<Variable>vrs[i]).scip_var
1065+
1066+
PY_SCIP_CALL(SCIPdecompGetVarsLabels(self.scip_decomp, scip_vars, labels, nvars))
1067+
1068+
var_labels = {}
1069+
for i in range(nvars):
1070+
var_labels[vrs[i]] = labels[i]
1071+
1072+
free(labels)
1073+
free(scip_vars)
1074+
return var_labels
1075+
1076+
def setVarLabels(self, var_labels):
1077+
"""set {var: label} pairs in decomposition"""
1078+
var_labels = var_labels.items()
1079+
1080+
cdef int nvars= len(var_labels)
1081+
cdef SCIP_VAR** scip_vars = <SCIP_VAR**> malloc(nvars * sizeof(SCIP_VAR*))
1082+
cdef int* labels = <int*> malloc(nvars * sizeof(int))
1083+
1084+
for i in range(nvars):
1085+
scip_vars[i] = (<Variable>var_labels[i][0]).scip_var
1086+
labels[i] = var_labels[i][1]
1087+
1088+
PY_SCIP_CALL(SCIPdecompSetVarsLabels(self.scip_decomp, scip_vars, labels, nvars))
1089+
1090+
free(scip_vars)
1091+
free(labels)
1092+
1093+
def clear(self, varlabels =True, conslabels = True):
1094+
"""clears variable and/or constraint labels from decomposition"""
1095+
1096+
if not (varlabels or conslabels):
1097+
... # TODO does decomp clear do anything if both options are false?
1098+
else:
1099+
PY_SCIP_CALL(SCIPdecompClear(self.scip_decomp, varlabels, conslabels))
9781100

9791101
cdef void relayMessage(SCIP_MESSAGEHDLR *messagehdlr, FILE *file, const char *msg) noexcept:
9801102
sys.stdout.write(msg.decode('UTF-8'))
@@ -3422,6 +3544,9 @@ cdef class Model:
34223544
"""Presolve the problem."""
34233545
PY_SCIP_CALL(SCIPpresolve(self._scip))
34243546

3547+
def addDecomposition(self, Decomposition decomp):
3548+
PY_SCIP_CALL(SCIPaddDecomp(self._scip, decomp.scip_decomp))
3549+
34253550
# Benders' decomposition methods
34263551
def initBendersDefault(self, subproblems):
34273552
"""initialises the default Benders' decomposition with a dictionary of subproblems

0 commit comments

Comments
 (0)