Decision Optimization

Expand all | Collapse all

Kill docplex

  • 1.  Kill docplex

    Posted Thu September 02, 2021 08:54 AM
    Hi,

    I have a program that calls cplex multiple times (to solve master problems in a branch and price).
    The program should stop after a timelimit, however when  cplex is running the function
    @contextmanager
    def time_limit(seconds, msg=''):
    timer = threading.Timer(seconds, lambda: _thread.interrupt_main())
    timer.start()
    try:
    yield
    except KeyboardInterrupt:
    raise TimeoutException("Timed out for operation {}".format(msg))
    finally:
    # if the action ends in specified time, timer is canceled
    timer.cancel()
    I cannot kill the program as CPLEX keeps running.

    I read it is possible to kill it via a callback function Is it possible to kill CPLEX from outside?

     however I am not sure how to use it.

    Q: how to kill CPLEX/docplex from outside either by using callbacks or any other way.

     


  • 2.  RE: Kill docplex

    Posted Thu September 02, 2021 10:50 AM
    Hi,

    to kill cplex have you tried the progress listener ?

    Let me adapt the tiny example

    from docplex.mp.model import Model
    from docplex.mp.progress import *
    import os
    
    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)
    
    class pl(ProgressListener):
      
        def __init__(self):
            ProgressListener.__init__(self, ProgressClock.Gap)
        
        def notify_progress(self, pdata):
            gap = pdata.mip_gap
            ms_time = 1000* pdata.time
            print('-- new gap: {0:.1%}, time: {1:.0f} ms'.format(gap, ms_time))
            os.abort()
            print("never here")
            
    
    # connect a listener to the model
    mdl.add_progress_listener(pl())
    
    mdl.solve(log_output=False,)
    
    mdl.export("c:\\temp\\buses.lp")
    
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)​



    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: Kill docplex

    Posted Fri September 03, 2021 11:27 AM
    Thanks for your reply,
    This solution will not only stop cplex from running but also kill the main thread, which is not what I want. I want to stop cplex from running and return the results I got. If I use this solution, the whole program is killed and all the data is lost. I need to tell Cplex, while it is running, to stop but the main program keeps on running to save the results.


  • 4.  RE: Kill docplex

    Posted Sat September 04, 2021 03:25 AM
    Hi

    then see example at 

    https://stackoverflow.com/questions/63805810/how-to-asynchronously-abort-a-solve-in-docplex

    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------