Decision Optimization

Expand all | Collapse all

Kill docplex

  • 1.  Kill docplex

    Posted Thu September 02, 2021 08:54 AM

    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
    def time_limit(seconds, msg=''):
    timer = threading.Timer(seconds, lambda: _thread.interrupt_main())
    except KeyboardInterrupt:
    raise TimeoutException("Timed out for operation {}".format(msg))
    # if the action ends in specified time, timer is canceled
    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

    to kill cplex have you tried the progress listener ?

    Let me adapt the tiny example

    from import Model
    from 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))
            print("never here")
    # connect a listener to the model
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)​

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

  • 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

    then see example at

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