[英]How to get the objective value of provided MIP start?
One can easily provide a MIP start solution, eg可以轻松提供 MIP 启动解决方案,例如
from gurobipy import Model
mdl = Model()
x = mdl.addVar(vtype="I")
y = mdl.addVar(ub = 5, vtype="I")
x.start = 2.0
y.start = 1.0
mdl.setObjective(x*x-y*y)
mdl.addConstr(x <= 2.0*y)
mdl.optimize()
In this example it is easily seen that the MIP start has objective value 3. How can I get the MIP start objective value by Gurobi without calculating it on my own?在这个例子中,很容易看出 MIP start 的目标值是 3。如何在不自己计算的情况下通过 Gurobi 获得 MIP start 目标值? The
mdl.objVal
attribute is only available after calling the mdl.optimize()
method and returns the optimal objective value then. mdl.objVal
属性只有在调用mdl.optimize()
方法后才可用,然后返回最佳目标值。
Assuming that you want to solve the model anyway, you could use a callback like this:假设你想解决 model 无论如何,你可以使用这样的回调:
from gurobipy import Model, GRB
# ... your model
# The callback saves the objective value of the first found incumbent,
# i.e. the mip start in your case
def callback(model, where):
if where == GRB.Callback.MIPSOL:
if model.cbGet(GRB.Callback.MIPSOL_SOLCNT) == 0:
# creates new model attribute '_startobjval'
model._startobjval = model.cbGet(GRB.Callback.MIPSOL_OBJ)
mdl.optimize(callback)
print(f"MIP Start objVal: {mdl._startobjval}, optimal objVal: {mdl.objVal}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.