• Eastham, Cape Cod, MA, US

work: using DOcplex in a Jupyter notebook

Now that you have installed IBM® Decision Optimization CPLEX® modeling for Python (DOcplex), installed IBM ILOG CPLEX Optimization Studio Community Edition, and obtained a DOcplexcloud API key, you can solve optimization problems both locally and on the cloud.

Our next step is to use DOcplex and solve optimization problems from within a Jupyter notebook. Jupyter notebooks have become a popular choice to share data, code, and descriptive text. DOcplex provides a number of illustrative examples as notebooks in the docplex-examples-master.zip zip file that you downloaded from GitHub. These files are in the jupyter subdirectories of the cp and mp directories.

Running DOcplex from a notebook:
1. To launch Jupyter via the terminal, go to your DOcplex/Tests directory and type:

jupyter notebook

The notebook should then launch in your browser.

launch jupyter
launch jupyter

2. Upload a notebook. We’ll start with the boxes example. Select “Upload” in the top right-hand corner of the notebook. In the Upload window, navigate to the directory where you installed the examples, such as docplex-examples-master/examples/mp/jupyter/boxes.ipynb. Once the example is uploaded, click on it to open the notebook.

boxes notebook
boxes notebook

3. Run the notebook.

We’re going to run the notebook locally at first, so leave these lines as is:

url = "ENTER YOUR URL HERE"
key = "ENTER YOUR KEY HERE"

Run All
Run All

4. Install any missing libraries.

When I “Cell > Run All”, the model solves:

* model solved with objective: 8858
* solution: [15, 11, 14, 4, 8, 12, 7, 9, 10, 3, 6, 1, 13, 2, 5]

But I get the error in the display:

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
in ()
----> 1 import matplotlib.pyplot as plt
2 from pylab import rcParams
3 get_ipython().magic('matplotlib inline')
4 rcParams['figure.figsize'] = 12, 6
5

ImportError: No module named 'matplotlib'

To fix this problem, I quit the notebook (Control-C in the Terminal window), then type pip install matplotlib at the command line. Since I have more than one version of Python installed, I have to check that I’m using pip with the correct version. At the command line, I type:

conda create -n py35 python=3.5 anaconda
source activate py35
pip install matplotlib

Then when I re-launch the notebooks and run boxes, I get the lovely plot.

boxes plot
boxes plot

5. Run on the cloud.

In the post solving a DOcplex model on the cloud, I described how to obtain a URL and API key for solving a DOcplex model on the cloud.

In the notebook, update the lines with your URL and API key:

url = "ENTER YOUR URL HERE"
key = "ENTER YOUR KEY HERE"

To clear the previous run, select “Cell > All Output > Clear”. Then to run, select “Cell > Run All”.

2 thoughts on “work: using DOcplex in a Jupyter notebook

    • Author gravatar

      Even when I followed your method, I got the following errors:
      DOcplexExceptionTraceback (most recent call last)
      in ()
      1 mdl.print_information()
      2
      —-> 3 assert mdl.solve(), “!!! Solve of the model fails”

      C:\ProgramData\Anaconda2\lib\site-packages\docplex\mp\model.pyc in solve(self, **kwargs)
      4150 else:
      4151 # no way to solve.. really
      -> 4152 return self.fatal(“CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud”)
      4153 finally:
      4154 try:

      C:\ProgramData\Anaconda2\lib\site-packages\docplex\mp\model.pyc in fatal(self, msg, *args)
      882
      883 def fatal(self, msg, *args):
      –> 884 self._error_handler.fatal(msg, args)
      885
      886 def fatal_ce_limits(self, *args):

      C:\ProgramData\Anaconda2\lib\site-packages\docplex\mp\error_handler.pyc in fatal(self, msg, args)
      208 resolved_message = resolve_pattern(msg, args)
      209 docplex_error_stop_here()
      –> 210 raise DOcplexException(resolved_message)
      211
      212 def fatal_limits_exceeded(self):

      DOcplexException: CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud

      Is there a way to resolve this error?

    • Author gravatar

      Hi Sameer –

      To resolve this error, I suggest that you visit https://www.gurobi.com/academia/academic-program-and-licenses/.

      Gwyneth

Leave a Reply

Your email address will not be published. Required fields are marked *