Passing extra input parameters for creation function in ga
조회 수: 7(최근 30일)
How to add an extra input parameter to the creation function in genetic algorithm tool. I have read the documentation on passing extra parameters but couldn't get how to implement it in this case since the creation function must have the following calling syntax :
function Population = myfun(GenomeLength, FitnessFcn, options)
Adam 2016년 12월 13일
I don't really know enough about the GA process in Matlab, but generally speaking, what you have is a function of 3 variables, but you can also 'bake in' any other parameters you want at function creation time e.g.
f = @(x,y,z) x^2 + y^2 + z^2
is a function of 3 arguments with calling syntax
res = f(x,y,z);
res = f( 2, 3, 4 );
i.e. similar to what you need to have.
But the following also has the same calling syntax:
a = 6;
b = 7;
g = @(x,y,z) x^2 + y^2 + z^2 + a + b;
res = g(x,y,z);
res = g( 2, 3, 4 );
So with function handle like this you can turn a function of e.g. 5 arguments into a function of 3 arguments by binding (to use a C++ and maybe other languages term) the other 2 arguments in at the time you create the function handle.
Here a and b must be known (as shown) at function creation time, x, y and z only need to be known at call time.
Coming back to your case this means that as long as you have those 3 input arguments in that list of variable arguments you can bind in whatever other arguments you want e.g.
a = 7;
b = 8;
f = @( GenomeLength, FitnessFcn, options) myfun(GenomeLength, FitnessFcn, options, a, b);
Then you can pass f in as your creation function, from my understanding of what is being asked for.