Search code examples
pyomoimport-from-excel

How to import a parameter with 3 indizes with Pyomo DataPortal? (Abstract Model, Excel Import)


I have a problem. I want to describe an abstract optimization model for general production planning. I have a huge excel data-frame with lots of values for parameters. I want to import them, but there is a constructing error for the parameters with 3 indices. I don't understand the failure, because the preview for the values are perfect.

FYI: if i import parameters with one or two indices, then there are no error messages. And if i initialize the values for parameters (also with 3 indices) in my jupyter notebook there are also no errors. But my wish is, that the data-frame and the jupyter notebook are separated.

Here's an abridged fictional example of my problem:

m.K = Set(initialize=['Golf','eGolf'], doc='Produkte K')
m.T = Set(initialize=['1','2','3','4','5','6','7','8','9','10','11','12'], doc='Perioden T')
m.J = Set(initialize=['Seg1','Seg2'], doc='Produktionssegmente J')
m.W = Set(initialize=['WOB','Z','DD'], doc='Werke W')
m.bc      = Param(m.W, m.K, doc='var. Fremdbezugskosten im Werk w für das Produkt k')
m.bmax    = Param(m.W, m.K, m.T, doc='max. verfügbare Zusatzkapazität im Werk w für Produkt k in Periode t')
m.cmax    = Param(m.W, m.J, m.T, doc='verfügbare Normalkapazität im Werk w für Segment J in Periode t')
m.d       = Param(m.W, m.K, m.T, doc='Bedarf im Werkw für Produkt k in Periode t') 
...

Import the values for parameters:

...
data.load(filename='Dataframe.xlsx',range='A8:D80', param=m.bmax)

data.load(filename='Dataframe.xlsx',range='F8:I80', param='cmax', index=('W','K','T')) 
...

error:

Failed to set value for param=bmax, index=('WOB', 'Golf', 1.0), value=0.0.
    source error message="Index '('WOB', 'Golf', 1.0)' is not valid for indexed component 'bmax'"

This error raise up for every parameter with 3 indices. But if I initialize this form direct in jupyter notebook the model will run. I don't understand the logic :(

I am really thankful, if you have a solution for my problem. Best greetings PE


Solution

  • welcome to the site.

    I believe your problem in the above model is that you are setting up your set m.T as strings for members instead of integers.

    Here is an example of what I think is going wrong, using a ConcreteModel as an example...

    from pyomo.environ import *
    
    m = ConcreteModel()
    
    m.I = Set(initialize=[1,'5',10])   # mixed type set for demo only.
    
    m.c = Param(m.I, within=NonNegativeReals, mutable=True)
    
    m.c[1] = 2.4   # OK
    
    m.c[5] = 3.2   # FAILS  
    
    m.pprint()
    

    If you are going to declare your sets without loading them, I would declare the set as such:

    m.T = Set(initialize=range(1, 13), doc='Perioden T')