简体   繁体   English

获取根节点松弛解

[英]Get root node relaxation solution

I'm working on a B&C framework in Gurobi/Python and I would like retrieve the optimal solution of the root node problem.我正在使用 Gurobi/Python 开发 B&C 框架,我想检索根节点问题的最佳解决方案。 How can I do that?.我怎样才能做到这一点?。

Thanks.谢谢。

Most likely we will want to use a MIPNODE callback .我们很可能希望使用 MIPNODE 回调 Note that the MIPNODE callback will be called once for each cut pass during the root node solve.请注意,在根节点求解期间,将为每个切割通道调用一次 MIPNODE 回调。 The MIPNODE_NODCNT value will remain at 0 until the root node is complete. MIPNODE_NODCNT 值将保持为 0,直到根节点完成。 If you query relaxation values from during the root node, the first MIPNODE callback will give the relaxation with no cutting planes, and the last will give the relaxation after all root cuts have been applied.如果您从根节点中查询松弛值,第一个 MIPNODE 回调将提供没有切割平面的松弛,最后一个将在应用所有根切割后提供松弛。

Here is an example that queries the relaxation solution at each cut pass at the root node (to compute the objecive value, for illustration purpose), and quits the optimization after the root node is completed.下面是一个示例,在根节点处的每个 cut pass 查询松弛解(计算目标值,用于说明目的),并在根节点完成后退出优化。

import sys
from gurobipy import *

def mycallback(model, where):
    if where != GRB.Callback.MIPNODE:
        return

    nodecount = model.cbGet(GRB.Callback.MIPNODE_NODCNT)
    if nodecount > 0:
        print("Root node completed, terminate now")
        model.terminate()
        return

    if model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.Status.OPTIMAL:
        x = model.cbGetNodeRel(model._vars)
        objval = 0.0
        for idx, c in enumerate(model._coef):
            objval += x[idx] * c

        print(f"Root relaxation value: {objval}")

if len(sys.argv) < 2:
    print('Usage: callback.py filename')
    quit()

model = read(sys.argv[1])
model._vars = model.getVars()
model._coef = [v.obj for v in model._vars]
model.Params.OutputFlag = 0
model.optimize(mycallback)

If you run the above code with the example model p0033.mps (distributed along with the examples in the Gurobi installation), you should see:如果您使用示例模型p0033.mps (与 Gurobi 安装中的示例一起分发)运行上述代码,您应该看到:

Read MPS format model from file /Library/gurobi811/mac64/examples/data/p0033.mps
Reading time = 0.00 seconds
P0033: 16 rows, 33 columns, 98 nonzeros
Root relaxation value: 2839.4918382913806
Root relaxation value: 2941.4
Root relaxation value: 2952.0
Root relaxation value: 2953.325
Root relaxation value: 2966.7142857142853
Root relaxation value: 2972.0083333333337
Root relaxation value: 2973.171211242146
Root relaxation value: 3023.0025940337223
Root relaxation value: 3033.999999999998
Root relaxation value: 3056.7500000000005
Root relaxation value: 3057.8333333333335
Root relaxation value: 3057.8333333333335

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM