model update
AnsweredI am working on updating RHS values for specific set of constraints that are beyond the feasibility tolerance. I am using the following code chunk. Couple of questions:
1. Is there an efficient way to achieve this.
2. I notice that model.update() doesn't really update the RHS values, is this expected behavior of model.update()
FEASIBILITY_TOLERANCE = 1e08import numpy as nprhs = np.array(model.getAttr("RHS", model.getConstrs()))rhs_idx = np.where(np.logical_and(rhs > 0, rhs < FEASIBILITY_TOLERANCE))print(""*60)if len(rhs_idx[0]) != []:for i in rhs_idx[0]:model.setAttr("RHS", model.getConstrs()[i], 0)print(rhs_idx)model.update()model.printStats()Statistics for modelUnnamed: Linear constraint matrix : 7282679 Constrs, 4551026 Vars, 34510656 NZs Variable types : 4155398 Continuous, 395628 Integer (132032 Binary)Matrix coefficient range : [ 0.009758, 988.02 ]Objective coefficient range : [ 0.0525596, 990 ]Variable bound range : [ 0.009, 2.42 ]RHS coefficient range : [ 3.46945e18, 42.0458 ]

Hi Krishna,
I think the following would be more efficient
FEASIBILITY_TOLERANCE = 1e08
cons = [c for c in model.getConstrs() if 0 < c.RHS < FEASIBILITY_TOLERANCE]
model.setAttr("RHS", cons, 0)I could not replicate your issue using your code on another model. The update() method should result in changes to the printStats() output. Can you double check your code, and insert another update() just after the line where you import numpy, as one possible explanation is that getConstrs() isn't returning anything  which can happen if the model has been built but not updated.
 Riley
0 
Hi Riley,
Thank you for your feedback. I executed the same block of code twice. I find that the model does indeed update however doesn't print the updated matrix. I print the length of cons which is 15 in first execution and zero in 2nd execution. (Cells 18 and 19 in screenshots below)
I also wrote the model to lp file and read it back and still see the RHS coefficients not updated, again no constraints can be found below feasibility tolerance. (Cells 2023 in screenshot below)
I am not sure if this is related to size of the matrix. I tried this on a small problem with 10 constraints and see the printStats does update the model.
Kindly advise if you have anything else I can try.
0 
Hi Krishna,
I'm guessing there may be small negative RHS values. Can you try with
cons = [c for c in m.getConstrs() if FEASIBILITY_TOLERANCE < c.RHS < FEASIBILITY_TOLERANCE]
 Riley
0 
Hi Riley,
Thank you, this worked and RHS coefficient range did improve substantially. Appreciate your time and feedback!
Regards,
Krishna
0
Please sign in to leave a comment.
Comments
4 comments