Search code examples
schedulingcplexdocplex

CPO Save Log/Intermediate solutions


I am using the python API of CPO Optimizer. Is there any way to save the log/intermediate highlighted solutions and timestamps to build a chart on the evolution of the optimization?

Thanks in advance!

I haven't found a workaround so far, but copy-pasting the log seems not a feasible way...


Solution

  • If you do not want to rely on callbacks you can also use CPO solution Iterator.

    Small changes in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocpoenumerate.py

    gives

    import time
    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',  TimeLimit=100)
    
    
    
    
    for msol in siter:
        print(msol[nbbus40]," buses 40 seats")
        print(msol[nbbus30]," buses 30 seats")
        print("cost = ",msol[cost])
        print("time = ",time.time())
        print("\n")
    

    which gives

    3  buses 40 seats
    6  buses 30 seats
    cost =  3900
    time =  1684850008.5023165
    
    
     *                      3  0.39s        1         4  = nbBus40
    4  buses 40 seats
    5  buses 30 seats
    cost =  4000
    time =  1684850008.6289783
    
    
     *                      4  0.51s        1         4 != nbBus40
    6  buses 40 seats
    2  buses 30 seats
    cost =  3800
    time =  1684850008.7602491
    
    
     ! ----------------------------------------------------------------------------
     ! Search completed, 3 solutions found.