如何将深度学习模型部署到MPC作为约束/方程?

huangapple go评论79阅读模式
英文:

How to deploy a deep learning model into MPC as a constraint/equation?

问题

我正在使用GEKKO解决MPC问题。我的任务是开发一个MPC控制器,生成室内设定点以节省电费,同时确保室内舒适度。目前,我有两个svr模型,一个用于房屋功率预测,一个用于室内温度预测。这两个svr模型都需要当前信息(如设定点(t)、太阳能(t)、室内温度(t)等)和上次的信息(如设定点(t-1)、室内温度(t-1))。所以我不确定如何将这两个模型实现为MPC约束。

以下是我的代码的一部分:

# ******************* MPC OPTIMIZER *******************
def Core2_0(time, S_p, R_p, Occup, GridRate, Spt0):

    m = GEKKO(remote=False)
    Np = 7

    # 用于优化的天气和占用预测数据
    df_data = pd.read_csv('RawData.csv')
    Solar_forecast = df_data.loc[:, ['Irradiance']].values.flatten()
    Tamb_forecast = df_data.loc[:, ['Temperature_C']].values.flatten()

    df = pd.DataFrame({'Solar': [Solar_forecast[time]],
                       'OutTemp': [Tamb_forecast[time]],
                       'Setpoint_previous': [S_p],
                       'RoomTemp_previous': [R_p],
                       'Setpoint': [Spt0]})

    m.time = np.linspace(0, Np - 1, Np)  # 预测时间范围

    Irr = m.Param(value=Solar_forecast[time:time + Np])
    TempAmb = m.Param(value=Tamb_forecast[time:time + Np])
    Occupancy = m.Param(value=Occup[time:time + Np])
    Price = m.Param(value=GridRate[time:time + Np])

    weight = np.zeros(Np)
    for j in range(Np):
        if Occup[time + j] == 1:
            weight[j] = 10

    M = m.Param(value=100)
    Ttar = m.Param(value=21)
    W = m.Param(value=weight)

    RoomTemp_previous = m.Var(value=R_p)
    Spt_previous = m.Var(value=S_p)

    Spt = m.MV(value=Spt0, lb=17, ub=27)
    Spt.STATUS = 1
    Spt.DCOST = 0.01
    Spt.DMAX = 1

    Pwr = m.CV(value=svr_power.predict(df[features_power]), lb=0)
    Trm = m.CV(value=svr_troom.predict(df[features_troom]))
    Pwr.STATUS = 0
    Trm.STATUS = 0
    m.options.CV_TYPE = 2

    Pwr.TR_INIT = 1
    Trm.TR_INIT = 1
    Pwr.TAU = 10
    Trm.TAU = 10

    pwr_in = np.array([Spt_previous, Spt, RoomTemp_previous], dtype=object)
    trm_in = np.array([RoomTemp_previous, Irr, Spt_previous, TempAmb, Spt], dtype=object)

    m.Equation(Trm == Gekko_SVR(svr_troom, m).predict(trm_in))
    m.Equation(Pwr == Gekko_SVR(svr_power, m).predict(pwr_in))

    m.delay(Trm, RoomTemp_previous, 1)
    m.delay(Spt, Spt_previous, 1)

    m.Obj(m.abs((Trm - Ttar)) ** 2 * W + (Pwr * Price) ** 2 * M)

    m.options.IMODE = 6
    m.solve(disp=False)

    return Spt.NEWVAL, Pwr.value, Trm.value, Spt.value, pwr_in

action, _, _, _, _ = Core2_0()

我将这两个模型放入了方程中,并使用m.delay()生成了先前的信息。运行结果看起来不错,但我不确定变量的定义是否合理。此外,我对GEKKO MPC中的滚动逻辑不是很清楚。是否有人可以帮助我检查这段代码?

英文:

I 'm working on solving MPC problem with GEKKO. My task is to develop a MPC controller generating indoor setpoint to saving power cost. At the same time, the indoor comfort level should be ensured. Currently, I have 2 svr models, one for house power forecasting, one for indoor temperature forecasting. Both svr model need current information (like setpoint(t), solar(t), room temp(t)...) and last time information(like setpoint(t-1), room temp(t-1)). So I'm confused how to implement these two model into MPC constraints.

Here is part of my code:

