Beispiel #1
0
        def set_cf_max(str_hy=None, reset=False, excl_pp=[]):
            ''' Select maximum capacity factors for selected year. '''

            if str_hy == None:
                reset = True
                str_hy = ''

            slct_col = 'cf_max' + (str_hy if not reset else '')
            msg = ('Resetting cf_max to base year values.' if reset else
                   'Setting cf_max to values' + str_hy.replace('_', ' '))

            if 'cf_max' in self.ml.m.dict_monthly_factors.keys():
                df_mt = self.ml.m.dict_monthly_factors['cf_max']
                df = cols2tuplelist(self.ml.m.df_plant_encar[[
                    'pp_id', 'ca_id'
                ]].loc[self.ml.m.df_plant_encar.pp_id.isin(
                    self.ml.m.setlst['pp'])],
                                    df_mt['mt_id'],
                                    return_df=True)
                df = df.join(self.ml.m.df_plant_encar.set_index(
                    ['pp_id', 'ca_id'])[slct_col],
                             on=['pp_id', 'ca_id']).fillna(0)
                df = df.join(df_mt.set_index(
                    ['mt_id', 'ca_id',
                     'pp_id'])['mt_fact' + (str_hy if not reset else '')],
                             on=['mt_id', 'pp_id', 'ca_id']).fillna(1)
                df['value'] = df[slct_col] * df['mt_fact' +
                                                (str_hy if not reset else '')]
                dct_cf_max = df.set_index(['mt_id', 'pp_id',
                                           'ca_id'])['value'].to_dict()

            else:

                dct_cf_max = (
                    self.ml.m.df_plant_encar.
                    loc[-self.ml.m.df_plant_encar.pp_id.isin(excl_pp)].loc[
                        self.ml.m.df_plant_encar.pp_id.isin(
                            self.ml.m.setlst['pp'])].set_index(
                                ['pp_id', 'ca_id'])[slct_col].to_dict())

            print(msg)
            for kk, vv in dct_cf_max.items():
                self.ml.m.cf_max[kk] = vv
Beispiel #2
0
        def set_co2_price(str_hy=None, reset=False):
            ''' Select CO2 price for selected year. '''

            if str_hy == None:
                reset = True
                str_hy = ''

            slct_col = 'price_co2' + (str_hy if not reset else '')
            msg = ('Resetting price_co2 to base year values.' if reset else
                   'Setting price_co2 to values' + str_hy.replace('_', ' '))

            if 'price_co2' in self.ml.m.dict_monthly_factors.keys():
                df_mt = self.ml.m.dict_monthly_factors['price_co2']
                df = cols2tuplelist(self.ml.m.df_def_node[[
                    'nd_id'
                ]].loc[self.ml.m.df_def_node.nd_id.isin(
                    self.ml.m.slct_node_id)],
                                    df_mt['mt_id'],
                                    return_df=True)
                df = df.join(self.ml.m.df_def_node.set_index(['nd_id'
                                                              ])[slct_col],
                             on=['nd_id']).fillna(0)
                df = df.join(df_mt.set_index(
                    ['mt_id',
                     'nd_id'])['mt_fact' + (str_hy if not reset else '')],
                             on=['mt_id', 'nd_id']).fillna(1)
                df['value'] = df[slct_col] * df['mt_fact' +
                                                (str_hy if not reset else '')]
                dct_price_co2 = df.set_index(['mt_id',
                                              'nd_id'])['value'].to_dict()

            else:

                dct_price_co2 = (self.ml.m.df_def_node.set_index(
                    ['nd_id'])[slct_col].to_dict())

            print(msg)
            for kk, vv in dct_price_co2.items():
                self.ml.m.price_co2[kk] = vv
