Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  how to print output in Cplex Python API

    Posted Fri March 25, 2016 08:02 PM

    Originally posted by: PingLiu


    I am trying to display the problem solving process on the screen. I want to see something as following:

    --------------------------------------------------------------------------

    Tried aggregator 3 times.
    MIP Presolve eliminated 2648 rows and 612 columns.
    MIP Presolve modified 62 coefficients.
    Aggregator did 13 substitutions.
    Reduced MIP has 4229 rows, 1078 columns, and 13150 nonzeros.
    Reduced MIP has 1071 binaries, 0 generals, 0 SOSs, and 0 indicators.
    Presolve time = 0.05 sec. (18.80 ticks)
    Probing fixed 24 vars, tightened 0 bounds.
    Probing time = 0.05 sec. (18.12 ticks)
    -------------------------------------------------------

    I can see the above by

    my_prob.set_results_stream(
    "results.log")
    

    but I want to display it on screen also. I tried the code below, but it never shows anything.

    
    my_prob.parameters.mip.display.set(
    5) # the range is between 0 and 5.
    

    Does anybody know the reason?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: how to print output in Cplex Python API

    Posted Sun March 27, 2016 09:05 AM

    If you want to print only to stdout then use

    my_prob.set_results_stream(sys.stdout)

    If you want to print to stdout and a file then you need to implement your own logger. Here are a few examples for set_results_stream:

    import sys
    import cplex
    
    # Very simple class to log output to screen and a file
    class Logger:
        def __init__(self, filename):
            self.f = open(filename, 'w')
        def __del__(self):
            self.close()
        def __enter__(self):
            return self
        def __exit__(self, exc_type, exc_value, traceback):
            self.close()
        def close(self):
            if not self.f is None:
                f = self.f
                self.f = None
                f.close()
        def flush(self):
            if not self.f is None:
                self.f.flush()
                sys.stdout.flush()
        def write(self,text):
            if not self.f is None:
                self.f.write(text)
                sys.stdout.write(text)
    
    cpx = cplex.Cplex()
    cpx.read(sys.argv[1])
    cpx.parameters.advance.set(0)
    
    # Disable output
    cpx.set_results_stream(None)
    cpx.solve()
    
    # Output to stdout only
    cpx.set_results_stream(sys.stdout)
    cpx.solve()
    
    # Output to stdout and file
    with Logger('results.txt') as logger:
        cpx.set_results_stream(logger)
        cpx.solve()
    
    

    Note that you may not only want to set the results stream but also the warning stream.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: how to print output in Cplex Python API

    Posted Tue March 29, 2016 07:13 PM

    Originally posted by: PingLiu


    Thank you for the solution.

    BTW, how may I control the precision of the solution? For example, I want to stop running cplex when the gap reached 1%.

    Can I also control the iteration number or turn on parallel computation?


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: how to print output in Cplex Python API

    Posted Wed March 30, 2016 01:35 AM

    Take a look at the list of CPLEX parameters to learn how to control these aspects of CPLEX.

    Parameters to stop at a certain gap are CPX_PARAM_EPAGAP and CPX_PARAM_EPGAP (note that CPLEX will stop as soon as one of the two is satisfied).

    Iteration limit can be controlled by CPX_PARAM_ITLIM.

    Parallel computation is enabled by default. By default CPLEX uses as many threads as the number of CPUs your operating system reports. If you use control callbacks then CPLEX switches to single-threaded mode by default but you can still configure it to use multiple threads. In any case, use CPX_PARAM_THREADS to specify the number of threads CPLEX should use.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 12:18 AM

    Originally posted by: PingLiu


    My problem is MILP.

    what's the corresponding usage in Python for CPX_PARAM_ITLIM?

    for parallel, I tried

    cpx.parameters.parallel
    

    for iteration, I tried the following, but it doesn't work

    cpx.parameters.mip.limits.strongit


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 02:57 AM

    Hi,

    have you tried

    parameters.simplex.limits.iterations

    ?

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 10:49 AM

    Originally posted by: PingLiu


    Yes, I tried it already. But it seems it is not working on MILP. Is there any way to control the maximum iteration number for MILP?


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 09:35 AM

    Did you notice that all the parameters I listed are links to the respective reference documentation? In this reference documentation you will find that the name of the parameter in Python is parameters.simplex.limits.iterations, so try

    cpx.parameters.simplex.limits.iterations.set(limit_value)

    But are you sure you want to set a simplex iteration limit for a MILP? For this type of problem you may want to apply other limits, number of nodes processed for example.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 10:50 AM

    Originally posted by: PingLiu


    It is definitely not working on MILP. I have tried the number of nodes, cuts, gap, relgap, I just want to know how to control the iteration number for MILP in cplex python Api.


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: how to print output in Cplex Python API

    Posted Thu March 31, 2016 12:28 PM

    I am pretty sure that is is working. I just tried and the following code definitely worked for me:

    import sys
    import cplex
    
    for model in sys.argv[1:]:
        cpx = cplex.Cplex()
        cpx.read(model)
        cpx.parameters.mip.limits.nodes.set(10)
        cpx.parameters.simplex.limits.iterations.set(10)
        cpx.solve()
    

    If I specify a very small iteration limit (like 10) then I quickly get an error that CPLEX failed to solve a subproblem because it hit the iteration limit before it could solve the root relaxation.


    #CPLEXOptimizers
    #DecisionOptimization