# ******************* MPC OPTIMIZER *******************
def Core2_0(time,S_p,R_p,Occup,GridRate,Spt0):
    
    m = GEKKO(remote = False)
    Np = 7
    
    # weather and occupancy forecast data for optimization
    df_data = pd.read_csv('RawData.csv')
    Solar_forecast = df_data.loc[:,['Irradiance']].values.flatten()
    # Solar_forecast = 100*Solar_forecast
    Tamb_forecast = df_data.loc[:,['Temperature_C']].values.flatten()
    
    df = pd.DataFrame({'Solar':[Solar_forecast[time]],
                        'OutTemp':[Tamb_forecast[time]],
                        'Setpoint_previous':[S_p],
                        'RoomTemp_previous':[R_p],
                        'Setpoint':[Spt0]})
    
    m.time = np.linspace(0,Np-1,Np) # prediction horizon
    
    
    Irr = m.Param(value = Solar_forecast[time:time+Np])
    TempAmb = m.Param(value = Tamb_forecast[time:time+Np])
    Occupancy = m.Param(value = Occup[time:time+Np])
    Price = m.Param(value = GridRate[time:time+Np])
    # previous ->datatype: variable
    # objfunc parameter
    weight = np.zeros(Np)
    for j in range (Np):
        if Occup[time+j] == 1:
            weight[j] = 10
    
    ## M for cost saving, W for comfort level
    M = m.Param(value = 100)
    Ttar = m.Param(value = 21)
    W = m.Param(value = weight)

    RoomTemp_previous = m.Var(value = R_p)
    # Pwr_previous = m.Var(value = P_p)
    Spt_previous = m.Var(value = S_p)
    
    # manipulated variable
    Spt = m.MV(value = Spt0, lb = 17,ub = 27)
    Spt.STATUS = 1 # allow optimizer to change
    Spt.DCOST = 0.01 # penalty to change the variable
    Spt.DMAX = 1 # maximum change for every time step
    
    # controlled variable
    Pwr = m.CV(value = svr_power.predict(df[features_power]), lb = 0)
    Trm = m.CV(value = svr_troom.predict(df[features_troom]))
    Pwr.STATUS = 0 # disable SP options; add SP to CV objective when equals 1
    Trm.STATUS = 0 # disable SP options
    m.options.CV_TYPE = 2 # squared error mode
#     Pwr.SP = 0 # reference
#     Trm.SP = 75
    Pwr.TR_INIT = 1 # trajectory
    Trm.TR_INIT = 1
    Pwr.TAU = 10 # time constant of trajectory
    Trm.TAU = 10
    
    
    # POWER input reshape->Setpoint_previous Setpoint RoomTemp_previous
    # TROOM input reshape->RoomTemp_previous Solar Setpoint_previous OutTemp Occupancy RoomTemp
    pwr_in = np.array([Spt_previous,Spt,RoomTemp_previous],dtype = object)
    trm_in = np.array([RoomTemp_previous,Irr,Spt_previous,TempAmb,Spt],dtype = object)
    
    # Equation
    m.Equation(Trm == Gekko_SVR(svr_troom,m).predict(trm_in))
    m.Equation(Pwr == Gekko_SVR(svr_power,m).predict(pwr_in))
    
    # if Pwr.value < 0:
    #     Pwr.value = 0
    
    # time delay, receding horizon, put predicted output into input of next receding prediction time window
    # m.delay(Pwr,Pwr_previous,1)
    m.delay(Trm,RoomTemp_previous,1)
    m.delay(Spt,Spt_previous,1)
    
    # Objective function

    m.Obj(m.abs((Trm-Ttar))**2*W + (Pwr*Price)**2*M)
    
    # mode and solve
    m.options.IMODE = 6 # MPC mode
    m.solve(disp = False)
    
    return Spt.NEWVAL,Pwr.value,Trm.value,Spt.value,pwr_in

action,_,_,_,_ = Core2_0()

I put these 2 models into the equation. And use m.delay() to generate the previous information.

The running result seems good but I'm not sure if the variable definition is reasonable. Also I'm not vary clear about the rolling logic in GEKKO MPC. Is there anyone can help me to check this code?

答案1

得分: 0

根据我所看到的和你所描述的,代码看起来很不错,假设svr模型是用正确的输入格式进行训练的。这是正确使用延迟函数的方式,以及访问先前时间步结果的最佳方法。延迟对象会创建来自先前时间步的值,因此确保先前的时间是您创建的模型所需的输入。如果您计划更改Np参数,请确保通过更改延迟的时间步数或对新的时间步数重新训练模型来进行调整。这是一个关于延迟函数如何使用的先前帖子:https://stackoverflow.com/questions/66126047/gekko-calculate-a-variable-that-is-a-function-of-the-previous-and-current-time

英文:

From what I can see and what you've described the code looks good, assuming the svr models were trained with the proper input format. That is the correct use of the delay function, and the best method of accessing the previous timesteps results. The delay object creates values from the previous timestep, so ensure that previous time is the desired input for the models that you created. If you plan on changing the Np parameter, make sure to account for this by either altering how many timesteps you delay or retraining a model on the new timestep. Here is a previous thread about the delay function and how to use it: https://stackoverflow.com/questions/66126047/gekko-calculate-a-variable-that-is-a-function-of-the-previous-and-current-time

huangapple
  • 本文由 发表于 2023年5月26日 09:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定