Skip to content
Advertisement

Get variable values at each iteration of GEKKO optimization

I would like to get the value of the variables at each iteration of the optimization process when using Python GEKKO.

For example, the following code from the documentation (https://gekko.readthedocs.io/en/latest/quick_start.html#example) solves problem HS71:

JavaScript

The output displayed in the console is the following:

JavaScript

This includes the value of the objective function at each iteration, but I would also like to get the value of the variables (x) at each iteration.

Advertisement

Answer

The solvers don’t have call-backs to Python. One way to get the values at each iteration is to call the solver multiple times with a max_iter (maximum iteration) limit and report the values until the solver is successful. Use debug=0 to not raise an exception with an unsuccessful solution.

JavaScript

Here is the iteration summary with iteration number, variable values x, and objective function value. The objective function may get worse (higher) because the equations are not yet satisfied, and the solution may be infeasible.

JavaScript

If you have a large model that you don’t want to redefine multiple times, then just put the m.solve() function in a loop with m.options.COLDSTART=1.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement