Question

I have a question about how to change a coefficient in the constraint. For example, I have the following constraint:

lhs.addTerms(temp, x[i])
model.addConstr(cost, GRB.EQUAL, 1.0/a*lhs, 'cost_cons')

If I want change "temp" in the next iteration, how to modify the model? Thanks very much

Was it helpful?

Solution

Could you give a little more information? What I think you want to do is change the coefficients of x[i] in that particular constraint. If that's it, then you should save the constraint by assigned it to a variable (or adding it to a list):

lhs.addTerms(temp, x[i])
savedConstraint = model.addConstr(cost, GRB.EQUAL, 1.0/a*lhs, 'cost_cons')

Then, in the next iteration, you can use chgCoeff (see http://www.gurobi.com/documentation/5.6/reference-manual/py_model_chgcoeff).

model.chgCoef(savedConstraint,x[i],newtemp)

Is that what you were looking for? Often people will save the constraints to a list. Lastly, you might want to be consistent with your naming (lhs on the right side is a bit off).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top