Asked by anu vru
on 5 Feb 2018

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.

Answer by Stephen Cobeldick
on 5 Feb 2018

Edited by 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;

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Birdman (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/380826-your-fitness-function-must-return-a-scalar-value#comment_531959

Sign in to comment.