2017-09-24 7 views
1

계산 시간이 오래 걸리는 MIP 문제로 인해 계산 시간이 예를 들어 1 시간보다 오래 걸리고 상대 간격이 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? view = kC# get_time). 'acceptablegap'의 경우 갭이 [CPXgetmiprelgap] (https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.cplex.help/refcallablelibrary/mipapi/getmiprelgap)과 같이 계산된다는 것을 알아야합니다. .html)을 호출 가능 C 라이브러리에 저장합니다. – rkersh

+0

도움을 주셔서 감사합니다. –