简体   繁体   English

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

[英]How do I display solution in GEKKO GUI?

I would like to explore the functionality of the GEKKO web GUI for visualising results as described in https://www.researchgate.net/publication/326740143_GEKKO_optimization_suite .我想探索 GEKKO web GUI 的功能,以可视化结果,如https://www.researchgate.net/publication/326740143_GEKKO_optimization_suite中所述。

I have enabled m.options.WEB=1 and solved with m.solve(GUI=True) .我已启用m.options.WEB=1并使用m.solve(GUI=True)解决。

The GUI spawns but with no trends as expected (see below). GUI 生成但没有预期的趋势(见下文)。 How do I show the variable content and how are the web interface html files generated used by the GUI?如何显示变量内容以及GUI使用生成的web接口html文件如何? 全局选项 变量显示 HTML文件

The Gekko display version is with GUI=True as is shown by the plot in your question.如您的问题中的 plot 所示, Gekko显示版本使用GUI=True Only FV , MV , SV , and CV values display in the web interface. web 界面中仅显示FVMVSVCV值。

Gekko 网页界面

The option WEB is the APMonitor version of the web-interface that uses AJAX and Flash plots (deprecated, to be replaced in a future version) to display the web interface. The option WEB is the APMonitor version of the web-interface that uses AJAX and Flash plots (deprecated, to be replaced in a future version) to display the web interface. This can remain as m.options.WEB=0 to use the Gekko GUI.这可以保留为m.options.WEB=0以使用 Gekko GUI。

Below is an example that generates the web-interface.下面是一个生成 Web 界面的示例。 The flask package is a dependency for the web-interface and the web-page needs to be refreshed the first time it is launched. flask package 是 Web 界面的依赖项,第一次启动时需要刷新网页。 Select the variables on the plot to display or add a new plot. Select plot 上的变量显示或添加新的 plot。

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)

Calling m.solve(GUI=True) generates (or updates) the web-interface with each call.调用m.solve(GUI=True)会在每次调用时生成(或更新)Web 界面。 It is also possible to display the MPC solution with Matplotlib.也可以使用 Matplotlib 显示 MPC 解决方案。

Matplotlib 绘图

# 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()

More complicated Matplotlib plots can also be generated to show a future prediction horizon, moving horizon estimation, or other customized features of the MPC solution.还可以生成更复杂的 Matplotlib 图,以显示 MPC 解决方案的未来预测范围、移动范围估计或其他自定义功能。

I'm having a similar issue with the variables not being in the GUI.我对不在 GUI 中的变量有类似的问题。 Flask is up to date, version 2.2.2... Flask 是最新的,版本 2.2.2...

I've found to ways that make them disappear would like to understand ways to work around this that don't require all the objects to be in the global scope.我找到了让它们消失的方法想了解解决这个问题的方法,不需要所有对象都在全局 scope 中。

Running the example above works the GUI shows the variables and their results.运行上面的示例,GUI 会显示变量及其结果。 But when init is separated into a function call (see 1st code block below) and the call to solve outside of it in python the GUI doesn't work.但是当 init 被分成一个 function 调用(见下面的第一个代码块)并且在 python 中解决它之外的调用时,GUI 不起作用。 I've also observed if I place the variables under the model using the dot notation (2nd code block) so I can pass the model and all of it's variables around example mu = m.MV() instead of u=m.MV() the GUI also stops showing the variables.我还观察到我是否使用点符号(第二个代码块)将变量放在 model 下,这样我就可以传递 model 及其所有围绕示例 mu = m.MV() 而不是 u=m.MV( ) GUI 也停止显示变量。

Passing model from function从 function 传递 model

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
def init():
    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
    return m

m=init()
m.solve(disp=False,GUI=True)

Using dot notation to group variables with model使用点符号将变量分组为 model

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
m.p = m.MV(value=0, lb=0, ub=100)
m.p.STATUS = 1  # allow optimizer to change
m.p.DCOST = 0.1 # smooth out gas pedal movement
m.p.DMAX = 20   # slow down change of gas pedal

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

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

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

Param show but variables are missing!显示参数但缺少变量! 显示参数但缺少变量!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM