python - 如何在 GEKKO GUI 中显示解决方案?

标签 python user-interface gekko

我想探索 GEKKO Web GUI 用于可视化结果的功能,如 https://www.researchgate.net/publication/326740143_GEKKO_optimization_suite 中所述。 .

我已启用 m.options.WEB=1 并使用 m.solve(GUI=True) 进行求解。

GUI 出现,但没有预期的趋势(见下文)。如何显示变量内容以及 GUI 如何使用生成的 Web 界面 html 文件? Global Options Variable display HTML files

最佳答案

Gekko 显示版本采用 GUI=True,如问题中的绘图所示。 Web 界面中仅显示 FVMVSVCV 值。

编辑:正如您的评论中所述,您通过升级 flask 解决了缺乏趋势显示的问题。

pip install flask --upgrade

Gekko web interface

选项 WEB 是 Web 界面的 APMonitor 版本,它使用 AJAX 和 Flash 绘图(已弃用,将在未来版本中替换)来显示 Web 界面。可以保留为 m.options.WEB=0 以使用 Gekko GUI。

下面是生成 Web 界面的示例。 Flask 包是 Web 界面的依赖项,第一次启动时需要刷新网页。选择绘图上的变量以显示或添加新绘图。

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

m = GEKKO()
m.time = np.linspace(0,20,41)

# Parameters
mass = 500
b = m.Param(value=50)
K = m.Param(value=0.8)

# Manipulated variable
p = m.MV(value=0, lb=0, ub=100)
p.STATUS = 1  # allow optimizer to change
p.DCOST = 0.1 # smooth out gas pedal movement
p.DMAX = 20   # slow down change of gas pedal

# Controlled Variable
v = m.CV(value=0)
v.STATUS = 1  # add the SP to the objective
m.options.CV_TYPE = 2 # squared error
v.SP = 40     # set point
v.TR_INIT = 1 # set point trajectory
v.TAU = 5     # time constant of trajectory

# Process model
m.Equation(mass*v.dt() == -v*b + K*b*p)

m.options.IMODE = 6 # control
m.solve(disp=False,GUI=True)

调用 m.solve(GUI=True) 每次调用都会生成(或更新)Web 界面。还可以使用 Matplotlib 显示 MPC 解决方案。

Matplotlib plot

# get additional solution information
import json
with open(m.path+'//results.json') as f:
    results = json.load(f)

plt.figure()
plt.subplot(2,1,1)
plt.plot(m.time,p.value,'b-',label='MV Optimized')
plt.legend()
plt.ylabel('Input')
plt.subplot(2,1,2)
plt.plot(m.time,results['v1.tr'],'k-',label='Reference Trajectory')
plt.plot(m.time,v.value,'r--',label='CV Response')
plt.ylabel('Output')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()

更多 complicated Matplotlib plots还可以生成显示 future 预测范围、移动范围估计或 MPC 解决方案的其他自定义功能。

关于python - 如何在 GEKKO GUI 中显示解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73729774/

相关文章:

Python scikit-学习 : Why is my LinearRegression classifier's score so low?

python - Python 3 中的可比较类

python - 我如何使用 gekko python 通过操纵到 jar 的入口流量来控制 cstr jar 的液位?

python - 如何修复 Python GEKKO 最优控制代码中的 "Solution Not Found"错误

python - 为什么我的 MV 无法收敛,而普通 Vars 可以收敛?

python - 无法在数据库中找到新保存的实例

python - 比较不同形状的矩阵

java - SmartGwt - 显示标签的全部内容

python - python点击关闭按钮后程序的执行没有结束(使用wxpython-GUI)

javascript - 使用多个超链接作为提交和输入