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

已安装CVXOPT但Pyomo无法调用该求解器的问题求助

问题:Pyomo调用CVXOPT求解器失败

我正在用Python/Pyomo编写优化问题,计划用CVXOPT求解器求解,完整代码如下:

import pyomo.environ as pyo
import cvxopt

Horizon = 3

model = pyo.ConcreteModel()


model.T = pyo.RangeSet(0, Horizon)

model.u = pyo.Var(model.T, within=pyo.NonNegativeReals)
model.x = pyo.Var(model.T, within = pyo.NonNegativeReals)

def transition(model, t) -> float :
    if t == model.T.first():
        return model.x[t] == model.u[t]
    else:
        return model.x[t] == model.x[model.T.prev(t)] + model.u[t]

model.transition = pyo.Constraint(model.T, rule=transition)

def objective(model):

    return sum(1 + model.x[t] - model.u[t]**2 for t in model.T)

model.obj = pyo.Objective(rule = objective, sense = pyo.maximize)

opt = pyo.SolverFactory('cvxopt')

opt.solve(model) 

已通过Conda安装CVXOPT,它在Anaconda环境包列表中,代码导入时无报错,但调用solve求解模型时,Spyder控制台报错:

WARNING: Failed to create solver with name 'CVXOPT': Failed to set executable
for solver asl. File with name=CVXOPT either does not exist or it is not
executable. To skip this validation, call set_executable with validate=False.
Traceback (most recent call last):
  File "C:\Users\gal\AppData\Local\anaconda3\Lib\site-packages\pyomo\opt\base\solvers.py", line 165, in __call__
    opt = self._cls[_implicit_solvers[mode]](**kwds)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\gal\AppData\Local\anaconda3\Lib\site-packages\pyomo\solvers\plugins\solvers\ASL.py", line 45, in __init__
    SystemCallSolver.__init__(self, **kwds)
  File "C:\Users\gal\AppData\Local\anaconda3\Lib\site-packages\pyomo\opt\solver\shellcmd.py", line 67, in __init__
    self.set_executable(name=executable, validate=validate)
  File "C:\Users\gal\AppData\Local\anaconda3\Lib\site-packages\pyomo\opt\solver\shellcmd.py", line 116, in set_executable
    raise ValueError(
ValueError: Failed to set executable for solver asl. File with name=CVXOPT either does not exist or it is not executable. To skip this validation, call set_executable with validate=False.
Traceback (most recent call last):

  File ~\AppData\Local\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\gal\dropbox\thèse\code\modèle_pyo.py:45
    opt.solve(model)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pyomo\opt\base\solvers.py:102 in solve
    self._solver_error('solve')

  File ~\AppData\Local\anaconda3\Lib\site-packages\pyomo\opt\base\solvers.py:119 in _solver_error
    raise RuntimeError(

RuntimeError: Attempting to use an unavailable solver.

The SolverFactory was unable to create the solver "CVXOPT"
and returned an UnknownSolver object.  This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").

The original solver was created with the following parameters:
    executable: CVXOPT
    type: CVXOPT
    _args: ()
    options: {}

请问是什么原因导致脚本无法找到该求解器,以及如何解决此问题?


原因分析和解决方法

原因

CVXOPT是Python库形式的求解器,并非独立的可执行程序,但Pyomo的SolverFactory('cvxopt')默认会尝试按外部可执行程序的方式查找,导致找不到对应可执行文件而报错。

解决方法

修改求解器初始化代码,指定使用CVXOPT的Python接口而非外部命令行工具:

# 替换原有的opt = pyo.SolverFactory('cvxopt')
opt = pyo.SolverFactory('cvxopt', solver_io='python')

额外验证步骤

  • 确认当前环境已正确安装依赖:
    conda list pyomo cvxopt
    
  • 若仍有问题,重新安装依赖:
    conda install -c conda-forge pyomo cvxopt
    
  • 确保Spyder运行的环境与安装依赖的Conda环境一致(可在Spyder的环境选择菜单中切换)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:40:36