Pyomo parmest KeyError - PullRequest
       29

Pyomo parmest KeyError

0 голосов
/ 07 февраля 2020

Я хочу использовать parmest вместе с этой моделью для экономичного c моделирования электростанции. Я могу заполнить и запустить модель. Но я продолжаю сталкиваться с тем же KeyError в целевой функции при использовании оценки параметров. Так что я уверен, что это как-то связано с parmest, но я не могу найти никакой подсказки.

# Imports
import pandas as pd
from pyomo.contrib.parmest import parmest
from pyomo.environ import ConcreteModel, Param, Var, NonNegativeReals, Boolean, value, maximize, Objective, Constraint, \
    TransformationFactory, Set
from pyomo.dae import DerivativeVar, ContinuousSet


def power_plant_model(data):
    # INITIALIZE CONCRETE MODEL
    model = ConcreteModel()

    # READ TIME AND PARAMETERS FROM DATA
    # Time index -> dt [h]

    model.t = Set(initialize=sorted(data['time']))
    model.time = ContinuousSet(bounds=(0, 7943), initialize=model.t)
    # Maximum Export Limit (MEL) [MW]
    model.export_limit = Param(model.time, default=data['exportlim'])
    # Wholesale electricity price [€/MWh]
    model.electricity_price = Param(model.time, default=data['eprice'])
    # Fuel price [€/MWh(fuel)]
    model.fuel_price = Param(model.time, default=data['fuelprice'])
    # Emissions price [€/tCO2]
    model.emission_price = Param(model.time, default=data['emission_price'])
    # Emission factor [tCO2/MWh(fuel)]
    model.emission_factor = Param(default=data['emission_factor'].iat[0])
    # Ramping up limits [MW/h]
    model.r_up = Param(default=data['rup'].iat[0])
    # Ramping down limits [MW/h]
    model.r_down = Param(default=data['rdown'].iat[0])

    # INITIALIZE ESTIMATION PARAMETERS
    # Thermal efficiency
    model.efficiency = Var(bounds=(0, 1), initialize=1)
    # Start-up costs [€]
    model.start_up_cost = Var(within=NonNegativeReals, bounds=(1, 100), initialize=1
    # Fixed O&M costs [€/h]
    model.fixed_operational_cost = Var(within=NonNegativeReals, bounds=(1, 15), initialize=1)
    # Variable O&M costs [€/MWh]
    model.variable_operational_cost = Var(within=NonNegativeReals, bounds=(1, 15), initialize=1)

    # DECLARING VARIABLES
    # decision variable Power plant output [MW]
    model.power = Var(model.time, domain=NonNegativeReals)
    # derivative Power [dMW/dt]
    model.dpowerdt = DerivativeVar(model.power, wrt=model.time)
    # Binary variable, one if power plant is on
    model.running = Var(model.time, domain=Boolean, initialize=0)
    # Binary variable, one if power plant has started
    model.starting = Var(model.time, domain=Boolean, initialize=0)

    # DECLARE CONSTRAINTS
    # Constraint 0 -> power plant output has to be lower than the export limit (for every time step)
...
    # Constraints 2-3 -> consider ramping limits
...
    # Constraints 4-6 running and starting
...
    # OBJECTIVE
    # declaring objective -> maximum profit [€]
    def obj_rule(m):
        return sum(m.power[t] *
                   (m.electricity_price[t]
                    - value(m.variable_operational_cost)
                    - (m.fuel_price[t] / (value(m.efficiency)))
                    - ((m.emission_factor * m.emission_price[t]) / value(m.efficiency)))
                   - m.running[t] * value(m.fixed_operational_cost)
                   - m.starting[t] * value(m.start_up_cost)
                   for t in m.time)

    model.obj = Objective(rule=obj_rule, sense=maximize)

    size = len(data['time']) - 1
    discretizer = TransformationFactory('dae.finite_difference')
    discretizer.apply_to(model, wrt=model.time, scheme='CENTRAL')

    return model


data = pd.read_excel('data_jan-nov19.xlsx')
m = power_plant_model(data)

print("----SOLVING----")

# Vars to estimate
theta_names = ['efficiency', 'start_up_cost', 'fixed_operational_cost', 'variable_operational_cost']


# Sum of squared error function
def sse_time_series(model, df):
    expr = 0
    for t in df['time']:
        expr = expr + ((float(df['hist'][t]) - model.power[t]) ** 2) * (1 / len(data['hist']))
    return expr


pest = parmest.Estimator(power_plant_model, data, theta_names, sse_time_series, diagnostic_mode=True)
obj, theta = pest.theta_est()

print("Object: ", obj)
print("Theta: ", theta)

Сообщение об ошибке:

ERROR: Rule failed when generating expression for objective obj: KeyError:
    7943
ERROR: Constructing component 'obj' from data=None failed: KeyError: 7943
ERROR: Failed to create model instance for scenario=Experiment0
...