{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "\n", "*This notebook contains course material from [CBE40455](https://jckantor.github.io/CBE40455) by\n", "Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE40455.git).\n", "The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode),\n", "and code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "\n", "< [Production Models with Constraints](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/03.01-Production-Models-with-Constraints.ipynb) | [Contents](toc.ipynb) | [Data Reconciliation](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/03.03-Data-Reconciliation.ipynb) >

\"Open

\"Download\"" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "# Transportation Networks" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "This notebook demonstrates the solution of transportation network problems using Pyomo and GLPK. The problem description and data are adapted from Chapter 5 of Johannes Bisschop, \"AIMMS Optimization Modeling\", AIMMS B. V., 2014. [pdf available](http://download.aimms.com/aimms/download/manuals/AIMMS3_OM.pdf)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "## Background" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "fragment" } }, "source": [ "The prototypical transportation problem deals with the distribution of a commodity from a set of sources to a set of destinations. The object is to minimize total transportation costs while satisfying constraints on the supplies available at each of the sources, and satisfying demand requirements at each of the destinations.\n", "\n", "Here we illustrate the transportation problem using an example from Chapter 5 of Johannes Bisschop, \"AIMMS Optimization Modeling\", Paragon Decision Sciences, 1999. In this example there are two factories and six customer sites located in 8 European cities as shown in the following map. The customer sites are labeled in red, the factories are labeled in blue.\n", "\n", "![TransportationNetworksMap.png](https://github.com/jckantor/CBE40455/blob/master/notebooks/figures/TransportationNetworksMap.png?raw=true)\n", "\n", "Transportation costs between sources and destinations are given in units of €/ton of goods shipped, and list in the following table along with source capacity and demand requirements." ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "fragment" } }, "source": [ "### Table of Transportation Costs, Customer Demand, and Available Supplies\n", "\n", "| Customer\\Source | Arnhem [€/ton] | Gouda [€/ton] | Demand [tons]|\n", "| :--: | :----: | :---: | :----: |\n", "| London | n/a | 2.5 | 125 |\n", "| Berlin | 2.5 | n/a | 175 |\n", "| Maastricht | 1.6 | 2.0 | 225 |\n", "| Amsterdam | 1.4 | 1.0 | 250 |\n", "| Utrecht | 0.8 | 1.0 | 225 |\n", "| The Hague | 1.4 | 0.8 | 200 |\n", "| **Supply [tons]** | 550 tons | 700 tons | |\n", "\n", "The situation can be modeled by links connecting a set nodes representing sources to a set of nodes representing customers.\n", "\n", "![TransportNet](https://github.com/jckantor/CBE40455/blob/master/notebooks/figures/TransportNet.png?raw=true)\n", "\n", "For each link we can have a parameter $T[c,s]$ denoting the cost of shipping a ton of goods over the link. What we need to determine is the amount of goods to be shipped over each link, which we will represent as a non-negative decision variable $x[c,s]$.\n", "\n", "The problem objective is to minimize the total shipping cost to all customers from all sources. \n", "\n", "$$\\mbox{minimize:}\\quad \\mbox{Cost} = \\sum_{c \\in Customers}\\sum_{s \\in Sources} T[c,s] x[c,s]$$\n", "\n", "Shipments from all sources can not exceed the manufacturing capacity of the source.\n", "\n", "$$\\sum_{c \\in Customers} x[c,s] \\leq \\mbox{Supply}[s] \\qquad \\forall s \\in Sources$$\n", "\n", "Shipments to each customer must satisfy their demand.\n", "\n", "$$\\sum_{s\\in Sources} x[c,s] = \\mbox{Demand}[c] \\qquad \\forall c \\in Customers$$" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "## Pyomo Model" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "### Data File" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true, "pycharm": {} }, "outputs": [], "source": [ "Demand = {\n", " 'Lon': 125, # London\n", " 'Ber': 175, # Berlin\n", " 'Maa': 225, # Maastricht\n", " 'Ams': 250, # Amsterdam\n", " 'Utr': 225, # Utrecht\n", " 'Hag': 200 # The Hague\n", "}\n", "\n", "Supply = {\n", " 'Arn': 600, # Arnhem\n", " 'Gou': 650 # Gouda\n", "}\n", "\n", "T = {\n", " ('Lon','Arn'): 1000,\n", " ('Lon','Gou'): 2.5,\n", " ('Ber','Arn'): 2.5,\n", " ('Ber','Gou'): 1000,\n", " ('Maa','Arn'): 1.6,\n", " ('Maa','Gou'): 2.0,\n", " ('Ams','Arn'): 1.4,\n", " ('Ams','Gou'): 1.0,\n", " ('Utr','Arn'): 0.8,\n", " ('Utr','Gou'): 1.0,\n", " ('Hag','Arn'): 1.4,\n", " ('Hag','Gou'): 0.8\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": {} }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "### Model File" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# ==========================================================\n", "# = Solver Results =\n", "# ==========================================================\n", "# ----------------------------------------------------------\n", "# Problem Information\n", "# ----------------------------------------------------------\n", "Problem: \n", "- Name: unknown\n", " Lower bound: 1705.0\n", " Upper bound: 1705.0\n", " Number of objectives: 1\n", " Number of constraints: 9\n", " Number of variables: 13\n", " Number of nonzeros: 25\n", " Sense: minimize\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " Termination condition: optimal\n", " Statistics: \n", " Branch and bound: \n", " Number of bounded subproblems: 0\n", " Number of created subproblems: 0\n", " Error rc: 0\n", " Time: 0.029633045196533203\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n" ] } ], "source": [ "from pyomo.environ import *\n", "\n", "# Step 0: Create an instance of the model\n", "model = ConcreteModel()\n", "model.dual = Suffix(direction=Suffix.IMPORT)\n", "\n", "# Step 1: Define index sets\n", "CUS = list(Demand.keys())\n", "SRC = list(Supply.keys())\n", "\n", "# Step 2: Define the decision \n", "model.x = Var(CUS, SRC, domain = NonNegativeReals)\n", "\n", "# Step 3: Define Objective\n", "model.Cost = Objective(\n", " expr = sum([T[c,s]*model.x[c,s] for c in CUS for s in SRC]),\n", " sense = minimize)\n", "\n", "# Step 4: Constraints\n", "model.src = ConstraintList()\n", "for s in SRC:\n", " model.src.add(sum([model.x[c,s] for c in CUS]) <= Supply[s])\n", " \n", "model.dmd = ConstraintList()\n", "for c in CUS:\n", " model.dmd.add(sum([model.x[c,s] for s in SRC]) == Demand[c])\n", " \n", "results = SolverFactory('glpk').solve(model)\n", "results.write()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "## Solution" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lon Arn 0.0\n", "Lon Gou 125.0\n", "Ber Arn 175.0\n", "Ber Gou 0.0\n", "Maa Arn 225.0\n", "Maa Gou 0.0\n", "Ams Arn 0.0\n", "Ams Gou 250.0\n", "Utr Arn 200.0\n", "Utr Gou 25.0\n", "Hag Arn 0.0\n", "Hag Gou 200.0\n" ] } ], "source": [ "for c in CUS:\n", " for s in SRC:\n", " print(c, s, model.x[c,s]())" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total Shipping Costs = 1705.0\n", "\n", "Shipping Table:\n", "Ship from Arn to Ber : 175.0\n", "Ship from Arn to Maa : 225.0\n", "Ship from Arn to Utr : 200.0\n", "Ship from Gou to Lon : 125.0\n", "Ship from Gou to Ams : 250.0\n", "Ship from Gou to Utr : 25.0\n", "Ship from Gou to Hag : 200.0\n" ] } ], "source": [ "if 'ok' == str(results.Solver.status):\n", " print(\"Total Shipping Costs = \",model.Cost())\n", " print(\"\\nShipping Table:\")\n", " for s in SRC:\n", " for c in CUS:\n", " if model.x[c,s]() > 0:\n", " print(\"Ship from \", s,\" to \", c, \":\", model.x[c,s]())\n", "else:\n", " print(\"No Valid Solution Found\")" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "The solution has the interesting property that, with the exception of Utrecht, customers are served by just one source.\n", "\n", "![TransportNet_soln](https://github.com/jckantor/CBE40455/blob/master/notebooks/figures/TransportNet_soln.png?raw=true)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "## Sensitivity Analysis" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "### Analysis by Source" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Sources:\n", "Source Capacity Shipped Margin\n", "Arn 600.0 600.0 -0.2000\n", "Gou 650.0 600.0 0.0000\n" ] } ], "source": [ "if 'ok' == str(results.Solver.status):\n", " print(\"\\nSources:\")\n", " print(\"Source Capacity Shipped Margin\")\n", " for m in model.src.keys():\n", " s = SRC[m-1]\n", " print(\"{0:10s}{1:10.1f}{2:10.1f}{3:10.4f}\".format(s,Supply[s],model.src[m](),model.dual[model.src[m]]))\n", "else:\n", " print(\"No Valid Solution Found\")" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "The 'marginal' values are telling us how much the total costs will be increased for each one ton increase in the available supply from each source. The optimization calculation says that only 650 tons of the 700 available from Gouda should used for a minimum cost solution, which rules out any further cost reductions by increasing the available supply. In fact, we could decrease the supply Gouda without any harm. The marginal value of Gouda is 0.\n", "\n", "The source at Arnhem is a different matter. First, all 550 available tons are being used. Second, from the marginal value we see that the total transportations costs would be reduced by 0.2 Euros for each additional ton of supply. \n", "\n", "The management conclusion we can draw is that there is excess supply available at Gouda which should, if feasible, me moved to Arnhem.\n", "\n", "Now that's a valuable piece of information!" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "### Analysis by Customer" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Customers:\n", "Customer Demand Shipped Margin\n", "Lon 125.0 125.0 2.5000\n", "Ber 175.0 175.0 2.7000\n", "Maa 225.0 225.0 1.8000\n", "Ams 250.0 250.0 1.0000\n", "Utr 225.0 225.0 1.0000\n", "Hag 200.0 200.0 0.8000\n" ] } ], "source": [ "if 'ok' == str(results.Solver.status): \n", " print(\"\\nCustomers:\")\n", " print(\"Customer Demand Shipped Margin\")\n", " for n in model.dmd.keys():\n", " c = CUS[n-1]\n", " print(\"{0:10s}{1:10.1f}{2:10.1f}{3:10.4f}\".format(c,Demand[c],model.dmd[n](),model.dual[model.dmd[n]]))\n", "else:\n", " print(\"No Valid Solution Found\")" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "Looking at the demand constraints, we see that all of the required demands have been met by the optimal solution.\n", "\n", "The marginal values of these constraints indicate how much the total transportation costs will increase if there is an additional ton of demand at any of the locations. In particular, note that increasing the demand at Berlin will increase costs by 2.7 Euros per ton. This is actually **greater** than the list price for shipping to Berlin which is 2.5 Euros per ton. Why is this?\n", "\n", "To see what's going on, let's resolve the problem with a one ton increase in the demand at Berlin.\n", "\n", "We see the total cost has increased from 1715.0 to 1717.7 Euros, an increase of 2.7 Euros just as predicted by the marginal value assocated with the demand constraint for Berlin.\n", "\n", "Now let's look at the solution.\n", "\n", "Here we see that increasing the demand in Berlin resulted in a number of other changes. This figure shows the changes shipments.\n", "\n", "![TransportNet_sens](https://github.com/jckantor/CBE40455/blob/master/notebooks/figures/TransportNet_sens.png?raw=true)\n", "\n", "* Shipments to Berlin increased from 175 to 176 tons, increasing costs for that link from 437.5 to 440.0, or a net increase of 2.5 Euros.\n", "* Since Arnhem is operating at full capacity, increasing the shipments from Arnhem to Berlin resulted in decreasing the shipments from Arhhem to Utrecht from 150 to 149 reducing those shipping costs from 120.0 to 119.2, a net decrease of 0.8 Euros.\n", "* To meet demand at Utrecht, shipments from Gouda to Utrecht had to increase from 75 to 76, increasing shipping costs by a net amount of 1.0 Euros.\n", "* The net effect on shipping costs is 2.5 - 0.8 + 1.0 = 2.7 Euros.\n", "\n", "The important conclusion to draw is that when operating under optimal conditions, a change in demand or supply can have a ripple effect on the optimal solution that can only be measured through a proper sensitivity analysis." ] }, { "cell_type": "markdown", "metadata": { "pycharm": {}, "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercises\n", "\n", "1. Move 50 tons of supply capacity from Gouda to Arnhem, and repeat the sensitivity analysis. How has the situation improved? In practice, would you recommend this change, or would you propose something different?\n", "2. What other business improvements would you recommend?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "pycharm": {} }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "\n", "< [Production Models with Constraints](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/03.01-Production-Models-with-Constraints.ipynb) | [Contents](toc.ipynb) | [Data Reconciliation](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/03.03-Data-Reconciliation.ipynb) >

\"Open

\"Download\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }