python - 尝试在 spyder 上使用 GLPK 运行时出错
问题描述
我在尝试使用 anaconda 在 spyder 上运行一些 python 代码时出错。代码很简单:
import picos
P = picos.Problem()
P.options.verbosity = 1
x = picos.IntegerVariable('x', 2, lower = [0,0])
P.add_constraint(x[0] + x[1] <= 8)
P.set_objective('max', 1*x[0] + 2*x[1])
print(P)
P.solve(solver="glpk")
print(x)
这是来自 spyder 的答案
runfile('/Users/Fermin/untitled0.py', wdir='/Users/Fermin')
------------------------------------------
Integer Linear Program
maximize x[0] + 2·x[1]
over
2×1 integer variable x (bounded below)
subject to
x[0] + x[1] ≤ 8
------------------------------------------
===================================
PICOS 2.0
===================================
Problem type: Integer Linear Program.
Searching a solution strategy for GLPK.
Traceback (most recent call last):
File "/Users/Fermin/untitled0.py", line 9, in <module>
P.solve(solver="glpk")
File "/Users/Fermin/opt/anaconda3/lib/python3.8/site-packages/picos/modeling/problem.py", line 1635, in solve
self._strategy = Strategy.from_problem(
File "/Users/Fermin/opt/anaconda3/lib/python3.8/site-packages/picos/modeling/strategy.py", line 187, in from_problem
raise RuntimeError(
RuntimeError: Selected solver GLPK is not available on the system.
奇怪的是,glpsol 已安装。当我glpsol --version
在终端上运行时,我得到了答案(v4.65)。如果我跑步,which glpsol
我会得到/Users/Fermin/opt/anaconda3/bin/glpsol
.
所以奇怪的是,即使它安装在我的电脑上(它是一个 mac),spyder 也无法找到它并使用它。我已经尝试过最简单的方法,即重新启动程序和/或我的计算机,删除所有内容并重新安装,等等。
谢谢您的帮助。
解决方案
问题是我必须安装 swiglpk。
pip install swiglpk
推荐阅读
- ios - 我在 swift 中的多行标签不起作用,该短语在一行中超出标签的大小
- asp.net - 修改 Azure DevOps Web 配置
- python - 如何使用 pvlib 的 pvwatts 模型计算有效辐照度?
- python - 我想在我的数据框中将 1-May-19 和 5/1/2019 转换为 1/5/2019
- python - 如何使用 AdaBoostClassifier 中的概率获得预测类别?
- javascript - 在jquery post请求中传递数组不起作用
- r - 如何仅捕获R中字符串的一部分?
- javascript - 如何在新对象中转换数组对象?
- redis - Redis 缓存中的持久部分键
- laravel - 如果旧行已经存在,则使用 laravel 插入新行或删除旧行