Search code examples
pythoncplexdocplexcp-optimizer

CP Optimizer CPLEX - Multiple optimal solutions


I am currently using the Python API of the CP Optimizer of CPLEX to obtain the optimal solution of a scheduling problem. Depending on the instance, there might be multiple equivalent optimal solutions.

I would assume that during the searching process, multiple optimal solutions might be found, and thus, I would like to store them.

I have been searching the whole internet looking for ways to do that, but I haven't found any information so far. I know that this is possible to do for CPLEX (with MIP models) using a solution pool, however, for the CP Optimizer I didn't find anything. Can somebody share some information on this or at least tell me that it is not possible?


Solution

  • You can use iterators. See example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooseveralcpo.py

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    
    nbbus40 = mdl.integer_var(0,6,name='nbBus40')
    nbbus30 = mdl.integer_var(0,6,name='nbBus30')
    cost= mdl.integer_var(0,1000000,name='cost')
    
    mdl.add(cost==nbbus40*500 + nbbus30*400)
    mdl.add(cost<=4000)
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    
    siter = mdl.start_search(SearchType='DepthFirst', Workers=1, TimeLimit=100)
    # Parameters needed to avoid duplicate solutions
    
    
    
    for msol in siter:
        print(msol[nbbus40]," buses 40 seats")
        print(msol[nbbus30]," buses 30 seats")
        print("cost = ",msol[cost])
        
        print("\n")