Beispiel #3
0
        def set_fuel_prices(str_hy=None, reset=False):
            ''' Select fuel price values for selected year. '''

            if str_hy == None:
                reset = True
                str_hy = ''

            slct_col = 'vc_fl' + (str_hy if not reset else '')
            msg = ('Resetting vc_fl to base year values' if reset else
                   'Setting vc_fl to values' + str_hy.replace('_', ' '))
            msg += ' from column {}'.format(slct_col)

            if 'vc_fl' in self.ml.m.dict_monthly_factors.keys():
                df_mt = self.ml.m.dict_monthly_factors['vc_fl']
                df = cols2tuplelist(
                    self.ml.m.df_fuel_node_encar[['fl_id', 'nd_id']],
                    df_mt['mt_id'],
                    return_df=True)
                df = df.join(self.ml.m.df_fuel_node_encar.set_index(
                    ['fl_id', 'nd_id'])[slct_col],
                             on=['fl_id', 'nd_id']).fillna(0)
                df = df.join(df_mt.set_index(
                    ['mt_id', 'fl_id',
                     'nd_id'])['mt_fact' + (str_hy if not reset else '')],
                             on=['mt_id', 'fl_id', 'nd_id']).fillna(1)
                df['value'] = df[slct_col] * df['mt_fact' +
                                                (str_hy if not reset else '')]
                dct_prices = df.set_index(['mt_id', 'fl_id',
                                           'nd_id'])['value'].to_dict()

            else:
                dct_prices = (self.ml.m.df_fuel_node_encar.set_index(
                    ['fl_id', 'nd_id'])[slct_col].to_dict())

            print(msg)
            for kk, vv in dct_prices.items():
                self.ml.m.vc_fl[kk] = vv
Beispiel #4
0
    def _init_tmsy_sets(self):
        '''

        The plant ids and the time slots are connected
        through the node-specific time resolution.
        '''

        self.tm = po.Set(initialize=self.df_tm_soy.tm_id.unique(),
                         ordered=True)

        list_tmsy = cols2tuplelist(self.df_tm_soy[['tm_id', 'sy']])
        self.tmsy = po.Set(within=self.tm * self.sy,
                           initialize=list_tmsy,
                           ordered=True)

        # only constructed if self.mt exists
        self.tmsy_mt = (po.Set(within=self.tmsy * self.mt,
                               initialize=cols2tuplelist(self.df_tm_soy[[
                                   'tm_id', 'sy', 'mt_id'
                               ]])) if not self.mt is None else None)

        df = pd.merge(self.df_def_node,
                      self.df_node_encar,
                      on='nd_id',
                      how='outer')[['nd_id', 'ca_id']]
        df = df.loc[~df.ca_id.isna()].drop_duplicates()
        df['tm_id'] = df.nd_id.replace(self.dict_nd_tm_id)
        cols = ['sy', 'nd_id', 'ca_id']
        list_syndca = pd.merge(self.df_tm_soy[['tm_id', 'sy']],
                               df,
                               on='tm_id',
                               how='outer')[cols]

        self.sy_ndca = po.Set(within=self.sy * self.ndca,
                              ordered=True,
                              initialize=cols2tuplelist(list_syndca))

        mask_pp = self.df_plant_encar.pp_id.isin(self.setlst['ppall'])
        df = self.df_plant_encar.loc[mask_pp, ['pp_id', 'ca_id']].copy()
        df['tm_id'] = (df.pp_id.replace(self.mps.dict_plant_2_node_id).replace(
            self.dict_nd_tm_id))
        cols = ['sy', 'pp_id', 'ca_id']
        list_syppca = pd.merge(self.df_tm_soy[['sy', 'tm_id']],
                               df,
                               on='tm_id',
                               how='outer')[cols]

        list_syppca = list_syppca.loc[~(list_syppca.pp_id.isna()
                                        | list_syppca.ca_id.isna())]

        list_syppca = cols2tuplelist(list_syppca)

        for slct_set in [
                'ppall', 'rp', 'st', 'hyrs', 'pr', 'pp', 'chp', 'ror', 'lin'
        ]:

            set_name = 'sy_%s_ca' % slct_set
            within = self.sy * getattr(self, slct_set) * self.ca

            if slct_set in self.setlst:

                logger.info('Defining set ' + set_name)

                set_pp = set(self.setlst[slct_set])

                setattr(
                    self, set_name,
                    po.Set(within=within,
                           ordered=True,
                           initialize=[
                               row for row in list_syppca if row[1] in set_pp
                           ]))
            else:
                setattr(self, set_name, po.Set(within=within, initialize=[]))
