forum.alglib.net
http://forum.alglib.net/

Optimizing non-static member function
http://forum.alglib.net/viewtopic.php?f=2&t=4430
Page 1 of 1

Author:  morfeus80 [ Sun Feb 06, 2022 6:29 pm ]
Post subject:  Optimizing non-static member function

Hi Everyone,

I need to optimize a function that is a non static member of the class. Because I can't pass a pointer to a non static member function, this command return an error:

Code:
minlmoptimize(state, func);


How can I pass the pointer to the function, considering that I can make it static?

Author:  Sergey.Bochkanov [ Sun Feb 06, 2022 10:03 pm ]
Post subject:  Re: Optimizing non-static member function

minlmoptimize() accepts additional ptr parameter (NULL by default) that is forwarded to the callback func.

You can make a wrapper function that accepts void* pointer, converts it to the pointer to an instance of your class and calls its method. Correct me if I wrong, but you may even use lambdas to create a wrapper function (if you refrain from capturing local variables - such lambdas can't be converted to function pointers).

Author:  morfeus80 [ Sun Feb 06, 2022 11:25 pm ]
Post subject:  Re: Optimizing non-static member function

Yes, I think using lambdas is the best way, but I can't find the correct syntax.
I have tried this:

Code:
minlmoptimize(state, [](auto _) {return fun(_); });


where fun is defined as:

Code:
void myClass::fun(const real_1d_array& X, real_1d_array& f, void* ptr){...}


But this doesn't work because it doesn't recognize this lambda as a pointer to "fun". Where am I wrong?

Author:  Sergey.Bochkanov [ Mon Feb 07, 2022 3:05 pm ]
Post subject:  Re: Optimizing non-static member function

You should change lambda signature to match that of the callback accepted by minlpoptimize: (const real_1d_array& X, real_1d_array& f, void* ptr). What you have here can be cast to pointer to function, but this pointer has completely different signature.

Author:  morfeus80 [ Mon Feb 07, 2022 3:39 pm ]
Post subject:  Re: Optimizing non-static member function

Thanks Sergey for your answer, but every syntax I try, it gives me an error.
Could you show me exactly what I should type?

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/