CVXPY最新版本SolverError问题排查及严格不等式疑问
Hey there, let's work through your CVXPY problems one by one!
1. SolverError After Upgrading from CVXPY 0.4
First off, the jump from CVXPY 0.4 to the latest version is a massive one—there were overhauls to how the library handles cone constraints and solver compatibility. Here's why you're hitting that error and how to fix it:
Why the Error Occurs
CVXPY 0.4 was far more lenient in mapping problems to solver cones, but modern versions strictly enforce which solvers support which cone types:
CVXOPT: Has limited support for non-linear programming cones. It struggles with ExpCone (used for log/exp-based objectives/constraints) and has restrictive SOC (second-order cone) support.ECOS: Supports SOC and ExpCone, but does NOT handle PSD (positive semi-definite) cones. If your problem includes PSD matrix constraints, ECOS will fail outright.
Steps to Diagnose & Fix
- Pinpoint the problematic cone: Scan your code for:
- SOC constraints: Things like
norm(x) <= t(wheretis a scalar andxis a vector) - ExpCone usage: Any
log()orexp()terms in your objective/constraints (CVXPY converts these to exponential cone constraints) - PSD constraints: Variables defined with
PSD=True(e.g.,Variable((n,n), PSD=True)) or constraints likeX >> 0
- SOC constraints: Things like
- Verify variable dimensions:
- For PSD variables: Ensure they're square matrices (e.g.,
(n,n)shape, not(n,m)where n≠m) - For SOC constraints: Double-check that the constraint follows the standard form
norm(x) <= t—modern CVXPY won't automatically reshape misaligned variables like the old 0.4 version did
- For PSD variables: Ensure they're square matrices (e.g.,
- Pick the right solver:
- If you have PSD constraints: Use
SCSorMOSEK(both support all three cone types you mentioned) - If no PSD constraints but have SOC/ExpCone: Stick with
ECOSbut confirm your constraints are properly formatted - Avoid
CVXOPTfor problems involving ExpCone or complex SOC setups
- If you have PSD constraints: Use
2. Why CVXPY Doesn't Support Strict Inequalities
This isn't a CVXPY limitation—it's rooted in convex optimization theory:
- In convex optimization, the feasible region for a strict inequality
f(x) < bhas the same closure asf(x) <= b. The optimal solution (if it exists) will lie on the boundary of the feasible region, so strict inequalities don't add practical value. - Numerically, strict inequalities are impossible to enforce perfectly—computers can't represent infinitely small gaps.
If you need to approximate a strict inequality, use a small epsilon value:
# Instead of f(x) < b f(x) <= b - 1e-6
Choose an epsilon that's small enough for your problem's precision requirements but not so tiny that it causes numerical instability.
内容的提问来源于stack exchange,提问作者OGARCH

