Solver Peculiarities
The default solver in CVXR
used to be ECOS
. However, it is not always the
best solver to use. As an example, let us consider again the catenary
problem.
We will change the problem slightly to use a finer discretization from 101 points to say 501.
## Problem data
m <- 501
L <- 2
h <- L / (m - 1)
## Form objective
x <- Variable(m)
y <- Variable(m)
objective <- Minimize(sum(y))
## Form constraints
constraints <- list(x[1] == 0, y[1] == 1,
x[m] == 1, y[m] == 1,
diff(x)^2 + diff(y)^2 <= h^2)
## Solve the catenary problem
prob <- Problem(objective, constraints)
result <- solve(prob, solver = "ECOS")
The solution status is no longer optimal.
cat("Solution status is", result$status)
## Solution status is optimal_inaccurate
## Testthat Results: No output is good
In such cases, using a different solver may give more accurate
results. Let us try MOSEK
for example.
result <- solve(prob, solver = "MOSEK")
cat("Solution status is", result$status)
## Solution status is optimal
## Testthat Results: No output is good
This returns an optimal solution.
Here again, even commercial solvers differ; GUROBI, for example, does not completely solve the problem and in fact throws an error.
Session Info
sessionInfo()
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-apple-darwin20
## Running under: macOS Sequoia 15.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## 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 datasets utils methods base
##
## other attached packages:
## [1] CVXR_1.0-15 testthat_3.2.1.1 here_1.0.1
##
## loaded via a namespace (and not attached):
## [1] gmp_0.7-5 utf8_1.2.4 sass_0.4.9 clarabel_0.9.0.1
## [5] slam_0.1-54 blogdown_1.19 lattice_0.22-6 digest_0.6.37
## [9] magrittr_2.0.3 evaluate_1.0.1 grid_4.4.2 bookdown_0.41
## [13] pkgload_1.4.0 fastmap_1.2.0 rprojroot_2.0.4 jsonlite_1.8.9
## [17] Matrix_1.7-1 ECOSolveR_0.5.5 brio_1.1.5 fansi_1.0.6
## [21] Rmosek_10.2.0 codetools_0.2-20 jquerylib_0.1.4 cli_3.6.3
## [25] Rmpfr_0.9-5 rlang_1.1.4 Rglpk_0.6-5.1 bit64_4.5.2
## [29] cachem_1.1.0 yaml_2.3.10 tools_4.4.2 Rcplex_0.3-6
## [33] rcbc_0.1.0.9001 gurobi_11.0-0 assertthat_0.2.1 vctrs_0.6.5
## [37] R6_2.5.1 lifecycle_1.0.4 bit_4.5.0 desc_1.4.3
## [41] cccp_0.3-1 bslib_0.8.0 pillar_1.9.0 glue_1.8.0
## [45] Rcpp_1.0.13-1 xfun_0.49 knitr_1.48 htmltools_0.5.8.1
## [49] rmarkdown_2.29 compiler_4.4.2