{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Maximizing the volume of a box\n",
    "\n",
    "*This example is adapted from Boyd, Kim, Vandenberghe, and Hassibi,* \"[A Tutorial on Geometric Programming](https://web.stanford.edu/~boyd/papers/pdf/gp_tutorial.pdf).\"\n",
    "\n",
    "In this example, we maximize the shape of a box with height $h$, width $w$, and depth $d$, with limits on the wall area $2(hw + hd)$\n",
    "and the floor area $wd$, subject to bounds on the aspect ratios $h/w$ and $w/d$. The optimization problem is\n",
    "\n",
    "$$\n",
    "\\begin{array}{ll}\n",
    "\\mbox{maximize} & hwd \\\\\n",
    "\\mbox{subject to} & 2(hw + hd) \\leq A_{\\text wall}, \\\\\n",
    "& wd \\leq A_{\\text flr}, \\\\\n",
    "& \\alpha \\leq h/w \\leq \\beta, \\\\\n",
    "& \\gamma \\leq d/w \\leq \\delta.\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "maximize h * w * d\n",
      "subject to 2.0 * (h * w + h * d) <= 100.0\n",
      "           w * d <= 10.0\n",
      "           0.5 <= h / w\n",
      "           h / w <= 2.0\n",
      "           0.5 <= d / w\n",
      "           d / w <= 2.0\n"
     ]
    }
   ],
   "source": [
    "import cvxpy as cp\n",
    "\n",
    "# Problem data.\n",
    "A_wall = 100\n",
    "A_flr = 10\n",
    "alpha = 0.5\n",
    "beta = 2\n",
    "gamma = 0.5\n",
    "delta = 2\n",
    "\n",
    "h = cp.Variable(pos=True, name=\"h\")\n",
    "w = cp.Variable(pos=True, name=\"w\")\n",
    "d = cp.Variable(pos=True, name=\"d\")\n",
    "\n",
    "volume = h * w * d\n",
    "wall_area = 2 * (h * w + h * d)\n",
    "flr_area = w * d\n",
    "hw_ratio = h/w\n",
    "dw_ratio = d/w\n",
    "constraints = [\n",
    "    wall_area <= A_wall,\n",
    "    flr_area <= A_flr,\n",
    "    hw_ratio >= alpha,\n",
    "    hw_ratio <= beta,\n",
    "    dw_ratio >= gamma,\n",
    "    dw_ratio <= delta\n",
    "]\n",
    "problem = cp.Problem(cp.Maximize(volume), constraints)\n",
    "print(problem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "77.45966630736292"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert not problem.is_dcp()\n",
    "assert problem.is_dgp()\n",
    "problem.solve(gp=True)\n",
    "problem.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.7459666715289766"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.872983364643079"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.581988871583608"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8333333206334043"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A 1% increase in allowed wall space should yield approximately\n",
    "# a 0.83% increase in maximum value.\n",
    "constraints[0].dual_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6666666801983365"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A 1% increase in allowed wall space should yield approximately\n",
    "# a 0.66% increase in maximum value.\n",
    "constraints[1].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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
