Increase the number of variable number by updating the gurobi model
回答済みHi, I use gurobi in the python API.
My model has 3 decision varaibles named x0, x1,x2.
x = m.addVars(3, vtype=GRB.BINARY, name="x")
the objective function is described by using gp.quicksum like below.
m.setObjective(gp.quicksum(x), GRB.MAXIMIZE)
After solving the model by model.optimize(), I want to add variable x3 to the model.
So I expect to increase the size of variable set x from 3 to 4.
I try to update the variable x, but I can`t find the way to change variable x.
After that, I update the objective function to solve the model.
Because adding variables will be repeated many times, I don`t want to introduce the new variables y,z,.... etc.
Is there any way to update the number of decision variables?
-
正式なコメント
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?. -
Hi Taejoon!
You can do the following to extend the existing tupledict x:
x = m.addVars(3, vtype="B", name="x")
m.setObjective(gp.quicksum(x), GRB.MAXIMIZE)
[...]
x.update({3: m.addVar(vtype="B", name="x[3]")})
m.setObjective(gp.quicksum(x), GRB.MAXIMIZE)The Gurobi tupledict is just an extension of the standard Python dictionary and also supports the update() function. You just need to make sure to name the variables accordingly.
There is no dedicated function to extend variables in the way you are describing. You also need to update the objective after extending the x tupledict. Alternatively, you could also add a 1 as objective coefficient when adding the variable in the update() call:
x.update({3: m.addVar(vtype="B", name="x[3]", obj=1)})I hope that helps.
Cheers,
Matthias0
投稿コメントは受け付けていません。
コメント
2件のコメント