Questions tagged [gekko]

GEKKO (pip install gekko) is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include parameter regression, dynamic simulation, and nonlinear predictive control. GEKKO is an object-oriented Python library and is released under the MIT License.

Introduction

GEKKO is a Python software library for large-scale nonlinear optimization. It is designed to find (local) solutions of mathematical optimization problems of the form

   min     f(x)
x in R^n

s.t.       g(dx/dt,x) <= 0
           h(dx/dt,x)  = 0
           x_L <=  x   <= x_U

where f(x): R^n --> R is the objective function, g(dx/dt,x): R^n --> R^m are the inequality constraint functions, and h(dx/dt,x): R^n --> R^m are the equality constraint functions. The vectors x_L and x_U are the simple bounds on the variables x. The functions f, g, and h can be nonlinear and nonconvex, but should be twice continuously differentiable. The variables may be binary, integer, differential, or continuous. GEKKO was created from NSF grant #1547110 and is released under the MIT License.

Background

GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include parameter regression, data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. GEKKO is an object-oriented Python library to facilitate local execution of APMonitor. The original author of the package is Logan Beal and it is maintained by the BYU PRISM group.

GEKKO can be used on Linux/UNIX, Mac OS X, ARM, and Windows platforms and any other platform that runs Python.

Example

The following is the solution to the Hock Schittkowski benchmark problem #71.

hs71_optimization

from gekko import GEKKO    
import numpy as np
m = GEKKO()
x = m.Array(m.Var,4,value=1,lb=1,ub=5)
x1,x2,x3,x4 = x
# change initial values
x2.value = 5; x3.value = 5
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Minimize(x1*x4*(x1+x2+x3)+x3)
m.solve()
print('x: ', x)
print('Objective: ',m.options.OBJFCNVAL)

Solution with IPOPT:

 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   9.699999995063990E-003 sec
 Objective      :    17.0140171270735     
 Successful solution
 ---------------------------------------------------
 
Results
x1: [1.000000057]
x2: [4.74299963]
x3: [3.8211500283]
x4: [1.3794081795]

Related Links

APMonitor is run locally or as a web-service as a backend solution engine for GEKKO.

Documentation: Installation, options, and examples.

GitHub: Source code repository.

Online Course: GEKKO for machine learning and dynamic optimization

References: GEKKO and APMonitor references.

Wikipedia: Overview of GEKKO.

798 questions
4
votes
1 answer

GEKKO: count ocurrences in array as condition

I'm using gekko for INLP, i.e. integer non linear programming. I have an array of integer variables; however, my condition is that no value must appear more than once, i.e. Code: from gekko import GEKKO model = GEKKO() model.options.SOLVER = 1 x…
Kenji
  • 61
  • 6
4
votes
1 answer

how to improve the quality of a nonlinear fit with python GEKKO?

I am working on a biochemical model: there is an enzyme that catalyzes twice a substrate. By naming: * E = the enzyme * S = the original substrate * P = the intermediate product, which is in turn substrate * F = the final product I have this…
robyrobur
  • 41
  • 2
4
votes
1 answer

Calling Gekko solve gives TypeError: object of type 'int' has no len()

I'm trying to solve an optimal control problem using Gekko. When I try to call m.solve(), it gives me TypeError: object of type 'int' has no len(), details below. I get this error regardless of my choice of objective function; however, the only…
Nikhil Haksar
  • 342
  • 2
  • 8
4
votes
1 answer

Trying to solve this non linear optimization using GEKKO, getting this error

@Error: setting an array element with a sequence I am trying to mninimize the downside risk. I have a two dimensional array of returns shape(1000, 10), and the portfolio starts with $100. Compound that 10 times by each return in a row. Do that for…
4
votes
1 answer

returning function calls and other info when optimization solves

I am benchmarking multiple problems for multiple systems using Gekko, and I would like to get my code to return the function calls, iterations, and time it takes to solve. I know that the solver automatically prints all of this data but is there an…
Andrew T
  • 562
  • 3
  • 9
4
votes
1 answer

APOPT is finding a better local minimum to a MINLP problem when I use fewer nlp iterations. I was expecting the opposite result, what am I missing?

I have the following boolean and continuous variables, where only some of the 'percents' have a status of 1. status[i] = m.Array(m.Var, p, lb=0, ub=1, integer=True) percent[i] = m.Array(m.FV, p, value=1, lb=0.6, ub=1.1) I've used some…
4
votes
2 answers

Optimizing a Summation function - GEKKO

I just started learning optimization and I have some issues finding the optimal value for the problem below. Note: This is just a random problem that came to my mind and has no real application. Problem: where x can be any value from the list…
Antione
  • 99
  • 6
4
votes
1 answer

Using Gekko in Python to run time series linear optimization

I am trying to run an electricity arbitrage model in Gekko. I have an electricity price array for every hour of a year (8760 total hours), a battery of energy size E, and for every hour I want to decide whether to charge the battery or discharge…
David
  • 51
  • 3
4
votes
1 answer

Gekko can't find solution of a small problem

I am making some tests with Gekko library from python, and have a small problem in which I know the solution. The complet code is as follows: from gekko import GEKKO P = [[3.0,3.55,5.18,7.9,5.98], [1.56,1.56,2.48,3.15,2.38], …
donut
  • 628
  • 2
  • 9
  • 23
4
votes
1 answer

Gekko infeasible solution with costraint that should be satisfied

I'm new to gekko and APM, I'm trying to solve a problem but the solution seems to get infeasible with a new equation in a binary variable that should be viable. Here's the simplified APM model: Model Variables int_v1 = 0, >= 0 int_v2 = 0, <=…
peter_b
  • 21
  • 8
4
votes
1 answer

when running gekko for 1st time, got this error "cannot import name 'dump_csp_header' from werkzeug.http'

I'm using Spyder to run Python 3.7 where I installed gekko. However, I tried running a simple gekko code from Wikipedia and it gives me the following error: ImportError: cannot import name 'dump_csp_header' from 'werkzeug.http'…
Zulfan
  • 75
  • 4
4
votes
1 answer

Variable bounds in MPC with GEKKO

I'm trying to implement a thermostat control using MPC and GEKKO. The state variable (temperature) should be within a lower and upper pre-specified temp values , temp_low and temp_upper in the code below. Both bound vary along the day: one value per…
Arraval
  • 1,110
  • 9
  • 20
4
votes
1 answer

Python gekko cant find "options.json" file

I haven't seen a question related to this, yet it pops up despite several of my efforts to the contrary, so I was hoping that someone could help me understand what's going on. I'm new to Python's Gekko package, and I'm trying to run a non-linear…
Lucas Spangher
  • 339
  • 2
  • 14
4
votes
1 answer

Use Gekko and Python to fit a numerical ODE solution to data

Use Gekko to fit a numerical ODE solution to data. Hi everyone! I was wondering, if it is possible to fit coefficients of an ODE using GEKKO. I unsuccessfully tried to replicate the example given here. This is what I have come up with (but is flawed…
Martin A
  • 45
  • 3
4
votes
1 answer

GEKKO error when running remote = False on Windows 10 Pro

I have encountered an error which I did not have before when running the optimizer locally. The problem: I was running my optimization problem in VS Code and wanted to cancel the execution, so I used ctrl+C in the terminal window. After that, I am…