#!/usr/bin/env python # coding: utf-8 # # Maximizing the volume of a box # # *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)." # # 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)$ # and the floor area $wd$, subject to bounds on the aspect ratios $h/w$ and $w/d$. The optimization problem is # # $$ # \begin{array}{ll} # \mbox{maximize} & hwd \\ # \mbox{subject to} & 2(hw + hd) \leq A_{\text wall}, \\ # & wd \leq A_{\text flr}, \\ # & \alpha \leq h/w \leq \beta, \\ # & \gamma \leq d/w \leq \delta. # \end{array} # $$ # In[1]: import cvxpy as cp # Problem data. A_wall = 100 A_flr = 10 alpha = 0.5 beta = 2 gamma = 0.5 delta = 2 h = cp.Variable(pos=True, name="h") w = cp.Variable(pos=True, name="w") d = cp.Variable(pos=True, name="d") volume = h * w * d wall_area = 2 * (h * w + h * d) flr_area = w * d hw_ratio = h/w dw_ratio = d/w constraints = [ wall_area <= A_wall, flr_area <= A_flr, hw_ratio >= alpha, hw_ratio <= beta, dw_ratio >= gamma, dw_ratio <= delta ] problem = cp.Problem(cp.Maximize(volume), constraints) print(problem) # In[2]: assert not problem.is_dcp() assert problem.is_dgp() problem.solve(gp=True) problem.value # In[3]: h.value # In[4]: w.value # In[5]: d.value # In[6]: # A 1% increase in allowed wall space should yield approximately # a 0.83% increase in maximum value. constraints[0].dual_value # In[7]: # A 1% increase in allowed wall space should yield approximately # a 0.66% increase in maximum value. constraints[1].dual_value