2017-09-24 1 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

ありがとうございました! –

関連する問題