GEKKO:不响应约束也不求解 obj 函数

2024-02-11

以下是与这个问题相关的内容:使用 Gekko 的 MPC 和 ARX 模型 https://stackoverflow.com/questions/63727310/mpc-with-arx-model-using-gekko/63731362#63731362..

我正在尝试用 15 分钟的数据来识别我的系统。我正在尝试在一天中每小时更新一次我的 MPC MV。这会影响我的控制器吗?

我运行了上一个问题中的更正代码,但它似乎没有维持约束或在一天中更改 MV。

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

m = GEKKO(remote = True)


#initialize variables

#Room Temprature:
T_external = [23,23,23,23,23.5,23.5,23.4,23.5,23.9,23.7,\
              23,23.9,23.9,23.4,23.9,24,23.6,23.7,23.8,\
              23,23,23,23,23]

# Temprature Lower Limit:
temp_low = 10*np.ones(24)

# Temprature Upper Limit:
temp_upper = 12*np.ones(24)

#Hourly Energy prices:
TOU_v = [39.09,34.93,38.39,40.46,40.57,43.93,25,11,9,24,51.28,45.22,45.72,\
            36,35.03,10,12,13,32.81,42.55,8,29.58,29.52,29.52]

###########################################
#System Identification:

#Time 
t = np.linspace(0,10,117)
#State of the Fridge
ud = np.append(np.zeros(78) ,np.ones(39),0)
#Temprature Data for 10 min 
y = [14.600000000000001,14.600000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
     14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
     14.700000000000001,14.700000000000001,14.700000000000001,14.8,14.8,14.8,14.8,14.8,14.8,14.8,14.8,\
    14.8,14.8,14.9,14.9,14.9,14.9,14.9,14.9,14.9,15,15,15,15,15,15,15,15,15,15,15,15,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,
    15,15,15,15,15,15,15,15,15,15,14.9,14.9,14.9,14.9,14.8,14.9,14.8,14.8,14.8,14.8,14.8,14.8,\
    14.8,14.700000000000001,14.8,14.700000000000001,14.700000000000001,14.700000000000001,\
    14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
    14.700000000000001,14.600000000000001,14.600000000000001,14.600000000000001,\
    14.600000000000001,14.600000000000001,14.60]

na = 1 # output coefficients
nb = 1 # input coefficients
print('Identification')
yp,p,K = m.sysid(t,ud,y,na,nb,objf=10000,scale=False,diaglevel=1)
#create control ARX model:

y = m.Array(m.CV,1)
uc = m.Array(m.MV,1)
m.arx(p,y,uc)

# rename CVs
T= y[0]

# rename MVs
u = uc[0]


###########################################

#Parameter
P = m.Param(value =100) #power
TL = m.Param(value=temp_low[0]) 
TH = m.Param(value=temp_upper[0])
c = m.Param(value=TOU_v[0])
# Manipilated variable:

u = m.MV(lb=0, ub=1, integer=True)
u.STATUS = 1  # allow optimizer to change the variable to attein the optimum.

# Controlled Variable (Affected with changes in the manipulated variable)
#T = m.CV()
# Soft constraints on temprature.

eH = m.CV(value=0)
eL = m.CV(value=0)

eH.SPHI=0       #Set point high for linear error model.
eH.WSPHI=100    #Objective function weight on upper set point for linear error model.
eH.WSPLO=0      # Objective function weight on lower set point for linear error model
eH.STATUS =1    # eH : Error is considered in the objective function.
eL.SPLO=0
eL.WSPHI=0
eL.WSPLO=100 
eL.STATUS = 1   
#Linear error (Deviation from the limits)
m.Equations([eH==T-TH,eL==T-TL])

#Objective: minimize costs.

m.Obj(c*P*u)

#Optimizer Options.

# steady state initialization
m.options.IMODE = 1
m.solve(disp=True)

TL.value = temp_low
TH.value = temp_upper
c.value  = TOU_v
T.value = 11 # Temprature starts at 11

#Set Up MPC
m.options.IMODE = 6 # MPC mode in Gekko.
m.options.NODES = 2  # Collocation nodes.
m.options.SOLVER = 1 # APOT solver for mixed integer linear programming.
m.time = np.linspace(0,23,24)

