CPLEX Python API Reference Manual¶
Contents:
IBM CPLEX Python API Reference Manual¶
The CPLEX Python API is a Python package named cplex that allows the CPLEX Callable Library to be accessed from the Python programming language. It is equally suitable for interactive use through the Python interpreter or for writing scripts or full-fledged applications.
For users of Python, CPLEX offers both a Python application programming interface (API) documented in this reference manual, as well as IBM Decision Optimization CPLEX Modeling for Python, also known as DOcplex. DOcplex is a library composed of two modules:
Mathematical Programming Modeling for Python using docplex.mp (DOcplex.MP)
Constraint Programming Modeling for Python using docplex.cp (DOcplex.CP)
For more about DOcplex, see the DOcplex landing page.
The CPLEX Python API can be installed directly from PyPI or from Anaconda, using the commands pip and conda.
If you are using the conda command to install, then you should make sure the correct channel will be searched.
You can add the right channel in the active environment with a command like:
conda config --add channels ibmdecisionoptimization
The following commands install the Community Edition CPLEX and CP Optimizer run-times as well as the low-level Python interface to CPLEX.
PyPI:
pip install cplex
Anaconda:
conda install cplex
The following commands install the higher-level “docplex” Python package which includes CPLEX and CP Optimizer modeling and solving support. The Community Edition run-times which come as standard are limited in the size of the optimization problems that they can solve. To upgrade the Community Edition runtimes and remove size limitations, use the command “docplex” which was installed when you installed the “docplex” module with pip and conda.
PyPI:
pip install docplex
Anaconda:
conda install docplex
Running:
docplex config --upgrade Full_Path_To_This_CPLEX_Studio_Installation
will upgrade the CPLEX and CP Optimizer Python run-times from the Community Edition version to a size-unlimited version.
What you need to know¶
This manual assumes that you are familiar with the Python programming language.
This manual also assumes that you are familiar with the operating system on which you are using CPLEX and have the appropriate version of Python installed on your system. For information about supported platforms, see Detailed System Requirements (DSR) on your IBM customer support site.
For interactive use, the tab completion functionality provided by the Python module rlcompleter is extremely useful as you browse the CPLEX Python API. GNU/Linux systems typically include this package by default. Mac users are advised to install the package readline for their system. Windows users are advised either to use IDLE, the Python IDE distributed with Python, or to install the package readline for their system.
Structure of the package cplex¶
The CPLEX Python API is an object-oriented API providing most of its functionality through the class Cplex defined in the top level of the package cplex. This class has a number of methods, such as Cplex.read and Cplex.solve for operating on optimization problems, but most of its functionality is provided through methods of data attributes such as Cplex.linear_constraints, Cplex.objective, and Cplex.solution. These objects provide methods for modifying and querying data associated with the corresponding parts of an optimization problem. These objects are instances of classes defined within the subpackage cplex._internal. As its name suggests, classes and other data defined in the package cplex._internal are not to be instantiated or otherwise accessed directly by the user, but rather should only be accessed as attributes of an instance of the object Cplex.