GEKKO Documentation

repository·master·Indexed 20 days ago

https://github.com/byu-prism/gekko

A Python package for machine learning and optimization specializing in time series and differential algebraic equations (DAE) systems. It provides a high-level interface to the APMonitor optimization suite, supporting LP, QP, NLP, MILP, and MINLP solvers. The ecosystem includes Gekko-MATLAB, a native MATLAB wrapper for the APMonitor engine, and the brain.Brain class for building and fitting Artificial Neural Networks.

Tokens
45.8K
Snippets
180
Records
250
Agent score
71%

What's inside GEKKO

  1. Overview of GEKKO capabilities

    master

    GEKKO is an object-oriented Python library designed for machine learning and optimization of mixed-integer and differential algebraic equations. It interfaces with large-scale solvers for various programming types:

    • LP: Linear Programming
    • QP: Quadratic Programming
    • NLP: Nonlinear Programming
    • MILP: Mixed-Integer Linear Programming
    • MINLP: Mixed-Integer Nonlinear Programming

    Core Modes of Operation:

    • Parameter regression
    • Data reconciliation
    • Real-time optimization
    • Dynamic simulation
    • Nonlinear predictive control
  2. Interface Machine Learning models with GEKKO

    master

    GEKKO's ML module allows you to import trained machine learning models from libraries like scikit-learn, gpflow, and tensorflow into the GEKKO optimization suite. This enables data-based optimization, model predictive control, and physics-informed hybrid modeling.

    To use these features, import the ML subpackage:

    from gekko import ML
  3. What is GEKKO and how does it model problems?

    master

    GEKKO is optimization software designed for mixed-integer and differential algebraic equations. It supports linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP) by coupling with large-scale solvers.

    Mathematical Modeling Abstractions

    GEKKO uses a high-level abstraction for mathematical optimization. You define models using the following components:

    • Constants, Parameters, and Variables: The fundamental values in your model.
    • Intermediates or Equations: The relationships that connect values.
    • Objective functions: Defined to either maximize or minimize specific values.
    • Objects: Built-in collections of values and relationships that can utilize object-oriented relationships to build upon one another.
  4. Summary of Machine Learning Uncertainty Methods in GEKKO

    master

    GEKKO supports several methods for calculating prediction uncertainty within interfaced models:

    MethodDescription
    Gaussian ProcessesUncertainty is calculated directly within the GP framework.
    ResamplingMultiple models are trained and used with resampling to produce a prediction mean and variation.
    Linear RegressionUses least squared equations to create uncertainty.
    Delta FunctionExtends the linear regression uncertainty method to other nonlinear regression methods.
    NonconformistUses the nonconformist library to generate a consistent uncertainty margin.
    Tensorflow NNsCan produce uncertainty based on the loss function.
  5. How Gekko-MATLAB modeling works

    master

    Gekko-MATLAB is a native MATLAB wrapper for the APMonitor optimization engine. It allows you to build models using a GEKKO-style interface directly in MATLAB without requiring Python.

    Models are assembled into APMonitor sections such as Constants, Parameters, Variables, Intermediates, Equations, Connections, and Objects. You can solve these models either locally using the apm executable or remotely on the APMonitor server by setting m.remote = true.

  6. Configure prediction and control horizons

    master

    Horizon settings define the time window for model predictions and manipulated variable (MV) movements.

    • PRED_HOR (Prediction Horizon): The total horizon, including the control horizon and additional points used to track towards steady state. It must be $\ge$ CTRL_HOR. No MV movement is allowed in the prediction horizon portion.
    • CTRL_HOR (Control Horizon): The portion of the horizon where MV movements are allowed.
    • PRED_TIME: The time duration for each step in the prediction horizon beyond the control horizon. It is typically larger than CTRL_TIME to allow the model to predict steady-state arrival.

    Note on CSV_READ: When CSV_READ is enabled and time points are provided in a data file, CTRL_HOR and PRED_HOR are set to the number of time steps in the file, and CTRL_TIME and PRED_TIME are set to the first time step's increment.

    PRED_HOR: Integer (Input/Output)
    Default: 1.0
    
    PRED_TIME: Real (Input/Output)
    Default: 60.0
  7. Use Gekko-MATLAB object helpers

    master

    The library provides several GEKKO-style math and object helpers implemented directly in MATLAB to handle non-linearities and complex structures.

    Common helpers include:

    • Nonlinearities: max2, max3, min2, min3, abs2, abs3, sign2, sign3, if2, if3.
    • Splines: bspline, cspline, pwl (Note: these expect a GEKKO variable as dependent output).
    • System/Control: state_space, arx, sysid, delay.
    • Others: integral, periodic, sum, vsum, Array (returns cell arrays of GEKKO objects).
    m = Gekko();
    x = m.Var();
    y = m.Var();
    z = m.max2(x,y); % Example using max2 helper
    
    m.Equation(x == 2);
    m.Equation(y == -1);
    m.solve();
    
    disp(z.value)
  8. Configure APMonitor modes of operation using imode

    master

    The APMonitor back-end uses the imode parameter to switch between different operational modes. These modes determine how the nonlinear model is interacted with (e.g., for simulation, estimation, or optimization).

    Modes of Operation

    CategoryModeDescription
    Steady-stateSSSteady-state simulation
    MPUModel parameter update
    RTOReal-time optimization
    DynamicSIMDynamic simulation
    ESTMoving horizon estimation
    CTLNonlinear control / dynamic optimization
    SequentialSQSSequential dynamic simulation
    SQESequential dynamic estimation
    SQOSequential dynamic optimization
    • Modes 1-3 are steady-state modes where all derivatives are set to zero.
    • Modes 4-6 are dynamic modes where differential equations define variable changes over time.
    • Modes 7-9 follow the same logic as 4-6 but use a sequential approach rather than a simultaneous approach.
  9. Use the GEKKO Solver Extension to access alternative solvers

    master

    The GEKKO solver extension module allows you to convert a GEKKO model into other mathematical optimization libraries, providing access to solvers not natively supported by GEKKO. Currently, it supports two converters:

    1. AMPLPY: Converts the model to AMPL syntax for use with the AMPLPY library.
    2. PYOMO: Converts the model to Pyomo syntax.

    Important Limitations:

    • remote=True is not supported when using the solver extension.
    • Dynamic optimization features (e.g., m.time, derivatives) are not supported by either the AMPLPY or Pyomo converters at this time.
    • Basic model building functions (variables, parameters, constants, intermediates, constraints, and objectives) are supported.
    from gekko import GEKKO
    m = GEKKO(remote=False)
    # ... define model ...
    m.options.SOLVER_EXTENSION = "AMPLPY" # or "PYOMO"
    m.options.SOLVER = "solver_name"
    m.solve()
  10. Configure automatic coldstart with AUTO_COLD and BAD_CYCLES

    master

    If a model repeatedly fails to find a solution, you can automate a COLDSTART to reset the application.

    • BAD_CYCLES (Integer, Input/Output): A counter that increments with each consecutive unsuccessful solution attempt. Defaults to 0.
    • AUTO_COLD (Integer, Input): The threshold of BAD_CYCLES required to trigger a coldstart.
      • 0: No auto coldstart.
      • 1+: Triggers COLDSTART = 1 after the specified number of bad cycles.

    Behavior: When the AUTO_COLD limit is reached, COLDSTART is set to 1 and remains on until a successful solution is found. Once successful, both COLDSTART and BAD_CYCLES are reset to 0.

  11. Use COLDSTART to handle infeasible solutions

    master

    The COLDSTART option defines the initialization mode for an application. This is useful for resetting an application after failure or handling difficult initialization.

    • 0 (Default): Warm start (no specific initialization).
    • 1: Cold start. Temporarily turns off all FV, MV, and CV STATUS values to reduce degrees of freedom, aiming for a feasible but suboptimal solution.
    • 2: Decompose problem. An aggressive initialization that analyzes problem sparsity to create and solve a sequence of lower block triangular segments. This helps identify specific equations or variable bounds causing infeasibility.

    Note: When COLDSTART >= 1, initialization values are taken from the file, the DBS file, and the CSV file (in that order), ignoring prior t0 files. Once a successful solution is found, COLDSTART is automatically reset to 0.

    m.options.COLDSTART = 1
  12. Handle logical conditions and discontinuities in optimization

    master

    Traditional logical expressions (like if statements) cannot be used directly in gradient-based optimization because they create discontinuities in derivatives. GEKKO provides specialized logical functions that use either Mathematical Programs with Complementarity Constraints (MPCC, Type 2) or binary variables (Type 3) to provide differentiable workarounds.

    Important: All Type 3 functions require a mixed-integer solver like APOPT (SOLVER=1). GEKKO will automatically switch the solver to APOPT if it detects these functions in your model.