Search code examples
pythonpyomo

How to write Pyomo variables with for loops


I'm trying to write a Pyomo model where I have a set of variables, indexed by a j in {1,2,...,N} and, for every j, a i in {1,...,N_j}.

My code right now is:

n0=28
n1=8
n2=8
n3=8
n4=10

N=[n0, n1, n2, n3, n4]
rN=range(5)

model = ConcreteModel()

model.J = [RangeSet(1,N[i]) for i in rN]
model.X = [Var(model.J[i], within=NonNegativeReals) for i in rN]

When I try to access a variable, I get the error:

>>>model.X[0][0]
Traceback (most recent call last):

  File "<ipython-input-177-297a76d94388>", line 1, in <module>
    model.X[0][0]

  File "/path/anaconda3/lib/python3.7/site- 
packages/pyomo/core/base/indexed_component.py", line 374, in __getitem__
    self._not_constructed_error(index)

  File "/path/anaconda3/lib/python3.7/site- 
   packages/pyomo/core/base/indexed_component.py", line 520, in _not_constructed_error
    "not been constructed." % (self.name, idx_str,))

ValueError: Error retrieving component IndexedVar[1]: The component has not been constructed.

I think that the error may be that I can't write variables in a list, but I can't think about any other solution.


Solution

  • You have 2 choices.... either construct a set manually with the indices that you want (my example below) and use that throughout the model, or you can can (carefully) only create/call legal indices when you use I and J by looping or making legal indices on the fly.

    # ragged set
    from pyomo.environ import *
    
    model = ConcreteModel()
    
    # SETS
    model.J = Set(initialize=range(3))
    model.I = Set(initialize=range(3))
    
    # construct the ragged set
    ij = [(i, j) for j in range(3) for i in range(j + 1)]
    model.IJ = Set(within=model.I * model.J, initialize=ij)
    
    # VARIABLE
    model.x = Var(model.IJ, domain=NonNegativeReals)
    
    model.pprint()
    

    Yields:

    4 Set Declarations
        I : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(0, 2)
            [0, 1, 2]
        IJ : Dim=0, Dimen=2, Size=6, Domain=IJ_domain, Ordered=False, Bounds=None
            [(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
        IJ_domain : Dim=0, Dimen=2, Size=9, Domain=None, Ordered=False, Bounds=None
            Virtual
        J : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(0, 2)
            [0, 1, 2]
    
    1 Var Declarations
        x : Size=6, Index=IJ
            Key    : Lower : Value : Upper : Fixed : Stale : Domain
            (0, 0) :     0 :  None :  None : False :  True : NonNegativeReals
            (0, 1) :     0 :  None :  None : False :  True : NonNegativeReals
            (0, 2) :     0 :  None :  None : False :  True : NonNegativeReals
            (1, 1) :     0 :  None :  None : False :  True : NonNegativeReals
            (1, 2) :     0 :  None :  None : False :  True : NonNegativeReals
            (2, 2) :     0 :  None :  None : False :  True : NonNegativeReals
    
    5 Declarations: J I IJ_domain IJ x
    [Finished in 2.5s]