{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linear program"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A linear program is an optimization problem with a linear objective and affine inequality constraints. A common standard form is the following:\n",
    "$$  \n",
    "    \\begin{array}{ll}\n",
    "    \\mbox{minimize}   & c^Tx \\\\\n",
    "    \\mbox{subject to} & Ax \\leq b.\n",
    "    \\end{array}\n",
    "$$\n",
    "Here $A \\in \\mathcal{R}^{m \\times n}$, $b \\in \\mathcal{R}^m$, and $c \\in \\mathcal{R}^n$ are problem data and $x \\in \\mathcal{R}^{n}$ is the optimization variable. The inequality constraint $Ax \\leq b$ is elementwise.\n",
    "\n",
    "For example, we might have $n$ different products, each constructed out of $m$ components. Each entry $A_{ij}$ is the amount of component $i$ required to build one unit of product $j$. Each entry $b_i$ is the total amount of component $i$ available. We lose $c_j$ for each unit of product $j$ ($c_j < 0$ indicates profit). Our goal then is to choose how many units of each product $j$ to make, $x_j$, in order to minimize loss without exceeding our budget for any component.\n",
    "\n",
    "In addition to a solution $x^\\star$, we obtain a dual solution $\\lambda^\\star$. A positive entry $\\lambda^\\star_i$ indicates that the constraint $a_i^Tx \\leq b_i$ holds with equality for $x^\\star$ and suggests that changing $b_i$ would change the optimal value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following code, we solve a linear program with CVXPY."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The optimal value is -15.220912604467838\n",
      "A solution x is\n",
      "[-1.10131657 -0.16370661 -0.89711643  0.03228613  0.60662428 -1.12655967\n",
      "  1.12985839  0.88200333  0.49089264  0.89851057]\n",
      "A dual solution is\n",
      "[0.         0.61175641 0.52817175 1.07296862 0.         2.3015387\n",
      " 0.         0.7612069  0.         0.24937038 0.         2.06014071\n",
      " 0.3224172  0.38405435 0.        ]\n"
     ]
    }
   ],
   "source": [
    "# Import packages.\n",
    "import cvxpy as cp\n",
    "import numpy as np\n",
    "\n",
    "# Generate a random non-trivial linear program.\n",
    "m = 15\n",
    "n = 10\n",
    "np.random.seed(1)\n",
    "s0 = np.random.randn(m)\n",
    "lamb0 = np.maximum(-s0, 0)\n",
    "s0 = np.maximum(s0, 0)\n",
    "x0 = np.random.randn(n)\n",
    "A = np.random.randn(m, n)\n",
    "b = A@x0 + s0\n",
    "c = -A.T@lamb0\n",
    "\n",
    "# Define and solve the CVXPY problem.\n",
    "x = cp.Variable(n)\n",
    "prob = cp.Problem(cp.Minimize(c.T@x),\n",
    "                 [A@x <= b])\n",
    "prob.solve()\n",
    "\n",
    "# Print result.\n",
    "print(\"\\nThe optimal value is\", prob.value)\n",
    "print(\"A solution x is\")\n",
    "print(x.value)\n",
    "print(\"A dual solution is\")\n",
    "print(prob.constraints[0].dual_value)"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
