CVXPY 二次规划 - ArpackNoConvergence 错误和 AssertionError

问题描述 投票:0回答:1

我正在尝试使用 Python 包 CVXPY 来解决二次问题,但我不断出错。你能帮帮我吗?

这是我的代码:

p = lasso.p # This is a number
Sigma_opt = lasso.Sigma_opt # This is a positive semi-definite matrix of shape (p,p)
rho_pair = lasso.rho_pair # Vector of shape (p,)
mu = lasso.mu # Positive scalar number.

def solve_2nd_problem(p, Sigma_opt, rho_pair, mu):
  beta = cp.Variable(p) # Variable to optimize
  obj = cp.Minimize(0.5 * cp.quad_form(beta, Sigma_opt) - rho_pair.T @ beta + mu * cp.norm1(beta)) # Objective to minimize
  constraints = [] # Constraints

  # Solve the optimization problem
  prob = cp.Problem(obj, constraints)
  prob.solve()

  return beta.value

beta_opt = solve_2nd_problem(p, Sigma_opt, rho_pair, mu)

我明白了

ArpackNoConvergence: ARPACK error -1: No convergence (11281 iterations, 0/1 eigenvectors converged) [ARPACK error -14: ZNAUPD did not find any eigenvalues to sufficient accuracy.]

阅读here讨论的内容后,我尝试通过

Sigma_opt
更改
cp.psd_wrap(Sigma_opt)
并设置
cp.settings.EIGVAL_TOL = 1e-06
,但这样做我现在收到此错误:

AssertionError                            Traceback (most recent call last)
<ipython-input-34-1fbde9ad6b58> in <cell line: 19>()
     17   return beta.value
     18 
---> 19 beta_opt = solve_2nd_problem(p, Sigma_opt, rho_pair, mu)

11 frames
/usr/local/lib/python3.9/dist-packages/cvxpy/reductions/complex2real/complex2real.py in canonicalize_expr(self, expr, real_args, imag_args, real2imag, leaf_map)
    179             return result
    180         else:
--> 181             assert all(v is None for v in imag_args)
    182             real_out = expr.copy(real_args)
    183             return real_out, None

AssertionError: 

我不确定我是否理解这里发生的事情,但我现在知道我的矩阵 Sigma_opt 是 PSD(因为

np.all(np.linalg.eigvals(Sigma_opt) > 0)
(Sigma_opt.T == Sigma_opt).all()
都返回
True
)。

矩阵 Sigma_opt 的形状为 (p = 946, p = 946),rho_pair 的形状为 (p = 946,)。

你知道我该如何解决这个问题吗?

谢谢。

python error-handling linear-algebra cvxpy convex-optimization
1个回答
0
投票

CVXPY使用的求解器存在数值不稳定导致的问题,它使用SCS求解器,可能会遇到大问题或病态问题,,让我们尝试不同的,例如MOSEK或ECOS,这可能更可靠。

首先安装它

    !pip install mosek
    !pip install ecos

then we can modify the `solver_2nd_problem` here is the code:

def solve_2nd_problem(p, Sigma_opt, rho_pair, mu):
    beta = cp.Variable(p) # Variable to optimize
    obj = cp.Minimize(0.5 * cp.quad_form(beta, Sigma_opt) - rho_pair.T @ beta + mu * cp.norm1(beta)) # Objective to minimize
    constraints = [] # Constraints

    # Solve the optimization problem
    prob = cp.Problem(obj, constraints)
    prob.solve(solver=cp.MOSEK, verbose=True)  # Change this to cp.ECOS if you prefer ECOS solver

    return beta.value
© www.soinside.com 2019 - 2024. All rights reserved.