両方の条件を強制するには、コールバックを使用する必要があります。 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.")
こんにちは、という点で 'timelim_cb.timelimit'と' timelim_cb.acceptablegap'です秒とパーセントのそれぞれ?ありがとう、 –
はい、それは正しいです。 '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
ありがとうございました! –