Skip to content

Commit b12dabd

Browse files
committed
Add getConsVals
1 parent 258f9bf commit b12dabd

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added addMatrixConsIndicator(), and tests
1010
- Added SCIPvarMarkRelaxationOnly, SCIPvarIsRelaxationOnly, SCIPvarMarkDeletable, SCIPvarIsDeletable, and tests
1111
- Wrapped SCIPgetNLPBranchCands
12+
- Added getConsVals, generalized getRhs, and tests
1213
### Fixed
1314
- Raised an error when an expression is used when a variable is required
1415
### Changed

src/pyscipopt/scip.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ cdef extern from "scip/scip.h":
858858
SCIP_RETCODE SCIPtransformCons(SCIP* scip, SCIP_CONS* cons, SCIP_CONS** transcons)
859859
SCIP_RETCODE SCIPgetTransformedCons(SCIP* scip, SCIP_CONS* cons, SCIP_CONS** transcons)
860860
SCIP_RETCODE SCIPgetConsVars(SCIP* scip, SCIP_CONS* cons, SCIP_VAR** vars, int varssize, SCIP_Bool* success)
861+
SCIP_RETCODE SCIPgetConsVals(SCIP* scip, SCIP_CONS* cons, SCIP_Real* vals, int valssize, SCIP_Bool* success)
861862
SCIP_RETCODE SCIPgetConsNVars(SCIP* scip, SCIP_CONS* cons, int* nvars, SCIP_Bool* success)
862863
SCIP_CONS** SCIPgetConss(SCIP* scip)
863864
const char* SCIPconsGetName(SCIP_CONS* cons)

src/pyscipopt/scip.pxi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6035,6 +6035,31 @@ cdef class Model:
60356035
vars.append(var)
60366036

60376037
return vars
6038+
6039+
def getConsVals(self, Constraint constraint):
6040+
"""
6041+
Returns the value array of an arbitrary SCIP constraint that can be represented as a single linear constraint.
6042+
6043+
Parameters
6044+
----------
6045+
constraint : Constraint
6046+
Constraint to get the values from.
6047+
6048+
Returns
6049+
-------
6050+
list of float
6051+
6052+
"""
6053+
cdef SCIP_Real* vals
6054+
cdef int nvars
6055+
cdef SCIP_Bool success
6056+
cdef int i
6057+
6058+
nvars = self.getConsNVars(constraint)
6059+
vals = <SCIP_Real*> malloc(nvars * sizeof(SCIP_Real))
6060+
PY_SCIP_CALL(SCIPgetConsVals(self._scip, constraint.scip_cons, vals, nvars*sizeof(SCIP_Real), &success))
6061+
6062+
return [vals[i] for i in range(nvars)]
60386063

60396064
def getNVarsAnd(self, Constraint and_cons):
60406065
"""
@@ -7275,6 +7300,8 @@ cdef class Model:
72757300
return SCIPgetRhsLinear(self._scip, cons.scip_cons)
72767301
elif constype == 'nonlinear':
72777302
return SCIPgetRhsNonlinear(cons.scip_cons)
7303+
elif constype == 'knapsack':
7304+
return SCIPgetCapacityKnapsack(self._scip, cons.scip_cons)
72787305
else:
72797306
raise Warning("method cannot be called for constraints of type " + constype)
72807307

tests/test_cons.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ def test_getConsVars():
2727
c = m.addCons(quicksum(x[i] for i in x) <= 1)
2828
assert m.getConsVars(c) == [x[i] for i in x]
2929

30+
def test_getConsVals():
31+
n_vars = 100
32+
m = Model()
33+
x = {}
34+
for i in range(n_vars):
35+
x[i] = m.addVar("%i" % i, vtype="B")
36+
37+
c1 = m.addCons(quicksum(x[i] for i in x) <= 1)
38+
c2 = m.addConsKnapsack([x[i] for i in x], [1]*n_vars, 10)
39+
vals1 = m.getConsVals(c1)
40+
vals2 = m.getConsVals(c2)
41+
42+
assert len(vals1) == n_vars
43+
assert all(isinstance(v, float) for v in vals1)
44+
assert len(vals2) == n_vars
45+
assert all(isinstance(v, float) for v in vals2)
3046

3147
def test_constraint_option_setting():
3248
m = Model()
@@ -266,6 +282,7 @@ def test_cons_knapsack():
266282
m.chgCapacityKnapsack(knapsack_cons, 5)
267283

268284
assert m.getCapacityKnapsack(knapsack_cons) == 5
285+
assert m.getRhs(knapsack_cons) == 5
269286

270287
m.addCoefKnapsack(knapsack_cons, z, 3)
271288
weights = m.getWeightsKnapsack(knapsack_cons)

0 commit comments

Comments
 (0)