2017-09-24 105 views
1

由于需要很长计算时间的MIP问题,当计算时间比例如一个小时长,并且相对间隔为5时,如何指示cplex返回当前最佳解决方案% 例如? 个别地,我相信我可以同时使用这两种功能:model.parameters.timelimit.set()model.parameters.mip.tolerances.mipgap.set(),但我该如何组合?返回当前最佳解决方案CPLEX Python API

回答

1

您将不得不使用回调来强制执行这两个条件。随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.") 
+0

嗨,在条款'timelim_cb.timelimit'和'timelim_cb.acceptablegap'秒和百分比分别为?谢谢, –

+0

是的,这是正确的。对于'timelimit',你可以从[Callback.get_time()]的文档中推断出来(https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.cplex.help/refpythoncplex/html/ cplex.callbacks.Callback-class.html?查看= KC#GET_TIME)。对于'可接受的差距',您必须知道差距是按照[CPXgetmiprelgap]计算的(https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.cplex.help/refcallablelibrary/mipapi/getmiprelgap Callable C Library中的.html)。 – rkersh

+0

非常感谢您的帮助! –