フィルターのクリア

Genetic Algorithm not enough input arguments despite single vector

2 ビュー (過去 30 日間)
Darin
Darin 2015 年 8 月 26 日
回答済み: Darin 2015 年 8 月 26 日
Hi All, bit of an odd problem.
Using genetic algorithm, my fitness function is (nested functions)
function residual = fitfun(inputs)
residual = sum(abs(fit_fcn(inputs)-cdfy));
end
function fitfcn_y = fit_fcn(inputs)
%calculate function values at each point, cdfx
mus = inputs(1:Npeaks);
sigmas = inputs((Npeaks+1):(2*Npeaks));
Amps = inputs((2*Npeaks+1):(3*Npeaks));
fitfcn_y = 0;
for index_peaks = 1:Npeaks
fitfcn_y = fitfcn_y + Amps(index_peaks)*(1/2)*(1+erf( cdfx-mus(index_peaks)./(sqrt(2.*sigmas(index_peaks).^2)) ));
end
end
But I am still getting a 'not enough input arguments' error when I call genetic algorithm:
x = ga(fitfun, nvars)
Npeaks is 4 and nvars is 3*Npeaks = 12.
Any ideas?
Thanks in advance!
  1 件のコメント
Adam
Adam 2015 年 8 月 26 日
Those are not nested functions, the second is just a local function. To be nested the function definition for the second would have to be inside the
function
...
end
block of the first.
What exactly are you getting the not enough inputs error on? Is it the call to 'ga' or the call within 'ga' to your outer fitness function or the local one that it subsequently calls?
If inputs is a cell array like varargin then you need to pass it on as:
residual = sum(abs(fit_fcn(inputs{:})-cdfy));
in order for the inputs to all be passed on as individual arguments to the 2nd function.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2015 年 8 月 26 日
This attempts to call fitfun with 0 input arguments and use the output argument as the first input argument to pass into GA. What you want to do instead is pass a function handle to fitfun into your call to GA as the first input. That way GA can call the function itself (via the function handle) with whatever input arguments it wants.
x = ga(@fitfun, nvars) % Note the @-symbol

その他の回答 (1 件)

Darin
Darin 2015 年 8 月 26 日
ahhh of course, that worked perfectly, thank you!!

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by