MATLAB Answers

Your fitness function must return a scalar value.

74 views (last 30 days)
anu vru
anu vru on 5 Feb 2018
Commented: Jafar Nur Arafah on 20 Jul 2020
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
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)

Stephen Cobeldick
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.
  17 Comments
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!