setObjective() not updating when called on GRBModel object
回答済みHi, I am having trouble with the setObjective() and getObjective() functions in Gurobi. Specifically, getObjective() does not seem to return the correct model objective after updating via setObjective() For example, in the code below,
mod = gp.Model("model")
x = mod.addVar(lb = 0, ub = 5, vtype = GRB.CONTINUOUS, name = "x")
y = mod.addVar(lb = 0, ub = 5, vtype = GRB.CONTINUOUS, name = "y")
z = mod.addVar(lb = 0, ub = 5, vtype = GRB.CONTINUOUS, name = "z")
obj = mod.setObjective(x+y, GRB.MAXIMIZE)
print(mod.getObjective())
the program returns "0" as the model objective. It does actually optimize and solve the model correctly, but I am trying to write a program in which I need to call mod.getObjective() as part of the argument of the setObjective() function, so this is causing issues. I am trying to negate the current objective, but when I retrieve the new objective with getObjective(), it returns the original objective still.
Additionally, if I split up setting the objective into two lines (the expression on the first, and then setting GRB.MAXIMIZE on the second), the program behaves as a minimization problem. Maybe this is just syntax that I'm not aware of.
Any help would very much be appreciated in figuring out this issue! Thanks!
-
You have to call the update method first before querying any model objects.
obj = mod.setObjective(x+y, GRB.MAXIMIZE)
mod.update()
print(mod.getObjective())This is due to the lazy update mechanism.
0 -
Thank you for your prompt response!
0
サインインしてコメントを残してください。
コメント
2件のコメント