Search code examples
pythonoptimizationmathematical-optimizationmodelingcplex

How to set gap tolerance in cplex-python?


I want to set a gap value (GAP) such that the optimization process stops when the current gap will be lower than GAP. I have read the cplex-python documentation and I found that:

Model.parameters.mip.tolerances.absmipgap(GAP)

but I get the next warning:

Model.parameters.mip.tolerances.mipgap(float(0.1))
TypeError: 'NumParameter' object is not callable

any ideas? please help me. Thanks in advance.


Solution

  • Based on the error you are getting, I think you might be using the CPLEX Python API rather than docplex (as in the other answers). To fix your problem, consider the following example:

    import cplex                                                                    
    Model = cplex.Cplex()                                                           
    # This will raise a TypeError                                                   
    #Model.parameters.mip.tolerances.mipgap(float(0.1))                             
    # This is the correct way to set the parameter                                  
    Model.parameters.mip.tolerances.mipgap.set(float(0.1))                          
    # Write the parameter file to check that it looks as you expect                 
    Model.parameters.write_file("test.prm")
    

    You need to use the set() method. You can make sure that the parameter is changed as you expected by writing the parameter file to disk with the write_file method and looking at it.