diff --git a/intro_to_gurobipy/intro_to_gurobipy/intro_to_gurobipy.ipynb b/intro_to_gurobipy/intro_to_gurobipy/intro_to_gurobipy.ipynb index 98a69a8..8f83ca4 100644 --- a/intro_to_gurobipy/intro_to_gurobipy/intro_to_gurobipy.ipynb +++ b/intro_to_gurobipy/intro_to_gurobipy/intro_to_gurobipy.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "aae0f1e0", + "id": "ff267917", "metadata": { "slideshow": { "slide_type": "slide" @@ -19,7 +19,7 @@ }, { "cell_type": "markdown", - "id": "4886f223", + "id": "c7add96f", "metadata": { "slideshow": { "slide_type": "slide" @@ -39,7 +39,7 @@ { "cell_type": "code", "execution_count": null, - "id": "cf907810", + "id": "becedc5b", "metadata": { "slideshow": { "slide_type": "slide" @@ -57,7 +57,7 @@ }, { "cell_type": "markdown", - "id": "d897e49c", + "id": "e6a335dc", "metadata": { "slideshow": { "slide_type": "slide" @@ -76,7 +76,7 @@ }, { "cell_type": "markdown", - "id": "98f0c164", + "id": "bf8838b1", "metadata": { "slideshow": { "slide_type": "slide" @@ -85,11 +85,10 @@ "source": [ "## How to Install gurobipy?\n", "\n", - "There are three main approaches to install gurobipy on any operating systems such as Linux, Windows, or macOS.\n", + "There are two main approaches to install gurobipy on any operating systems such as Linux, Windows, or macOS.\n", "\n", - "- [Pip](https://www.gurobi.com/documentation/10.0/quickstart_mac/cs_using_pip_to_install_gr.html#subsubsection:pip)\n", - "- [Conda](https://www.gurobi.com/documentation/10.0/quickstart_mac/cs_anaconda_and_grb_conda_.html#subsubsection:Anaconda)\n", - "- [Manual installation via the Gurobi distribution package](https://www.gurobi.com/documentation/10.0/quickstart_mac/cs_manual_installation.html#subsubsection:manualinstall)\n", + "- Pip\n", + "- Conda\n", "\n", "Relevant Knowledge Base (KB) articles:\n", "- [Which Python versions are supported by Gurobi?](https://support.gurobi.com/hc/en-us/articles/360013195212)\n", @@ -98,7 +97,7 @@ }, { "cell_type": "markdown", - "id": "4a93e6b2", + "id": "6a5efc36", "metadata": { "slideshow": { "slide_type": "slide" @@ -136,7 +135,7 @@ }, { "cell_type": "markdown", - "id": "fd2775ec", + "id": "5b2738d7", "metadata": { "slideshow": { "slide_type": "slide" @@ -157,7 +156,7 @@ { "cell_type": "code", "execution_count": null, - "id": "26ccb11a", + "id": "4a1cf970", "metadata": { "code_folding": [ 22 @@ -199,7 +198,7 @@ }, { "cell_type": "markdown", - "id": "d867a3cb", + "id": "1db40407", "metadata": { "slideshow": { "slide_type": "slide" @@ -228,7 +227,7 @@ }, { "cell_type": "markdown", - "id": "057765ad", + "id": "e0a8c8df", "metadata": { "slideshow": { "slide_type": "slide" @@ -237,29 +236,29 @@ "source": [ " ## Extented Data Structures in Gurobi Python API\n", " \n", - " - [tuplelist()](https://www.gurobi.com/documentation/10.0/refman/py_tuplelist.html)\n", + " - [tuplelist()](https://www.gurobi.com/documentation/current/refman/py_tuplelist.html)\n", " - A sub-class of Python list \n", " - Important methods to build sub-lists efficiently \n", - " - [tuplelist.select(pattern)](https://www.gurobi.com/documentation/10.0/refman/py_tuplelist_select.html) --> tuplelist()\n", + " - [tuplelist.select(pattern)](https://www.gurobi.com/documentation/current/refman/py_tuplelist_select.html) --> tuplelist()\n", " \n", " \n", " \n", - "- [tupledict()](https://www.gurobi.com/documentation/10.0/refman/py_tupledict.html)\n", + "- [tupledict()](https://www.gurobi.com/documentation/current/refman/py_tupledict.html)\n", " - A sub-class of Python dict\n", " - The keys of a tupledict() are stored as tuplelist() and the values are Gurobi variable objects\n", " - Important methods to build linear expressions efficiently:\n", - " - [tupledict.select(pattern)](https://www.gurobi.com/documentation/10.0/refman/py_tupledict_select.html) --> List\n", - " - [tupledict.sum(pattern)](https://www.gurobi.com/documentation/10.0/refman/py_tupledict_sum.html) --> LinExpr()\n", - " - [tupledict.prod(coeff, pattern)](https://www.gurobi.com/documentation/10.0/refman/py_tupledict_prod.html) --> LinExpr()\n", + " - [tupledict.select(pattern)](https://www.gurobi.com/documentation/current/refman/py_tupledict_select.html) --> List\n", + " - [tupledict.sum(pattern)](https://www.gurobi.com/documentation/current/refman/py_tupledict_sum.html) --> LinExpr()\n", + " - [tupledict.prod(coeff, pattern)](https://www.gurobi.com/documentation/current/refman/py_tupledict_prod.html) --> LinExpr()\n", " \n", " \n", - "- [multidict()](https://www.gurobi.com/documentation/10.0/refman/py_multidict.html): A convenience function to define multiple dictionaries in one statement." + "- [multidict()](https://www.gurobi.com/documentation/current/refman/py_multidict.html): A convenience function to define multiple dictionaries in one statement." ] }, { "cell_type": "code", "execution_count": null, - "id": "8e3e45bf", + "id": "c4540670", "metadata": { "slideshow": { "slide_type": "fragment" @@ -286,7 +285,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a2f0b69e", + "id": "4763eaef", "metadata": {}, "outputs": [], "source": [ @@ -298,7 +297,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6d88a28a", + "id": "812389ed", "metadata": {}, "outputs": [], "source": [ @@ -312,7 +311,7 @@ { "cell_type": "code", "execution_count": null, - "id": "85713ffb", + "id": "b1260ab8", "metadata": { "slideshow": { "slide_type": "fragment" @@ -338,14 +337,14 @@ }, { "cell_type": "markdown", - "id": "d86bacf9", + "id": "f2eba8ea", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Environments](https://www.gurobi.com/documentation/10.0/refman/py_env2.html)\n", + "## [Environments](https://www.gurobi.com/documentation/current/refman/py_env2.html)\n", "\n", "```\n", "+--------------------------------------------+\n", @@ -368,20 +367,20 @@ "- Using remote resources such as floating, cloud, or compute server licenses for optimization\n", "- Garbage collection when using Jupyter notebooks\n", "\n", - "Note: It is better to create new environments via the context manager." + "Note: It is better to create new environments via the [context manager](https://support.gurobi.com/hc/en-us/articles/4424054948881-How-do-I-manage-Gurobi-environments-in-gurobipy)." ] }, { "cell_type": "markdown", - "id": "7f0177cb", + "id": "d822c113", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Model](https://www.gurobi.com/documentation/10.0/refman/py_model.html)\n", - "Model building in the [Python API](https://www.gurobi.com/documentation/10.0/refman/py_python_api_overview.html) is object oriented. The reference manual contains [a full list of methods](https://www.gurobi.com/documentation/10.0/refman/py_python_api_details.html#sec:Python-details) on a model object. \n", + "## [Model](https://www.gurobi.com/documentation/current/refman/py_model.html)\n", + "Model building in the [Python API](https://www.gurobi.com/documentation/current/refman/py_python_api_overview.html) is object oriented. The reference manual contains [a full list of methods](https://www.gurobi.com/documentation/current/refman/py_python_api_details.html#sec:Python-details) on a model object. \n", "\n", "The signature for constructing a model object is:\n", "```\n", @@ -392,7 +391,7 @@ { "cell_type": "code", "execution_count": null, - "id": "70b5f668", + "id": "3ece5640", "metadata": { "slideshow": { "slide_type": "fragment" @@ -422,7 +421,7 @@ }, { "cell_type": "markdown", - "id": "89b3d23f", + "id": "39e336b8", "metadata": { "slideshow": { "slide_type": "slide" @@ -431,7 +430,7 @@ "source": [ "## Decision Variables\n", "\n", - "Since variables are associated with a particular model object, use the [Model.addVar()](https://www.gurobi.com/documentation/10.0/refman/py_model_addvar.html#pythonmethod:Model.addVar) method to create a Gurobi variable object ([Var](https://www.gurobi.com/documentation/10.0/refman/py_var.html)):\n", + "Since variables are associated with a particular model object, use the [Model.addVar()](https://www.gurobi.com/documentation/current/refman/py_model_addvar.html#pythonmethod:Model.addVar) method to create a Gurobi variable object ([Var](https://www.gurobi.com/documentation/current/refman/py_var.html)):\n", "```\n", "Model.addVar(lb=0, ub=float(\"inf\"), obj=0, vtype=GRB.CONTINUOUS, name=\"\", column=None)\n", "```\n", @@ -449,7 +448,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4d532b26", + "id": "00a3f876", "metadata": { "slideshow": { "slide_type": "fragment" @@ -469,14 +468,14 @@ }, { "cell_type": "markdown", - "id": "8e815095", + "id": "da2b7c97", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Model.addVars()](https://www.gurobi.com/documentation/10.0/refman/py_model_addvars.html#pythonmethod:Model.addVar)\n", + "## [Model.addVars()](https://www.gurobi.com/documentation/current/refman/py_model_addvars.html#pythonmethod:Model.addVar)\n", "\n", "To add multiple decision variables to the model, use the Model.addVars() method which returns a Gurobi tupledict object containing the newly created variables:\n", "```\n", @@ -496,7 +495,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a055a6ff", + "id": "b39a6221", "metadata": { "slideshow": { "slide_type": "fragment" @@ -535,7 +534,7 @@ }, { "cell_type": "markdown", - "id": "500bc5af", + "id": "88c44161", "metadata": { "slideshow": { "slide_type": "slide" @@ -543,12 +542,12 @@ }, "source": [ "## Constraints\n", - "Like variables, constraints are also associated with a model. Use the method [Model.addConstr()](https://www.gurobi.com/documentation/10.0/refman/py_model_addconstr.html) to add a constraint to a model.\n", + "Like variables, constraints are also associated with a model. Use the method [Model.addConstr()](https://www.gurobi.com/documentation/current/refman/py_model_addconstr.html) to add a constraint to a model.\n", "```\n", "Model.addConstr(constr, name=\"\")\n", "```\n", "\n", - "`constr` is a [TempConstr](https://www.gurobi.com/documentation/10.0/refman/py_tempconstr.html#pythonclass:TempConstr) object that can take different types:\n", + "`constr` is a [TempConstr](https://www.gurobi.com/documentation/current/refman/py_tempconstr.html#pythonclass:TempConstr) object that can take different types:\n", "\n", "- Linear Constraint: `x + y <= 1` \n", "- Ranged Linear Constraint: `x + y == [1, 3]`\n", @@ -564,7 +563,7 @@ { "cell_type": "code", "execution_count": null, - "id": "be75ff75", + "id": "600cc5c8", "metadata": { "slideshow": { "slide_type": "fragment" @@ -609,14 +608,14 @@ }, { "cell_type": "markdown", - "id": "a00d33e3", + "id": "23e0adc0", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Model.addConstrs](https://www.gurobi.com/documentation/10.0/refman/py_model_addconstrs.html)\n", + "## [Model.addConstrs](https://www.gurobi.com/documentation/current/refman/py_model_addconstrs.html)\n", "\n", "To add multiple constraints to the model, use the Model.addConstrs() method which returns a Gurobi tupledict that contains the newly created constraints:\n", "\n", @@ -628,7 +627,7 @@ { "cell_type": "code", "execution_count": null, - "id": "db345945", + "id": "1c2ea69a", "metadata": { "slideshow": { "slide_type": "fragment" @@ -656,7 +655,7 @@ }, { "cell_type": "markdown", - "id": "8b403781", + "id": "87741684", "metadata": { "slideshow": { "slide_type": "slide" @@ -665,13 +664,13 @@ "source": [ "## Objective Function\n", "\n", - "To set the model objective equal to a linear or a quadratic expression, use the [Model.setObjective()](https://www.gurobi.com/documentation/10.0/refman/py_model_setobjective.html) method:\n", + "To set the model objective equal to a linear or a quadratic expression, use the [Model.setObjective()](https://www.gurobi.com/documentation/current/refman/py_model_setobjective.html) method:\n", "```\n", "Model.setObjective(expr, sense=GRB.MINIMIZE)\n", "```\n", "- expr: \n", - " - [LinExpr()](https://www.gurobi.com/documentation/10.0/refman/py_lex.html): constant + coefficient-variable pairs capturing linear terms\n", - " - [QuadExpr()](https://www.gurobi.com/documentation/10.0/refman/py_qex.html): linear expression + list of coefficient-variable-variable triples\n", + " - [LinExpr()](https://www.gurobi.com/documentation/current/refman/py_lex.html): constant + coefficient-variable pairs capturing linear terms\n", + " - [QuadExpr()](https://www.gurobi.com/documentation/current/refman/py_qex.html): linear expression + list of coefficient-variable-variable triples\n", "- sense:\n", " - GRB.MINIMIZE (default) or GRB.MAXIMIZE" ] @@ -679,7 +678,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7ff2b7cc", + "id": "0774a681", "metadata": { "slideshow": { "slide_type": "fragment" @@ -724,14 +723,14 @@ }, { "cell_type": "markdown", - "id": "eb627d15", + "id": "af44c698", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Attributes](https://www.gurobi.com/documentation/10.0/refman/attributes.html)\n", + "## [Attributes](https://www.gurobi.com/documentation/current/refman/attributes.html)\n", "\n", "The primary mechanism for querying and modifying properties of a Gurobi object is through the attribute interface. You can see the complete set of Gurobi attributes in the reference linked above.\n", "\n", @@ -741,7 +740,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f60e8ffc", + "id": "49c1ecd3", "metadata": { "slideshow": { "slide_type": "fragment" @@ -753,7 +752,7 @@ "import gurobipy as gp\n", "from gurobipy import GRB\n", "\n", - "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/gurobi_days_digital_2023/intro_to_gurobipy/data/glass4.mps.bz2\"\n", + "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/intro_to_gurobipy/data/glass4.mps.bz2\"\n", "path_to_file, _ = urllib.request.urlretrieve(url, \"./glass4.mps.bz2\")\n", "\n", "# running notebook locally:\n", @@ -775,21 +774,21 @@ }, { "cell_type": "markdown", - "id": "05547bf2", + "id": "dca53115", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [Parameters](https://www.gurobi.com/documentation/10.0/refman/parameter_descriptions.html)\n", + "## [Parameters](https://www.gurobi.com/documentation/current/refman/parameter_descriptions.html)\n", "Parameters control the mechanics of the Gurobi Optimizer." ] }, { "cell_type": "code", "execution_count": null, - "id": "36214b31", + "id": "61277a1d", "metadata": { "slideshow": { "slide_type": "fragment" @@ -801,7 +800,7 @@ "import gurobipy as gp\n", "from gurobipy import GRB\n", "\n", - "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/gurobi_days_digital_2023/intro_to_gurobipy/data/glass4.mps.bz2\"\n", + "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/intro_to_gurobipy/data/glass4.mps.bz2\"\n", "path_to_file, _ = urllib.request.urlretrieve(url, \"./glass4.mps.bz2\")\n", "\n", "# running notebook locally:\n", @@ -814,14 +813,14 @@ }, { "cell_type": "markdown", - "id": "6471825d", + "id": "f3c629c6", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ - "## [General Constraints](https://www.gurobi.com/documentation/10.0/refman/constraints.html#subsubsection:GeneralConstraints)\n", + "## [General Constraints](https://www.gurobi.com/documentation/current/refman/constraints.html#subsubsection:GeneralConstraints)\n", "\n", "General constraints allow defining certain variable relationships easily without going to the hardship of modeling these relationships in terms of the more fundamental constraints of MIP. Capturing a single one of these general constraints can often require a large set of constraints and a number of auxiliary decision variables.\n", "\n", @@ -859,7 +858,7 @@ { "cell_type": "code", "execution_count": null, - "id": "170805d1", + "id": "66195906", "metadata": { "slideshow": { "slide_type": "fragment" @@ -913,7 +912,7 @@ }, { "cell_type": "markdown", - "id": "17d42d14", + "id": "11d889db", "metadata": { "slideshow": { "slide_type": "slide" @@ -941,7 +940,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2dfa0562", + "id": "9a94383c", "metadata": { "slideshow": { "slide_type": "fragment" @@ -953,7 +952,7 @@ "from urllib.request import urlopen\n", "import numpy as np\n", "\n", - "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/gurobi_days_digital_2023/intro_to_gurobipy/data/portfolio-example.json\"\n", + "url = \"https://raw.githubusercontent.com/Gurobi/modeling-examples/master/intro_to_gurobipy/data/portfolio-example.json\"\n", "response = urlopen(url)\n", "data = json.loads(response.read())\n", "\n", @@ -967,7 +966,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f0751e84", + "id": "e47a1de3", "metadata": { "slideshow": { "slide_type": "fragment" @@ -1021,7 +1020,7 @@ }, { "cell_type": "markdown", - "id": "286f54d3", + "id": "d132cb20", "metadata": { "slideshow": { "slide_type": "slide" @@ -1033,14 +1032,13 @@ "- Do ensure the separation between your model and data\n", "- Do use descriptive names where appropriate\n", "- Exploit sparsity when creating variables/constraints - Only create variables/constraints for valid combinations\n", - "- Do consider using the Package [gurobipy stubs](https://support.gurobi.com/hc/en-us/articles/4415139954449-How-do-I-enable-type-hinting-in-the-Gurobi-Python-API-) for Type Hinting\n", "- Don’t forget to dispose of your model and environment\n", "- Use the documentation" ] }, { "cell_type": "markdown", - "id": "60cb57da", + "id": "8e29bb28", "metadata": { "slideshow": { "slide_type": "slide" @@ -1055,7 +1053,7 @@ }, { "cell_type": "markdown", - "id": "fb64859f", + "id": "8150f61c", "metadata": { "slideshow": { "slide_type": "slide" @@ -1063,8 +1061,8 @@ }, "source": [ "## Resources\n", - "- [Gurobi Python Documentation](https://www.gurobi.com/documentation/10.0/refman/py_python_api_overview.html)\n", - "- [Gurobi Python Examples](https://www.gurobi.com/documentation/10.0/examples/python_examples.html)\n", + "- [Gurobi Python Documentation](https://www.gurobi.com/documentation/current/refman/py_python_api_overview.html)\n", + "- [Gurobi Python Examples](https://www.gurobi.com/documentation/current/examples/python_examples.html)\n", "- [Gurobi Jupyter Notebook Modeling Examples](https://www.gurobi.com/jupyter_models/)\n", "- [Gurobi Knowledge Base](https://support.gurobi.com/hc/en-us/categories/360000840331-Knowledge-Base)" ]