Search code examples
pythonlinear-programmingpyomo

Getting KeyError: 1822253855912 when using pyomo (any solver)


I am using Pyomo with Spyder IDE, and running a simple linear programming example and while I have installed pyomo, gurabi, CPLEX, GLPK and other solvers, no matter which one I use, I get an error similar to (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

I can't really make sense of what this error means and I have seen someone else also asking a similar question a while back but with no answers. here is the code I am using:

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)

The code runs fine until "opt = SolverFactory('glpk')", the error is raised when it reaches the line "opt.solve(model)".

When I check "pyomo help --solvers" i get a "+" next to gurabi, glpk and cplex(as well as some others) so wondering if this has something to do with th epython environment in Spyder(it does run other code fine)


Solution

  • The problem is that you're not attaching any Var to the actual problem. You're defining all Var outside the model and you need to define them as part of your class model.x = pyo.Var(bounds=(0,10))

    The following model yields the following results:

    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)
    

    This Yields:

    X:  4.0
    y:  6.0