Skip to content

Commit d6febca

Browse files
committed
generalize getRhs, getLhs
1 parent 51a1d75 commit d6febca

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Added SCIPvarMarkRelaxationOnly, SCIPvarIsRelaxationOnly, SCIPvarMarkDeletable, SCIPvarIsDeletable, and tests
1111
- Wrapped SCIPgetNLPBranchCands
1212
- Added getConsVals() and generalized getLhs() as well as getRhs() for linear type constraints
13+
- Generalized getRhs() to use SCIPconsGetRhs(), same for getLhs().
1314
### Fixed
1415
- Raised an error when an expression is used when a variable is required
1516
### Changed

src/pyscipopt/scip.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,8 @@ cdef extern from "scip/cons_linear.h":
14781478
SCIP_RETCODE SCIPchgRhsLinear(SCIP* scip, SCIP_CONS* cons, SCIP_Real rhs)
14791479
SCIP_Real SCIPgetLhsLinear(SCIP* scip, SCIP_CONS* cons)
14801480
SCIP_Real SCIPgetRhsLinear(SCIP* scip, SCIP_CONS* cons)
1481+
SCIP_Real SCIPconsGetRhs(SCIP* scip, SCIP_CONS* cons, SCIP_Bool* success)
1482+
SCIP_Real SCIPconsGetLhs(SCIP* scip, SCIP_CONS* cons, SCIP_Bool* success)
14811483
SCIP_RETCODE SCIPchgCoefLinear(SCIP* scip, SCIP_CONS* cons, SCIP_VAR* var, SCIP_Real newval)
14821484
SCIP_RETCODE SCIPdelCoefLinear(SCIP* scip, SCIP_CONS* cons, SCIP_VAR*)
14831485
SCIP_RETCODE SCIPaddCoefLinear(SCIP* scip, SCIP_CONS* cons, SCIP_VAR*, SCIP_Real val)

src/pyscipopt/scip.pxi

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,18 @@ cdef class Constraint:
21962196
constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(self.scip_cons))).decode('UTF-8')
21972197
return constype == 'knapsack'
21982198

2199+
def isLinearRepresentable(self):
2200+
"""
2201+
Returns True if constraint can be represented as a linear constraint.
2202+
2203+
Returns
2204+
-------
2205+
bool
2206+
2207+
"""
2208+
constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(self.scip_cons))).decode('UTF-8')
2209+
return constype in ('linear', 'knapsack', 'setppc')
2210+
21992211
def isNonlinear(self):
22002212
"""
22012213
Returns True if constraint is nonlinear.
@@ -7293,13 +7305,13 @@ cdef class Model:
72937305
float
72947306
72957307
"""
7308+
cdef SCIP_Bool success
72967309
constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(cons.scip_cons))).decode('UTF-8')
7297-
if constype == 'linear':
7298-
return SCIPgetRhsLinear(self._scip, cons.scip_cons)
7310+
7311+
if cons.isLinearRepresentable():
7312+
return SCIPconsGetRhs(self._scip, cons.scip_cons, &success)
72997313
elif constype == 'nonlinear':
73007314
return SCIPgetRhsNonlinear(cons.scip_cons)
7301-
elif constype == 'knapsack':
7302-
return SCIPgetCapacityKnapsack(self._scip, cons.scip_cons)
73037315
else:
73047316
raise Warning("method cannot be called for constraints of type " + constype)
73057317

@@ -7336,9 +7348,11 @@ cdef class Model:
73367348
float
73377349
73387350
"""
7351+
cdef SCIP_Bool success
73397352
constype = bytes(SCIPconshdlrGetName(SCIPconsGetHdlr(cons.scip_cons))).decode('UTF-8')
7340-
if constype == 'linear':
7341-
return SCIPgetLhsLinear(self._scip, cons.scip_cons)
7353+
7354+
if cons.isLinearRepresentable():
7355+
return SCIPconsGetLhs(self._scip, cons.scip_cons, &success)
73427356
elif constype == 'nonlinear':
73437357
return SCIPgetLhsNonlinear(cons.scip_cons)
73447358
else:

tests/test_cons.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ def test_cons_knapsack():
283283

284284
assert m.getCapacityKnapsack(knapsack_cons) == 5
285285
assert m.getRhs(knapsack_cons) == 5
286+
assert m.getLhs(knapsack_cons) == -m.infinity()
286287

287288
m.addCoefKnapsack(knapsack_cons, z, 3)
288289
weights = m.getWeightsKnapsack(knapsack_cons)

0 commit comments

Comments
 (0)