Gurobi python change value of the defined value

微笑、不失礼 提交于 2021-02-19 12:52:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!