Maximizing the volume of a box

This example is adapted from Boyd, Kim, Vandenberghe, and Hassibi, "A Tutorial on Geometric Programming."

In this example, we maximize the shape of a box with height $h$, width $w$, and depth $w$, 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)
maximize h * w * d
subject to 2.0 * (h * w + h * d) <= 100.0
           w * d <= 10.0
           0.5 <= h / w
           h / w <= 2.0
           0.5 <= d / w
           d / w <= 2.0
In [2]:
assert not problem.is_dcp()
assert problem.is_dgp()
problem.solve(gp=True)
problem.value
Out[2]:
77.45966630736292
In [3]:
h.value
Out[3]:
7.7459666715289766
In [4]:
w.value
Out[4]:
3.872983364643079
In [5]:
d.value
Out[5]:
2.581988871583608
In [6]:
# A 1% increase in allowed wall space should yield approximately
# a 0.83% increase in maximum value.
constraints[0].dual_value
Out[6]:
0.8333333206334043
In [7]:
# A 1% increase in allowed wall space should yield approximately
# a 0.66% increase in maximum value.
constraints[1].dual_value
Out[7]:
0.6666666801983365