Not enough input arguments

2 Ansichten (letzte 30 Tage)
nesrine nesrine
nesrine nesrine am 15 Aug. 2014
Bearbeitet: Geoff Hayes am 24 Nov. 2014
Hi, I'm trying to turning a genetic algorithm for the following function :
function L=fonction(x1,x2,x3)
L=abs(118.04-(x1.*218.46+x2.*31.09+x3.*125.61));
end
but when i wrote the following command :
[x1,x2,x3,Fval,exitFlag,Output] = ga(@fonction,3);
fprintf('The number of generations was : %d\n', Output.generations);
fprintf('The number of function evaluations was : %d\n', Output.funccount);
fprintf('The best function value found was : %g\n', Fval);
matlab return this error :
Error using fonction (line 2)
Not enough input arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in fcnvectorizer (line 14)
y(i,:) = feval(fun,(pop(i,:)));
Error in makeState (line 47)
Score = fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in gaunc (line 41)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
Please help

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 16 Aug. 2014
nesrine - the problem is how you have defined the signature for your fitness function, fonction. Check out the documentation from MATLAB Genetic Algorithm as it relates to the function handle that you pass to ga
fitnessfcn: Handle to the fitness function. The fitness function should accept a row vector of length nvars and return a scalar value.
As described above, the fitness function accepts one input row vector of length nvars. In your case, that would be a row vector of size three. The above code has defined your fitness function to accept three inputs and NOT one. The error message Not enough input arguments is caused because the GA is passing only one input (a row vector with three elements) to your function that requires three inputs.
You just need to change your code to accept a single row vector instead of three variable inputs
function L=fonction(xData)
x1 = xData(1);
x2 = xData(2);
x3 = xData(3);
L=abs(118.04-(x1*218.46+x2*31.09+x3*125.61));
end
Try the above and see what happens!
  1 Kommentar
Ulyana Dudko
Ulyana Dudko am 24 Nov. 2014
Thank you, Geoff! Very helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by