1

I just started to use pyomo. I am trying to solve a dynamic optimization problem with it. But I am getting KeyError: 2472563968992, which I could not understand.

import numpy as np
import matplotlib.pyplot as plt
from pyomo.dae import ContinuousSet, DerivativeVar
from pyomo.environ import ConcreteModel, TransformationFactory, Var, \
                          NonNegativeReals, Constraint, \
                          SolverFactory, Objective, cos, sin, minimize, \
                          NonNegativeReals
# Define parameters of the problem
g=9.81
m_e=1e2
theta=10
C_RR=0.0035
C_D=0.65
rho=1.23
V=3 #m/s
U_max=16 #m/s, approx equivalent to 60km/h
AWC=1.5e4
CP=335
A=0.423
mm=m_e/1.014 #effective body mass 1.4% larger than actual mass
P_max=-((4*0.039*90+CP)/pow((0.01*90+163),2))*pow(90,2)+(4*(0.039*90+CP)/pow((0.01*90+163),2))*90 

model = ConcreteModel('CyclingRace')
model.T = Var(domain=NonNegativeReals)
model.t = ContinuousSet(bounds=(0, 1))
model.s = Var(model.t, domain=NonNegativeReals,bounds=(0,2e4))
model.U = Var(model.t, domain=NonNegativeReals,bounds=(1,20))
model.W = Var(model.t, domain=NonNegativeReals,bounds=(0,AWC))
model.P = Var(model.t, domain=NonNegativeReals,bounds=(0,P_max))
model.sdot = DerivativeVar(model.s, wrt=model.t, domain=NonNegativeReals)
model.Udot = DerivativeVar(model.U, wrt=model.t, domain=NonNegativeReals)
model.Wdot = DerivativeVar(model.W, wrt=model.t, domain=NonNegativeReals)

# Dynamics
model.sode = Constraint(model.t, rule=lambda m, t: m.sdot[t] == m.U[t]*m.T)
model.Uode=Constraint(model.t, rule=lambda m, t: m.Udot[t]==(m.P[t]/m.U[t]/m_e))
model.Wode=Constraint(model.t, rule=lambda m, t: m.Wdot[t]== -(m.P[t]-CP)*m.T)
model.s[1].fix(2e4)
model.obj = Objective(expr=model.T, sense=minimize)
solver = SolverFactory('ipopt')
results = solver.solve(model)

and the error I got was:

  KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14804/1174518676.py in <module>
      1 model.obj = Objective(expr=model.T, sense=minimize)
      2 solver = SolverFactory('ipopt')
----> 3 results = solver.solve(model)

C:\Anaconda\anaconda\lib\site-packages\pyomo\opt\base\solvers.py in solve(self, *args, **kwds)
    567             initial_time = time.time()
    568 
--> 569             self._presolve(*args, **kwds)
    570 
    571             presolve_completion_time = time.time()

C:\Anaconda\anaconda\lib\site-packages\pyomo\opt\solver\shellcmd.py in _presolve(self, *args, **kwds)
    203         self._define_signal_handlers = kwds.pop('use_signal_handling',None)
    204 
--> 205         OptSolver._presolve(self, *args, **kwds)
    206 
    207         #

C:\Anaconda\anaconda\lib\site-packages\pyomo\opt\base\solvers.py in _presolve(self, *args, **kwds)
    664             write_start_time = time.time()
    665             (self._problem_files, self._problem_format, self._smap_id) = \
--> 666                 self._convert_problem(args,
    667                                       self._problem_format,
    668                                       self._valid_problem_formats,

C:\Anaconda\anaconda\lib\site-packages\pyomo\opt\base\solvers.py in _convert_problem(self, args, problem_format, valid_problem_formats, **kwds)
    715                          valid_problem_formats,
    716                          **kwds):
--> 717         return convert_problem(args,
    718                                problem_format,
    719                                valid_problem_formats,

C:\Anaconda\anaconda\lib\site-packages\pyomo\opt\base\convert.py in convert_problem(args, target_problem_type, valid_problem_types, has_capability, **kwds)
     98                     tmpkw = kwds
     99                     tmpkw['capabilities'] = has_capability
--> 100                     problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
    101                     return problem_files, ptype, symbol_map
    102 

C:\Anaconda\anaconda\lib\site-packages\pyomo\solvers\plugins\converter\model.py in apply(self, *args, **kwds)
    178                 else:
    179                     (problem_filename, symbol_map_id) = \
--> 180                         instance.write(
    181                             filename=problem_filename,
    182                             format=args[1],

C:\Anaconda\anaconda\lib\site-packages\pyomo\core\base\block.py in write(self, filename, format, solver_capability, io_options)
   1820         if solver_capability is None:
   1821             def solver_capability(x): return True
-> 1822         (filename, smap) = problem_writer(self,
   1823                                           filename,
   1824                                           solver_capability,

pyomo\repn\plugins\ampl\ampl_.pyx in pyomo.repn.plugins.ampl.ampl_.ProblemWriter_nl.__call__()

pyomo\repn\plugins\ampl\ampl_.pyx in pyomo.repn.plugins.ampl.ampl_.ProblemWriter_nl.__call__()

pyomo\repn\plugins\ampl\ampl_.pyx in pyomo.repn.plugins.ampl.ampl_.ProblemWriter_nl.__call__()

pyomo\repn\plugins\ampl\ampl_.pyx in pyomo.repn.plugins.ampl.ampl_.ProblemWriter_nl._print_model_NL()

pyomo\repn\plugins\ampl\ampl_.pyx in genexpr()

pyomo\repn\plugins\ampl\ampl_.pyx in genexpr()

KeyError: 2472563968992

I have tried to search what this key error code means but has not been successful. Could you please help me solve it? Thank you very much!

  • If you are just starting w/ pyomo, you are kinda swinging for the fences here... :) Try at least printing your model *before* solving with `model.pprint()`. I see that your value for P_max is negative, so minimally, your variable for P is infeasible. There are likely other issues too. Inspect the printout and test some values. – AirSquid Feb 25 '22 at 19:11
  • @AirSquid Thank you for the answer! I reformulated my equations and it is working now. Have a wonderful day! – lily thepooh Mar 01 '22 at 11:25
  • 1
    A bit late now, but maybe useful for someone else. I have found that KeyError:12345698765 (any 'random' assortment of numbers), is often due to referencing a Pyomo object that doesn't exist. This can occur if you are deleting or changing part of the model after it has initially been built. – Michael May 12 '22 at 03:13

0 Answers0