将CVX半定规划问题转换为CVXPY时遇复数值问题求助
First, let's get to the root of your problem: CVXPY does not support optimizing complex-valued objective functions. Even though the trace of your Hermitian matrix f is real, if c[0] is complex, your objective c[0] - cvx.trace(f) becomes a complex expression, which CVXPY can't handle for maximization/minimization. This is the core reason your code fails when c contains complex numbers.
Key Fixes & Explanations
Ensure a Real-Valued Objective
In most Fourier-related SDP problems, the objective should be a real scalar. Ifc[0]is a complex Fourier coefficient, you likely want to maximize the real part of your objective (since maximizing a complex number isn't well-defined). Modify your objective to explicitly take the real part ofc[0]:obj = cvx.Maximize(cvx.real(c[0]) - cvx.trace(f))If
c[0]is supposed to be real (e.g., the DC component of Fourier coefficients), double-check your data preprocessing to ensure it's cast to a real number.Check Constraint Compatibility
Complex equality constraints are supported in CVXPY—they automatically split into real and imaginary part constraints. Sincefis Hermitian, the elements referenced in your constraints can be complex, so matching them to complex values incis valid.Use a Solver That Supports Complex SDPs
Not all CVXPY solvers handle complex problems well. Stick to solvers like SCS or MOSEK (which requires a license but is highly optimized) for complex semidefinite programming. Specify the solver explicitly when callingsolve():sol = prob.solve(solver=cvx.SCS)
Modified Working Code
import cvxpy as cvx import numpy as np # Example complex-valued Fourier coefficients c = [1 + 0j, 2 + 3j] n = len(c) # Create Hermitian optimization variable f = cvx.Variable((n, n), hermitian=True) # Build constraints constraints = [f >> 0] for k in range(1, n): indices = [(i * n) + i - (n - k) for i in range(n - k, n)] constraints += [cvx.sum(cvx.vec(f)[indices]) == c[n - k]] # Real-valued objective function (critical fix) obj = cvx.Maximize(cvx.real(c[0]) - cvx.trace(f)) # Solve with a complex-compatible solver prob = cvx.Problem(obj, constraints) sol = prob.solve(solver=cvx.SCS) print("Optimal value:", sol) print("Optimized f matrix:\n", f.value)
Why This Works
- The objective is now strictly real, which aligns with CVXPY's optimization requirements.
- Complex constraints are handled correctly by splitting into real/imaginary components under the hood.
- Specifying a complex-aware solver ensures the problem is processed without compatibility errors.
内容的提问来源于stack exchange,提问作者Wulfsta

