Main

GEKKO Python Optimization

GEKKO Python is designed for large-scale optimization and accesses solvers of constrained, unconstrained, continuous, and discrete problems. Problems in linear programming, quadratic programming, integer programming, nonlinear optimization, systems of dynamic nonlinear equations, and multiobjective optimization can be solved. The platform can find optimal solutions, perform tradeoff analyses, balance multiple design alternatives, and incorporate optimization methods into external modeling and analysis software. It is free for academic and commercial use under the MIT license.

The GEKKO package is available through the package manager pip in Python.

 python pip install gekko

Another method to obtain GEKKO is to include the following code snippet at the beginning of a Python script. If GEKKO is not available, it uses the pip module to install it.

try:
    from pip import main as pipmain
except:
    from pip._internal import main as pipmain
pipmain(['install','gekko'])
# to upgrade: pipmain(['install','--upgrade','gekko'])

GEKKO Documentation and Source Code

The latest GEKKO source is available on Github. Functionality is tested with Python 2.7 and Python 3+.

The development roadmap for this and other interfaces to APMonitor are detailed in the release notes. Example problems with GEKKO are provided below.


18 Example Applications


$$ \min x_1 x_4 (x_1 + x_2 + x_3) + x_3 $$

$$ \mathrm{subject\;to} \quad x_1 x_2 x_3 x_4 \ge 25$$

$$\quad x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40$$

$$\quad 1 \le x_1, x_2, x_3, x_4 \le 5$$

$$\quad x_0 = (1,5,5,1)$$

Solve this optimization problem from a web-browser interface or with GEKKO Python.

from gekko import GEKKO    
import numpy as np

#Initialize Model
m = GEKKO()

#help(m)

#define parameter
eq = m.Param(value=40)

#initialize variables
x1,x2,x3,x4 = [m.Var() for i in range(4)]

#initial values
x1.value = 1
x2.value = 5
x3.value = 5
x4.value = 1

# lower bounds
x1.lb = 1
x2.lb = 1
x3.lb = 1
x4.lb = 1

# upper bounds
x1.ub = 5
x2.ub = 5
x3.ub = 5
x4.ub = 5

#Equations
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==eq)

#Objective
m.Obj(x1*x4*(x1+x2+x3)+x3)

#Set global options
m.options.IMODE = 3 #steady state optimization

#Solve simulation
m.solve(remote=True)

#Results
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))

Learn GEKKO Python with Online Course

The Dynamic Optimization Course is graduate level course taught over fourteen weeks to introduce concepts in mathematical modeling, data reconciliation, estimation, and control. There are many other applications and instructional material posted to this freely available course web-site. The online course is generally offered starting each year in January. Below is an example student presentation at the end of the course. It is representative of the modeling, estimation, and control methods learned.