Your fitness function must return a scalar value.

26 views (last 30 days)
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.
  4 Comments
Walter Roberson
Walter Roberson on 18 Dec 2019
load('Z.mat')
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
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.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 5 Feb 2018
Edited: Stephen23 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.
  17 Comments
Jafar Nur Arafah
Jafar Nur Arafah on 20 Jul 2020
Hi Walter Roberson,
I'm sorry what do you mean by two vectors of length 28?
FYI, I have attached the files for your reference.
Thank you.
Regards,
Nur Arafah
Jafar Nur Arafah
Jafar Nur Arafah on 20 Jul 2020
Hi,
Yes, I had ask this question in the link above. The discussion in the link really help me alot in improving my code. However, the discussion comes to an end as we did not came out with the good solution for optimization with non-scalar objective function. Then, I came across to this Q&A and found out the previous researcher have a quite similar problem with me. That is why I ask in this Q&A session, hoping that I can get a good solution to my problem.
Came back to your reply above, what do you mean by two vectors of length 28? Because if you run the Fifth_edit.m file you can found out that the output in a matrix 28x1. So, I think it is one vector of length 28?
Thank you.
Regards,
Nur Arafah

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!