# Your fitness function must return a scalar value.

61 views (last 30 days)
anu vru on 5 Feb 2018
Commented: Walter Roberson on 18 Dec 2019
i am currently working on Genetic Algorithm. and i am getting this error everytime i try to execute. 'Your fitness function must return a scalar value.' i am new to optimization and matlab. could someone please fix this and help me out. thank you. ps: i have attached code.

Show 1 older comment
mohammad hodaei on 18 Dec 2019
Dear Friends,
I am also currently facing with the same problem. Your objective function must return a scalar value.
My fucntion is a long function as I call it with another fuicntion. I put the name of second function, reader, on the ga or fmincon or others, but I still get the same response.
A COPY OF MY FUNCTIONS ARE ATTAHCED
Thanks and a dvice
M.H.
Walter Roberson on 18 Dec 2019
We do not have Z.mat to test with.
Omega=10:10000:4e6;
% % % % x=0.1;
for i=1:1:400
finalresp(i)=abs((TransOpt(x,Omega(i))-Z(i))^2);
end
You should be pre-allocating finalresp.
It would be better not to assume that Omega is exactly length 400, and to loop to length(Omega) instead of to 400.
y=(finalresp);
Well, that is obviously going to be a vector result, of length 400. ga() cannot process a vector result. gamultiobj() can process a vector result -- but do you really want to optimize for the pareto fronts of finalresp, or do you instead want to optimize some property of finalresp() as a whole, such as sum(finalresp) ?
If you are going for minimum residue, then I recommend that you switch to
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
finalresp(i)=(TransOpt(x,Omega(i))-Z(i))^2;
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
because abs() is not differentiable .
Exception: if Z can be complex valued, then
finalresp = zeros(size(Omega));
for i = 1 : length(Omega)
temp = (TransOpt(x,Omega(i))-Z(i))^2;
finalresp(i) = temp .* conj(temp);
end
y = sum(finalresp); %no need to take sqrt() for optimization purposes
Walter Roberson on 18 Dec 2019
Note that
Z=inv(T)*Y;
would be better written as
Z = T\Y;
inv() is not as numerically stable as using the \ operator.

Stephen Cobeldick on 5 Feb 2018
Edited: Stephen Cobeldick on 5 Feb 2018
The error message refers to the fast that ga requires the objective function to return a scalar, as the help clearly states "The fitness function should accept a row vector of length nvars and return a scalar value.". Instead of returning a scalar value your objective function returns a vector f which has length(pvy) elements:
for i=1:length(pvy)
f(i)=pvy(i)+pwy(i)-pdy(i)
end
Your code has other bugs, such as that the filenames differ from the function names. The objective function calls xlsread multiple times: this is going to be very inefficient because the objective function gets called many times and reading from file is slow. You would be much better off parameterizing your function using either an anonymous function or nested functions:
Plotting inside the objective function will also waste a lot of time: it would be better to wait until after the GA algorithm has returned a result, or if you want to follow the results as the ga algorithm progresses then use one of the inbuilt ga plotting functions:
You have a lot of code and its purpose is not clear so I would not hazard any guesses how to fix it. I recommend that you should start small before jumping into such a complex task as this one (with file reading, etc.): copy one of the examples from the help and make sure that it works for you. Understand how it works, before starting to make small changes to incrementally turn it into the solver that you require. Your current approach of writing lots of code without testing it nor knowing what it is doing is a total waste of your time.

Walter Roberson on 8 Feb 2018
You are missing a basic structure:
function [a, b, c, Fvals] = drive_ga
...
bestx = ga(@func, 33, ....); %find best values
a = bestx(1:11).'; %break the vector into variables
b = bestx(12:22).';
c = bestx(23:33).';
Fvals = F(a, b, c); %calculate F at that optimal position
function r = func(x) %called by ga
a = x(1:11); %break the vector into variables
b = x(12:22);
c = x(23:33);
r = cost(a, b, c); %call routine that determines cost for that configuration
function r = cost(a, b, c)
... something that might possibly call F(a,b,c)
In this construction, the cost() function does not need to be the same as F, but could be. For example, "cost" might be the norm of the outputs of F, if what you were looking for was the inputs that give the F value that is closest to the origin -- but the output you might be looking for might be the actual position vector implied by inputs a, b, and c.
anu vru on 8 Feb 2018
could you show the above code you have given with an example please.
Walter Roberson on 8 Feb 2018
function [a, b, c, Fvals] = drive_ga
bestx = ga(@func, 33, ....); %find best values
a = bestx(1:11).'; %break the vector into variables
b = bestx(12:22).';
c = bestx(23:33).';
Fvals = F(a, b, c); %calculate F at that optimal position
function r = func(x) %called by ga
a = x(1:11); %break the vector into variables
b = x(12:22);
c = x(23:33);
r = cost(a, b, c); %call routine that determines cost for that configuration
function r = cost(a, b, c)
r = norm( F(a, b, c) );
function r = F(a, b, c)
r = a.^2 - b.^c;