Due to MIP problems which take long computation time, how do I instruct cplex to return current best solution when the computation time takes longer than, an hour for example, and the relative gap is at 5% for example?
Individually, I believe I can use both functions: model.parameters.timelimit.set()
and model.parameters.mip.tolerances.mipgap.set()
, but how do I combine both?
Asked
Active
Viewed 1,078 times
3

Stanley Gan
- 481
- 1
- 7
- 19
1 Answers
3
You will have to use a callback to enforce both conditions. The mipex4.py
example that is shipped with CPLEX shows exactly how to do this.
Here is the callback from the example:
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()
And the relevant part of the rest:
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.")

rkersh
- 4,447
- 2
- 22
- 31
-
Hi, are the `timelim_cb.timelimit` and `timelim_cb.acceptablegap` in terms of seconds and percentage respectively? Thanks, – Stanley Gan Sep 26 '17 at 23:39
-
Yes, that's correct. For `timelimit` you can infer this from the documentation for [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). For `acceptablegap` you have to know that the gap is computed as with [CPXgetmiprelgap](https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.cplex.help/refcallablelibrary/mipapi/getmiprelgap.html) in the Callable C Library. – rkersh Sep 26 '17 at 23:52
-
Thanks a lot for your help! – Stanley Gan Sep 27 '17 at 00:25