cureos / csnumerics

Portable numerical algorithms in C#
GNU Lesser General Public License v3.0
32 stars 11 forks source link

Passing extrapamaters to objective fun #10

Open afabrild opened 2 years ago

afabrild commented 2 years ago

Dear csnumerics team,

I'm trying to solve an optimization problem where the objective function is calculated using precalculated variables, therefore these variables need to be passed as additional parameters to the "calcfun", however, Bobyqa class only permits two parameters in the objective function: n & x vector. How can I pass extra parameters to the objective function?

Best Regards.

dsanalytics commented 2 years ago

@afabrild Essentially, one would use lambda expressions to define new function to be passed to Bobyqa with the original function called behind the scenes with fixed params embedded. Partial code something like this:

double[] x_params_fixed = {0.2, 3.4, 10.2}; // e.g. 3 precalculated variables Func<int, double[], double> fun_to_optimize = (n, x_params_to_optimize) => original_objective_fun(n, x_params_to_optimize, x_params_fixed); // new function 'fun_to_optimize' defined

and then one would use Bobyqa to minize fun_to_optimize.

For full details see: C# lambda expression - using lambda expressions in C# https://zetcode.com/csharp/lambda-expression/

Hope this helps. Let us know how it goes.