Gekko (optimization software)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Gekko logo.png
Developer(s)Logan Beal
Stable release
0.1.2 / July 31, 2018; 5 months ago (2018-07-31)
Operating systemCross-Platform
TypeTechnical computing

The GEKKO Python package[1] solves large-scale mixed-integer and differential algebraic equations with nonlinear programming solvers (IPOPT, APOPT, BPOPT, SNOPT, MINOS). 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 (LP), Quadratic programming (QP), Quadratically constrained quadratic program (QCQP), Nonlinear programming (NLP), Mixed integer programming (MIP), and Mixed integer linear programming (MILP). GEKKO is available in Python and installed with pip from PyPI of the Python Software Foundation.

pip install gekko

GEKKO works on all platforms (Windows, MacOS, Linux, ARM processors) 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 is a Windows and Linux option 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[2] 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
# Initialize variables
x1 = m.Var(value=1,lb=1,ub=5)
x2 = m.Var(value=5,lb=1,ub=5)
x3 = m.Var(value=5,lb=1,ub=5)
x4 = m.Var(value=1,lb=1,ub=5)
# Equations
m.Obj(x1*x4*(x1+x2+x3)+x3) # Objective
m.solve(disp=False) # Solve
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
print('Objective: ' + str(m.options.objfcnval))

Applications of GEKKO[edit]

Applications include cogeneration (power and heat)[3], drilling automation[4], severe slugging control[5], solar thermal energy production[6], solid oxide fuel cells[7][8], flow assurance [9], Enhanced oil recovery [10], Essential oil extraction[11], and Unmanned Aerial Vehicles (UAVs)[12]. There are many other references to APMonitor and GEKKO as a sample of the types of applications that can be solved. GEKKO is developed from the National Science Foundation (NSF) research grant #1547110 [13][14][15][16] and is detailed in a Special Issue collection on combined scheduling and control[17]. Other notable mentions of GEKKO are the listing in the Decision Tree for Optimization Software[18], added support for APOPT and BPOPT solvers[19], projects reports of the online Dynamic Optimization course from international participants[20]. GEKKO is a topic in online forums where users are solving optimization and optimal control problems[21][22]. GEKKO is used for advanced control in the Temperature Control Lab (TCLab)[23] for process control education at 20 universities[24][25][26][27].

Machine Learning[edit]

Artificial Neural Network

One application of machine learning is to perform regression from training data to build a correlation. In this example a regression model is generated from training data 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 above program produces parameter weights for , , , and that minimize the sum of squared errors between the ten 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 GEKKO
import numpy as np
import matplotlib.pyplot as plt
# generate training data
x = np.linspace(-np.pi,3*np.pi,10)
y = 1.0 - np.cos(x)
# neural network structure
n1 = 3   # hidden layer 1 (linear)
n2 = 5   # hidden layer 2 (nonlinear)
n3 = 3   # hidden layer 3 (linear)
# Initialize gekko
m = GEKKO()
# input(s)
m.inpt = m.Param(x)
# layer 1
m.w1 = m.Array(m.FV, (1,n1), value=1,lb=0.1)
m.l1 = [m.Intermediate(m.w1[0,i]*m.inpt) for i in range(n1)]
# layer 2
m.w2a = m.Array(m.FV, (n1,n2), value=1.0)
m.w2b = m.Array(m.FV, (n1,n2), value=1.0)
m.l2 = [m.Intermediate(sum([m.tanh(m.w2a[j,i]+m.w2b[j,i]*m.l1[j]) \
                        for j in range(n1)])) for i in range(n2)]
# layer 3
m.w3 = m.Array(m.FV, (n2,n3), value=1.0)
m.l3 = [m.Intermediate(sum([m.w3[j,i]*m.l2[j] for j in range(n2)])) for i in range(n3)]
# output(s)
m.outpt = m.CV(y)
m.Equation(m.outpt==sum([m.l3[i] for i in range(n3)]))
# flatten matrices
m.w1 = m.w1.flatten()
m.w2a = m.w2a.flatten()
m.w2b = m.w2b.flatten()
m.w3 = m.w3.flatten()
# Fit parameter weights
m.outpt.FSTATUS = 1
for i in range(len(m.w1)):
for i in range(len(m.w2a)):
for i in range(len(m.w3)):
m.options.IMODE = 2
m.options.EV_TYPE = 2

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 (if available) to impose a structure that is valid over a wider range of conditions. In the example above, the hyperbolic tangent activation function (hidden layer 2) could be replaced with a sine 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.

# Test sample points
for i in range(len(m.w1)):
for i in range(len(m.w2a)):
for i in range(len(m.w3)):
x_test = np.linspace(-2*np.pi,4*np.pi,100)
m.options.IMODE = 2
# Plot ANN results
plt.plot(x,y,'bo',label='training data')
plt.plot(m.inpt.value,m.outpt.value, 'r-',label='neural network')
plt.ylabel('Output (y)')
plt.xlabel('Input (x)')

Optimal Control[edit]

Optimal control problem benchmark (Luus) with an integral objective, inequality, and differential constraint.

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(0,2,nt)
# Variables
x1 = m.Var(value=1)
x2 = m.Var(value=0)
u = m.Var(value=0,lb=-1,ub=1)
p = np.zeros(nt) # mark final time point
p[-1] = 1.0
final = m.Param(value=p)
# Equations
m.Obj(x2*final) # Objective function
m.options.IMODE = 6 # optimal control mode
m.solve() # solve
plt.figure(1) # plot results

