Gekko (optimization software)


The GEKKO Python package solves large-scale mixed-integer and differential algebraic equations with nonlinear programming solvers. Modes of operation include machine learning, data reconciliation, real-time optimization, dynamic simulation, and nonlinear model predictive control. In addition, the package solves Linear programming, Quadratic programming, Quadratically constrained quadratic program, Nonlinear programming, Mixed integer programming, and Mixed integer linear programming. GEKKO is available in Python and installed with pip from PyPI of the Python Software Foundation.

pip install gekko


GEKKO works on all platforms and with Python 2.7 and 3+. By default, the problem is sent to a public server where the solution is computed and returned to Python. There are Windows, MacOS, Linux, and ARM processor options to solve without an Internet connection. GEKKO is an extension of the APMonitor Optimization Suite but has integrated the modeling and solution visualization directly within Python. A mathematical model is expressed in terms of variables and equations such as the Hock & Schittkowski Benchmark Problem #71 used to test the performance of nonlinear programming solvers. This particular optimization problem has an objective function and subject to the inequality constraint and equality constraint. The four variables must be between a lower bound of 1 and an upper bound of 5. The initial guess values are. This optimization problem is solved with GEKKO as shown below.

from gekko import GEKKO
m = GEKKO # Initialize gekko
  1. Initialize variables
x1 = m.Var
x2 = m.Var
x3 = m.Var
x4 = m.Var
  1. Equations
m.Equation
m.Equation
m.Obj # Objective
m.solve # Solve
print
print
print
print
print

Applications of GEKKO

Applications include cogeneration, drilling automation, severe slugging control, solar thermal energy production, solid oxide fuel cells, flow assurance, Enhanced oil recovery, Essential oil extraction, and Unmanned Aerial Vehicles. There are many other references to as a sample of the types of applications that can be solved. GEKKO is developed from the National Science Foundation research grant #1547110 and is detailed in a Special Issue collection on combined scheduling and control. Other notable mentions of GEKKO are the listing in the Decision Tree for Optimization Software, added support for APOPT and BPOPT solvers, projects reports of the online Dynamic Optimization course from international participants. GEKKO is a topic in online forums where users are solving optimization and optimal control problems. GEKKO is used for advanced control in the Temperature Control Lab for process control education at 20 universities.

Machine Learning

One application of machine learning is to perform regression from training data to build a correlation. In this example, deep learning generates a model from training data that is generated with the function. An artificial neural network with three layers is used for this example. The first layer is linear, the second layer has a hyperbolic tangent activation function, and the third layer is linear. The program produces parameter weights that minimize the sum of squared errors between the measured data points and the neural network predictions at those points. GEKKO uses gradient-based optimizers to determine the optimal weight values instead of standard methods such as backpropagation. The gradients are determined by automatic differentiation, similar to other popular packages. The problem is solved as a constrained optimization problem and is converged when the solver satisfies Karush–Kuhn–Tucker conditions. Using a gradient-based optimizer allows additional constraints that may be imposed with domain knowledge of the data or system.

from gekko import brain
import numpy as np
b = brain.Brain
b.input_layer
b.layer
b.layer
b.layer
b.output_layer
x = np.linspace
y = 1 - np.cos
b.learn

The neural network model is tested across the range of training data as well as for extrapolation to demonstrate poor predictions outside of the training data. Predictions outside the training data set are improved with hybrid machine learning that uses fundamental principles to impose a structure that is valid over a wider range of conditions. In the example above, the hyperbolic tangent activation function could be replaced with a sine or cosine function to improve extrapolation. The final part of the script displays the neural network model, the original function, and the sampled data points used for fitting.

xp = np.linspace
yp = b.think
import matplotlib.pyplot as plt
plt.figure
plt.plot
plt.plot
plt.show

Optimal Control

is the use of mathematical optimization to obtain a policy that is constrained by differential, equality, or inequality equations and minimizes an objective/reward function. The basic optimal control is solved with GEKKO by integrating the objective and transcribing the differential equation into algebraic form with orthogonal collocation on finite elements.

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO # initialize gekko
nt = 101
m.time = np.linspace
  1. Variables
x1 = m.Var
x2 = m.Var
u = m.Var
p = np.zeros # mark final time point
p = 1.0
final = m.Param
  1. Equations
m.Equation
m.Equation
m.Obj # Objective function
m.options.IMODE = 6 # optimal control mode
m.solve # solve
plt.figure # plot results
plt.plot
plt.plot
plt.plot
plt.legend
plt.xlabel
plt.ylabel
plt.show