Genetic Algorithm not enough input arguments despite single vector
2 次查看(过去 30 天)
显示 更早的评论
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
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
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
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!