# Error when using genetic algorithm

6 views (last 30 days)

Show older comments

Currently I am trying to use genetic algorithm to optimize the parameters of the controller. The optimized controller then is to be applied on a multivariable space robot. In the simulation process I encounter an error which bother me for quite some time and I cant find any solution about it.

I always receive the error message " Failure in user-supplied fitness function evaluation. GA cannot continue." when ever I try to activate the simulation.Anyone can kindly give me some advice on how to solve this error? The attachments are the program and the simulink model that I am using. The main file is "Main_Genetic_Algorithm.m"

##### 8 Comments

Geoff Hayes
on 1 Aug 2014

### Accepted Answer

Geoff Hayes
on 1 Aug 2014

The error message Failure in user-supplied fitness function evaluation. GA cannot continue indicated that there was a problem with the fitness function, calculation_error. In Main_Genetic_Algorithm.m, the fitness function and options to the genetic algorithm were being set as

%Place function handle to the function above

fitnessfunction = @calculation_error;

%Number of variables to be optimized

nvar = 11;

%Perform optimization via genetic algorithm

%Set the settings for the genetic algorithm

%Define the population size, stall generation limit and plot functions

options = gaoptimset('PopulationSize',300,'StallGenLimit',100,'PlotFcns',

{@gaplotbestf,@gaplotstopping});

%Define the initial range for the variables

options = gaoptimset(options,'PopInitRange',[1.454 3.331 1.046 0.08953 1.335 2.435 0.5701 2.285

0.4707 0.7709 4.299;2.577 6.711 2.045 0.7713 3.811 3.484 0.944 4.478 0.7994 1.838 4.99]);

% options = gaoptimset(options,'PopInitRange',[1.454;2.577]);

%Define the information to be seen on the command window

options = gaoptimset(options,'Display','diagnose');

%Define the setting for the mutation

options = gaoptimset(options,'MutationFcn',{@mutationuniform,.1});

%Switch on vectorization

options=gaoptimset(options,'Vectorize','on');

%Set population type

options=gaoptimset(options,'PopulationType','doubleVector');

x = ga(fitnessfunction,nvar,[],[],[],[],[],[],[],[],options);

From the documentation on the input fitness function, see GA Fitness Function for details, it stated that

The fitness function should accept a row vector of length nvars and return a scalar value. When the 'Vectorized' option is 'on', fitnessfcn should accept a pop-by-nvars matrix, where pop is the current population size. In this case fitnessfcn should return a vector the same length as pop containing the fitness function values. fitnessfcn should not assume any particular size for pop, since ga can pass a single member of a population even in a vectorized calculation.

Given that the defined GA options included the Vectorize being set to on, then this would have meant that the calculation_error code should be expecting a matrix input rather than a vector. As this fitness function wasn't designed to handle a matrix, we removed the Vectorize set to on statement.

As well, the fitness function must only accept one input row vector of 11 variables (for each member of the population). The function signature for calculation_error was defined to be

function error = calculation_error(output1,output2)

with two inputs rather than the expected one. When we tested out the fitness function defined as

fitnessfunction = @(X)calculation_error(X,zeros(1,11));

with a dummy set of data for the second input, then the error (from before) was no longer evident.

A review of the fitness function is needed to ensure that it meets the requirements of the GA, and that is written in such a way that when provided an input (from any member within the population) then the output is such that it can be determined whether the input minimizes the function (or not).

##### 0 Comments

### More Answers (0)

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!