Getting started with constraint programming in DOcplex

To get started using the Constraint Programming Modeling for Python feature of IBM Decision Optimization CPLEX Modeling for Python library (DOcplex.CP), you first need to:

  • verify that your system meets the requirements,
  • choose which CPLEX Optimizer you want to use,
  • install Python and DOcplex.

As an alternative, you can choose to use the Data Scientist Workbench and not need to install anything on your computer.

Installation

This section contains information about installing IBM Decision Optimization CPLEX Modeling for Python.

System requirements

  • Windows, Linux or Mac OS operating systems.
  • Python with version 2.7.9, 2.7.10, 3.4, or 3.5.
  • An IBM Decision Optimization on Cloud service account.

Get Python development tools

  • If you are new to Python, you might want a development studio with editors and debuggers. Both PyCharm and PyDev have free editions.
  • You can download appropriate Python version from Python.org.
  • A good alternative is to use Anaconda, which includes a Python interpreter and around 200 of the most popular Python packages for science, math, engineering, and data analysis.

Install the CPLEX modeling library

The IBM Decision Optimization CPLEX Modeling for Python (DOcplex) library can be installed via pip from PyPI.

Use pip to install the modeling library:

> pip install docplex

pip is the standard tool that is used to install Python packages and is included in Python 2.7.9 (and later) and Python 3.4 (and later). See pip for more details. Refer to the pip documentation for easy access to the upgrade, uninstall, and version checking commands.

See README.md for a detailed list of dependencies that are automatically downloaded and installed.

Solve model with a local solver

Local solve is available if you have a local installation of CPLEX Optimization Studio, with a version number higher or equal to 12.7.0.

The local solving is enabled by default. , create a Python file cpo_config.py somewhere that is visible from the PYTHONPATH, and add the following line:

set_default(LOCAL_CONTEXT)

Details are available in section Solve a model with local solver.

Get access to IBM Decision Optimization on Cloud

To use the CP Optimizer solver available on the IBM Decision Optimization on Cloud service:

Configuration

  • A first option is to set the DOcplexcloud url and key directly in the model source file, as parameters of the solve() request:

    msol = mdl.solve(url=..., key=...)
    
  • For a persistent setting, create a Python file docloud_config.py somewhere that is visible from the PYTHONPATH, for example in the directory where examples are located. Edit this file and set the following lines:

    context.solver.docloud.url = "..."
    context.solver.docloud.key = "..."
    

Test an example

A set of examples is available for download here.

  • Retrieve the examples archive docplex_examples.zip and uncompress it where you want.

  • Go to the directory where the root directory of the examples is located.

  • Open a command prompt and enter (on Windows, for example):

    > set PYTHONPATH=.
    > python examples/cp/basic/color.py