Search code examples
pythonlinear-programmingcplex

Warm Start in CPLEX Python


I have been trying to implement warm start in CPLEX solver using its Python API to solve a linear programming model. From IBM's official website, I have found the definition of the function and a code snippet.

The function definition is given as this:

set_start(self, col_status, row_status, col_primal, row_primal, col_dual, row_dual)

Here is the code snippet from the website:

import cplex
c = cplex.Cplex()
indices = c.variables.add(names = ["v" + str(i) for i in range(5)])
indices = c.linear_constraints.add(names = ["r" + str(i) for i in range(3)])
s = c.start.status
c.start.set_start([s.basic] * 3 + [s.at_lower_bound] * 2, [s.basic] + [s.at_upper_bound] * 2,
                     [0.0] * 5, [1.0] * 3, [2.0] * 5, [3.0] * 3)

However, I couldn't understand the inputs of the function set_start and I couldn't find any examples either. Are there any example codes that can be helpful for me to implement it?

Here is another thing I wonder about warm start. I know that we provide variables and their values from previous solutions, but do constraints remain the same or do they have to be recreated when re-solving the problem?

I'd appreciate any kind of help. Thank you in advance.


Solution

  • you should try to use docplex instead of the matrix api:

    Example warm start through API in Easy optimization with python

    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)
    
    warmstart=mdl.new_solution()
    warmstart.add_var_value(nbbus40,8)
    warmstart.add_var_value(nbbus30,0)
    mdl.add_mip_start(warmstart)
    
    
    sol=mdl.solve(log_output=True)
    
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)
    

    which gives

    nbBus40  =  6.0
    nbBus30  =  2.0
    

    and in the log we see

    1 of 1 MIP starts provided solutions.
    MIP start 'm1' defined initial solution with objective 4000.0000.