CVXR 使用 Mosek 解决二次最小化问题

CVXR using Mosek for a quadratic minimization problem

我正在尝试使用 R 包 CVXR 解决具有线性约束的二次优化问题。尽管默认求解器能够求解优化,但 Mosek 求解器不能。我希望使用 Mosek 的原因是因为我需要解决一个具有超过 250 个约束的更大问题,而默认求解器给出了一个不准确的解决方案,因此我希望使用 Mosek 解决更大的问题。这是一个简单的例子,Mosek 没有工作:

suppressMessages(suppressWarnings(library(CVXR)))

问题数据

set.seed(10)
n <- 10
SAMPLES <- 100
mu <- matrix(abs(rnorm(n)), nrow = n)
Sigma <- matrix(rnorm(n^2), nrow = n, ncol = n)
Sigma <- t(Sigma) %*% Sigma

表格问题

w <- Variable(n)
ret <- t(mu) %*% w
risk <- quad_form(w, Sigma)
constraints <- list(w >= 0, sum(w) == 1,ret==mean(mu))

风险规避参数

prob <- Problem(Minimize(risk), constraints)
result <- solve(prob,solver='MOSEK')

它给出了以下错误。

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: 'int' object is not iterable 
10.stop(structure(list(message = "TypeError: 'int' object is not iterable", 
    call = py_call_impl(callable, dots$args, dots$keywords), 
    cppstack = structure(list(file = "", line = -1L, stack = c("1   reticulate.so                       0x000000010d278f9b _ZN4Rcpp9exceptionC2EPKcb + 219", 
    "2   reticulate.so                       0x000000010d27fa35 _ZN4Rcpp4stopERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE + 53",  ... 
9.mosek_intf at mosekglue.py#51
8.get_mosekglue()$mosek_intf(reticulate::r_to_py(A), b, reticulate::r_to_py(G), 
    h, c, dims, offset, reticulate::dict(solver_opts), verbose) 
7.Solver.solve(solver, objective, constraints, object@.cached_data, 
    warm_start, verbose, ...) 
6.Solver.solve(solver, objective, constraints, object@.cached_data, 
    warm_start, verbose, ...) 
5.CVXR::psolve(a, b, ...) 
4.CVXR::psolve(a, b, ...) 
3.solve.Problem(prob, solver = "MOSEK") 
2.solve(prob, solver = "MOSEK") 
1.solve(prob, solver = "MOSEK")

有人知道怎么解决,可能是重新表达问题?

我的会话信息如下:

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.1

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reticulate_1.10  Matrix_1.2-15    CVXR_0.99-2      e1071_1.7-0.1    rstudioapi_0.9.0
[6] openxlsx_4.1.0  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0        lattice_0.20-38   class_7.3-14      gmp_0.5-13.2      R.methodsS3_1.7.1
 [6] grid_3.5.2        R6_2.3.0          jsonlite_1.6      zip_1.0.0         Rmpfr_0.7-2      
[11] R.oo_1.22.0       R.utils_2.7.0     tools_3.5.2       bit64_0.9-7       bit_1.1-14       
[16] compiler_3.5.2    scs_1.1-1         ECOSolveR_0.4    

谢谢

Python错误

TypeError: 'int' object is not iterable'

表示在mosekglue.py expected a list to iterate over (somewhere unspecified), but received a scalar. This might be caused by the fact that -- since everything is a list in R -- reticulate handles single-element and multi-element lists different (see its type conversions中调用了mosek_intf)。

只阅读了源代码,我最好的猜测是 mosekglue.py (line 102) 失败了,因为你的问题只有一个二阶锥(具体来说,我相信网状发送 dims=dict(q: n) 而不是dims=dict(q: [n]))。

您的选择是在 GitHub 上向 CVXR 项目提交错误报告并等待,and/or 确认我的怀疑(甚至可能提出修复并将其贡献给项目), and/or 用虚拟 material 修改你的问题,直到它通过界面。

这确实是 mosekglue.py 中的一个问题,已在 CVXR-0.99-3 及更高版本中解决;见 changelog

这是你的例子:

> suppressMessages(suppressWarnings(library(CVXR)))
> set.seed(10)
> n <- 10
> SAMPLES <- 100
> mu <- matrix(abs(rnorm(n)), nrow = n)
> Sigma <- matrix(rnorm(n^2), nrow = n, ncol = n)
> Sigma <- t(Sigma) %*% Sigma
> w <- Variable(n)
> ret <- t(mu) %*% w
> risk <- quad_form(w, Sigma)
> constraints <- list(w >= 0, sum(w) == 1,ret==mean(mu))
> prob <- Problem(Minimize(risk), constraints)
> result <- solve(prob,solver='MOSEK', verbose=TRUE)
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 24              
  Cones                  : 1               
  Scalar variables       : 23              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator - tries                  : 0                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 24              
  Cones                  : 1               
  Scalar variables       : 23              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 13
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 22                conic                  : 12              
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 89                after factor           : 91              
Factor     - dense dim.             : 0                 flops                  : 2.54e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  6.9e+00  1.0e+00  0.00e+00   2.756709152e+01   2.756709152e+01   1.0e+00  0.00  
1   2.7e-01  1.8e+00  2.2e-01  -6.02e-01  -9.936262607e+00  4.762263760e+00   2.7e-01  0.00  
2   9.9e-02  6.8e-01  4.3e-01  1.23e+00   9.201188225e-01   -6.807855573e-01  9.9e-02  0.00  
3   3.6e-02  2.5e-01  4.4e-01  1.97e+00   -1.264256365e-01  -5.225584219e-01  3.6e-02  0.00  
4   9.2e-03  6.3e-02  1.8e-01  2.33e+00   2.868592743e-01   2.542157558e-01   9.2e-03  0.00  
5   2.0e-03  1.4e-02  8.8e-02  1.17e+00   2.840102253e-01   2.769232377e-01   2.0e-03  0.00  
6   3.7e-04  2.5e-03  3.9e-02  1.04e+00   2.879204145e-01   2.866174921e-01   3.7e-04  0.00  
7   6.3e-05  4.3e-04  1.6e-02  1.02e+00   2.901039465e-01   2.898751147e-01   6.3e-05  0.00  
8   1.3e-05  8.8e-05  7.4e-03  1.00e+00   2.902381000e-01   2.901914751e-01   1.3e-05  0.00  
9   3.6e-06  2.5e-05  4.0e-03  1.00e+00   2.902501323e-01   2.902366424e-01   3.6e-06  0.00  
10  1.1e-06  7.4e-06  2.2e-03  1.00e+00   2.902666158e-01   2.902625442e-01   1.1e-06  0.00  
11  3.6e-07  2.5e-06  1.3e-03  1.00e+00   2.902689540e-01   2.902675850e-01   3.6e-07  0.00  
12  5.7e-08  3.9e-07  5.1e-04  1.00e+00   2.902716546e-01   2.902714343e-01   5.7e-08  0.00  
13  6.6e-09  4.6e-08  1.7e-04  1.00e+00   2.902720234e-01   2.902719974e-01   6.6e-09  0.00  
Optimizer terminated. Time: 0.00    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.9027202344e-01    nrm: 1e+00    Viol.  con: 6e-09    var: 0e+00    cones: 0e+00  
  Dual.    obj: 2.9027199740e-01    nrm: 1e+01    Viol.  con: 0e+00    var: 8e-08    cones: 0e+00  
> result$value
[1] 0.290272
>