General question about gekko and python: is there a possibility to ptimize a function that is defined as a separate python function?

30 views
Skip to first unread message

Alex Zivenko

unread,
Nov 2, 2022, 12:44:17 PM11/2/22
to apmonitor
Hi there!

I am a newbie in optimisation and I have one problem regarding the optimisation of a complexed function.

Actually, I have 2 questions regarding GEKKO usage or maybe you can suggest another package that can be used for that purpose (non-commercial).

1) I have a very complexed function - f1 that is calculating the values for a set of input parameters.

The inputs for that function are arrays, objects and a dataframe.

Is it possible to use a function f1 (not an equation) for optimisation in GEKKO?

The function f1 is quite complex, non-linear and non-convex, it uses a lot of supplementary procedures from other packages and libraries so it's impossible to convert it into equation.  

def function1(E:ndarray, pair: someclass_object, input_df: pd.Dataframe) -> ndarray:
    ....
    return resulting_array

I need to optimize the parameters in a input_df so the resulting_array will be as close as possible to the - exp_resulting_array (I have an experimental data for that).

I can only construct a dataframe input_df from parameters that are searched, and use objects: E, pair as fixed one - they are unchangeable for that purpose (fixed).

So I am actually trying to solve fitting task - the parameters I am searching in a dataframe are the model parameters for the nonlinear fitting function.

Can you suggest an approach or give an example or some explanation?



John Hedengren

unread,
Nov 2, 2022, 12:58:02 PM11/2/22
to apmo...@googlegroups.com

Alex,

 

Here is a similar question on Stack Overflow: https://stackoverflow.com/questions/73850403/can-i-use-implicit-objective-function-in-a-design-optimization-process-in-gekko/73858161

 

You can’t use a black-box function with Gekko, but the black box model can be used to fit a cspline (1D)bspline (2D), or machine learning models (higher dimension functions).

 

For future gekko questions, please use Stack Overflow with tag [gekko].

 

-John Hedengren

--
--
APMonitor user's group e-mail list.
- To post a message, send email to apmo...@googlegroups.com
- To unsubscribe, send email to apmonitor+...@googlegroups.com
- Visit this group at http://groups.google.com/group/apmonitor
---
You received this message because you are subscribed to the Google Groups "apmonitor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apmonitor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apmonitor/d494bbe3-35f1-4fcd-a6ed-b6d11c35b651n%40googlegroups.com.

Alex Zivenko

unread,
Nov 3, 2022, 9:50:45 AM11/3/22
to apmo...@googlegroups.com
Dear John, thank you for your reply.

As I understood your answer - it's not possible to optimize parameters if you haven't complete direct functional form, right?

Maybe I have confused you with the previous description.

The key moments: I am having the experimental data and a complexed model that is described in f1(set of parameters).
And I need to find such parameters (search_params) which will lead to the minimum of a distance such as f1(search_params) will be as close as possible to the experimental data I have.

For each parameter of a set I have initial value and boundaries and even some constraints..

So i need to to something like this:

---
m = GEKKO()

#parameters I need to find
param_1 = m.FV(value = val_1, lb = lb_1, ub=rb_1)
param_2 = m.FV(value = val_2, lb = lb_2, ub=rb_2)
...
param_n = m.FV(value = val_n, lb = lb_n, ub=rb_n) #constructing the input for the function f1()
params_dataframe = ....()# some function that collects all the parameters and arranges all of them to a proper form to an input of f1()

#exp data description
x = m.Param(value = xData)
z = m.Param(value = yData)

y = m.Var()

#model description - is it possible to use other function inside equation? because f1 is very complexed with a lot of branches and options.. I don't really want to translate it in equation form..

m.Equation(
y==f1(params_dataframe)
)

#add some constraints
min = m.Param(value=some_val_min)
m.Equation(min <= (param_1+param_2) / (param_1+param_2)**2))

# trying to solve and minimize the sum of squares
m.Minimize(((y-z))**2)

# Options for solver
param_1.STATUS = 1
param_2.STATUS = 1
...
param_n.STATUS = 1

m.options.IMODE = 2
m.options.SOLVER = 1
m.options.MAX_ITER = 1000

m.solve(disp=1)
---

Is it possible to use GEKKO this way or it's not allowed?
--
--
Best regards,
Alex Zivenko

On Wed, Nov 2, 2022 at 10:10 PM Alex Zivenko <a...@zivenko.com.ua> wrote:
Dear John, thank you for your reply.

As I understood your answer - it's not possible to optimize parameters if you haven't complete direct functional form, right?

Maybe I have confused you with the previous description..

The key moments: I am having the experimental data and a complexed model that is described in f1(set of parameters).
And I need to find such parameters (search_params) which will lead to the minimum of a distance such as f1(search_params) will be as close as possible to the experimental data I have.

For each parameter of a set I have initial value and boundaries and even some constraints..

So i need to to something like this:

---
m = GEKKO()

#parameters I need to find
param_1 = m.FV(value = val_1, lb = lb_1, ub=rb_1)
param_2 = m.FV(value = val_2, lb = lb_2, ub=rb_2)
...
param_n = m.FV(value = val_n, lb = lb_n, ub=rb_n) #constructing the input for the function f1()
params_dataframe = ....()# some function that collects all the parameters and arranges all of them to a proper form to an input of f1()

#exp data description
x = m.Param(value = xData)
z = m.Param(value = yData)

y = m.Var()

#model description - is it possible to use other function inside equation? because f1 is very complexed with a lot of branches and options.. I don't really want to translate it in equation form..

m.Equation(
y==f1(params_dataframe)
)

#add some constraints
min = m.Param(value=some_val_min)
m.Equation(min <= (param_1+param_2) / (param_1+param_2)**2))

# trying to solve and minimize the sum of squares
m.Minimize(((y-z))**2)

# Options for solver
param_1.STATUS = 1
param_2.STATUS = 1
...
param_n.STATUS = 1

m.options.IMODE = 2
m.options.SOLVER = 1
m.options.MAX_ITER = 1000

m.solve(disp=1)
---

Is it possible to use GEKKO this way or it's not allowed?


You received this message because you are subscribed to a topic in the Google Groups "apmonitor" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/apmonitor/49vRUS2rhfE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to apmonitor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apmonitor/BYAPR08MB4600F96E73E0CD19240707D5EE399%40BYAPR08MB4600.namprd08.prod.outlook.com.


--
--
Best regards,
Oleksii Zivenko
Additional mail: z...@ieee.org

John Hedengren

unread,
Nov 3, 2022, 9:53:22 AM11/3/22
to apmo...@googlegroups.com
Alex,

You can't use f1(params) directly in the problem but you can use a cspline (1D), bspline (2D), or ML for higher dimensional function approximation. Here is an example with cspline:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

"""
minimize y
s.t.     y = f(x)
using cubic spline with random sampling of data
"""

# function to generate data for cspline
def f(x):
    return 3*np.sin(x) - (x-3)

x_data = np.random.rand(50)*10+10
y_data = f(x_data)

c = GEKKO()
x = c.Var(value=np.random.rand(1)*10+10)
y = c.Var()
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve(disp=True)

if c.options.SOLVESTATUS == 1:
    plt.figure()
    plt.scatter(x_data,y_data,5,'b')
    plt.scatter(x.value,y.value,200,'r','x')
else:
    print ('Failed!')
    print(x_data,y_data)
    plt.figure()
    plt.scatter(x_data,y_data,5,'b')
plt.show()



--
Best regards,

John Hedengren
GEKKO Optimization Suite
Reply all
Reply to author
Forward
0 new messages