Search code examples
pyomodata-import

Pyomo: What dataformats can I pass into .create_instance()


I want to import parameters from 1 excel sheet, (in the future also 1 csv file) and some parameters that I want to set in the code. I am importing this values using pandas. But than I don´t know how to pass them to the instance. I tried various options but I am only guessing... I saw variable examples but I am not able to understand and adopt them.

import pandas as pd   
from pyomo.environ import *
from pyomo.opt import SolverFactory
from pyomo.core import Var

infinity = float('inf')

opt = SolverFactory('glpk') # GNU Linear Programming Kit for solving large-scale linear programming (LP), mixed integer programming (MIP), and other 


df1 = pd.read_excel("datosPvaD.xlsx")
df2 = pd.read_excel("otrosDatos.xlsx")
#demand = consumption['Consumo (Wh)']
#demand.index += 1
#demand_list = demand.tolist()
data1 = df1.to_dict()
#data2 = df2.to_dict(orient='index')
#data2 = df2.to_dict()

"""
# is the same as otros datos
data2 = {None: {
     'pRdImp': {None: 0.35},
     'pRdExp': {None: 0.1},
     'rend':  {None: 0.9},
     'CAB': {None: 0.082},
     'CABasic': {None: 0.082},
     'CAPV': {None: 0.224},
     'CI': {None: 0.06849},
     'M': {None: 1000},
     'dt': {None: 1},
}}
"""


data2 = {'pRdImp': 0.35,
     'pRdExp': 0.1,
     'rend': 0.9,
     'CAB': 0.08,
     'CABasic': 0.082,
     'CAPV':  0.224,
     'CI':  0.06849,
     'M':  1000,
     'dt':  1
}

#z = {**x, **y}
data = {**data1, **data2}

#from Fotovoltaica_V2_csvread import model # import model
from Fotovoltaica_V1 import model # import model

#instance = model.create_instance('Fotovoltaica_V2.dat')
#instance = model.create_instance(data)
instance = model.create_instance(data1,'Fotovoltaica_V2.dat')

Solution

  • It's hard to tell without seeing your entire model, but the section you have commented out for data2 should work:

    data2 = {
        None:{
            'param':{None:val},
            ...
        }
    }
    

    I'm assuming that all of your parameters are not indexed. If they are indexed, then you would need something as follows:

    model = AbstractModel()
    model.t = Set()
    model.thing = Param(t)
    
    input_data = {
        None:{
            't':{None:[1, 2, 3]},
            'thing':{1:100, 2:200, 3:300}
        }
    }
    

    You would then create a model instance by calling model.create_instance(input_data)

    You can import the data from a csv into python as you normally would with pandas, but then there will be a little rework you need to do to get it in the correct pyomo format