# 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{split}\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}\end{split}$
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

assert not problem.is_dcp()
assert problem.is_dgp()
problem.solve(gp=True)
problem.value

77.45966630736292

h.value

7.7459666715289766

w.value

3.872983364643079

d.value

2.581988871583608

# A 1% increase in allowed wall space should yield approximately
# a 0.83% increase in maximum value.
constraints[0].dual_value

0.8333333206334043

# A 1% increase in allowed wall space should yield approximately
# a 0.66% increase in maximum value.
constraints[1].dual_value

0.6666666801983365