#Solve the optimization problem.

m.solve() 

#Calculate the costs.
c= 0
cost_list = []
for i in range(0,len(u)):
    c = c + TOU_v[i]*u[i]
    cost_list.append(c)
print('The daily energy cost is' ,c/100, 'Euro') 

plt.subplot(5,1,1)
plt.plot(m.time,temp_low,'k--', label='Lower limit')
plt.plot(m.time,temp_upper,'k--',label='Upper limit')
plt.plot(m.time,T.value,'r-')
plt.ylabel('Temperature')
plt.legend()
plt.subplot(5,1,2)
plt.step(m.time,u.value,'b:')
plt.ylabel('Fridge State')
plt.legend()
plt.subplot(5,1,3)
plt.plot(m.time, eH.value, 'k--', label='Upper Tempratue Limit Error')
plt.plot(m.time, eL.value, 'b--', label='Lower Temprature Limit Error')
plt.ylabel('Cumulative Linar Error')
plt.legend()
plt.subplot(5,1,4)
plt.plot(m.time, cost_list, 'r-')
plt.ylabel('Costs in cent')

plt.show()

结果如下:

我将不胜感激任何形式的帮助:)


你需要定义u = m.MV() and T=m.CV()在致电之前m.arx()模型,以便这些值将用作输入和输出。我还增加了WSPHI值,以便成本目标不会导致温度限制被忽略。目前的制冷系统似乎不足以冷却到这个水平。它需要一个大约 3 倍强大的系统来维持温度限制。我将制冷系统的上限设置为4,这样它就可以将温度保持在极限范围内。它最终放弃了温度控制,因为它发现节省的能源比在这么短的时间内满足温度限制更有价值。您可以通过增加限制来强制执行限制WSPHI and WSPLO或者用TH.UPPER = 0作为硬约束。如果制冷系统不能满足该约束,则硬约束可能会导致解决方案不可行。

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

m = GEKKO(remote = True)


#initialize variables

#Room Temprature:
T_external = [23,23,23,23,23.5,23.5,23.4,23.5,23.9,23.7,\
              23,23.9,23.9,23.4,23.9,24,23.6,23.7,23.8,\
              23,23,23,23,23]

# Temprature Lower Limit:
temp_low = 10*np.ones(24)

# Temprature Upper Limit:
temp_upper = 12*np.ones(24)

#Hourly Energy prices:
TOU_v = [39.09,34.93,38.39,40.46,40.57,43.93,25,11,9,24,51.28,45.22,45.72,\
            36,35.03,10,12,13,32.81,42.55,8,29.58,29.52,29.52]

###########################################
#System Identification:

#Time 
t = np.linspace(0,10,117)
#State of the Fridge
ud = np.append(np.zeros(78) ,np.ones(39),0)
#Temprature Data for 10 min 
y = [14.600000000000001,14.600000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
     14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
     14.700000000000001,14.700000000000001,14.700000000000001,14.8,14.8,14.8,14.8,14.8,14.8,14.8,14.8,\
    14.8,14.8,14.9,14.9,14.9,14.9,14.9,14.9,14.9,15,15,15,15,15,15,15,15,15,15,15,15,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,\
    15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,15.100000000000001,
    15,15,15,15,15,15,15,15,15,15,14.9,14.9,14.9,14.9,14.8,14.9,14.8,14.8,14.8,14.8,14.8,14.8,\
    14.8,14.700000000000001,14.8,14.700000000000001,14.700000000000001,14.700000000000001,\
    14.700000000000001,14.700000000000001,14.700000000000001,14.700000000000001,\
    14.700000000000001,14.600000000000001,14.600000000000001,14.600000000000001,\
    14.600000000000001,14.600000000000001,14.60]

na = 1 # output coefficients
nb = 1 # input coefficients
print('Identification')
yp,p,K = m.sysid(t,ud,y,na,nb,objf=10000,scale=False,diaglevel=1)
#create control ARX model:

# Controlled variable:
T = m.CV()
# Manipulated variable:
u = m.MV(value=0,lb=0, ub=4, integer=True)
# Create ARX Model
m.arx(p,T,u)

###########################################

#Parameter
P = m.Param(value =100) #power
TL = m.Param(value=temp_low[0]) 
TH = m.Param(value=temp_upper[0])
c = m.Param(value=TOU_v[0])

