2

I am new to programming and my first Python project is nonlinear programming. I am using the Gekko Optimization Suite and I have everything running properly, but need guidance on how exactly to run it locally. Below is the explanation and code provided by the documentation, but I could use some help on how exactly to do it myself and what exactly it all means. Please act as if you are explaining to a small child or a golden retriever.

The run directory m.path contains the model file gk0_model.apm and other files required to run the optimization problem either remotely (default) or locally (m=GEKKO(remote=False)). Use m.open_folder() to open the run directory. The run directory also contains diagnostic files such as infeasibilities.txt that is produced if the solver fails to find a solution. The default run directory can be changed:

from gekko import GEKKO
import numpy as np
import os
# create and change run directory
rd=r'.\RunDir'
if not os.path.isdir(os.path.abspath(rd)):
    os.mkdir(os.path.abspath(rd))
m = GEKKO(remote=False)         # solve locally
m.path = os.path.abspath(rd)   # change run directory
JackW24
  • 135
  • 9
  • nice quote reference...what was that damn market crash movie? The documentation link below says if you try to run locally, then it looks for a build of `APMonitor`, which is what is running on the server. Sounds like a PITA. Have you tried other frameworks that are not tightly lied to a remote server like GEKKO is? https://gekko.readthedocs.io/en/latest/model_methods.html – AirSquid Sep 20 '22 at 03:30
  • Haha- Margin Call. I tried optimization using scipy first, but had trouble and figured there was a simpler implementation out there and that's how I stumbled upon Gekko. – JackW24 Sep 20 '22 at 03:35
  • Yes. Margin Call. Anyhow, it appears you need to install `APMonitor` locally. There is a reference here that looks pretty outdated https://apmonitor.com/wiki/index.php/Main/PythonApp Do you *really* need to do this locally? – AirSquid Sep 20 '22 at 03:46
  • Not now, but in the future I may need to. Maybe I will give scipy another go if this is overkill. It may be easier and it probably makes more sense – JackW24 Sep 20 '22 at 03:49
  • Depending on structure of your problem, there are probably several options. If you want to do it all locally, you might want to try to find a way to use `ipopt` which is a stellar NLP solver and interfaces with several packages including `pyomo` – AirSquid Sep 20 '22 at 03:59
  • 1
    Another big hurdle for me is that one of my two variables needs to be an integer. Gekko makes that constraint very simple, can’t say the same for anything else I’ve tried. – JackW24 Sep 20 '22 at 05:31

1 Answers1

1

Local Solve

m=GEKKO(remote=False)

The only option needed to run Gekko locally is remote=False. With remote=False, no Internet connection is required. There is no need to change the run directory. A default directory at m.path is created in the temporary folder to store the files that are compiled to byte code. This folder can be accessed with m.open_folder().

Local Solve with Intranet Server

m=GEKKO(remote=True,server='http://10.0.0.10')

There is an APMonitor server option (see Windows APMonitor Server or Linux APMonitor Server) for remote=True and server=http://10.0.0.10 (change to IP of local Intranet server). This is used as a local compute engine that runs control and optimization problems for microprocessors. This is useful for compute architectures that do not have sufficient memory or CPU power to solve the optimization problems, but when it is desirable to solve locally. This is an Edge compute option to complete the solution in the required cycle time (e.g. for a Model Predictive Controller). Some organizations use this option to have multiple clients that connect to one compute engine. This compute server can be upgraded so that all of the clients automatically use the updated version.

Remote Solve

m=GEKKO(remote=True)

A public server is available as a default server option. With remote=True (default option), Gekko sends the optimization problem to a remote server and then returns the solution. The public server is running a Linux APMonitor Server but with additional solver options that aren't in the local options due to distribution constraints.

Example Gekko and Scipy Optimize Minimize Solutions

GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations (see documentation). 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. Below is a simple optimization example with remote=False (local solution mode). There are local options for MacOS, Windows, Linux, and Linux ARM. Other architectures need to use the remote=True option.

HS71

Python Gekko

from gekko import GEKKO
m = GEKKO(remote=False)
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)

Scipy Optimize Minimize

import numpy as np
from scipy.optimize import minimize

def objective(x):
    return x[0]*x[3]*(x[0]+x[1]+x[2])+x[2]

def constraint1(x):
    return x[0]*x[1]*x[2]*x[3]-25.0

def constraint2(x):
    sum_eq = 40.0
    for i in range(4):
        sum_eq = sum_eq - x[i]**2
    return sum_eq

# initial guesses
n = 4
x0 = np.zeros(n)
x0[0] = 1.0
x0[1] = 5.0
x0[2] = 5.0
x0[3] = 1.0

# show initial objective
print('Initial Objective: ' + str(objective(x0)))

# optimize
b = (1.0,5.0)
bnds = (b, b, b, b)
con1 = {'type': 'ineq', 'fun': constraint1} 
con2 = {'type': 'eq', 'fun': constraint2}
cons = ([con1,con2])
solution = minimize(objective,x0,method='SLSQP',\
                    bounds=bnds,constraints=cons)
x = solution.x

# show final objective
print('Final Objective: ' + str(objective(x)))

# print solution
print('Solution')
print('x1 = ' + str(x[0]))
print('x2 = ' + str(x[1]))
print('x3 = ' + str(x[2]))
print('x4 = ' + str(x[3]))

Additional Examples

There are many additional optimization packages in Python and additional Gekko tutorials and benchmark problems. One additional example is a Mixed Integer Linear Programming solution.

MILP solution

from gekko import GEKKO
m = GEKKO(remote=False)
x,y = m.Array(m.Var,2,integer=True,lb=0) 
m.Maximize(y)
m.Equations([-x+y<=1,
             3*x+2*y<=12,
             2*x+3*y<=12])
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', x.value[0])
print('y: ', y.value[0])

The APOPT solver is a Mixed Integer Nonlinear Programming (MINLP) solver (that also solves MILP problems) and is included as a local solver for MacOS, Linux, and Windows.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • If I simply write "m = GEKKO(remote=False)", then I get "Error: The system cannot find the path specified. Error: 'results.json' not found. Check above for additional error details" – JackW24 Sep 20 '22 at 16:06
  • That is an error message when the solver does not converge because a `results.json` file is not created. Try using `m.options.SOLVER=1` or other strategies to address the failed solution. If you post the problem on Stack Overflow, a more detailed suggestion can be provided. – John Hedengren Sep 20 '22 at 17:23
  • m.options.SOLVER=1, m.options.SOLVER=2, and m.options.SOLVER=3 all produce the same error – JackW24 Sep 20 '22 at 17:40
  • There may be an infeasible equation or a challenging nonlinearity that needs better initialization. There are additional strategies presented in Safdarnejad, S.M., Hedengren, J.D., Lewis, N.R., Haseltine, E., Initialization Strategies for Optimization of Dynamic Systems, Computers and Chemical Engineering, 2015, Vol. 78, pp. 39-50, DOI: 10.1016/j.compchemeng.2015.04.016. https://www.sciencedirect.com/science/article/pii/S0098135415001179 Can you post the problem to StackOverflow? – John Hedengren Sep 20 '22 at 17:43
  • The problem in more detail is posted: https://stackoverflow.com/questions/73792563/path-error-when-running-gekko-optimization-solver – JackW24 Sep 20 '22 at 20:58