I got a pyomo parameter, which I want to initialize with the values of a python dictionary.
dictionary(example) looks like following:
dict
{('A', 'B', 'x', 'y'): 100,
('A', 'C', 'x', 'y'): 10,
('D', 'E', 'x', 'y'): 20,
('D', 'A', 'x', 'y'): 1}
and my parameter is indexed by A
, B
, C
and D
.
what I wanted to achieve is that to create a parameter, initialized with values as follows: m.param[A] = 110
, m.param[B] = 0
, m.param[C] = 0
and m.param[D] = 21
m.param= pyomo.Param(
m.index, # this has indexes {A,B,C,D}
initialize= #sum???
mutable=True)
PS: I am not looking for creating m.param
with initialize=0
, and then setting the values with a for-loop
. Rather using the initialize=some_command
to set the right values to right indexes.
Something like this(it is not working and I use pandas series, rather than dict. But to get to the point):
m.param = pyomo.Param(
m.index,
initialize=sum(dict_df.loc[m.index]),
mutable=True)
initialize
takes a function with signature (parent_block, *indices)
.
That is, for m.param = Param(m.index, initialize=init_func)
, you would want to have:
mydict = {
('A', 'B', 'x', 'y'): 100,
('A', 'C', 'x', 'y'): 10,
('D', 'E', 'x', 'y'): 20,
('D', 'A', 'x', 'y'): 1}
def init_func(m, idx):
return sum(val for tup, val in mydict.items() if tup[0] == idx)