dataframeparameterspyomoindexed

Setting a default value to an indexed pyomo parameter


I got a dataframe which looks like following:

>data
         x     y
Name                              
A        NaN   0
B        65,2  NaN
C        NaN   100

I create a pyomo Set-Object m.index, for indexing a mutable pyomo Parameter-Object m.parameter_y.

m.index:

# code
m.index = pyomo.Set(
    initialize=data.index.get_level_values(0).unique(),
    doc='Index Set')

# output
(Pdb) m.index.pprint()
index : Index Set
    Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Mutable=True, Bounds=None
    ['A', 'B', 'C']

m.parameter_y:

# code
m.parameter_y = pyomo.Param(
    m.index,
    default=data['y'].values,
    mutable=True
    doc='Par y')

# output
(Pdb) m.parameter_y.pprint()
parameter_y : Par y
    Size=3, Index=index, Domain=Any, Default=None, Mutable=True
    Key : Value
    A   : [  0   nan   100. ]
    B   : [  0   nan   100. ]
    C   : [  0   nan   100. ]

As you can see using default=df['y'].values, gets all the values of the column y as a value of the parameter.

How would I set the values of a mutable indexed pyomo Parameter-Object if I wanted to expect following output?

(Pdb) m.parameter_y.pprint()
parameter_y : Par y
    Size=3, Index=index, Domain=Any, Default=None, Mutable=True
    Key : Value
    A   : [  0  ]
    B   : [ nan ]
    C   : [ 100 ]

PS: Keep in mind this isn't the actual dataframe, index-set, or the parameter. So Answers like adding values somehow manually won't work in my case. What would work, some kind of a better value-getting-function from a dataframe, so that, I can set these values on that parameter as default.


Solution

  • Assuming that you want the parameter values to be singleton values and not a list, the following should work:

    m.parameter_y = pyomo.Param(
        m.index,
        default=data['y'].to_dict(),
        mutable=True
        doc='Par y')
    

    Is there a reason you are using the default argument instead of initialize?