How can one pass arguments to a custom CrossoverFunction in ga?

5 ビュー (過去 30 日間)
Mitsu
Mitsu 2020 年 11 月 6 日
コメント済み: Mitsu 2020 年 11 月 6 日
Hello,
I am implementing a custom crossover function following the documentation.
I need to pass 2 additional variables along with the solution and the rest of the GA-related parameters (parents, options, etc).
The previous doc page refer to "Passing Extra Parameters" to see how to pass additional arguments, and I believe I am doing it the same way it is explained there.
I define the function this way:
function xoverKids = ga_crossover(parents,options,nvars,FitnessFcn,~,thisPopulation,par1,par2)
xoverKids = []; %test
end
And I tell ga() to use it via:
options.CrossoverFcn = @(x) ga_crossover(x,par1,par2);
% (...)
x = ga(ObjectiveFunction,nvars,[],[],[],[],lb,ub,ConstraintFunction,options);
I get the error that there are too many input arguments.
Inside MATLAB's own stepGa.m, the variable options.CrossoverFcnArgs that is passed as argument to the crossover function appears empty, so I think I am not setting up the function+arguments properly in the options.
Could you help me fix this?
Thank you!

採用された回答

Matt J
Matt J 2020 年 11 月 6 日
編集済み: Matt J 2020 年 11 月 6 日
The CrossoverFcn must accept 5 input arguments,
options.CrossoverFcn = @(parents,options,nvars,FitnessFcn,unused,thisPopulation) ...
ga_crossover(parents,options,nvars,FitnessFcn,unused,thisPopulation,par1,par2)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by