首页 > 解决方案 > 尝试在 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 也无法找到它并使用它。我已经尝试过最简单的方法,即重新启动程序和/或我的计算机,删除所有内容并重新安装,等等。

谢谢您的帮助。

标签: pythonanacondaspyderglpk

解决方案


问题是我必须安装 swiglpk。

pip install swiglpk

推荐阅读