How to update the model after editing the coefficients.
回答済みI am doing a job in which I first create some dictionaries with the coefficients that appear in both the linear constraints and the objective function.
Then I create a MILP (MIP) model with dozens of pairs of variables of the type with some dummy values (for the sake of not getting error while attempting to create the model. The workflow I am following is: create dummy coefficients -> create the model -> update the values of the coefficients with real values -> update the model -> solve it)
I = {i1,i2,...,in}
T =set(t for t in range(0,k))
Coefficients:
C[i,t] = value #coefficients
a = 123
b = -0.456
...
Variables:
V = Model.addConstrs(I,T,name="name") #variables
And then all of the constraints and the objective function.
Then I input the correct values of the coefficients in their dictionaries, call Model.update() and later Model.solve().
The behaviour I am expecting is that the model takes in the new values of the coefficients and then is solved giving the appropriate solution.
What actually happens is that the model keeps the old values of the coefficients and it seems it ignores the changes in the dictionaries.
The workaround I am using is to re-create the model each time I update the coefficients. Since I will be using a for loop to solve the same model with new coefficients each time, and since the model in the future will have a high value of i and t, I would like to avoid to unnecessarily create the model from scratch each time I need to update some values.
Is there a way to solve this situation? I am not very expert on this topic, so please be patient and explain things as simple as you can.
Thank you.
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Unfortunately, what you want is not how the API works. Consider this small Python example:
from gurobipy import *
m = Model()
c = [1, 2]
x = m.addVars(2, name='x')
m.addConstr(c[0]*x[0] + c[1]*x[1] <= 2)Once you call Model.addConstr(), it uses the values of c to create the constraint, without any reference to c. Any changes to c will not change the existing change.
To change the constraint, you have 2 options:
- Remove the constraint and add the new one
- Call Model.chgCoeff() to modify the coefficient in the model
Note that you would have this issue for any programming language, not just Python.
0
投稿コメントは受け付けていません。
コメント
2件のコメント