3

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.

Snel23
  • 1,381
  • 1
  • 9
  • 19

3 Answers3

4

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.

rkersh
  • 4,447
  • 2
  • 22
  • 31
1

Let me adapt my bus example to your question:

from docplex.mp.model import Model

mdl = Model(name='buses')

# gap tolerance
mdl.parameters.mip.tolerances.mipgap=0.001;


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)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

print("gap tolerance = ",mdl.parameters.mip.tolerances.mipgap.get())

which gives:

nbBus40  =  6.0
nbBus30  =  2.0
gap tolerance =  0.001
halfer
  • 19,824
  • 17
  • 99
  • 186
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
1

Your mistake is to call the parameter as if it was a function. The correct way to change a parameter is to assign to it:

Model.parameters.mip.tolerances.absmipgap = GAP

Also make sure that you don't use the Model class but an instance of this class:

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

Also be aware that there are two gap parameters: absolute and relative. The relative gap is the most frequently used. You can find the documentation for both here and here (the parameter for relative tolerance is called just mipgap).

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22