gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:23 27 4
gpt4 key购买 nike

由于 MIP 问题需要很长的计算时间,当计算时间超过一个小时,例如相对差距为 5%,我如何指示 cplex 返回当前最佳解决方案?就个人而言,我相信我可以使用这两个函数:model.parameters.timelimit.set()model.parameters.mip.tolerances.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()

其余的相关部分:

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.")

关于python - 返回当前最佳解决方案 CPLEX Python API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com