I'm using CVXR
in R to solve a convex problem. My problem is the sum of a convex expression over many matrices: (my actual expression isn't the sum of squares, but a more complicated convex expression)
library(CVXR)
set.seed(1)
mats = array(rnorm(16 * 16 * 1000), dim = c(16, 16, 1000))
w <- Variable(16)
obj = 0.0
for (i in 1:1000) {
obj <- obj + sum_squares(mats[,, i] %*% w)
}
prob <- Problem(Minimize(obj))
The problem is that building prob
, before even solving the problem, is very slow. I assume this is because every single expression is stored in memory. Meanwhile, the non-symbolic code
set.seed(1)
mats = array(rnorm(16 * 16 * 1000), dim = c(16, 16, 1000))
w <- rnorm(16)
obj = 0.0
for (i in 1:1000) {
obj <- obj + sum((mats[,, i] %*% w)^2)
}
runs very quickly because it is iterative and not all the slices need to be stored in memory.
My questions are:
- Is there a way to formulate the problem so that CVXR can build it quickly?
- Are there convex solvers in R that can solve an arbitrary objective function, assuming the user has done the job of proving its convexity? I saw a few solvers that took matrix form convex problems like ECOSolveR but I'm not fully sure how to use them.