Reputation: 177
I'm trying to create a Pyomo index that would apply to both the column names and the row index of a pandas dataframe.
The reason being that I have constrains on each column but would also like to subset the df rows into 4 parts, with various constraints on each part. I can get this formulation to work by creating 4 seperate Params indexed only on the matrix column names, but this is inflexible if the subset needs to be a different number of parts.
My code attempting to subset the x and y of a matrix is below:
model=ConcreteModel()
model.N = Set(initialize=col_names)
model.I = Set(initialize=full_df.index)
model.M = Param(model.I, model.N, initialize=full_df[col_names].values)
Any help would be appreciated.
Upvotes: 0
Views: 769
Reputation: 1068
Param can support dictionaries, and dictionaries support tuple keys.
Create a set of (i,n) tuples, where i is element of I and n is element of N. This set, instead of containing numbers or string, will contain tuples.
model.your_set = Set(initialize=list_of_tuples)
Make sure you have a dictionary or data object representation that allows one and only one numeric value per tuple (i.e. the content of your matrix). Let's unoriginally call it d
.
The parameter must be declared this way:
model.M = Param(model.your_set, initialize=d)
That way, your tuples will serve as the keys of the dict.
Upvotes: 1