Passing extra input parameters for creation function in ga

조회 수: 5 (최근 30일)
Hari
Hari 2016년 12월 13일
댓글: Hari 2016년 12월 22일
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)

답변 (1개)

Adam
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);
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
Adam
Adam 2016년 12월 19일
It gave an error that f was used as a variable even when you named it something else?
Hari
Hari 2016년 12월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by