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

cvxopt求解线性规划报ValueError(Rank(A) < p或Rank([G; A]) < n)如何解决

cvxopt求解线性规划Rank报错解决方案

错误根源

  • 参数传位错误:cvxopt.solvers.lp的前三个输入参数分别为目标函数系数c、不等式约束矩阵G(满足Gx ≤ h)、不等式约束右端项h,等式约束的系数矩阵A和右端项b需要作为第4、5个参数传入。你直接将等式约束的A、b传到了不等式约束的参数位置,导致求解器识别约束类型错误。
  • cvxopt不会默认添加决策变量非负约束,若你的问题隐含要求x_i ≥ 0,需要手动构造对应约束传入,否则求解器允许变量取负值。

修正后代码

import cvxopt
A = cvxopt.matrix([[1.0, 10.0, 12.0],
                   [1.0, 6.0, 1.0],
                   [1.0, 8.0, 15.0],
                   [1.0, 4.0, 6.0],
                   [1.0, 5.0, 3.0]])
b = cvxopt.matrix([1.0, 6.0, 12.0])
c = cvxopt.matrix([-4.1, -3.5, -3.8, -2.9, -3.3])

# 无不等式约束时G和h传None,显式传入等式约束A和b
sol = cvxopt.solvers.lp(c, None, None, A, b)
print(sol['x'])

可选补充(添加变量非负约束)

如果需要所有决策变量满足x_i ≥ 0,添加对应不等式约束即可:

import cvxopt
import numpy as np

A = cvxopt.matrix([[1.0, 10.0, 12.0],
                   [1.0, 6.0, 1.0],
                   [1.0, 8.0, 15.0],
                   [1.0, 4.0, 6.0],
                   [1.0, 5.0, 3.0]])
b = cvxopt.matrix([1.0, 6.0, 12.0])
c = cvxopt.matrix([-4.1, -3.5, -3.8, -2.9, -3.3])

# 构造x≥0对应的不等式约束:-x ≤ 0,即G=-单位矩阵,h=0向量
n = c.size[0]
G = cvxopt.matrix(-np.eye(n))
h = cvxopt.matrix(np.zeros(n))

sol = cvxopt.solvers.lp(c, G, h, A, b)
print(sol['x'])

内容的提问来源于stack exchange,提问作者dominikmeyer95

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 17:18:03