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?
3
votes
1 Answers
3
votes
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.")