You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将CVX半定规划问题转换为CVXPY时遇复数值问题求助

Fixing Complex Number Issues in CVXPY SDP Conversion

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. If c[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 of c[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. Since f is Hermitian, the elements referenced in your constraints can be complex, so matching them to complex values in c is 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 calling solve():

    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:31:23