How to initialize an indexed Parameter with data from a dictionary

340 Views Asked by At

I'm working with two blocks, one to use pandas to import data from a .csv file and another one to use this information to construct variable values.

The first block is working fine, I'm able to construct a table with indexed values (in this case is Energy Tariff prices):

import pandas as pd

df_1 = pd.read_csv('time_tff_data.csv', sep=';', usecols=['time', 'tff'], index_col='time', header=0)

data_Tariff = {
    'tff':{'time': df_1['tff'].to_dict()} #Set of tariff prices
    }              

data = {None: dict(tariff=data_Tariff)}

The problem is that on the other block, the one that I need to use the data, I'm not able to initialize a parameter with the data within the dictionary. Altough I'm using Pyomo (for optimization) my question isn't about Pyomo itself, but about how to initialize a Parameter with the data stored in a dictionary (self.tff):

from pyomo.environ import *
from data import data_Tariff
from pyomo.environ import SimpleBlock

class tariff(SimpleBlock):
     
    def __init__(self, *args, **kwds):
        super().__init__(*args, **kwds)
        self.time = Set()
        
        self.Tmax = Param(self.time, doc='Maximum tariff price',  default=1.06, multable=True)
        self.Tmin = Param(self.time, doc='Minimum tariff price',  default=0.39, multable=True)
        self.tff = Param(self.time, doc='Set of tariff prices',  default=data_Tariff['tff'], mutable=True)
        self.Tc = Var(self.time, doc='Tariff priority index', initialize=0)
        
        def _Tc(m, t):
            if tff is not None:
                return (m.Tc[t] == (m.Tmax-m.tff[t])/(m.Tmax-m.Tmin) for t in m.time)
            return Constraint.Skip
    
        self.Tc = Constraint(self.time, rule=_Tc, doc='Tariff priority index')

My question is: how do I import tariff data "tff[t]" from the data block, since the set is idexed by time [t]?

1

There are 1 best solutions below

0
On

Couple quick observations...

First, you should be using the keyword initialize not default to initialize from a collection. Also, I can't see why you would make this mutable, so you might remove that. Try:

self.tff    = Param(self.time, doc='Set of tariff prices',  initialize=data_Tariff['tff'])

This assumes that data_Tariff[tff] returns a properly constructed dictionary that is indexed by self.time

Backing up, I see that you also need to initialize the self.time:

self.time = Set(initialize=data_Tariff[tff].keys())

your constraint... Your constraint appears incorrect. The for t in m.time part is taken care of when you call the rule with a set. It will make a constraint for each value of t. And the check for tff... Probably unnecessary, right? If it is necessary, you need to reference it as self.tff. So:

def _Tc(m, t):
    return m.Tc[t] == (m.Tmax-m.tff[t])/(m.Tmax-m.Tmin)

self.Tc = Constraint(self.time, rule=_Tc, doc='Tariff priority index')

Also, your Tmax and Tmin appear to be just constants (not indexed). If that is the case, you can simplify a little bit and just treat them as constants that are regular python variables and take them out of the model declaration, if desired.