u.STATUS = 1  # allow optimizer to change the variable to attein the optimum.

# Controlled Variable (Affected with changes in the manipulated variable)
#T = m.CV()
# Soft constraints on temprature.

eH = m.CV(value=0)
eL = m.CV(value=0)

eH.SPHI=0         #Set point high for linear error model.
eH.WSPHI=100000     #Objective function weight on upper set point for linear error model.
eH.WSPLO=0        # Objective function weight on lower set point for linear error model
eH.STATUS =1      # eH : Error is considered in the objective function.

eL.SPLO=0
eL.WSPHI=0
eL.WSPLO=100000 
eL.STATUS = 1   
#Linear error (Deviation from the limits)
m.Equations([eH==T-TH,eL==T-TL])

#Objective: minimize costs.
m.Minimize(c*P*u)

#Optimizer Options.

# steady state initialization
m.options.IMODE = 1
m.solve(disp=True)

TL.value = temp_low
TH.value = temp_upper
c.value  = TOU_v
T.value = 11 # Temprature starts at 11

#Set Up MPC
m.options.IMODE = 6 # MPC mode in Gekko.
m.options.NODES = 2  # Collocation nodes.
m.options.SOLVER = 1 # APOT solver for mixed integer linear programming.
m.time = np.linspace(0,23,24)

#Solve the optimization problem.

m.solve()
m.solve() 

#Calculate the costs.
c= 0
cost_list = []
for i in range(0,len(u)):
    c = c + TOU_v[i]*u[i]
    cost_list.append(c)
print('The daily energy cost is' ,c/100, 'Euro') 

plt.subplot(4,1,1)
plt.plot(m.time,temp_low,'k--', label='Lower limit')
plt.plot(m.time,temp_upper,'k--',label='Upper limit')
plt.plot(m.time,T.value,'r-')
plt.ylabel('Temperature')
plt.legend()
plt.subplot(4,1,2)
plt.step(m.time,u.value,'b:',label='u')
plt.ylabel('Fridge State')
#plt.grid()
plt.legend()
plt.subplot(4,1,3)
plt.plot(m.time, eH.value, 'k--', label='Upper Temperatue Limit Error')
plt.plot(m.time, eL.value, 'b--', label='Lower Temperature Limit Error')
plt.ylabel('Cumulative Linear Error')
plt.legend()
plt.subplot(4,1,4)
plt.plot(m.time, cost_list, 'r-')
plt.ylabel('Costs in cent')

plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GEKKO:不响应约束也不求解 obj 函数 的相关文章

  • 区分大小写的实体识别

    我的关键字全部以小写形式存储 例如 折扣耐克鞋 我正在尝试对其执行实体提取 我遇到的问题是 spaCy 在 NER 方面似乎区分大小写 请注意 我不认为这是 spaCy 特有的 当我跑步时 doc nlp u i love nike sho
  • Python Numpy Reshape错误[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我在尝试重塑 3D numpy 数组时遇到一个奇怪的错误 数组 x 的形状为 6 10 300 我想将其重塑为 6 3000 我正
  • 高效地将大型 Pandas 数据帧写入磁盘

    我正在尝试找到使用 Python Pandas 高效地将大型数据帧 250MB 写入磁盘或从磁盘写入的最佳方法 我已经尝试了所有方法Python 数据分析 但表现却非常令人失望 这是一个更大项目的一部分 该项目探索将我们当前的分析 数据管理
  • 在 Jupyter Notebook 中设置环境变量的不同方法

    在某些情况下 我在 Windows 10 计算机上使用 Jupyter 笔记本 我想通过设置环境变量 GOOGLE APPLICATION CREDENTIALS 来向 GCP 进行身份验证 我想知道 这两种设置环境变量的方式有什么区别 当
  • sy.sympify(str(表达式)) 不等于表达式

    据我了解 str将 SymPy 表达式转换为字符串并sympify将字符串转换为 SymPy 表达式 因此 我希望以下内容成立 对于合理的表达 gt gt gt sy sympify str expr expr True 我尝试过这个 确实
  • 计算熊猫数据帧几个月的总和

    我有一个 pandas 数据框 如下所示 ID Year R1 R1 f KAR1 20201001 1 5 KAR1 20201101 2 6 KAR1 20201201 3 7 KAR1 20210101 4 8 KAR1 202102
  • 如何将 numpy rearray 的子集转换为连续数组?

    我有一个recarray来自读取 csv 文件 我有兴趣将列的子集转换为连续浮点数组 我想避免将它们转换为列表或将它们一一堆叠 我尝试了中的建议https stackoverflow com a 11792956 https stackov
  • 如何在动态执行的代码字符串中使用inspect.getsource?

    如果我在文件中有这段代码 import inspect def sample p1 print p1 return 1 print inspect getsource sample 当我运行脚本时 它按预期工作 在最后一行 源代码sampl
  • dask apply:AttributeError:“DataFrame”对象没有属性“name”

    我有一个参数数据框 并对每一行应用一个函数 该函数本质上是几个 sql queries 和对结果的简单计算 我正在尝试利用 Dask 的多处理 同时保持结构和界面 下面的例子有效并且确实有显着的提升 def get metrics row
  • Django Web 应用程序中的 SMTP 问题

    我被要求向使用 Django Python 框架实现的现有程序添加一个功能 此功能将允许用户单击一个按钮 该按钮将显示一个小对话框 表单以输入值 我确实编写了一些代码 显示电子邮件已发送的消息 但实际上 它没有发送 My code from
  • 如何仅注释堆积条形图的一个类别

    我有一个数据框示例 如下所示 data Date 2021 07 18 2021 07 19 2021 07 20 2021 07 21 2021 07 22 2021 07 23 Invalid NaN 1 1 NaN NaN NaN N
  • Bokeh 中单独的节点和边缘悬停工具?

    我正在尝试为 Bokeh 中的节点和边缘获取单独的悬停工具提示 但未能使其正常工作 有人可以指出我做错了什么吗 我相信代码应该如下所示 from bokeh io import show output notebook from bokeh
  • 如何让 Streamlit 每 5 秒重新加载一次?

    我必须每 5 秒重新加载 Streamlit 图表 以便在 XLSX 报告中可视化新数据 如何实现这一目标 import streamlit as st import pandas as pd import os mainDir os pa
  • 如何全局安装 Python(开发)依赖项,以便我不必在每个 venv 中重新安装它们?

    我希望在为每个项目创建的每个 venv 虚拟环境 中都可以使用一些 Python 依赖项 例如 black flake8 和 pytest 这可能吗 如果可以 如何实现 我想安装这三个once在我的主要 Python 安装下 我必须在启动新
  • 更新 matplotlib 中颜色条的范围

    我想更新一个contourf在函数内绘制 效果很好 然而 数据的范围发生了变化 因此我还必须更新颜色条 这就是我未能做到的地方 请参阅以下最小工作示例 import matplotlib pyplot as plt import numpy
  • Python 或 C 语言中的 Matlab / Octave bwdist()

    有谁知道 Matlab Octave bwdist 函数的 Python 替代品 此函数返回给定矩阵的每个单元格到最近的非零单元格的欧几里得距离 我看到了一个 Octave C 实现 一个纯 Matlab 实现 我想知道是否有人必须用 AN
  • 重定向 python 交互式帮助()

    我正在为使用 Qt 的应用程序开发交互式 python shell 但是我似乎无法获得重定向的交互式帮助 我的 python 代码中有这个 class OutputCatcher def init self self data def wr
  • 通过新数据更新绘图,而不是在 Jupyter 笔记本中制作新绘图

    我有一些问题 希望你能帮我解决 我需要使用下拉小部件创建交互式绘图 我可以在其中选择并绘制感兴趣的数据 我通过以下方式做到这一点 import plotly graph objects as go import ipywidgets as
  • 适用于多应用项目的 Grunt 和 requirejs 优化器

    我在让 Grunt 对具有以下结构的项目执行 requirejs 优化时遇到问题 static js apps app js dash js news js many more app files build collections lib
  • 如何在supervisord中设置组?

    因此 我正在设置 Supervisord 并尝试控制多个进程 并且一切正常 现在我想设置一个组 以便我可以启动 停止不同的进程集 而不是全部或全无 这是我的配置文件的片段 group tapjoy programs tapjoy game1

随机推荐