Genetic Algorithm not enough input arguments despite single vector

2 vues (au cours des 30 derniers jours)
Darin
Darin le 26 Août 2015
Réponse apportée : Darin le 26 Août 2015
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 commentaire
Adam
Adam le 26 Août 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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 26 Août 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

Plus de réponses (1)

Darin
Darin le 26 Août 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