{ "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", "< [Getting Started with CVXPY](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/01.01-Getting-Started-with-CVXPY.ipynb) | [Contents](toc.ipynb) | [Getting Started with GNU MathProg in Jupyter Notebooks](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/01.03-Getting-Started-with-GNU-MathProg.ipynb) >

\"Open

\"Download\"" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "# Getting Started with Gurobi\n", "\n", "[Gurobi](http://www.gurobi.com) is a commercial, state-of-the-art mathematical programming engines used in a diverse array of industries. It is available under academic licensing terms that allow free use by faculty and students in accredited insitutions, and comes with a very complete Python interface. The purpose of this notebook is to help you get started using Gurobi via the Python interface.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "## Simple Two-Variable LP" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "pycharm": {} }, "outputs": [ { "data": { "text/plain": [ "[2.4, 1.6]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from gurobi import *\n", "m = Model()\n", "\n", "v0 = m.addVar()\n", "v1 = m.addVar()\n", "m.update()\n", "\n", "m.getVars()\n", "\n", "m.addConstr(v0 - v1 <= 4)\n", "m.addConstr(v0 + v1 <= 4)\n", "m.addConstr(-0.25*v0 + v1 <= 1)\n", "m.setObjective(v1, GRB.MAXIMIZE)\n", "m.params.outputflag = 0\n", "m.optimize()\n", "[v0.x,v1.x]" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "## Assignment Problem" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "### Problem Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "pycharm": {} }, "outputs": [ { "data": { "text/html": [ "

\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AtlantaBoiseCharlotteDallasFresno
Austin921162711661961594
Boston1078266183717673107
Chicago71616937569252140
Denver140081515617881142
Edmonton37641718384833102835
\n", "
" ], "text/plain": [ " Atlanta Boise Charlotte Dallas Fresno\n", "Austin 921 1627 1166 196 1594\n", "Boston 1078 2661 837 1767 3107\n", "Chicago 716 1693 756 925 2140\n", "Denver 1400 815 1561 788 1142\n", "Edmonton 3764 1718 3848 3310 2835" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "LOCATIONS = ['Austin','Boston','Chicago','Denver','Edmonton']\n", "\n", "distances = pd.DataFrame(index=LOCATIONS)\n", "distances['Atlanta'] = [921,1078,716,1400,3764]\n", "distances['Boise'] = [1627, 2661, 1693, 815, 1718]\n", "distances['Charlotte'] = [1166, 837, 756, 1561, 3848]\n", "distances['Dallas'] = [196, 1767, 925, 788, 3310]\n", "distances['Fresno'] = [1594, 3107, 2140, 1142, 2835]\n", "\n", "distances" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "### Model and Optimization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimize a model with 10 rows, 25 columns and 50 nonzeros\n", "Variable types: 0 continuous, 25 integer (25 binary)\n", "Coefficient statistics:\n", " Matrix range [1e+00, 1e+00]\n", " Objective range [2e+02, 4e+03]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [1e+00, 1e+00]\n", "Found heuristic solution: objective 8790\n", "Presolve time: 0.00s\n", "Presolved: 10 rows, 25 columns, 50 nonzeros\n", "Variable types: 0 continuous, 25 integer (25 binary)\n", "\n", "Root relaxation: objective 4.609000e+03, 6 iterations, 0.00 seconds\n", "\n", " Nodes | Current Node | Objective Bounds | Work\n", " Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n", "\n", "* 0 0 0 4609.0000000 4609.00000 0.00% - 0s\n", "\n", "Explored 0 nodes (6 simplex iterations) in 0.02 seconds\n", "Thread count was 8 (of 8 available processors)\n", "\n", "Solution count 2: 4609 8790 \n", "Pool objective bound 4609\n", "\n", "Optimal solution found (tolerance 1.00e-04)\n", "Best objective 4.609000000000e+03, best bound 4.609000000000e+03, gap 0.0000%\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gurobi as grb\n", "\n", "m = grb.Model()\n", "\n", "RESOURCES = distances.index\n", "TASKS = distances.columns\n", "\n", "x = m.addVars(RESOURCES,TASKS,vtype=grb.GRB.BINARY)\n", "m.addConstrs(sum([x[r,t] for t in TASKS]) == 1 for r in RESOURCES)\n", "m.addConstrs(sum([x[r,t] for r in RESOURCES]) == 1 for t in TASKS)\n", "m.setObjective(sum([distances.ix[r,t]*x[r,t] for r in RESOURCES for t in TASKS]),\n", " grb.GRB.MINIMIZE)\n", "m.optimize();\n", "m.status" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assign Austin to Dallas Cost: 196\n", "Assign Boston to Charlotte Cost: 837\n", "Assign Chicago to Atlanta Cost: 716\n", "Assign Denver to Fresno Cost: 1142\n", "Assign Edmonton to Boise Cost: 1718\n" ] } ], "source": [ "for r in RESOURCES:\n", " for t in TASKS:\n", " if int(round(x[r,t].x)):\n", " print(\"Assign {0:10s} to {1:10s} Cost: {2:5.0f}\".format(r,t,distances.ix[r,t]))\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "pycharm": {} }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "\n", "< [Getting Started with CVXPY](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/01.01-Getting-Started-with-CVXPY.ipynb) | [Contents](toc.ipynb) | [Getting Started with GNU MathProg in Jupyter Notebooks](http://nbviewer.jupyter.org/github/jckantor/CBE40455/blob/master/notebooks/01.03-Getting-Started-with-GNU-MathProg.ipynb) >

\"Open

\"Download\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }