필터 지우기
필터 지우기

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 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