Passing extra input parameters for creation function in ga

5 views (last 30 days)
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)

Answers (1)

Adam on 13 Dec 2016
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.
Adam on 19 Dec 2016
It gave an error that f was used as a variable even when you named it something else?
Hari on 22 Dec 2016
No, it changed with the variable name I gave. I solved the problem. Declaring the variable as global and then using it in the function

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!