在 Python API 中管理 CPLEX 参数
阐述 Python API 中的 CPLEX 参数。
通常,CPLEX 可使用缺省设置很好地求解各种问题。 但是,产品提供了众多参数,您可对其进行调整以满足问题的特定需求。 参考手册CPLEX 的参数对这些 CPLEX 参数作了阐述。 该手册列出每个参数在 Python、C、C++、Java 和 .NET API 中的名称以及在 Interactive Optimizer 中的名称。 参数的文档指出该参数可采用的值,并概述在给定问题中更改参数缺省值可能有帮助的条件。
对于熟悉 Interactive Optimizer 或 CPLEX MATLAB Toolbox 中的参数层次结构的用户来说,Python API 的参数以相同方式组织。
Python 中 CPLEX 参数的帮助
每个参数都具有一个
help
方法,此方法显示参数效果的简短描述;对于特定参数,
help
方法还显示可接受的参数值。 例如:
>>> 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 参数具有一个
values
属性。
对于这些参数,使用内置 Python help 以查看可能性列表。 例如:
>>> 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
CPLEX 参数的符号名称
接受符号名称的参数具有成员
values
,该成员由描述性名称组成。 例如,下面这一行将 barrier 优化器设置为求解连续模型 (LP) 的方法。
>>> c.parameters.lpmethod.set(c.parameters.lpmethod.values.barrier)
在 Python 使用使用 CPLEX 参数的示例
作为实际 CPLEX 参数的示例,这里给出一个典型的 Python 会话,它首先设置 5 分钟(300 秒)的时间限制以进行性能调节,然后访问刚刚设置的值。 此示例演示 Python API 中 CPLEX 参数的命名模式和调用约定。
>>> c = cplex.Cplex()
>>> c.parameters.tuning.timelimit.set(300.0)
>>> c.parameters.tuning.timelimit.get()
300.0
作为 Python API 的对象,CPLEX 参数具有用于访问其最小值、最大值和缺省值的方法,如下列各行所示。
>>> 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