Filter löschen
Filter löschen

Genetic Algorithm not enough input arguments despite single vector

2 Ansichten (letzte 30 Tage)
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 Kommentar
Adam
Adam am 26 Aug. 2015
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 26 Aug. 2015
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

Weitere Antworten (1)

Darin
Darin am 26 Aug. 2015
ahhh of course, that worked perfectly, thank you!!

Community Treasure Hunt

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

Start Hunting!

Translated by