{ "cells": [ { "cell_type": "markdown", "id": "236fc575-3f76-4906-b795-7ff47cdcb2b8", "metadata": {}, "source": [ "```{index} dual problem\n", "```\n", "```{index} single: solver; cbc\n", "```\n", "```{index} single: solver; highs\n", "```\n", "```{index} single: application; production planning\n", "```\n", "\n", "# BIM production" ] }, { "cell_type": "code", "execution_count": null, "id": "88547152-b76c-4a6f-9b6d-2e43ace3a637", "metadata": {}, "outputs": [], "source": [ "# install dependencies and select solver\n", "%pip install -q amplpy matplotlib pandas\n", "\n", "SOLVER = \"highs\"\n", "\n", "from amplpy import AMPL, ampl_notebook\n", "\n", "ampl = ampl_notebook(\n", " modules=[\"highs\"], # modules to install\n", " license_uuid=\"default\", # license to use\n", ") # instantiate AMPL object and register magics" ] }, { "cell_type": "markdown", "id": "69b647c3-f706-4591-8287-f10921ff6efa", "metadata": {}, "source": [ "## General LO formulation\n", "\n", "The simplest and most scalable class of optimization problems is the one where the objective function and the constraints are formulated using the simplest possible type of functions - linear functions. A **linear optimization (LO)** is a problem of the form\n", "\n", "$$\n", "\\begin{align*}\n", " \\min \\quad & c^\\top x\\\\\n", " \\text{s.t.} \\quad & A x \\leq b\\\\\n", " & x \\geq 0, \\nonumber \n", "\\end{align*}\n", "$$\n", "\n", "where the $n$ (decision) variables are grouped in a vector $x \\in \\mathbb{R}^n$, $c \\in \\mathbb{R}^n$ are the objective coefficients, and the $m$ linear constraints are described by the matrix $A \\in \\mathbb{R}^{m \\times n}$ and the vector $b \\in \\mathbb{R}^m$. \n", "\n", "Of course, linear problems could also (i) be maximization problems, (ii) involve equality constraints and constraints of the form $\\geq$, and (iii) have unbounded or non-positive decision variables $x_i$'s. In fact, any LP problem with such features can be easily converted to the 'canonical' LP form by adding/removing variables and/or multiplying specific inequalities by $-1$." ] }, { "cell_type": "markdown", "id": "20f541ba-0e04-4ebb-a712-2ccf3af9d293", "metadata": {}, "source": [ "## The microchip production problem\n", "The company BIM (Best International Machines) produces two types of microchips, logic chips (1 g silicon, 1 g plastic, 4 g copper) and memory chips (1 g germanium, 1 g plastic, 2 g copper). Each of the logic chips can be sold for a 12 € profit, and each of the memory chips for a 9 € profit. The current stock of raw materials is as follows: 1000 g silicon, 1500 g germanium, 1750 g plastic, 4800 g of copper. How many microchips of each type should be produced to maximize the profit while respecting the raw material stock availability? " ] }, { "cell_type": "markdown", "id": "a312986d-2b2c-49c6-b2b9-6ad62b80ac2b", "metadata": {}, "source": [ "Let $x_1$ denote the number of logic chips and $x_2$ that of memory chips. This decision can be reformulated as an optimization problem of the following form:\n", "\n", "$$\n", "\\begin{align}\n", "\\max \\quad & 12 x_1 + 9 x_2 \\\\\n", "\\text{s.t.} \\quad\n", " & x_1 \\leq 1000 &\\text{silicon}\\\\\n", " & x_2 \\leq 1500 &\\text{germanium}\\\\\n", " & x_1 + x_2 \\leq 1750 &\\text{plastic}\\\\\n", " & 4 x_1 + 2 x_2 \\leq 4800 &\\text{copper}\\\\\n", " & x_1, x_2 \\geq 0 \n", "\\end{align}\n", "$$" ] }, { "cell_type": "markdown", "id": "cb8529f5-8a0f-4192-a056-55a6e7db57c1", "metadata": {}, "source": [ "The problem has $n=2$ decision variables and $m=4$ constraints. Using the standard notation introduced above, denote the vector of decision variables by $x = \\begin{pmatrix} x_1 \\\\ x_2 \\end{pmatrix}$ and define the problem coefficients as\n", "\n", "$$\n", "\\begin{align*}\n", " c = \\begin{pmatrix} 12 \\\\ 9 \\end{pmatrix},\n", " \\qquad\n", " A = \n", " \\begin{bmatrix}\n", " 1 & 0\\\\\n", " 0 & 1\\\\\n", " 1 & 1\\\\\n", " 4 & 2\\\\\n", " \\end{bmatrix},\n", " \\quad \\text{ and } \\quad\n", " b = \\begin{pmatrix} 1000 \\\\ 1500 \\\\ 1750 \\\\ 4800 \\end{pmatrix}.\n", "\\end{align*}\n", "$$\n", "\n", "This model can be implemented and solved in AMPL as follows." ] }, { "cell_type": "code", "execution_count": 2, "id": "eba415d4", "metadata": {}, "outputs": [], "source": [ "%%ampl_eval\n", "\n", "var x1 >= 0;\n", "var x2 >= 0;\n", "\n", "maximize profit: 12 * x1 + 9 * x2;\n", "\n", "s.t. silicon: x1 <= 1000;\n", "s.t. germanium: x2 <= 1500;\n", "s.t. plastic: x1 + x2 <= 1750;\n", "s.t. copper: 4 * x1 + 2 * x2 <= 4800;" ] }, { "cell_type": "code", "execution_count": 3, "id": "a0c47721-754d-421c-93d1-a5539852ff41", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HiGHS 1.5.1: HiGHS 1.5.1: optimal solution; objective 17700\n", "2 simplex iterations\n", "0 barrier iterations\n", "x = (650.0, 1100.0)\n", "optimal value = 17700.00\n" ] } ], "source": [ "ampl.option[\"solver\"] = SOLVER\n", "ampl.solve()\n", "\n", "print(f'x = ({ampl.var[\"x1\"].value():.1f}, {ampl.var[\"x2\"].value():.1f})')\n", "print(f'optimal value = {ampl.obj[\"profit\"].value():.2f}')" ] }, { "cell_type": "markdown", "id": "c86c5987-096d-4268-9a11-e24eeff28aa4", "metadata": {}, "source": [ "## Dual problem\n", "\n", "One can construct bounds for the value of objective function by multiplying the constraints by non-negative numbers and adding them to each other so that the left-hand side looks like the objective function, while the right-hand side is the corresponding bound.\n", "\n", "Let $\\lambda_1,\\lambda_2,\\lambda_3,\\lambda_4$ be non-negative numbers. If we multiply each of these variables by one of the four constraints of the original problem and sum all of them side by side to obtain the inequality\n", "\n", "$$\n", "\\begin{align*}\n", " (\\lambda_1+\\lambda_3+4\\lambda_4) x_1 + (\\lambda_2+\\lambda_3+2 \\lambda_4) x_2 \\leq 1000 \\lambda_1 + 1500 \\lambda_2 + 1750 \\lambda_3 + 4800 \\lambda_4.\n", "\\end{align*}\n", "$$\n", "\n", "It is clear that if $\\lambda_1,\\lambda_2,\\lambda_3,\\lambda_4 \\geq 0$ satisfy\n", "\n", "$$\n", "\\begin{align*}\n", "\\lambda_1+\\lambda_3+4\\lambda_4 & \\geq 12,\\\\\n", "\\lambda_2+\\lambda_3+2 \\lambda_4 & \\geq 9,\n", "\\end{align*}\n", "$$\n", "\n", "then we have the following:\n", "\n", "$$\n", "\\begin{align*}\n", "12 x_1 + 9 x_2 \\leq (\\lambda_1+\\lambda_3+4\\lambda_4) x_1 + (\\lambda_2+\\lambda_3+2 \\lambda_4) x_2 \\leq 1000 \\lambda_1 + 1500 \\lambda_2 + 1750 \\lambda_3 + 4800 \\lambda_4,\n", "\\end{align*}\n", "$$\n", "\n", "where the first inequality follows from the fact that $x_1, x_2 \\geq 0$, and the most right-hand expression becomes an upper bound on the optimal value of the objective.\n", "\n", "If we seek $\\lambda_1,\\lambda_2,\\lambda_3,\\lambda_4 \\geq 0$ such that the upper bound on the RHS is as tight as possible, that means that we need to **minimize** the expression $1000 \\lambda_1 + 1500 \\lambda_2 + 1750 \\lambda_3 + 4800 \\lambda_4$. This can be formulated as the following LP, which we name the **dual problem**:\n", "\n", "$$\n", "\\begin{align*}\n", " \\min \\quad & 1000 \\lambda_1 + 1500 \\lambda_2 + 1750 \\lambda_3 + 4800 \\lambda_4 \\\\\n", " \\text{s.t.} \\quad & \\lambda_1+\\lambda_3+4\\lambda_4 \\geq 12,\\\\\n", " & \\lambda_2+\\lambda_3+2 \\lambda_4 \\geq 9,\\\\\n", " & \\lambda_1,\\lambda_2,\\lambda_3,\\lambda_4 \\geq 0.\n", "\\end{align*}\n", "$$\n", "\n", "It is easy to solve and find the optimal solution $(\\lambda_1,\\lambda_2,\\lambda_3,\\lambda_4)=(0,0,6,1.5)$, for which the objective functions takes the value $17700$. Such a value is (the tightest) upper bound for the original problem. Here, we present the AMPL code for this example." ] }, { "cell_type": "code", "execution_count": 4, "id": "ef910195", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing bim_dual.mod\n" ] } ], "source": [ "%%writefile bim_dual.mod\n", "\n", "var y1 >= 0;\n", "var y2 >= 0;\n", "var y3 >= 0;\n", "var y4 >= 0;\n", "\n", "minimize obj: 1000 * y1 + 1500 * y2 + 1750 * y3 + 4800 * y4;\n", "\n", "s.t. x1: y1 + y3 + 4 * y4 >= 12;\n", "s.t. x2: y2 + y3 + 2 * y4 >= 9;" ] }, { "cell_type": "code", "execution_count": 5, "id": "4c09c77c-8ace-43d1-b94d-dc6a11694f9b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HiGHS 1.5.1: HiGHS 1.5.1: optimal solution; objective 17700\n", "3 simplex iterations\n", "0 barrier iterations\n", "y = (0.0, 0.0, 6.0, 1.5)\n", "optimal value = 17700.00\n" ] } ], "source": [ "ampl = AMPL()\n", "ampl.read(\"bim_dual.mod\")\n", "ampl.option[\"solver\"] = SOLVER\n", "ampl.solve()\n", "\n", "print(\n", " f'y = ({ampl.var[\"y1\"].value():.1f}, {ampl.var[\"y2\"].value():.1f}, {ampl.var[\"y3\"].value():.1f}, {ampl.var[\"y4\"].value():.1f})'\n", ")\n", "print(f'optimal value = {ampl.obj[\"obj\"].value():.2f}')" ] }, { "cell_type": "markdown", "id": "da40407c-164d-4b47-a8af-5f610823b307", "metadata": {}, "source": [ "Note that since the original LP is feasible and bounded, strong duality holds and the optimal value of the primal problem coincides with the optimal value of the dual problem." ] } ], "metadata": { "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }