Managing CPLEX parameters in the Python API
Documents CPLEX parameters in the Python API.
CPLEX normally performs well on a wide variety of problems at default settings. However, the product offers a number of parameters that you can adjust to meet specific needs of your problem. These CPLEX parameters are documented in the reference manual, Parameters of CPLEX. That manual shows the name of each parameter in the Python, C, C++, Java, and .NET APIs, as well as the name in the Interactive Optimizer. The documentation of a parameter specifies values that the parameter can take and outlines conditions under which changing the default value of a parameter may be useful in a given problem.
For users familiar with the hierarchy of parameters in the Interactive Optimizer, the parameters of the Python API are organized in the same way.
Help in Python for CPLEX parameters
Each parameter has a help method
that displays a short description of what the parameter does; for certain parameters,
the help method also shows
acceptable values of the parameter. For example:
>>> print(c.parameters.lpmethod.help())
method for linear optimization :
0 = automatic
1 = primal simplex
2 = dual simplex
3 = network simplex
4 = barrier
5 = sifting
6 = concurrent optimizers
CPLEX parameters that accept a discrete set of values
have a values attribute.
For those parameters, use the built-in Python help
to see the list of possibilities. For example:
>>> help(c.parameters.lpmethod.values)
Help on instance of alg_constants in module cplex._internal._parameter_classes:
class alg_constants
| Methods defined here:
|
| __getitem__(self, item)
| Converts a constant to a string.
|
| Example usage:
|
| >>> import cplex
| >>> c = cplex.Cplex()
| >>> c.parameters.mip.strategy.startalgorithm.values.auto
| 0
| >>> c.parameters.mip.strategy.startalgorithm.values[0]
| 'auto'
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| auto = 0
|
| barrier = 4
|
| concurrent = 6
|
| dual = 2
|
| network = 3
|
| primal = 1
|
| sifting = 5
Symbolic names of CPLEX parameters
Parameters that accept symbolic names have a member,
values, comprising descriptive names. For example,
the following line sets the barrier optimizer as the method for solving
continuous models (LPs).
>>> c.parameters.lpmethod.set(c.parameters.lpmethod.values.barrier)
Example using a CPLEX parameter in Python
As an example of a CPLEX parameter in use, here is a typical Python session that first sets a time limit of five minutes (that is, three hundred seconds) for performance tuning and then accesses the value that was just set. The example illustrates the naming pattern and calling convention of CPLEX parameters in the Python API.
>>> c = cplex.Cplex()
>>> c.parameters.tuning.timelimit.set(300.0)
>>> c.parameters.tuning.timelimit.get()
300.0
As objects of the Python API, CPLEX parameters have methods to access their minimum, maximum, and default values, as the following lines illustrate.
>>> c.parameters.simplex.tolerances.markowitz.min()
0.0001
>>> c.parameters.simplex.tolerances.markowitz.default()
0.01
>>> c.parameters.simplex.tolerances.markowitz.max()
0.99999000000000005
>>> c.parameters.simplex.tolerances.markowitz.set(2.0)
Traceback … cplex.exceptions.CplexError: Invalid argument