From 1c26a840cc6ea6e9b4725f73c7a853512e820019 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 6 Mar 2025 12:20:54 +0100 Subject: [PATCH 1/3] Add getLinearConsIndicator --- CHANGELOG.md | 1 + src/pyscipopt/scip.pxd | 1 + src/pyscipopt/scip.pxi | 17 +++++++++++++++++ tests/test_cons.py | 2 ++ 4 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6367f3ca..f310be426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ### Added +- Added getLinearConsIndicator ### Fixed ### Changed ### Removed diff --git a/src/pyscipopt/scip.pxd b/src/pyscipopt/scip.pxd index 4f6084e0a..b05ec4e8a 100644 --- a/src/pyscipopt/scip.pxd +++ b/src/pyscipopt/scip.pxd @@ -1828,6 +1828,7 @@ cdef extern from "scip/cons_indicator.h": SCIP_Real val) SCIP_VAR* SCIPgetSlackVarIndicator(SCIP_CONS* cons) + SCIP_CONS* SCIPgetLinearConsIndicator(SCIP_CONS* cons) cdef extern from "scip/misc.h": SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP** hashmap, BMS_BLKMEM* blkmem, int mapsize) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index 93362f5c3..749048ed9 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -6229,6 +6229,23 @@ cdef class Model: PY_SCIP_CALL(SCIPreleaseCons(self._scip, &scip_cons)) return pyCons + + def getLinearConsIndicator(self, Constraint cons): + """ + Get the linear constraint corresponding to the indicator constraint. + + Parameters + ---------- + cons : Constraint + The indicator constraint + + Returns + ------- + Constraint + """ + + cdef SCIP_CONS* lincons = SCIPgetLinearConsIndicator(cons.scip_cons); + return Constraint.create(lincons) def getSlackVarIndicator(self, Constraint cons): """ diff --git a/tests/test_cons.py b/tests/test_cons.py index 42801bd93..ab53e9a24 100644 --- a/tests/test_cons.py +++ b/tests/test_cons.py @@ -118,6 +118,8 @@ def test_cons_indicator(): slack = m.getSlackVarIndicator(c1) + lin_cons = m.getLinearConsIndicator(c1) + m.optimize() assert m.getNConss(transformed=False) == 5 From 75b77362f2309c7f84d8db212cdba1fa3a7d80d0 Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 6 Mar 2025 14:05:38 +0100 Subject: [PATCH 2/3] Add check for NULL return --- src/pyscipopt/scip.pxi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index 749048ed9..bfc54e60b 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -6244,7 +6244,9 @@ cdef class Model: Constraint """ - cdef SCIP_CONS* lincons = SCIPgetLinearConsIndicator(cons.scip_cons); + cdef SCIP_CONS* lincons = SCIPgetLinearConsIndicator(cons.scip_cons) + if lincons == NULL: + return None return Constraint.create(lincons) def getSlackVarIndicator(self, Constraint cons): @@ -6262,7 +6264,7 @@ cdef class Model: Variable """ - cdef SCIP_VAR* var = SCIPgetSlackVarIndicator(cons.scip_cons); + cdef SCIP_VAR* var = SCIPgetSlackVarIndicator(cons.scip_cons) return Variable.create(var) def addPyCons(self, Constraint cons): From ff9c0dd8d09f78229cd0d8405dc368e5ce6b999f Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Thu, 6 Mar 2025 14:14:03 +0100 Subject: [PATCH 3/3] Update return type --- src/pyscipopt/scip.pxi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index bfc54e60b..b91acdd4d 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -6241,7 +6241,7 @@ cdef class Model: Returns ------- - Constraint + Constraint or None """ cdef SCIP_CONS* lincons = SCIPgetLinearConsIndicator(cons.scip_cons)