Maximizing the Volume of a Box

Author

CVXPY Developers and Balasubramanian Narasimhan

Introduction

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

In this example, we maximize the volume 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 \(d/w\). 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} \]

Problem Formulation

# Problem data
A_wall <- 100
A_flr <- 10
alpha <- 0.5
beta <- 2
gamma <- 0.5
delta <- 2

h <- Variable(pos = TRUE)
w <- Variable(pos = TRUE)
d <- Variable(pos = TRUE)

volume <- h * w * d
wall_area <- 2 * (h * w + h * d)
flr_area <- w * d
hw_ratio <- h / w
dw_ratio <- d / w

constraints <- list(
  wall_area <= A_wall,
  flr_area <= A_flr,
  hw_ratio >= alpha,
  hw_ratio <= beta,
  dw_ratio >= gamma,
  dw_ratio <= delta
)

problem <- Problem(Maximize(volume), constraints)
cat("Is problem DGP?", is_dgp(problem), "\n")
Is problem DGP? TRUE 

Solution

result <- psolve(problem, gp = TRUE)
check_solver_status(problem)
cat("Optimal value (volume):", result, "\n")
cat("h:", value(h), "\n")
cat("w:", value(w), "\n")
cat("d:", value(d), "\n")
Optimal value (volume): 77.45967 
h: 7.745967 
w: 3.872983 
d: 2.581989 

Sensitivity Analysis via Dual Values

The dual values provide sensitivity information. A 1% increase in the allowed wall area should yield approximately a proportional increase in the maximum volume value.

cat("Dual value for wall area constraint:", dual_value(constraints[[1]]), "\n")
cat("Dual value for floor area constraint:", dual_value(constraints[[2]]), "\n")
Dual value for wall area constraint: 0.8333587 
Dual value for floor area constraint: 0.6666413 

The dual value for the wall area constraint is approximately 0.83, meaning a 1% increase in allowed wall space yields approximately a 0.83% increase in the maximum volume. The dual value for the floor area constraint is approximately 0.67, meaning a 1% increase in allowed floor space yields approximately a 0.67% increase in maximum volume.

Session Info

R version 4.6.1 (2026-06-24)
Platform: aarch64-apple-darwin23
Running under: macOS Tahoe 26.6.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] CVXR_1.9.2

loaded via a namespace (and not attached):
 [1] slam_0.1-56       cli_3.6.6         knitr_1.51        ECOSolveR_0.6.1  
 [5] rlang_1.3.0       xfun_0.60         clarabel_0.11.3   otel_0.2.0       
 [9] Rglpk_0.6-5.1     highs_1.14.0-2    cccp_0.3-3        scs_3.2.7        
[13] S7_0.2.2          jsonlite_2.0.0    backports_1.5.1   rprojroot_2.1.1  
[17] htmltools_0.5.9   gmp_0.7-5.1       piqp_0.6.2        rmarkdown_2.31   
[21] grid_4.6.1        evaluate_1.0.5    fastmap_1.2.0     yaml_2.3.12      
[25] compiler_4.6.1    codetools_0.2-20  htmlwidgets_1.6.4 Rcpp_1.1.2       
[29] here_1.0.2        osqp_1.0.0        lattice_0.23-1    digest_0.6.39    
[33] checkmate_2.3.4   Matrix_1.7-6      tools_4.6.1      

References

  • Boyd, S., Kim, S.-J., Vandenberghe, L., Hassibi, A. (2007). A Tutorial on Geometric Programming. Optimization and Engineering, 8(1), 67–127.