如何使用 CPLEX Python API 获得最优性差距?

How to get optimality gap using CPLEX Python API?

如何使用 CPLEX Python API(CPLEX 12.5,Python 2.7.15)获得最优性差距(相对和绝对)?是否有诸如 get_optimal_gap() 之类的函数会给出最优性差距?或者正在解析输出(如 ) the only way? I see that there are custom functions such as solution.get_objective_value() - it would be nice if someone can suggest online resources that have a list of all functions that can be applied on the cplex object/file. Currently I am using the following code (courtesy: this IBM 文档所述):

    import cplex
    import sys

    def sample1(filename):
        c = cplex.Cplex(filename)
        try:
            c.solve()
        except CplexSolverError:
            print "Exception raised during solve"
            return

        # solution.get_status() returns an integer code
        status = c.solution.get_status()
        print "Solution status = " , status, ":",
        print c.solution.status[status]
        print "Objective value = " , c.solution.get_objective_value()

    sample1(filename)

CPLEX Python API 的文档是 here(当前版本 12.8)。

要获取相对mip间隙,您可以使用c.solution.MIP.get_mip_relative_gap(). To calculate the absolute mip gap you'll need c.solution.MIP.get_best_objective()

如果还没有,您还需要查看 CPX_PARAM_EPGAP and CPX_PARAM_EPAGAP 参数。