我正在尝试使用Pyomo + glpk Solver(Anaconda2 64bit Spyder)运行一些简单的例子:

from pyomo.environ import *
model = ConcreteModel()
model.x_1 = Var(within=NonNegativeReals)
model.x_2 = Var(within=NonNegativeReals)
model.obj = Objective(expr=model.x_1 + 2*model.x_2)
model.con1 = Constraint(expr=3*model.x_1 + 4*model.x_2 >= 1)
model.con2 = Constraint(expr=2*model.x_1 + 5*model.x_2 >= 2)

opt = SolverFactory("glpk")
instance = model.create()
#results = opt.solve(instance)
#results.write()


但是我收到以下错误消息:

invalid literal for int() with base 10: 'c'
Traceback (most recent call last):

  File "<ipython-input-5-e074641da66d>", line 1, in <module>
    runfile('D:/..../Exampe.py', wdir='D:.../exercises/pyomo')

  File "C:\...\Continuum\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)

  File "C:\....\Continuum\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "D:/...pyomo/Exampe.py", line 34, in <module>
    results = opt.solve(instance)

  File "C:\....\Continuum\Anaconda21\lib\site-packages\pyomo\opt\base\solvers.py", line 580, in solve
    result = self._postsolve()

  File "C:\...Continuum\Anaconda21\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 267, in _postsolve
    results = self.process_output(self._rc)

  File "C:\...\Continuum\Anaconda21\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 329, in process_output
    self.process_soln_file(results)

  File "C:\....\Continuum\Anaconda21\lib\site-packages\pyomo\solvers\plugins\solvers\GLPK.py", line 454, in process_soln_file
    raise ValueError(msg)

ValueError: Error parsing solution data file, line 1


我从http://winglpk.sourceforge.net/->下载glpk,解压缩并向环境变量“ path”添加了parth。

希望有人能帮助我-谢谢!

最佳答案

这是GLPK 4.60的一个已知问题(glpsol更改了其输出格式,从而破坏了Pyomo 4.3的解析器)。您可以下载GLPK的旧版本,也可以将Pyomo升级到4.4.1(包含更新的解析器)。

关于python - 使用glpk求解器的Python/Pyomo-错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38654200/

10-11 06:20