Error using makeState (line 56) Your fitness function must return a scalar value.

9 views (last 30 days)
Hello Matlab users
I'm getting started with GA and trying to apply it to minimization problem I have. Running the Genetic Algorithm gives the following error:
Error using makeState (line 56)
Your fitness function must return a scalar value.
Error in galincon (line 17)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 359)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in minimizer (line 45)
[x,f] = ga(objfunc, nvars, [], [] , [], [], LB, UB, [],options);
I read somewhere that should run GA function with the following options structure:
options = gaoptimset('Vectorized','off');
I did it but still have this error. I set 'dbstop if error' in my file and after running GA this message is received:
error(message('globaloptim:makestate:fitnessCheck'));
I would be grateful if you help me.
Thanks
Nasrin Sadeghi

Answers (1)

Walter Roberson
Walter Roberson on 7 Sep 2015
Your objfunc needs to be a function handle for a function that returns a single value (a scalar). Currently it is returning a vector or an array.
We would need to see the code for your objfunc in order to say why your code is returning a vector or array.
  3 Comments
nasrin
nasrin on 7 Sep 2015
function [x, f] = minimizer
objfunc = @(x)evaluator(x);
nvars = 3;
LB = [1 1 1];
UB = [22 5 46];
options = gaoptimset('InitialPopulation',[],'PopulationSize',100,... 'Generations',50,'StallGenLimit',100,'SelectionFcn',@selectionroulette,'FitnessScalingFcn',@fitscalingrank,'Vectorized','off'); [x,f] = ga(objfunc, nvars, [], [] , [], [], LB, UB, [],options);
Walter Roberson
Walter Roberson on 7 Sep 2015
In your function, you ignore the input x, and you construct f as a 22 x 5 x 46.
You need to be taking a row vector of values as input and you need to construct from that a scalar output.
The exception to that would be if you set Vectorized 'on' in the options. In that case your objective function should take as input a 2D array which has one row for each member of the population, and you would need to return a scalar value for each.
Your code is using a number of variables that are not parameters, including m_t, n, a, A, and density_t . With the summation it is doing to create f2(), I do not see any obvious way of converting it into a function that creates scalars. It looks to me as if your function is intended to sweep x1, x2, and x3 across a range of values. If so then x1, x2, and x3 should be variables passed within the parameter x, as in
x1 = x(1);
x2 = x(2);
x3 = x(3);
and you should be constructing a scalar result for that particular combination. The range of values to use for each one would then become the lower and upper bounds for those particular elements of x.

Sign in to comment.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!