You can create and run Decision
Optimization models in Python notebooks by using DOcplex, a native
Python API for Decision
Optimization. Several Decision
Optimizationnotebooks are already available for you to
use.
You can use Decision
Optimization with Python 3.11 and 3.10
(deprecated). The following runtimes give you access to the Community Edition of
the CPLEX engines. The Community Edition is limited to solving problems with up to 1000 constraints
and 1000 variables, or with a search space of 1000 X 1000 for Constraint Programming problems.
Runtime 24.1 on Python 3.11
S/XS/XXS
Runtime 23.1 on Python 3.10
(deprecated) S/XS/XXS
To run larger problems, select a runtime that includes the full CPLEX
commercial edition. The Decision
Optimization environment (DOcplex) is available in the following runtimes (full CPLEX commercial edition):
NLP + DO runtime 24.1 on Python 3.11 with CPLEX 22.1.1.0
DO + NLP runtime 23.1 on Python 3.10
(deprecated) with CPLEX 20.1.0.1
You can easily change environments (runtimes and Python version) inside a
notebook by using the Environment
tab (see Changing the environment of a notebook). Thus, you can formulate
optimization models and test them with small data sets in one environment. Then, to solve models
with bigger data sets, you can switch to a different environment, without having to rewrite or copy
the notebook code.
Multiple examples of Decision
Optimizationnotebooks are available in the Resource
hub, 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 to ensure that the customers' demand for
the products is satisfied.
These and more examples are also available in the jupyter folder of
the DO-samples
All Decision
Optimizationnotebooks use DOcplex.
DOcplex
Copy link to section
The Decision
Optimizationnotebooks use DOcplex, a
native Python API for modeling and solving Decision
Optimization problems. The API is
available by default as part of the Python environment.
It is composed of two modules:
Mathematical Programming Modeling for Python that uses docplex.mp
Constraint Programming Modeling for Python that uses 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
Copy to clipboardCopied to clipboard
The API is licensed under the Apache License V2.0 and is numpy/pandas
friendly.
In addition to DOcplex, a Decision
Optimization client
API is available for you to create scenarios and handle models that are made in the Decision
Optimizationexperiment UI. For example, see Generating multiple Decision Optimization scenarios.
Depending on whether you are
interested in Constraint Programming or Linear Programming, choose one of the two notebooks presented earlier and run it as follows.
If
you already have a project in watsonx.ai:
From the Resource
hub, open the notebook you want to work with.
If you have already created a project,
click Add to project.
Select an existing project in the drop-down list, and select a runtime, for example
Runtime 24.1 on Python 3.11 XS (or for larger
models that require the Commercial Edition of CPLEX engines, select DO + NLP Runtime 24.1 on Python 3.11 XS), and click
Create. The notebook is
added to your project.
If you do not already have a project , click the Download button to download the example onto your computer.
Create a new project: select Projects > View all
Projects from the menu and click the New Project button.
Select Create an empty project and in the window that opens enter a name
and click Create.
Select the Assets tab.
Select New asset > Work with data and
models in Python or R notebooks in the Work with
models section.
Choose Local
file. Then click Drag and drop files or
upload and browse to the notebook onto your computer.
Click Create Notebook.The notebook is added to your project.
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
notebooks, see Jupyter notebook samples. These examples do not
use the experiment UI.
Also a Python notebook that shows you how to generate multiple scenarios and
that uses randomized data, is provided in the jupyter folder of the
DO-samples. This approach can be useful to test a model made in the
experiment UI with different data sets. For
more information, see Generating
multiple scenarios.
Decision Optimization tutorials
Copy link to section
You can find more DOcplex examples that introduce you to the
DOcplex Python API on the Decision Optimization GitHub: