【问题标题】:Base Apply and Elementwise Function CVXRBase Apply 和 Elementwise 函数 CVXR
【发布时间】:2020-04-10 14:24:59
【问题描述】:

有没有办法使用 base::apply 和 CVXR 元素函数将元素函数应用于矩阵的每一列?假设向量的长度与矩阵的行数相匹配,我希望能够使我的目标函数如下:

new_matrix <- apply(constant_matrix, 2, function(x) x * variable_vector)
objective <- sum_entries( max_entries( as.matrix(new_matrix), axis = 2 ) )

解决问题时出现以下错误: min(constant) 中的错误:参数的“类型”(列表)无效

(如果我的代码不清楚,我的目标是将变量向量乘以常数矩阵的每一列,然后得到每个缩放列的最大值,然后对所有这些最大值求和。)

谢谢!

【问题讨论】:

    标签: cvx cvxr


    【解决方案1】:

    下面我使用base::applyCVXR 的元素乘法以及输出计算出一个示例。有关更复杂的示例,请参阅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
    `
    

    【讨论】:

    • 谢谢,这个例子对我很有帮助并且可以正确执行。但是,将其转换为更复杂的示例时,在运行 result$getValue() 时出现以下错误。 (求解器指出找到了最佳解决方案,但显然不是最佳解决方案。)Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘is_zero’ for signature ‘"matrix"’您能翻译一下该错误消息的含义吗?
    • 很难回答没有细节。一个代表会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多