Passing extra input parameters for creation function in ga

9 views (last 30 days)
Hari
Hari on 13 Dec 2016
Commented: Hari on 22 Dec 2016
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
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);
e.g.
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);
e.g.
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.
  5 Comments
Hari
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!