## Problem data
set.seed(1)
m <- 200
n <- 100
A <- matrix(rnorm(m*n), nrow = m, ncol = n)
b <- rnorm(m)
## Define the problem once using a Parameter
x <- Variable(n)
gamma <- Parameter(nonneg = TRUE)
obj <- Minimize(.5 * sum_squares(A %*% x - b) + gamma * norm1(x))
constraint <- list(x >= 0)
prob <- Problem(obj, constraint)
## Sweep over gamma values
gamma_vals <- c(0.1, 0.5, 1, 2, 5, 10)
timings <- numeric(length(gamma_vals))
opt_vals <- numeric(length(gamma_vals))
for (i in seq_along(gamma_vals)) {
value(gamma) <- gamma_vals[i]
timings[i] <- system.time(opt_vals[i] <- psolve(prob, solver = "OSQP"))["elapsed"]
check_solver_status(prob)
}
results <- data.frame(
gamma = gamma_vals,
optimal_value = round(opt_vals, 4),
elapsed = round(timings, 3)
)Warm Starts
Version 1.0 features
Starting with version 0.11.1, CVXR supports Parameter objects that allow you to change problem data and re-solve without reconstructing the problem from scratch. This uses the EvalParams reduction under the hood to substitute parameter values before each solve.
Lasso Example
We demonstrate the parametric workflow in a simple lasso problem:
\[ \begin{array}{ll} \underset{x}{\mbox{minimize}} & \frac{1}{2}\|Ax - b\|_2^2 + \gamma \|x\|_1\\ \mbox{subject to} & x \geq 0 \end{array} \]
where \(\gamma > 0\) is a regularization parameter.
The first solve includes one-time compilation overhead; subsequent re-solves skip this step entirely:
knitr::kable(results, col.names = c("$\\gamma$", "Optimal Value", "Elapsed (s)"))| \(\gamma\) | Optimal Value | Elapsed (s) |
|---|---|---|
| 0.1 | 69.0649 | 0.343 |
| 0.5 | 70.3046 | 0.109 |
| 1.0 | 71.8543 | 0.022 |
| 2.0 | 74.9537 | 0.021 |
| 5.0 | 84.2518 | 0.021 |
| 10.0 | 99.7486 | 0.021 |
Session Info
R version 4.5.3 (2026-03-11)
Platform: aarch64-apple-darwin20
Running under: macOS Tahoe 26.3.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/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] Matrix_1.7-4 CVXR_1.8.1
loaded via a namespace (and not attached):
[1] slam_0.1-55 cli_3.6.5 knitr_1.51 ECOSolveR_0.6.1
[5] rlang_1.1.7 xfun_0.56 clarabel_0.11.2 otel_0.2.0
[9] gurobi_13.0-1 Rglpk_0.6-5.1 highs_1.12.0-3 cccp_0.3-3
[13] scs_3.2.7 S7_0.2.1 jsonlite_2.0.0 backports_1.5.0
[17] rprojroot_2.1.1 htmltools_0.5.9 Rmosek_11.1.1 gmp_0.7-5.1
[21] piqp_0.6.2 rmarkdown_2.30 grid_4.5.3 evaluate_1.0.5
[25] fastmap_1.2.0 yaml_2.3.12 compiler_4.5.3 codetools_0.2-20
[29] htmlwidgets_1.6.4 Rcpp_1.1.1 here_1.0.2 osqp_1.0.0
[33] lattice_0.22-9 digest_0.6.39 checkmate_2.3.4 tools_4.5.3