GEKKO Optimization Suite
GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations (DAE) systems, coupled with large-scale solvers for various programming types (LP, QP, NLP, MILP, MINLP). It provides an object-oriented interface to the APMonitor optimization suite, supporting modes like parameter regression, dynamic data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. Currently at version 1.3.2, GEKKO is actively maintained with frequent releases and consistently sees over 100,000 downloads per month.
Common errors
-
ModuleNotFoundError: No module named 'gekko'
cause The GEKKO library is not installed in the current Python environment.fixRun `pip install gekko` in your terminal or environment. -
Exception: @error: Solution Not Found. Check model formulation and solver options.
cause The solver failed to find a feasible solution or reached maximum iterations. This could be due to an ill-posed problem, infeasible constraints, poor initial guesses, or solver limitations.fixExamine `m.options.APPSTATUS` and `m.options.APPINFO` for specific error codes. Try different initial values for variables, relax constraints, or switch to a different solver (`m.options.SOLVER = 1` for APOPT, `3` for IPOPT if available). Set `m.options.DEBUG=1` or `disp=True` during `m.solve()` for more verbose output. -
AttributeError: 'GEKKO' object has no attribute 'exp' (or 'sin', 'log', etc.) TypeError: object of type 'int' has no len() (or similar with numpy/math functions)
cause Attempting to use standard Python `math` or `numpy` functions directly on GEKKO symbolic variables. GEKKO variables require GEKKO's own symbolic operations for automatic differentiation.fixReplace `math.function(m.Var)` or `np.function(m.Var)` with `m.function(m.Var)`. For example, use `m.exp(x)` instead of `math.exp(x)`. -
GEKKO: Invalid APM server or file. Check if 'apm.exe' (Windows), 'apm' (Linux), 'apm_mac' (MacOS) exists in C:\Users\...\AppData\Local\Temp\...
cause GEKKO is configured for a local solve (`remote=False`), but the necessary APM solver executables are missing or cannot be found in the specified temporary directory or system PATH.fixEnsure GEKKO is correctly installed and its executables are present. If running on an unsupported architecture for local solve, or if executables are corrupted, consider using `m = GEKKO(remote=True)` to offload solving to the public server. Reinstalling GEKKO (`pip install --upgrade gekko`) might also resolve missing executables.
Warnings
- breaking The default behavior for solving models changed from remote (cloud server) to local execution (`remote=False`). Users accustomed to models running on public servers will now have models solve locally by default.
- gotcha When defining equations or objective functions, use GEKKO's symbolic math operations (e.g., `m.exp()`, `m.sin()`) rather than standard Python `math` or `numpy` functions directly on GEKKO variables. GEKKO performs automatic differentiation, which requires its internal representations.
- gotcha Local solver availability and performance can vary by operating system and architecture. Some advanced solvers (like IPOPT for Linux/macOS) are often only available via the `remote=True` option due to distribution size or licensing restrictions.
- gotcha Complex models with many equations or variables can encounter a 'Max Equation Length' error, indicating the model is too large for the current configuration.
Install
-
pip install gekko
Imports
- GEKKO
from gekko import GEKKO
Quickstart
from gekko import GEKKO
m = GEKKO() # Initialize model
# Define variables with initial guess and bounds
x = m.Var(value=1, lb=0, ub=4)
y = m.Var(value=1, lb=0, ub=4)
# Define equations (constraints)
m.Equation(x + y == 3)
m.Equation(x**2 + y**2 >= 5)
# Define objective function (to minimize)
m.Minimize((x-2)**2 + (y-1)**2)
# Solve the optimization problem
m.solve(disp=False) # disp=False suppresses solver output
# Print results
print(f"Optimal Solution: x = {x.value[0]}, y = {y.value[0]}")
print(f"Optimal Objective: {m.options.objfcnval}")