0

Let us assume that I have created a mathematical model in python and want to solve it using the below code (the docplex library.)

start = time.perf_counter()  # CPU time calculator of the CPLEX solver
# Obj 1
mdl.minimize(obj1)
solution = mdl.solve(log_output=True)
if (solution is not None) and (solution.is_feasible_solution()):
    lb[0] = obj1.solution_value
    if obj2.solution_value > ub[1]: ub[1] = obj2.solution_value
    if obj3.solution_value > ub[2]: ub[2] = obj3.solution_value
    sol[0, 0] = obj1.solution_value
    sol[0, 1] = obj2.solution_value
    sol[0, 2] = obj3.solution_value
    sol[0, 3] = round(time.perf_counter() - start, 3)

Given that I have set mdl.time_limit=480, why could the time recorded in sol[0, 3] be greater than 480 seconds?

Thanks!

mdslt
  • 155
  • 1
  • 10

2 Answers2

1

Within CPLEX docplex you can get solve time:

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve(log_output=True,)

mdl.export("c:\\temp\\buses.lp")

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

print(mdl.solve_details)
print("solve time =",mdl.solve_details.time)

gives

status  = integer optimal solution
time    = 0.109 s.
problem = MILP
gap     = 0%

solve time = 0.10900000005494803
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
1

To answer your initial question, The time_limit parameter applies only to the solve call, and is handled internally by CPLEX, counting exclusively solve time.

The Python time module, on the other hand, counts time in a different manner, including Python code that is executed after the call to solve but before the second call to time.time()

Philippe Couronne
  • 826
  • 1
  • 5
  • 6
  • Thanks! I see. However, I still have a question. why should it take much more than 480 (the time I set to solve models)? I have seen that it takes 520+ seconds in some cases. I do not think the operations that I have shown above are that time-consuming. – mdslt Mar 16 '21 at 17:51
  • Am I correct to assume that your question is *not* about CPLEX solve time, but more about extra time between the two calls of time()? If so, can you refine your analysis by adding intermediate calls to time.time()? Within the two class, I see `minimize()` and `is_solution_feasible()` , maybe time is spent there, depending on your model size. – Philippe Couronne Mar 17 '21 at 08:45
  • BTW, why call `is_solution_feasible() anyway? Model.solve() is supposed to return a valid solution or None if the model is infeasible. – Philippe Couronne Mar 17 '21 at 08:47