Ниже я разрабатываю пример, используя base::apply
и поэлементное умножение на CVXR
вместе с выводом. Для более сложного примера см. Pliable Lasso .
library(CVXR)
library(magrittr)
constant_matrix <- base::matrix(1:20, nrow = 4)
variable_vector <- CVXR::Variable(4, pos = TRUE)
constant_matrix %>%
base::apply(MARGIN = 2, FUN = function(x) CVXR::multiply(lh_exp = x, rh_exp = variable_vector)) %>%
base::lapply(max) ->
column_max_list
objective <- base::Reduce(f = '+', x = column_max_list)
prob <- CVXR::Problem(CVXR::Minimize(objective), constraints = list(variable_vector >= 2))
result <- CVXR::solve(prob, verbose = TRUE)
result$value
result$getValue(variable_vector)
Вот вывод:
-----------------------------------------------------------------
OSQP v0.6.0 - Operator Splitting QP Solver
(c) Bartolomeo Stellato, Goran Banjac
University of Oxford - Stanford University 2019
-----------------------------------------------------------------
problem: variables n = 9, constraints m = 28
nnz(P) + nnz(A) = 48
settings: linear system solver = qdldl,
eps_abs = 1.0e-05, eps_rel = 1.0e-05,
eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive),
sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
check_termination: on (interval 25),
scaling: on, scaled_termination: off
warm start: on, polish: on, time_limit: off
iter objective pri res dua res rho time
1 -1.0811e+02 1.94e+01 1.56e+01 1.00e-01 6.07e-05s
200 1.1999e+02 3.92e-04 4.69e-04 1.00e-01 2.31e-04s
300 1.2000e+02 1.46e-05 2.92e-06 1.00e-01 2.86e-04s
status: solved
solution polish: unsuccessful
number of iterations: 300
optimal objective: 120.0005
run time: 3.11e-04s
optimal rho estimate: 3.06e-01
> result$value
[1] 120.0005
> result$getValue(variable_vector)
[,1]
[1,] 2.248055
[2,] 2.149815
[3,] 2.057720
[4,] 2.000007
`