@@ -60,6 +60,16 @@ cdef class SCIPBackend(GenericBackend):
60
60
self .obj_constant_term = 0.0
61
61
self .variables = []
62
62
self .model.hideOutput()
63
+ # always set this to None if the list of constraints may change
64
+ self .constraints = None
65
+
66
+ def get_constraints (self ):
67
+ """
68
+ Get all constraints of the problem.
69
+ """
70
+ if self .constraints is None :
71
+ self .constraints = self .model.getConss()
72
+ return self .constraints
63
73
64
74
cpdef _get_model(self ):
65
75
"""
@@ -360,7 +370,9 @@ cdef class SCIPBackend(GenericBackend):
360
370
raise ValueError (" The constraint's index i must satisfy 0 <= i < number_of_constraints" )
361
371
if self .model.getStatus() != ' unknown' :
362
372
self .model.freeTransform()
363
- self .model.delCons(self .model.getConss()[i])
373
+ self .constraints = None
374
+ self .model.delCons(self .get_constraints()[i])
375
+ self .constraints = None
364
376
365
377
cpdef add_linear_constraint(self , coefficients, lower_bound, upper_bound, name = None ):
366
378
"""
@@ -408,6 +420,7 @@ cdef class SCIPBackend(GenericBackend):
408
420
409
421
cons = lower_bound <= (linfun <= upper_bound)
410
422
self .model.addCons(cons, name = name)
423
+ self .constraints = None
411
424
412
425
cpdef row(self , int index):
413
426
r """
@@ -438,7 +451,7 @@ cdef class SCIPBackend(GenericBackend):
438
451
( 2. 0, 2. 0)
439
452
"""
440
453
namedvars = [var.name for var in self .variables]
441
- valslinear = self .model.getValsLinear(self .model.getConss ()[index])
454
+ valslinear = self .model.getValsLinear(self .get_constraints ()[index])
442
455
cdef list indices = []
443
456
cdef list values = []
444
457
for var, coeff in valslinear.iteritems():
@@ -470,7 +483,7 @@ cdef class SCIPBackend(GenericBackend):
470
483
sage: p.row_bounds(0)
471
484
(2.0, 2.0)
472
485
"""
473
- cons = self .model.getConss ()[index]
486
+ cons = self .get_constraints ()[index]
474
487
lhs = self .model.getLhs(cons)
475
488
rhs = self .model.getRhs(cons)
476
489
if lhs == - self .model.infinity():
@@ -548,7 +561,8 @@ cdef class SCIPBackend(GenericBackend):
548
561
sage: p.nrows()
549
562
5
550
563
"""
551
- mcons = self .model.getConss()
564
+ mcons = self .get_constraints()
565
+ self .constraints = None # is this necessary?
552
566
# after update of
553
567
index = self .add_variable(lower_bound = - self .model.infinity())
554
568
var = self .variables[index]
@@ -770,7 +784,7 @@ cdef class SCIPBackend(GenericBackend):
770
784
sage: lp. get_row_prim( 2)
771
785
8. 0
772
786
"""
773
- return self .model.getActivity(self .model.getConss ()[i])
787
+ return self .model.getActivity(self .get_constraints ()[i])
774
788
775
789
cpdef int ncols(self ):
776
790
"""
@@ -803,7 +817,7 @@ cdef class SCIPBackend(GenericBackend):
803
817
sage: p.nrows()
804
818
2
805
819
"""
806
- return len (self .model.getConss ())
820
+ return len (self .get_constraints ())
807
821
808
822
cpdef col_name(self , int index):
809
823
"""
@@ -840,7 +854,7 @@ cdef class SCIPBackend(GenericBackend):
840
854
sage: p.row_name(0)
841
855
'Empty constraint 1'
842
856
"""
843
- return self .model.getConss ()[index].name
857
+ return self .get_constraints ()[index].name
844
858
845
859
cpdef bint is_variable_binary(self , int index):
846
860
"""
@@ -1159,10 +1173,13 @@ cdef class SCIPBackend(GenericBackend):
1159
1173
assert self .ncols() == cp.ncols()
1160
1174
1161
1175
for i in range (self .nrows()):
1162
- cp.add_linear_constraint(zip (* self .row(i)),
1163
- self .row_bounds(i)[0 ],
1164
- self .row_bounds(i)[1 ],
1165
- name = self .row_name(i))
1176
+ coefficients = zip (* self .row(i))
1177
+ lower_bound, upper_bound = self .row_bounds(i)
1178
+ name = self .row_name(i)
1179
+ cp.add_linear_constraint(coefficients,
1180
+ lower_bound,
1181
+ upper_bound,
1182
+ name = name)
1166
1183
assert self .nrows() == cp.nrows()
1167
1184
return cp
1168
1185
0 commit comments