CMA-ES / libcmaes

libcmaes is a multithreaded C++11 library with Python bindings for high performance blackbox stochastic optimization using the CMA-ES algorithm for Covariance Matrix Adaptation Evolution Strategy
Other
315 stars 79 forks source link

Pass data to fitfunction #217

Open Optiuse opened 3 years ago

Optiuse commented 3 years ago

Hi, is it possible to pass own data (by reference) from scope of upper_function directly to the FitFunc? (directly from function to FitFunc, without use of global variables)

FitFunc fsphere = [](const double *x, const int N)
{
   // use Data from scope of upper_function() ?
}
void upper_function()
{
   // data. for example a struct/class/int/double/etc
   //...
   CMASolutions cmasols = cmaes<>(fsphere,cmaparams);
   //...
}
beniz commented 3 years ago

Hi, I guess there's no placeholder into FitFunc for this (if I understand the issue correctly), what's wrong with using a global variable/pointer ?

Optiuse commented 3 years ago

Hi, because i want to run several different optimizations at the same time in several threads. The FitFunctions need access to identifiable individual data each from its thread. Therefore a global variable/pointer is not possible because the fit functions will not get access to individual data.

Maybe something like a void pointer would be good to pass individual pointers to the FitFunc or other solution? FitFunc fsphere = [](const double x, const int N, void ptr_Data) Something like this or a placeholder to pass data would be great.

beniz commented 3 years ago

Seems like a design issue for your application. You can modify FitFunc as needed I guess. If this proves useful somehow, you may provide a pull request once tested.

RobertBlakeAnderson commented 3 weeks ago

Function binding provides the solution to this.