Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回当前最佳解决方案CPLEX Python API_Python_Cplex - Fatal编程技术网

返回当前最佳解决方案CPLEX Python API

返回当前最佳解决方案CPLEX Python API,python,cplex,Python,Cplex,由于MIP问题需要较长的计算时间,当计算时间超过(例如一小时)且相对间隔为5%时,我如何指示cplex返回当前最佳解决方案? 就个人而言,我相信我可以使用这两个函数:model.parameters.timelimit.set()和model.parameters.mip.tolerance.mipgap.set(),但如何将两者结合起来呢 您必须使用回调来强制执行这两个条件。CPLEX附带的mipex4.py示例准确地说明了如何执行此操作 以下是示例中的回调: class TimeLimitC

由于MIP问题需要较长的计算时间,当计算时间超过(例如一小时)且相对间隔为5%时,我如何指示cplex返回当前最佳解决方案?
就个人而言,我相信我可以使用这两个函数:
model.parameters.timelimit.set()
model.parameters.mip.tolerance.mipgap.set()
,但如何将两者结合起来呢

您必须使用回调来强制执行这两个条件。CPLEX附带的
mipex4.py
示例准确地说明了如何执行此操作

以下是示例中的回调:

class TimeLimitCallback(MIPInfoCallback):

    def __call__(self):
        if not self.aborted and self.has_incumbent():
            gap = 100.0 * self.get_MIP_relative_gap()
            timeused = self.get_time() - self.starttime
            if timeused > self.timelimit and gap < self.acceptablegap:
                print("Good enough solution at", timeused, "sec., gap =",
                      gap, "%, quitting.")
                self.aborted = True
                self.abort()

您好,
timelim\u cb.timelimit
timelim\u cb.acceptablegap
是否分别以秒和百分比表示?谢谢,是的,没错。对于
timelimit
,您可以从的文档中推断出这一点。对于
acceptablegap
您必须知道,gap的计算方法与可调用C库中的相同。非常感谢您的帮助!
c = cplex.Cplex(filename)

timelim_cb = c.register_callback(TimeLimitCallback)
timelim_cb.starttime = c.get_time()
timelim_cb.timelimit = 1
timelim_cb.acceptablegap = 10
timelim_cb.aborted = False

c.solve()

sol = c.solution

print()
# solution.get_status() returns an integer code
print("Solution status = ", sol.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(sol.status[sol.get_status()])

if sol.is_primal_feasible():
    print("Solution value  = ", sol.get_objective_value())
else:
    print("No solution available.")