Using CPLEX parameters in the CPLEX Python API

Manage CPLEX parameters in the CPLEX Python API.

For CPLEX users familiar with the Interactive Optimizer, setting and querying parameters in the CPLEX Python API is similar to parameter handling in the Interactive Optimizer. The class Cplex offers a data member named parameters containing names of parameters (such as lpmethod and threads) and names of groups of related parameters (such as barrier and output). These groups can in turn contain individual parameters and groups of parameters themselves. The hierarchy is the same as that found in the Interactive Optimizer, with the exception of parameters such as output dialog; their functionality is handled by other parts of the CPLEX Python API.

Tip:

If you are already familiar with the names of parameters in the Interactive Optimizer, then you quickly recognize names of parameters in the Python API. For example, the command “set mip limits nodes 1” in the Interactive Optimizer corresponds to “c.parameters.mip.limits.nodes.set(1)” in a Python session.

For users whose first contact with CPLEX is through the CPLEX Python API, the following topics introduce setting parameters, querying their current value, and using groups of parameters.

Setting and querying parameters in the CPLEX Python API

As objects themselves, parameters offer the following methods:

  • get() returns the current value of the parameter.

  • set(value) sets the invoking parameter to value. If value is of the wrong type for the invoking parameter, or if value is less than the minimum value or greater than the maximum value for the parameter, CPLEX raises an exception.

  • reset() sets the parameter to its default value.

  • default() returns the default value of the parameter.

  • type() returns the type of the parameter.

  • help() returns a brief description of the parameter.

Numerical parameters offer these additional methods:

  • min() returns the minimum value allowed for the parameter.

  • max() returns the maximum value allowed for the parameter.

Parameters of type float with no restrictions on their value return 0.0 (zero) when you invoke the method min() or max().

Certain integer parameters, such as lpmethod, have values with a special meaning. Such parameters also have a data attribute named values, which has as its attributes the values that the parameter can take. For example, if cpx is an instance of the class Cplex encapsulating an LP problem, then:

>>> cpx.parameters.lpmethod.set(cpx.parameters.lpmethod.values.primal)
>>> cpx.solve()

results in CPLEX solving the problem by the primal simplex optimizer.

Parameter groups

For your convenience, CPLEX parameters are organized into groups of parameters that you can manage simultaneously. A parameter group is an instance of the class ParameterGroup.

The class ParameterGroup offers these methods:

  • reset sets all parameters within the group to their default value.

  • get_changed returns a list of pairs (parameter, current value) for the members of the group not currently at their default value.

The parameter group that encompasses all parameters is an instance of the class RootParameterGroup, a subclass of the class ParameterGroup. It offers these methods:

  • read(filename) reads a set of parameters from the named file.

  • write(filename) writes a set of parameters to the named file.

  • tune_problem(fixed_parameters_and_values=[]) tunes the parameters to improve performance on an instance of the class Cplex. The argument fixed_parameters_and_values is a sequence of pairs (parameter, value) as returned by the method ParameterGroup.get_changed.

  • tune_problem_set(filenames, filetypes=[], fixed_parameters_and_values=[]) tunes the parameters to improve performance on a set of problems. Again, the argument fixed_parameters_and_values is a sequence of pairs (parameter, value) as returned by the method ParameterGroup.get_changed.