{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial: Beyond Linear Programming, (CPLEX Part2)\n", "\n", "This notebook describes some special cases of LP, as well as some other non-LP techniques, and also under which conditions they should be used. \n", "\n", "Before continuing, you should ensure that you followed the CPLEX Tutorial Part 1.\n", "\n", "After completing this unit, you should be able to describe what a network model is, and the benefits of using network models, explain the concepts of nonlinearity and convexity, describe what a piecewise linear function is, and describe the differences between Linear Programming (LP), Integer Programming (IP), Mixed-Integer Programming (MIP), and Quadratic Programming (QP). You should also be able to construct a simple MIP model. \n", "\n", ">This notebook is part of **[Prescriptive Analytics for Python](http://ibmdecisionoptimization.github.io/docplex-doc/)**\n", ">\n", ">It requires either an [installation of CPLEX Optimizers](http://ibmdecisionoptimization.github.io/docplex-doc/getting_started.html) or it can be run on [IBM Cloud Pak for Data as a Service](https://www.ibm.com/products/cloud-pak-for-data/as-a-service/) (Sign up for a [free IBM Cloud account](https://dataplatform.cloud.ibm.com/registration/stepone?context=wdp&apps=all>)\n", "and you can start using `IBM Cloud Pak for Data as a Service` right away).\n", ">\n", "> CPLEX is available on IBM Cloud Pack for Data and IBM Cloud Pak for Data as a Service:\n", "> - IBM Cloud Pak for Data as a Service: Depends on the runtime used:\n", "> - Python 3.x runtime: Community edition\n", "> - Python 3.x + DO runtime: full edition\n", "> - Cloud Pack for Data: Community edition is installed by default. Please install the `DO` addon in `Watson Studio Premium` for the full edition\n", "\n", "\n", "\n", "Table of contents:\n", "\n", "* [CPLEX Modeling for Python](#Use-IBM-Decision-Optimization-CPLEX-Modeling-for-Python)\n", "* [Network models](#Network-models)\n", "* [Non-linearity and Convexity](#Non-linearity-and-Convexity)\n", "* [Integer Optimization](#Integer-Optimization)\n", "* [Quadratic Programming](#Quadratic-Programming)\n", "\n", "We will use DOcplex to write small samples to illustrate the topics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use IBM Decision Optimization CPLEX Modeling for Python\n", "\n", "Let's use the [DOcplex](http://ibmdecisionoptimization.github.io/docplex-doc/) Python library to write sample models in Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download the library\n", "\n", "Install `CPLEX` (Community Edition) and `docplex` if they are not installed.\n", "\n", "In `IBM Cloud Pak for Data as a Service` notebooks, `CPLEX` and `docplex` are preinstalled." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "try:\n", " import cplex\n", "except:\n", " if hasattr(sys, 'real_prefix'):\n", " #we are in a virtual env.\n", " !pip install cplex\n", " else:\n", " !pip install --user cplex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installs `DOcplex`if needed" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import sys\n", "try:\n", " import docplex.mp\n", "except:\n", " if hasattr(sys, 'real_prefix'):\n", " #we are in a virtual env.\n", " !pip install docplex\n", " else:\n", " !pip install --user docplex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If either `CPLEX` or `docplex` where installed in the steps above, you will need to restart your jupyter kernel for the changes to be taken into account." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Network models\n", "\n", "In this topic, you’ll learn what a network model is, and how its structure can be exploited for more efficient solutions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Networks in real life\n", "\n", "Several problems encountered in Operations Research (OR) involve networks, such as:\n", "Distribution problems (for example, transportation networks)\n", "Assignment problems (for example, networks of workers and jobs they could be assigned to)\n", "Planning problems (for example, critical path analysis for project planning)\n", "\n", "Many network models are special LP problems for which specialized solution algorithms exist. \n", "\n", "It is important to know whether a problem can be formulated as a network model to exploit the special structure.\n", "\n", "This topic introduces networks in general, as well as some well-known instances of network models.\n", "\n", "## Network modeling concepts\n", "\n", "Any network structure can be described using two types of objects:\n", "\n", "- Nodes: Defined points in the network, for example warehouses.\n", "- Arcs: An arc connects two nodes, for example a road connecting two warehouses. \n", "\n", "An arc can be _directed_, which means than an arc $a_{ij}$ from node $i$ to node $j$ is different from arc $a_ji$ that begins at node $j$ and ends at node $i$.\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " | return | \n", "area | \n", "
---|---|---|
sector | \n", "\n", " | \n", " |
treasury | \n", "5 | \n", "N-Am. | \n", "
hardware | \n", "17 | \n", "N-Am. | \n", "
theater | \n", "26 | \n", "N-Am. | \n", "
telecom | \n", "12 | \n", "N-Am. | \n", "
brewery | \n", "8 | \n", "ww | \n", "
highways | \n", "9 | \n", "ww | \n", "
cars | \n", "7 | \n", "ww | \n", "
bank | \n", "6 | \n", "ww | \n", "
software | \n", "31 | \n", "ww | \n", "
electronics | \n", "21 | \n", "ww | \n", "
sector | \n", "treasury | \n", "hardware | \n", "theater | \n", "telecom | \n", "brewery | \n", "highways | \n", "cars | \n", "bank | \n", "software | \n", "electronics | \n", "
---|---|---|---|---|---|---|---|---|---|---|
sector | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
treasury | \n", "0.1 | \n", "0.0 | \n", "0 | \n", "0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "
hardware | \n", "0.0 | \n", "19.0 | \n", "-2 | \n", "4 | \n", "1.0 | \n", "1.0 | \n", "1.0 | \n", "0.5 | \n", "10.0 | \n", "5.0 | \n", "
theater | \n", "0.0 | \n", "-2.0 | \n", "28 | \n", "1 | \n", "2.0 | \n", "1.0 | \n", "1.0 | \n", "0.0 | \n", "-2.0 | \n", "-1.0 | \n", "
telecom | \n", "0.0 | \n", "4.0 | \n", "1 | \n", "22 | \n", "0.0 | \n", "1.0 | \n", "2.0 | \n", "0.0 | \n", "3.0 | \n", "4.0 | \n", "
brewery | \n", "0.0 | \n", "1.0 | \n", "2 | \n", "0 | \n", "4.0 | \n", "-1.5 | \n", "-2.0 | \n", "-1.0 | \n", "1.0 | \n", "1.0 | \n", "
highways | \n", "0.0 | \n", "1.0 | \n", "1 | \n", "1 | \n", "-1.5 | \n", "3.5 | \n", "2.0 | \n", "0.5 | \n", "1.0 | \n", "1.5 | \n", "
cars | \n", "0.0 | \n", "1.0 | \n", "1 | \n", "2 | \n", "-2.0 | \n", "2.0 | \n", "5.0 | \n", "0.5 | \n", "1.0 | \n", "2.5 | \n", "
bank | \n", "0.0 | \n", "0.5 | \n", "0 | \n", "0 | \n", "-1.0 | \n", "0.5 | \n", "0.5 | \n", "1.0 | \n", "0.5 | \n", "0.5 | \n", "
software | \n", "0.0 | \n", "10.0 | \n", "-2 | \n", "3 | \n", "1.0 | \n", "1.0 | \n", "1.0 | \n", "0.5 | \n", "25.0 | \n", "8.0 | \n", "
electronics | \n", "0.0 | \n", "5.0 | \n", "-1 | \n", "4 | \n", "1.0 | \n", "1.5 | \n", "2.5 | \n", "0.5 | \n", "8.0 | \n", "16.0 | \n", "
\n", " | return | \n", "area | \n", "is_na | \n", "
---|---|---|---|
sector | \n", "\n", " | \n", " | \n", " |
treasury | \n", "5 | \n", "N-Am. | \n", "1 | \n", "
hardware | \n", "17 | \n", "N-Am. | \n", "1 | \n", "
theater | \n", "26 | \n", "N-Am. | \n", "1 | \n", "
telecom | \n", "12 | \n", "N-Am. | \n", "1 | \n", "
brewery | \n", "8 | \n", "ww | \n", "0 | \n", "
highways | \n", "9 | \n", "ww | \n", "0 | \n", "
cars | \n", "7 | \n", "ww | \n", "0 | \n", "
bank | \n", "6 | \n", "ww | \n", "0 | \n", "
software | \n", "31 | \n", "ww | \n", "0 | \n", "
electronics | \n", "21 | \n", "ww | \n", "0 | \n", "