-1

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)
oakca
  • 1,408
  • 1
  • 18
  • 40
  • You want to translate m.param = {'A': 110, 'C': 0, 'B': 0, 'D': 21} into "pyomo"? I would suggest plugging that into initialize, but I'm not sure what pyomo is. – Kenny Ostrom Aug 04 '18 at 15:25
  • I found something called "www.pyomo.org" with some documentation and examples. This pyomo example shows initialize http://nbviewer.jupyter.org/github/Pyomo/PyomoGallery/blob/master/transport/transport.ipynb You can set with a dict, as I speculated earlier; however, whatever you're doing with x, y might require initialize to be a function which takes (model, x, y) ... or something like that. – Kenny Ostrom Aug 04 '18 at 15:33
  • I forgot the sum. You can do that in the function you pass to initialize. – Kenny Ostrom Aug 04 '18 at 17:28
  • @KennyOstrom no, if I wanted to set `{'A': 110, 'C': 0, 'B': 0, 'D': 21}` to some parameter obj, it would be easy. But what I am looking for is some method(if there is one) to set this parameter as init: which would eventually do the following: sum values of this dict, if their first index is equal of this parameters index. – oakca Aug 04 '18 at 17:30

1 Answers1

0

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)
Qi Chen
  • 1,648
  • 1
  • 10
  • 19