问题
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
回答1:
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).
来源:https://stackoverflow.com/questions/21970789/gurobi-python-change-value-of-the-defined-value