# NOT RUN {
## example, adapted from the osqp documentation
# }
# NOT RUN {
library(rosqp)
library(Matrix)
set.seed(1)
n = 10
m = 1000
Ad = matrix(0, m, n)
Ad[sample(n*m, n*m/2, FALSE)] = runif(n*m/2)
x_true = (runif(n) > 0.8) * runif(n) / sqrt(n)
b = drop(Ad %*% x_true) + 0.5 * runif(m)
gammas = seq(1, 10, length.out = 11)
# % OSQP data
P = .sparseDiagonal(2*n+m, c(numeric(n), rep_len(1, m), numeric(n)))
q = numeric(2*n+m);
A = rbind(cbind(Ad,
-Diagonal(m),
sparseMatrix(numeric(), numeric(), x=numeric(), dims=c(m, n))),
cbind(Diagonal(n),
sparseMatrix(numeric(), numeric(), x=numeric(), dims=c(n, m)),
-Diagonal(n)),
cbind(Diagonal(n),
sparseMatrix(numeric(), numeric(), x=numeric(), dims=c(n, m)),
Diagonal(n))
)
l = c(b, rep_len(-Inf, n), numeric(n))
u = c(b, numeric(n), rep_len(Inf, n))
model = osqp(P, q, A, l, u, osqpSettings(verbose = FALSE))
res = sapply(gammas, function(gamma) {
q_new = c(numeric(n+m), rep_len(gamma, n))
model$Update(q=q_new)
res = model$Solve()
res$x
})
# }
Run the code above in your browser using DataLab