Search code examples
pythonuser-interfacegekko

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.

I have enabled m.options.WEB=1 and solved with m.solve(GUI=True).

The GUI spawns but with no trends as expected (see below). How do I show the variable content and how are the web interface html files generated used by the GUI? Global Options Variable display HTML files


Solution

  • The Gekko display version is with GUI=True as is shown by the plot in your question. Only FV, MV, SV, and CV values display in the web interface.

    Edit: As noted in your comment, you resolved the lack of trend display by upgrading flask.

    pip install flask --upgrade
    

    Gekko 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.

    Below is an example that generates the web-interface. The flask package is a dependency for the web-interface and the web-page needs to be refreshed the first time it is launched. Select the variables on the plot to display or add a new 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. It is also possible to display the MPC solution with Matplotlib.

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

    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.