Q:

Pyomo: Initialize variable with two indices

I am looking for a way to initialize a variable in pyomo which has two indices. My first idea was to use a nested dictionary, which has the first index in the upper level of the dictionary and the second index in the lower level of the dictionary.
Using this approach, I get following error:
ERROR: Constructing component 'p_norm_generator' from data=None failed:
KeyError: "Index 'PV' is not valid for indexed component
'p_norm_generator'"
Does someone have an idea how to properly implement the initialization?
The code looks as following:
#Electricity generation
p_nom_dict['PV'] = p_nom_pv_dict
p_nom_dict['Onshore'] = p_nom_wind_onshore_dict

generators = ['PV', 'Onshore']
model.GENERATOR = generators
model.p_norm_generator = Param(model.GENERATOR, model.TIME, initialize=p_nom_dict)

A:

The data source for things with multiple indices needs to be indexed by tuples with the appropriate indices in them.  So in your case above, your source dictionary needs to have tuples of (model.GENERATOR, model.TIME) as keys.
Here is a toy example:
from pyomo.environ import *

sources = ['sun', 'wind', 'voodoo']
time_periods = list(range(4))

data = {    ('sun', 2):     24,
            ('wind', 3):    60,
            ('voodoo', 1):  12}

m = ConcreteModel()
# sets
m.sources = Set(initialize=sources)
m.time = Set(initialize=time_periods)

# param
m.energy = Param(m.sources, m.time, initialize=data)

# check it....
m.pprint()

yields:
3 Set Declarations
    energy_index : Dim=0, Dimen=2, Size=12, Domain=None, Ordered=False, Bounds=None
        Virtual
    sources : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=None
        ['sun', 'voodoo', 'wind']
    time : Dim=0, Dimen=1, Size=4, Domain=None, Ordered=False, Bounds=(0, 3)
        [0, 1, 2, 3]

1 Param Declarations
    energy : Size=3, Index=energy_index, Domain=Any, Default=None, Mutable=False
        Key           : Value
           ('sun', 2) :    24
        ('voodoo', 1) :    12
          ('wind', 3) :    60

4 Declarations: sources time energy_index energy*