See also[edit]


  1. ^ Beal, L. (2018). "GEKKO Optimization Suite". Processes. 6 (8): 106. doi:10.3390/pr6080106.
  2. ^ W. Hock and K. Schittkowski, Test Examples for Nonlinear Programming Codes, Lecture Notes in Economics and Mathematical Systems, Vol. 187, Springer 1981.
  3. ^ Mojica, J. (2017). "Optimal combined long-term facility design and short-term operational strategy for CHP capacity investments". Energy. 118: 97–115. doi:10.1016/
  4. ^ Eaton, A. (2017). "Real time model identification using multi-fidelity models in managed pressure drilling". Computers & Chemical Engineering. 97: 76–84. doi:10.1016/j.compchemeng.2016.11.008.
  5. ^ Eaton, A. (2015). "Post-installed fiber optic pressure sensors on subsea production risers for severe slugging control" (PDF). OMAE 2015 Proceedings, St. John's, Canada.
  6. ^ Powell, K. (2014). "Dynamic Optimization of a Hybrid Solar Thermal and Fossil Fuel System". Solar Energy. 108: 210–218. doi:10.1016/j.solener.2014.07.004.
  7. ^ Spivey, B. (2010). "Dynamic Modeling of Reliability Constraints in Solid Oxide Fuel Cells and Implications for Advanced Control" (PDF). AIChE Annual Meeting Proceedings, Salt Lake City, Utah.
  8. ^ Spivey, B. (2012). "Dynamic modeling, simulation, and MIMO predictive control of a tubular solid oxide fuel cell". Journal of Process Control. 22: 1502–1520. doi:10.1016/j.jprocont.2012.01.015.
  9. ^ Hedengren, J. (2018). "New flow assurance system with high speed subsea fiber optic monitoring of pressure and temperature". ASME 37th International Conference on Ocean, Offshore and Arctic Engineering, OMAE2018/78079, Madrid, Spain.
  10. ^ Udy, J. (2017). "Reduced order modeling for reservoir injection optimization and forecasting" (PDF). FOCAPO / CPC 2017, Tuscon, AZ.
  11. ^ Valderrama, F. (2018). "An optimal control approach to steam distillation of essential oils from aromatic plants". Computers & Chemical Engineering.
  12. ^ Sun, L. (2013). "Optimal Trajectory Generation using Model Predictive Control for Aerially Towed Cable Systems" (PDF). Journal of Guidance, Control, and Dynamics.
  13. ^ Beal, L. (2018). "Integrated scheduling and control in discrete-time with dynamic parameters and constraints". Computers & Chemical Engineering. doi:10.1016/j.compchemeng.2018.04.010.
  14. ^ Beal, L. (2017). "Combined model predictive control and scheduling with dominant time constant compensation". Computers & Chemical Engineering. doi:10.1016/j.compchemeng.2017.04.024.
  15. ^ Beal, L. (2017). "Economic benefit from progressive integration of scheduling and control for continuous chemical processes". Processes. 5. doi:10.3390/pr5040084.
  16. ^ Petersen, D. (2017). "Combined noncyclic scheduling and advanced control for continuous chemical processes". Processes. 5. doi:10.3390/pr5040083.
  17. ^ Hedengren, J. (2018). "Special issue: combined scheduling and control". Processes. doi:10.3390/pr6030024.
  18. ^ Mittleman, Hans (1 May 2018). "Decision Tree for Optimization Software". Plato. Arizona State University. Retrieved 1 May 2018. Object-oriented python library for mixed-integer and differential-algebraic equations
  19. ^ "Solver Solutions". Advanced Process Solutions, LLC. Retrieved 1 May 2018. GEKKO Python with APOPT or BPOPT Solvers
  20. ^ Everton, Colling. "Dynamic Optimization Projects". Petrobras. Petrobras, Statoil, Facebook. Retrieved 1 May 2018. Example Presentation: Everton Colling of Petrobras shares his experience with GEKKO for modeling and nonlinear control of distillation
  21. ^ "APMonitor Google Group: GEKKO". Google. Retrieved 1 May 2018.
  22. ^ "Computational Science: Is there a high quality nonlinear programming solver for Python?". SciComp. Retrieved 1 May 2018.
  23. ^ Kantor, Jeff (2 May 2018). "TCLab Documentation" (PDF). ReadTheDocs. University of Notre Dame. Retrieved 2 May 2018. pip install tclab
  24. ^ Kantor, Jeff (2 May 2018). "Chemical Process Control". GitHub. University of Notre Dame. Retrieved 2 May 2018. Using the Temperature Control Lab (TCLab)
  25. ^ Hedengren, John (2 May 2018). "Advanced Temperature Control Lab". Dynamic Optimization Course. Brigham Young University. Retrieved 2 May 2018. Hands-on applications of advanced temperature control
  26. ^ Sandrock, Carl (2 May 2018). "Jupyter notebooks for Dynamics and Control". GitHub. University of Pretoria, South Africa. Retrieved 2 May 2018. CPN321 (Process Dynamics), and CPB421 (Process Control) at the Chemical Engineering department of the University of Pretoria
  27. ^ "CACHE News (Winter 2018): Incorporating Dynamic Simulation into Chemical Engineering Curricula" (PDF). CACHE: Computer Aids for Chemical Engineering. University of Texas at Austin. 2 May 2018. Retrieved 2 May 2018. Short Course at the ASEE 2017 Summer School hosted at SCSU by Hedengren (BYU), Grover (Georgia Tech), and Badgwell (ExxonMobil)

External links[edit]