pythonmatrixpyomoevaluate

Proper way to define a matrix using pyomo's sets


I have been given a pyomo deterministic optimization model which I have transformed in a stochastic one using stochastic colocation methods.

However, I am having issues to save the output. I have defined the number of stochastic nodes (number of values of the state variables)

m.particle_number = Param(initialize = count)
m.K = Set(initialize = range(1,value(m.particle_number)+1))  

After this, the model defines a series of lists of length m.n, number of points in which the model is evaluated. Here is an example.

position_phi     = list(range(0,value(m.n)+1))

I need to turn these 1D lists into m.K dimensions. I have tried to write

position_phi     = list(range(0,value(m.n)+1), range(0,value(m.K)+1))

instead. However, I get the following error:

TypeError: Cannot evaluate object with unknown type: SimpleSet

Can someone explain to me why I can't construct a matrix of size m.n*m.k?


Solution

  • I'm not completely clear what your model is attempting to do, but you can create a cross product of two sets in Pyomo by multiplying the two.

    import pyomo.environ as pe
    m = pe.ConcreteModel()
    m.K = pe.Set(initialize=[1, 2, 3])
    m.n = pe.Set(initialize=[4, 5, 6, 7])
    m.position_phi = m.K * m.n
    

    This will make the elements of m.position_phi be [(1, 4), (1, 5), ...]

    You can then use this set as follows:

    m.whatever = pe.Param(m.position_phi, intitialize={(1, 4):4, (3, 6):6}, default=0)
    

    And then when you call m.pprint() you'll see something like this:

    3 Set Declarations
        K : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(1, 3)
            [1, 2, 3]
        n : Dim=0, Dimen=1, Size=4, Domain=None, Ordered=False, Bounds=(4, 7)
            [4, 5, 6, 7]
        position_phi : Dim=0, Dimen=2, Size=12, Domain=None, Ordered=False, Bounds=None
            Virtual
    
    1 Param Declarations
        whatever : Size=12, Index=position_phi, Domain=Any, Default=0, Mutable=False
            Key    : Value
            (1, 4) :     4
            (3, 6) :     6
    
    4 Declarations: K n position_phi whatever
    

    Also, calling list() with more than 1 argument in Python should be throwing a TypeError.