{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "GxSxNDFYuq6n", "pycharm": {} }, "source": [ "# Running Pyomo on Google Colab\n", "\n", "Keywords: installation" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pJHU-Rpkglqq", "pycharm": {} }, "source": [ "This note notebook shows how to install the basic pyomo package on Google Colab, and then demonstrates the subsequent installation and use of various solvers including\n", "\n", "* GLPK\n", "* COIN-OR CBC\n", "* COIN-OR Ipopt\n", "* COIN-OR Bonmin\n", "* COIN-OR Couenne\n", "* COIN-OR Gecode" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jJrxAaqcuzFw", "pycharm": {} }, "source": [ "## Basic installation of Pyomo\n", "\n", "We'll do a quiet installation of pyomo using `pip`. This needs to be done once at the start of each Colab session." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "colab_type": "code", "executionInfo": { "elapsed": 7917, "status": "ok", "timestamp": 1555692692656, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "yTGBrqQO3vT2", "outputId": "7350a7c8-06c0-4e2f-d47b-6b7304da142b", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K 100% |████████████████████████████████| 2.1MB 12.6MB/s \n", "\u001b[K 100% |████████████████████████████████| 51kB 19.4MB/s \n", "\u001b[K 100% |████████████████████████████████| 256kB 28.6MB/s \n", "\u001b[?25h" ] } ], "source": [ "!pip install -q pyomo" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pa5VjPfw7Xfm", "pycharm": {} }, "source": [ "The installation of pyomo can be verified by entering a simple model. We'll use the model again in subsequent cells to demonstrate the installation and execution of various solvers." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 442 }, "colab_type": "code", "executionInfo": { "elapsed": 2600, "status": "ok", "timestamp": 1555692697285, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "LkpGUtiS60lD", "outputId": "08752c69-b55d-47e0-9745-64eb07dd3d1f", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 Var Declarations\n", " x : Size=1, Index=None\n", " Key : Lower : Value : Upper : Fixed : Stale : Domain\n", " None : 0 : None : None : False : True : NonNegativeReals\n", " y : Size=1, Index=None\n", " Key : Lower : Value : Upper : Fixed : Stale : Domain\n", " None : 0 : None : None : False : True : NonNegativeReals\n", "\n", "1 Objective Declarations\n", " profit : Size=1, Index=None, Active=True\n", " Key : Active : Sense : Expression\n", " None : True : maximize : 40*x + 30*y\n", "\n", "3 Constraint Declarations\n", " demand : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : x : 40.0 : True\n", " laborA : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : x + y : 80.0 : True\n", " laborB : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : 2*x + y : 100.0 : True\n", "\n", "6 Declarations: x y profit demand laborA laborB\n" ] } ], "source": [ "from pyomo.environ import *\n", "\n", "# create a model\n", "model = ConcreteModel()\n", "\n", "# declare decision variables\n", "model.x = Var(domain=NonNegativeReals)\n", "model.y = Var(domain=NonNegativeReals)\n", "\n", "# declare objective\n", "model.profit = Objective(expr = 40*model.x + 30*model.y, sense=maximize)\n", "\n", "# declare constraints\n", "model.demand = Constraint(expr = model.x <= 40)\n", "model.laborA = Constraint(expr = model.x + model.y <= 80)\n", "model.laborB = Constraint(expr = 2*model.x + model.y <= 100)\n", "\n", "model.pprint()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "dGbhGS4u7vyV", "pycharm": {} }, "source": [ "## GLPK installation\n", "\n", "Keywords: GLPK\n", "\n", "[GLPK](https://en.wikibooks.org/wiki/GLPK) is a the open-source **G**NU **L**inear **P**rogramming **K**it available for use under the GNU General Public License 3. GLPK is a single-threaded simplex solver generally suited to small to medium scale linear-integer programming problems. It is written in C with minimal dependencies and is therefore highly portable among computers and operating systems. GLPK is often 'good enough' for many examples. For larger problems users should consider higher-performance solvers, such as COIN-OR CBC, that can take advantage of multi-threaded processors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "aXNQCk6ywINU", "pycharm": {} }, "outputs": [], "source": [ "!apt-get install -y -qq glpk-utils" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 765 }, "colab_type": "code", "executionInfo": { "elapsed": 418, "status": "ok", "timestamp": 1555692730263, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "HsTASLpi8sdg", "outputId": "aeb070c9-e206-4f87-89c9-5554618d9ad5", "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: 2600.0\n", " Upper bound: 2600.0\n", " Number of objectives: 1\n", " Number of constraints: 4\n", " Number of variables: 3\n", " Number of nonzeros: 6\n", " Sense: maximize\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.020076274871826172\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.0\n", "\n", "Decision Variables\n", "x = 20.0\n", "y = 60.0\n", "\n", "Constraints\n", "Demand = 20.0\n", "Labor A = 80.0\n", "Labor B = 100.0\n" ] } ], "source": [ "SolverFactory('glpk', executable='/usr/bin/glpsol').solve(model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', model.x())\n", "print('y = ', model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', model.demand())\n", "print('Labor A = ', model.laborA())\n", "print('Labor B = ', model.laborB())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4bV-uLVe8yu0", "pycharm": {} }, "source": [ "## COIN-OR CBC installation\n", "\n", "Keywords: cbc installation\n", "\n", "[COIN-OR CBC](https://github.com/coin-or/Cbc) is a multi-threaded open-source **C**oin-or **b**ranch and **c**ut mixed-integer linear programming solver written in C++ under the Eclipse Public License (EPL). CBC is generally a good choice for a general purpose MILP solver for medium to large scale problems." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "gFF1UnKYwPOe", "pycharm": {} }, "outputs": [], "source": [ "!apt-get install -y -qq coinor-cbc" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 714 }, "colab_type": "code", "executionInfo": { "elapsed": 6909, "status": "ok", "timestamp": 1555692739042, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "SgtBao9G9K2R", "outputId": "d880e879-1939-49ca-b985-92a963a5999a", "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: -2600.0\n", " Upper bound: -2600.0\n", " Number of objectives: 1\n", " Number of constraints: 4\n", " Number of variables: 3\n", " Number of nonzeros: 6\n", " Sense: minimize\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " User time: -1.0\n", " Termination condition: optimal\n", " Error rc: 0\n", " Time: 0.019547700881958008\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.0\n", "\n", "Decision Variables\n", "x = 20.0\n", "y = 60.0\n", "\n", "Constraints\n", "Demand = 20.0\n", "Labor A = 80.0\n", "Labor B = 100.0\n" ] } ], "source": [ "SolverFactory('cbc', executable='/usr/bin/cbc').solve(model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', model.x())\n", "print('y = ', model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', model.demand())\n", "print('Labor A = ', model.laborA())\n", "print('Labor B = ', model.laborB())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "OdcuIo9yvA7E", "pycharm": {} }, "source": [ "## COIN-OR Ipopt installation\n", "\n", "Keywords: Ipopt installation\n", "\n", "[COIN-OR Ipopt](https://github.com/coin-or/Ipopt) is an open-source **I**nterior **P**oint **Opt**imizer for large-scale nonlinear optimization available under the Eclipse Public License (EPL). It is well-suited to solving nonlinear programming problems without integer or binary constraints." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "dMyKsixjvCAj", "pycharm": {} }, "outputs": [], "source": [ "!wget -N -q \"https://ampl.com/dl/open/ipopt/ipopt-linux64.zip\"\n", "!unzip -o -q ipopt-linux64" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 697 }, "colab_type": "code", "executionInfo": { "elapsed": 2236, "status": "ok", "timestamp": 1555692743494, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "LvFczDKg96aX", "outputId": "3c55e96a-779a-45c3-fb83-328caa085fd9", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# ==========================================================\n", "# = Solver Results =\n", "# ==========================================================\n", "# ----------------------------------------------------------\n", "# Problem Information\n", "# ----------------------------------------------------------\n", "Problem: \n", "- Lower bound: -inf\n", " Upper bound: inf\n", " Number of objectives: 1\n", " Number of constraints: 3\n", " Number of variables: 2\n", " Sense: unknown\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " Message: Ipopt 3.12.8\\x3a Optimal Solution Found\n", " Termination condition: optimal\n", " Id: 0\n", " Error rc: 0\n", " Time: 0.022800445556640625\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.000025994988\n", "\n", "Decision Variables\n", "x = 20.0000001998747\n", "y = 60.0000006\n", "\n", "Constraints\n", "Demand = 20.0000001998747\n", "Labor A = 80.0000007998747\n", "Labor B = 100.0000009997494\n" ] } ], "source": [ "SolverFactory('ipopt', executable='/content/ipopt').solve(model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', model.x())\n", "print('y = ', model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', model.demand())\n", "print('Labor A = ', model.laborA())\n", "print('Labor B = ', model.laborB())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "njG-SkkatYpP", "pycharm": {} }, "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Q1ru4X2kuxdl", "pycharm": {} }, "source": [ "## COIN-OR Bonmin installation\n", "\n", "[COIN-OR Bonmin](https://www.coin-or.org/Bonmin/Intro.html) is a **b**asic **o**pen-source solver for **n**onlinear **m**ixed-**in**teger programming problems (MINLP). It utilizes CBC and Ipopt for solving relaxed subproblems." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "PVB4C4rZGd_o", "pycharm": {} }, "outputs": [], "source": [ "!wget -N -q \"https://ampl.com/dl/open/bonmin/bonmin-linux64.zip\"\n", "!unzip -o -q bonmin-linux64" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 697 }, "colab_type": "code", "executionInfo": { "elapsed": 2349, "status": "ok", "timestamp": 1555692747800, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "BO_KJVUs-bwS", "outputId": "6b1b15c1-d8fa-4d01-e8f9-216ffeba5573", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# ==========================================================\n", "# = Solver Results =\n", "# ==========================================================\n", "# ----------------------------------------------------------\n", "# Problem Information\n", "# ----------------------------------------------------------\n", "Problem: \n", "- Lower bound: -inf\n", " Upper bound: inf\n", " Number of objectives: 1\n", " Number of constraints: 0\n", " Number of variables: 2\n", " Sense: unknown\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " Message: bonmin\\x3a Optimal\n", " Termination condition: optimal\n", " Id: 3\n", " Error rc: 0\n", " Time: 0.025995254516601562\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.0000259999797\n", "\n", "Decision Variables\n", "x = 20.000000199999512\n", "y = 60.00000059999998\n", "\n", "Constraints\n", "Demand = 20.000000199999512\n", "Labor A = 80.0000007999995\n", "Labor B = 100.000000999999\n" ] } ], "source": [ "SolverFactory('bonmin', executable='/content/bonmin').solve(model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', model.x())\n", "print('y = ', model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', model.demand())\n", "print('Labor A = ', model.laborA())\n", "print('Labor B = ', model.laborB())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TWi6TZIN_RFh", "pycharm": {} }, "source": [ "## COIN-OR Couenne installation\n", "\n", "[COIN-OR Couenne](https://www.coin-or.org/Couenne/) is attempts to find global optima for mixed-integer nonlinear programming problems (MINLP)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "LDhJE0ba_RFi", "pycharm": {} }, "outputs": [], "source": [ "!wget -N -q \"https://ampl.com/dl/open/couenne/couenne-linux64.zip\"\n", "!unzip -o -q couenne-linux64" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 697 }, "colab_type": "code", "executionInfo": { "elapsed": 401, "status": "ok", "timestamp": 1555692755988, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "rysHICYQ_RFl", "outputId": "d338b840-1389-4c54-e85e-8190a4869a14", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# ==========================================================\n", "# = Solver Results =\n", "# ==========================================================\n", "# ----------------------------------------------------------\n", "# Problem Information\n", "# ----------------------------------------------------------\n", "Problem: \n", "- Lower bound: -inf\n", " Upper bound: inf\n", " Number of objectives: 1\n", " Number of constraints: 0\n", " Number of variables: 2\n", " Sense: unknown\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " Message: couenne\\x3a Optimal\n", " Termination condition: optimal\n", " Id: 3\n", " Error rc: 0\n", " Time: 0.023235797882080078\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.00002599998\n", "\n", "Decision Variables\n", "x = 20.000000199999512\n", "y = 60.00000059999999\n", "\n", "Constraints\n", "Demand = 20.000000199999512\n", "Labor A = 80.0000007999995\n", "Labor B = 100.00000099999902\n" ] } ], "source": [ "SolverFactory('couenne', executable='/content/couenne').solve(model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', model.x())\n", "print('y = ', model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', model.demand())\n", "print('Labor A = ', model.laborA())\n", "print('Labor B = ', model.laborB())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rJTfzez9AtRg", "pycharm": {} }, "source": [ "## Gecode installation\n", "\n", "Keywords: Gecode installation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "NcVDCv1qAtRi", "pycharm": {} }, "outputs": [], "source": [ "!wget -N -q \"https://ampl.com/dl/open/gecode/gecode-linux64.zip\"\n", "!unzip -o -q gecode-linux64" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "GBVLdNHKBYdI", "pycharm": {} }, "source": [ "Gecode solves constraint programming problems and does not support continuous variables. We therefore create a second model using exclusively discrete variables." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 442 }, "colab_type": "code", "executionInfo": { "elapsed": 241, "status": "ok", "timestamp": 1555692763080, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "l0XB0yXVA-Lq", "outputId": "670cd493-6cff-4db2-f93e-0b824e1e6ea7", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 Var Declarations\n", " x : Size=1, Index=None\n", " Key : Lower : Value : Upper : Fixed : Stale : Domain\n", " None : 0 : None : None : False : True : NonNegativeIntegers\n", " y : Size=1, Index=None\n", " Key : Lower : Value : Upper : Fixed : Stale : Domain\n", " None : 0 : None : None : False : True : NonNegativeIntegers\n", "\n", "1 Objective Declarations\n", " profit : Size=1, Index=None, Active=True\n", " Key : Active : Sense : Expression\n", " None : True : maximize : 40*x + 30*y\n", "\n", "3 Constraint Declarations\n", " demand : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : x : 40.0 : True\n", " laborA : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : x + y : 80.0 : True\n", " laborB : Size=1, Index=None, Active=True\n", " Key : Lower : Body : Upper : Active\n", " None : -Inf : 2*x + y : 100.0 : True\n", "\n", "6 Declarations: x y profit demand laborA laborB\n" ] } ], "source": [ "from pyomo.environ import *\n", "\n", "# create a model\n", "discrete_model = ConcreteModel()\n", "\n", "# declare decision variables\n", "discrete_model.x = Var(domain=NonNegativeIntegers)\n", "discrete_model.y = Var(domain=NonNegativeIntegers)\n", "\n", "# declare objective\n", "discrete_model.profit = Objective(expr = 40*discrete_model.x + 30*discrete_model.y, sense=maximize)\n", "\n", "# declare constraints\n", "discrete_model.demand = Constraint(expr = discrete_model.x <= 40)\n", "discrete_model.laborA = Constraint(expr = discrete_model.x + discrete_model.y <= 80)\n", "discrete_model.laborB = Constraint(expr = 2*discrete_model.x + discrete_model.y <= 100)\n", "\n", "discrete_model.pprint()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 697 }, "colab_type": "code", "executionInfo": { "elapsed": 368, "status": "ok", "timestamp": 1555692765518, "user": { "displayName": "Jeffrey Kantor", "photoUrl": "https://lh5.googleusercontent.com/-8zK5aAW5RMQ/AAAAAAAAAAI/AAAAAAAAKB0/kssUQyz8DTQ/s64/photo.jpg", "userId": "09038942003589296665" }, "user_tz": 240 }, "id": "7fu6Nb6lAtRm", "outputId": "fa07c8ab-0153-4e8c-8a78-72a392c0a10f", "pycharm": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# ==========================================================\n", "# = Solver Results =\n", "# ==========================================================\n", "# ----------------------------------------------------------\n", "# Problem Information\n", "# ----------------------------------------------------------\n", "Problem: \n", "- Lower bound: -inf\n", " Upper bound: inf\n", " Number of objectives: 1\n", " Number of constraints: 0\n", " Number of variables: 2\n", " Sense: unknown\n", "# ----------------------------------------------------------\n", "# Solver Information\n", "# ----------------------------------------------------------\n", "Solver: \n", "- Status: ok\n", " Message: gecode 4.4.0\\x3a optimal solution; 201 nodes, 0 fails, objective 2600\n", " Termination condition: optimal\n", " Id: 0\n", " Error rc: 0\n", " Time: 0.019869565963745117\n", "# ----------------------------------------------------------\n", "# Solution Information\n", "# ----------------------------------------------------------\n", "Solution: \n", "- number of solutions: 0\n", " number of solutions displayed: 0\n", "\n", "Profit = 2600.0\n", "\n", "Decision Variables\n", "x = 20.0\n", "y = 60.0\n", "\n", "Constraints\n", "Demand = 20.0\n", "Labor A = 80.0\n", "Labor B = 100.0\n" ] } ], "source": [ "SolverFactory('gecode', executable='/content/gecode').solve(discrete_model).write()\n", "\n", "# display solution\n", "print('\\nProfit = ', discrete_model.profit())\n", "\n", "print('\\nDecision Variables')\n", "print('x = ', discrete_model.x())\n", "print('y = ', discrete_model.y())\n", "\n", "print('\\nConstraints')\n", "print('Demand = ', discrete_model.demand())\n", "print('Labor A = ', discrete_model.laborA())\n", "print('Labor B = ', discrete_model.laborB())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "uQLFeAJ9D9wn", "pycharm": {} }, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "01.01-Getting-Started-with-Pyomo-on-Google-Colab.ipynb", "provenance": [], "toc_visible": true, "version": "0.3.2" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }