CPU time in docplex - Python

780 Views Asked by At

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!

2

There are 2 best solutions below

2
Alex Fleischer On

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
3
Philippe Couronne On

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()