简体   繁体   English

如何获得提供的 MIP start 的客观价值?

[英]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.

相关问题 如何在python mip上设置可行的开始 - How to set a feasible start on python mip 如何在CPLEX中获得MIP的多种最佳解决方案? - How to get multiple optimal solutions for MIP in CPLEX? 如何使用 PuLP 的 Gurobi 求解器设置 MIP 启动(初始解决方案)? - How to set MIP start (initial solution) with Gurobi solver from PuLP? 达到时限后如何获得相对MIP最优差距? - How to get the relative MIP optimality gap after timelimit is met? 如何在gurobi中保存解决方案差距(MIP Gap)和上限(目标边界)? - How to save solution gap (MIP Gap) and upper bound (objective bound) in gurobi? 如何在Gekko获得目标函数的价值 - How to get value for objective function in Gekko ValueError:用户提供的目标 function 必须返回一个标量值 - ValueError: The user-provided objective function must return a scalar value OR-Tools MIP Solver - 根据 int 而不是 IntVar 定义目标 - OR-Tools MIP Solver - Defining an objective in terms of int, instead of IntVar 使用 MIP 库声明目标函数时出现断言错误 - Getting an assertion error when declaring objective function using MIP library 针对 python 中的 MIP 调度问题修改 Gurobi 目标 function - Modifying Gurobi objective function for MIP scheduling problem in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM