Very simply, I’m using an optimization library in C++ that takes in a function of a single variable. I would like to be able to pass in multiple parameters though (which this library does not support). What I would like to do is create a lambda function of the sort (kind of like in Python) that lets me represent the cost function as a function of a single variable that passes in two parameters.
Here’s a simplified version of what I’m going for in pseudocode. Any help would be much appreciated. I can’t seem to get this to work with lambda in C++.
Optimize comes from a library (asa047). The version I wrote here isn’t at all realistic, but is just meant to demonstrate what this function takes in.
double cost(double x, double param1, double param2){
return x*param1 + param2;
}
double optimize(double fn( double x), double initial_value){
return optimal_x;
}
int main(){
double param1 = 2;
double param2 = 3;
function_object f; //What I would like to create
f(double x){
return cost(x,param1,param2);
}
optimize(f,2);
}