Search code examples
pythonoptimizationvalueerrorpyomo

Pyomo ValueError: Error retrieving component


My code is the following:

from coopr.pyomo import *
import numpy as np
from scipy.optimize import minimize
import math

model = ConcreteModel() 

model.days = RangeSet(1, 31)  #model.time)

T = model.days

M_b1_O_stored_T = Var(T,bounds=(0, None))



def obj_rule(model):
  return sum( M_b1_O_stored_T[i] for i in model.days )


model.funcobj = Objective( rule =obj_rule , sense=maximize)

It shows the following error: ValueError: Error retrieving component IndexedVar[1]: The component has not been constructed. Do anyone can please help me on this please? The constraints do not show problem, but the objective function is showing...


Solution

  • Welcome to the site...

    You neglected to put your variable "into the model" with the model. prefix. Note my fix below in both the declaration and in your objective function.

    from pyomo.environ import *
    
    # from coopr.pyomo import *
    # import numpy as np
    # from scipy.optimize import minimize
    # import math
    
    model = ConcreteModel() 
    
    model.days = RangeSet(1, 31)  #model.time)
    
    # T = model.days
    
    model.M_b1_O_stored_T = Var(model.days,bounds=(0, None))
    
    
    def obj_rule(model):
      return sum( model.M_b1_O_stored_T[i] for i in model.days )
    
    
    model.funcobj = Objective( rule =obj_rule , sense=maximize)