Decision Optimization notebooks

You can create and run Decision Optimization models in Python notebooks using DOcplex, a native Python API for Decision Optimization. There are several Decision Optimization notebooks already available for you to use.

The Decision Optimization for Watson Studio environment currently supports Python V3.6. All Watson Studio Python environments give you access to the Community Edition of the CPLEX engines. This makes it possible to solve problems with up to 1000 constraints and 1000 variables, or with a search space of 1000 X 1000 for Constraint Programming problems. To run larger problems, select the Default Python 3.6 XS + DO environment which includes the full CPLEX commercial edition.

You can easily change environments inside a notebook using the Environment tab. This means that you can formulate optimization models and test them with small data sets in one environment, and then, to solve with bigger data sets, you can just switch to a different environment, without having to rewrite or copy the notebook code.

There are multiple example Decision Optimization notebooks available in the Gallery in Watson Studio, including:
  • The Sudoku example, a Constraint Programming example in which the objective is to solve a 9x9 Sudoku grid.
  • The Pasta Production Problem example, a Linear Programming example in which the objective is to minimize the production cost for some pasta products and ensure that the customers' demand for the products is satisfied.

All Decision Optimization notebooks use DOcplex.

DOcplex

The Decision Optimization notebooks use DOcplex, a native Python API for modeling and solving Decision Optimization problems. The API is available by default in Watson Studio as part of the Python environment.

It is composed of two modules:
  • Mathematical Programming Modeling for Python using docplex.mp
  • Constraint Programming Modeling for Python using docplex.cp
In your code you can specify the library you want to use as follows, for example for Mathematical Programming libraries:
from docplex.mp.model import Model

The API is licensed under the Apache License V2.0 and is numpy/pandas friendly.

You can read the full API documentation on RawGit. You can find DOcplex examples on the Decision Optimization GitHub.

Python scenario API

In addition to DOcplex, a scenario API is available for you to create scenarios in Watson Studio and handle models made in the Decision Optimization model builder. For example, see Generating multiple scenarios.

See the dd-scenario-client-python documentation. You can also find the previous example in the jupyter folder in the DO-samples Decision Optimization GitHub.

Running Decision Optimization notebooks

Depending on whether you are interested in Constraint Programming or Linear Programming, choose one of the two notebooks presented earlier in this section and run it in Watson Studio as follows.

If you have already created a project in Watson Studio:
  1. From the Gallery, open the notebook you want to work with.
  2. If you have already created a project in Watson Studio, click the Copy button Copy button.
  3. Select an existing project in the drop-down list, and select a runtime, for example Default Python 3.6 XS (or for larger models which require the Commercial Edition of CPLEX engines, select Default Python 3.6 XS + DO) and click Create Notebook. The notebook is added to your project.

If you have not already created a project in Watson Studio, click the Download button Download buttonto download the example on your machine.

  1. Create a new project: select Projects > View all Projects from the menu and click the New Project button.
  2. Select Create an empty project and in the window that opens enter a name and click Create.
  3. To create a new notebook, click the Add to Project button and select Notebook.
  4. Choose From File. Then click Choose file and browse to the notebook on your machine.
  5. Click Create Notebook.
Your notebook automatically opens.

To run your notebook, click Cell > Run All.

Example Python notebooks are provided in the Decision Optimization GitHub. To use these, see Python notebook samples. These examples do not use the model builder.

Also a Python notebook that shows you how to generate multiple scenarios using randomized data is provided in the DO-samples. This can be useful to test a model made in the model builder with different data sets. See Generating multiple scenarios for how to do this.

Decision Optimization tutorials

You can find more DOcplex examples that will introduce you to the DOcplex Python API on the Decision Optimization GitHub:

  • Linear Programming

    You can read the detailed description of this notebook here. You can clone or download this notebook here.

  • Beyond Linear Programming

    You can read the detailed description of this notebook here. You can clone or download this notebook here.

  • Getting started with Scheduling in CPLEX for Python

    You can read the detailed description of this notebook here. You can clone or download this notebook here.