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

使用cvxpy求解混合整数规划时服务器报TypeError求助

解决CVXPY在Linux服务器上的“TypeError: Object arrays are not currently supported”错误

问题背景

相同的混合整数规划代码在本地笔记本可正常运行并得到结果,但在已安装对应依赖的Linux服务器上执行时,在constraints = [cp.trace(C @ X.T) >= 5]行抛出错误:TypeError: Object arrays are not currently supported。

可能原因

  • 依赖版本不一致:本地与服务器的CVXPY、NumPy或GLPK_MI solver版本不匹配,旧版本CVXPY对NumPy数组的类型处理存在兼容性问题,触发对象数组错误。
  • 数组类型隐式转换:服务器环境中,生成的NumPy数组可能因依赖冲突或特殊安装方式被隐式转为object类型,导致CVXPY无法正确解析。
  • GLPK版本过低:服务器上的GLPK_MI solver版本过旧,与当前CVXPY版本不兼容,处理矩阵约束时出现类型错误。

解决方案

1. 统一依赖版本

  • 本地执行以下命令查看版本:
    pip show cvxpy numpy
    
  • 服务器上安装完全一致的版本:
    pip install cvxpy==<本地CVXPY版本> numpy==<本地NumPy版本>
    

2. 显式指定数组类型

在生成数组时强制指定float64类型,避免隐式转换:

A = np.random.rand(n, p).astype(np.float64)
C = np.random.rand(n, p).astype(np.float64)
Budget = np.random.rand(n, p).astype(np.float64)

3. 更新GLPK_MI solver

  • 若通过系统包管理安装GLPK(以Ubuntu为例):
    sudo apt-get update && sudo apt-get install glpk-utils
    
  • 若使用CVXPY绑定的GLPK接口,确保CVXPY为最新兼容版本。

4. 更换MIP solver

尝试使用其他兼容的混合整数规划求解器,比如CBC:

prob.solve(solver='CBC', verbose=True)

(需提前安装对应依赖:pip install cylp)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 17:11:09