Getting KeyError: 1822253855912 when using pyomo (any solver)
Getting KeyError: 1822253855912 when using pyomo (any solver)
我正在将 Pyomo 与 Spyder IDE 一起使用,并且 运行 正在使用一个简单的线性规划示例,同时我已经安装了 pyomo、gurabi、CPLEX、GLPK 和其他求解器,无论我使用哪一个使用,我收到类似于 (KeyError: 1822253855912):
的错误
Traceback (most recent call last):
File "<ipython-input-2-3550848663cc>", line 18, in <module>
opt.solve(model)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 569, in solve
self._presolve(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\solvers\plugins\solvers\CBCplugin.py", line 289, in _presolve
super(CBCSHELL, self)._presolve(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 205, in _presolve
OptSolver._presolve(self, *args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 669, in _presolve
**kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 721, in _convert_problem
**kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\convert.py", line 100, in convert_problem
problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\solvers\plugins\converter\model.py", line 81, in apply
io_options=io_options)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\core\base\block.py", line 1825, in write
io_options)
File "pyomo\repn\plugins\cpxlp.pyx", line 157, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 158, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 159, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 539, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp._print_model_LP
File "pyomo\repn\plugins\cpxlp.pyx", line 212, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp._print_expr_canonical
KeyError: 1822253855912
我真的不明白这个错误是什么意思,不久前我看到其他人也问过类似的问题,但没有答案。这是我正在使用的代码:
import pyomo.environ as pyo
from pyomo.environ import *
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
x = pyo.Var(bounds=(0,10))
y = pyo.Var(bounds=(0,10))
model.C1 = pyo.Constraint(expr = -x+2*y<=8)
model.C2 = pyo.Constraint(expr = 2*x+y<=14)
model.C3 = pyo.Constraint(expr = 2*x-y<=10)
model.obj = pyo.Objective(expr= x+y, sense=maximize)
opt = SolverFactory('glpk')
opt.solve(model)
x_value = pyo.value(x)
y_value = pyo.value(y)
print("X: ", x_value)
print("y: ", y_value)
代码 运行 在“opt = SolverFactory('glpk')”之前都很好,当它到达行“opt.solve(model)”时引发错误。
当我检查“pyomo help --solvers”时,我在 gurabi、glpk 和 cplex(以及其他一些)旁边得到一个“+”,所以想知道这是否与 Spyder 中的 epython 环境有关(它 运行 其他代码很好)
问题是您没有将任何 Var
附加到实际问题。您在模型外定义了所有 Var
,您需要将它们定义为 class model.x = pyo.Var(bounds=(0,10))
的一部分
以下模型产生以下结果:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
model.x = pyo.Var(bounds=(0,10))
model.y = pyo.Var(bounds=(0,10))
model.C1 = pyo.Constraint(expr = -model.x + 2*model.y<=8)
model.C2 = pyo.Constraint(expr = 2*model.x + model.y<=14)
model.C3 = pyo.Constraint(expr = 2*model.x - model.y<=10)
model.obj = pyo.Objective(expr= model.x + model.y, sense=pyo.maximize)
opt = pyo.SolverFactory('glpk')
opt.solve(model)
x_value = pyo.value(model.x)
y_value = pyo.value(model.y)
print("X: ", x_value)
print("y: ", y_value)
这产生:
X: 4.0
y: 6.0
我正在将 Pyomo 与 Spyder IDE 一起使用,并且 运行 正在使用一个简单的线性规划示例,同时我已经安装了 pyomo、gurabi、CPLEX、GLPK 和其他求解器,无论我使用哪一个使用,我收到类似于 (KeyError: 1822253855912):
的错误Traceback (most recent call last):
File "<ipython-input-2-3550848663cc>", line 18, in <module>
opt.solve(model)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 569, in solve
self._presolve(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\solvers\plugins\solvers\CBCplugin.py", line 289, in _presolve
super(CBCSHELL, self)._presolve(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 205, in _presolve
OptSolver._presolve(self, *args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 669, in _presolve
**kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 721, in _convert_problem
**kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\opt\base\convert.py", line 100, in convert_problem
problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\solvers\plugins\converter\model.py", line 81, in apply
io_options=io_options)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyomo\core\base\block.py", line 1825, in write
io_options)
File "pyomo\repn\plugins\cpxlp.pyx", line 157, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 158, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 159, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp.__call__
File "pyomo\repn\plugins\cpxlp.pyx", line 539, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp._print_model_LP
File "pyomo\repn\plugins\cpxlp.pyx", line 212, in pyomo.repn.plugins.cpxlp.ProblemWriter_cpxlp._print_expr_canonical
KeyError: 1822253855912
我真的不明白这个错误是什么意思,不久前我看到其他人也问过类似的问题,但没有答案。这是我正在使用的代码:
import pyomo.environ as pyo
from pyomo.environ import *
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
x = pyo.Var(bounds=(0,10))
y = pyo.Var(bounds=(0,10))
model.C1 = pyo.Constraint(expr = -x+2*y<=8)
model.C2 = pyo.Constraint(expr = 2*x+y<=14)
model.C3 = pyo.Constraint(expr = 2*x-y<=10)
model.obj = pyo.Objective(expr= x+y, sense=maximize)
opt = SolverFactory('glpk')
opt.solve(model)
x_value = pyo.value(x)
y_value = pyo.value(y)
print("X: ", x_value)
print("y: ", y_value)
代码 运行 在“opt = SolverFactory('glpk')”之前都很好,当它到达行“opt.solve(model)”时引发错误。
当我检查“pyomo help --solvers”时,我在 gurabi、glpk 和 cplex(以及其他一些)旁边得到一个“+”,所以想知道这是否与 Spyder 中的 epython 环境有关(它 运行 其他代码很好)
问题是您没有将任何 Var
附加到实际问题。您在模型外定义了所有 Var
,您需要将它们定义为 class model.x = pyo.Var(bounds=(0,10))
以下模型产生以下结果:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
model.x = pyo.Var(bounds=(0,10))
model.y = pyo.Var(bounds=(0,10))
model.C1 = pyo.Constraint(expr = -model.x + 2*model.y<=8)
model.C2 = pyo.Constraint(expr = 2*model.x + model.y<=14)
model.C3 = pyo.Constraint(expr = 2*model.x - model.y<=10)
model.obj = pyo.Objective(expr= model.x + model.y, sense=pyo.maximize)
opt = pyo.SolverFactory('glpk')
opt.solve(model)
x_value = pyo.value(model.x)
y_value = pyo.value(model.y)
print("X: ", x_value)
print("y: ", y_value)
这产生:
X: 4.0
y: 6.0