Beispiel #5
0
    def define_sets(self):
        r'''
        Add all required sets to the model.

        Adds sets as defined by

        * the ``setlst`` dictionary initialized in the :func:`get_setlst`
          method
        * the DataFrame attributes of the :class:`ModelBase` class for more
          complex derived sets

        %s


        '''

        self.nd = po.Set(initialize=self.setlst['nd'])
        self.ca = po.Set(initialize=self.setlst['ca'])
        self.fl = po.Set(initialize=self.setlst['fl'])
        self.pf = po.Set(initialize=self.setlst['pf'])

        df_ndca = self.df_def_plant[['pp_id', 'nd_id']].set_index('pp_id')
        df_ndca = self.df_plant_encar[['pp_id', 'ca_id']].join(df_ndca,
                                                               on='pp_id')
        df_ndca = df_ndca[['pp_id', 'nd_id', 'ca_id']]

        slct_cols = ['pp_id', 'ca_id']

        for iset in self.slct_sets:

            logger.info('Defining basic sets for {}'.format(iset))
            ''' SUB SETS PP'''
            setattr(
                self, iset,
                po.Set(within=(None if iset == 'ppall' else self.ppall),
                       initialize=self.setlst[iset])
                if iset in self.setlst.keys() else po.Set(within=self.ppall,
                                                          initialize=[]))
            ''' SETS PP x ENCAR '''
            _df = self.df_plant_encar.copy()
            _df = _df.loc[_df['pp_id'].isin(getattr(self, iset))]
            setattr(
                self, iset + '_ca',
                po.Set(within=getattr(self, iset) * self.ca,
                       initialize=cols2tuplelist(_df[slct_cols])))
            ''' SETS PP x ND x ENCAR '''
            _df = df_ndca.copy()
            _df = _df.loc[df_ndca['pp_id'].isin(self.setlst[iset] if iset in
                                                self.setlst.keys() else [])]
            setattr(
                self, iset + '_ndca',
                po.Set(within=getattr(self, iset) * self.nd * self.ca,
                       initialize=cols2tuplelist(_df)))

        # no scf fuels in the _cafl and _ndcafl
        # These are used to calculate fuel costs, that's why we don't want
        # the generated fuels in there.
        df_0 = self.df_def_plant[['pp_id', 'nd_id', 'fl_id']]
        df_0 = df_0.set_index('pp_id')
        df_0 = self.df_plant_encar[['pp_id', 'ca_id']].join(df_0, on='pp_id')
        df_0 = df_0.loc[df_0.fl_id.isin(self.setlst['fl'])]

        list_sets = ['ppall', 'hyrs', 'pp', 'chp', 'ror', 'st', 'lin']
        #        list_sets = [st for st in list_sets if st in self.setlst.keys()]

        for iset in list_sets:

            if iset in self.setlst:
                cols_ppcafl = ['pp_id', 'ca_id', 'fl_id']
                df = df_0.loc[df_0['pp_id'].isin(self.setlst[iset]),
                              cols_ppcafl]
                new_set = po.Set(within=getattr(self, iset) * self.ca *
                                 self.fl,
                                 initialize=cols2tuplelist(df))
                setattr(self, iset + '_cafl', new_set)

                slct_cols_ppndcafl = ['pp_id', 'nd_id', 'ca_id', 'fl_id']
                df = df_0.loc[df_0.pp_id.isin(self.setlst[iset]),
                              slct_cols_ppndcafl]

                setattr(
                    self, iset + '_ndcafl',
                    po.Set(within=(getattr(self, iset) * self.nd * self.ca *
                                   self.fl),
                           initialize=cols2tuplelist(df)))
            else:

                new_set_cafl = po.Set(within=getattr(self, iset) * self.ca *
                                      self.fl,
                                      initialize=[])
                setattr(self, iset + '_cafl', new_set_cafl)

                new_set_ndcafl = po.Set(within=getattr(self, iset) * self.nd *
                                        self.ca * self.fl,
                                        initialize=[])
                setattr(self, iset + '_ndcafl', new_set_ndcafl)

        # plants selling fuels ... only ppall, therefore outside the loop
        lst = cols2tuplelist(df.loc[df.pp_id.isin(
            self.setlst['sll'] if 'sll' in self.setlst else [])])
        setattr(self, 'pp_ndcafl_sll',
                po.Set(within=self.pp_ndcafl, initialize=lst))

        # temporal
        self.sy = po.Set(initialize=list(self.df_tm_soy.sy.unique()),
                         ordered=True)

        self.sy_hydbc = (po.Set(within=self.sy,
                                initialize=set(self.df_plant_month.sy))
                         if not self.df_plant_month is None else None)

        self.mt = (po.Set(initialize=list(self.df_def_month['mt_id']))
                   if not self.df_def_month is None else None)
        self.wk = (po.Set(initialize=list(self.df_def_week['wk_id']))
                   if not self.df_def_week is None else None)

        # pp_cacafcl; used to account for conversions of ca in the supply rule
        if 'fl_id' in self.df_def_encar:
            df_cafl = self.df_def_encar.set_index('fl_id')['ca_id']
            df_cafl = df_cafl.rename('ca_fl_id')
            df_ppca = self.df_plant_encar.set_index('pp_id')['ca_id']
            df = (self.df_def_plant.join(df_ppca, on='pp_id').join(df_cafl,
                                                                   on='fl_id'))
            df = df.loc[-df.ca_fl_id.isnull()
                        & -df.ca_id.isnull(),
                        ['pp_id', 'nd_id', 'ca_id', 'ca_fl_id']]
            self.pp_ndcaca = po.Set(within=self.pp_ndca * self.ca,
                                    initialize=cols2tuplelist(df))
        else:
            self.pp_ndcaca = None

        # inter-node connections
        if not self.df_node_connect is None and not self.df_node_connect.empty:
            df = self.df_node_connect[['nd_id', 'nd_2_id', 'ca_id']]
            self.ndcnn = po.Set(within=self.nd * self.nd * self.ca,
                                initialize=cols2tuplelist(df),
                                ordered=True)

            df = self.df_symin_ndcnn[['symin', 'nd_id', 'nd_2_id', 'ca_id']]
            self.symin_ndcnn = po.Set(within=self.sy * self.nd * self.nd *
                                      self.ca,
                                      initialize=cols2tuplelist(df),
                                      ordered=True)
        else:
            self.ndcnn = po.Set(within=self.nd * self.nd * self.ca)
            self.symin_ndcnn = po.Set(within=self.sy * self.nd * self.nd *
                                      self.ca)

        # ndca for electricity only; mainly used for flexible demand;
        # then again: why would only EL have flexible demand?
        df = pd.concat([
            pd.Series(self.slct_node_id, name='nd_id'),
            pd.Series(
                np.ones(len(self.slct_node_id)) * self.mps.dict_ca_id['EL'],
                name='ca_id')
        ],
                       axis=1)
        self.ndca_EL = po.Set(within=self.nd * self.ca,
                              initialize=cols2tuplelist(df),
                              ordered=True)

        # general ndca
        df = self.df_node_encar[['nd_id', 'ca_id']].drop_duplicates()
        self.ndca = po.Set(within=self.nd * self.ca,
                           initialize=cols2tuplelist(df),
                           ordered=True)

        # general ndcafl
        if not self.df_fuel_node_encar is None:
            df = self.df_fuel_node_encar[['nd_id', 'ca_id', 'fl_id']]
            self.ndcafl = po.Set(within=self.nd * self.ca * self.fl,
                                 initialize=cols2tuplelist(df),
                                 ordered=True)
        else:
            self.ndcafl = None

        # fuels with energy constraints
        if 'is_constrained' in self.df_def_fuel:
            lst = self.df_def_fuel.loc[self.df_def_fuel.is_constrained == 1,
                                       'fl_id'].tolist()
            self.fl_erg = po.Set(within=self.fl, initialize=lst, ordered=True)
        else:
            self.fl_erg = po.Set(within=self.fl, initialize=[])

        # set pf_id for profiles
        for pf_set in ['dmnd_pf', 'supply_pf', 'pricesll_pf', 'pricebuy_pf']:
            setattr(
                self, pf_set,
                po.Set(within=self.pf,
                       initialize=self.setlst[pf_set],
                       ordered=True))

        self._init_tmsy_sets()