Passing extra input parameters for creation function in ga

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)

Respuestas (1)

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 comentarios

This is showing an error:
Error: "f" was previously used as a variable, conflicting with its use here as the name of a function or command.
Adam
Adam el 13 de Dic. de 2016
Editada: Adam el 13 de Dic. de 2016
Well, obviously I just used 'f' as an example. You can use whatever name you want and should use something more meaningful
I don't think it was problem with the name specified to the variable because it showed error when I tried with other names too.
It gave an error that f was used as a variable even when you named it something else?
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

Iniciar sesión para comentar.

Preguntada:

el 13 de Dic. de 2016

Comentada:

el 22 de Dic. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by