Error when using genetic algorithm
Mostrar comentarios más antiguos
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 comentarios
Geoff Hayes
el 29 de Jul. de 2014
Your fitness function is calculation_error, so there must be something about it that the optimizer does not like. The function description
The objective of this function is to calculate the error which determine the difference between the actual and the desired response.
Could you describe how the two input vectors are used to do the above? Is the dimension of each vector, 11 (which is what nvars has been set to in the main program)?
Because you have the following line in the main program
options=gaoptimset(options,'Vectorize','on');
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.
Does the make sense given the implementation of calculation_error?
Kah Wei
el 30 de Jul. de 2014
Geoff Hayes
el 30 de Jul. de 2014
Hi Kah,
Since you have turned Vectorization to off, going back to GA Input Args, the description for fitnessfcn also states
Handle to the fitness function. The fitness function should accept a row vector of length nvars and return a scalar value.
Yet your code is passing in two row vectors...
I have neither Simulink nor the Global Optimization Toolbox, just an interest in genetic algorithms from way back, so it is unclear to me how the code in Main_Genetic_Algorithm.m gets the outputs from Simulink to calculate the error
sim('Taguchi_GA_alternative');
% [output_1,output_2,x] = activate_simulation(R1,R2,tfinal)
% %Determine the error and then perform optimization via genetic algorithm
% % [error1,x1] = calculation_error(output_1,output_2,tfinal)
error = calculation_error(output_1,output_2)
Where do output_1 and output_2 come from? Are they automatically generated in the workspace from the call to sim('Taguchi…')?
On the genetic algorithm, from your options definition, you have stated that the population size should be 300, with the population initialization range from the 2x11 vector defined for PopInitRange.
So the GA code will generate 300 members of the population, each with a 1x11 vector of variable data from this range. Then it will (probably) score each member by applying the fitness function to its 1x11 vector of data with the idea that that member of the population with the smallest score (output from the fitness function that you are trying to minimize) is "more worthy/likely" to produce children etc. for the next generation/iteration of the algorithm.
But since the calculation_error function expects two inputs, then I suspect this is why the optimization is failing with the Failure in user-supplied fitness function evaluation. GA cannot continue: the GA is only supplying the first input, and the
for j = 1:length(output2)
error2 = error2 + norm(output2(j));
end
fails since there is no output2.
You have 11 variables - what do they define? How would you write a fitness function that should be minimized given these 11 variables? Do you want to compare (in some way) these variables to the actual and desired output from Simulink?
Kah Wei
el 30 de Jul. de 2014
Kah Wei
el 30 de Jul. de 2014
Geoff Hayes
el 30 de Jul. de 2014
So you are saying that, the reason why I am encountering this error because my fitness function only accepts two inputs but I am having 11 variables that need to be optimized???I hope I get your point correct.
Not quite, Kah. The documentation for this function states that The fitness function should accept a row vector of length nvars and return a scalar value. So your fitness function, that which you are trying to minimize, can only accept a single row vector of length 11 (which is what you have set nvars to). Your fitness function, calculation_error, expects two row vectors and I think that is where you are experiencing the problem.
The GA is creating a population of 300 members, each of which starts with a random 1x11 vector of data that is taken from the interval (for each of the 11 variables) that you have provided. The GA then evaluates the fitness of each member by passing each member's vector into your fitness function. But your fitness function is expecting two vectors...not just the one.
Here's an example. Suppose I have the following fitness function for a function of three variables
function [val] = fitness_func(data)
x = data(1);
y = data(2);
z = data(3);
val = (x-1)^2 + (y-2)^2 + (z-3)^2;
So I want to find the minimum of the function (x-1)^2 + (y-2)^2 + (z-3)^2. So the GA creates a fixed-sized population of N members, assigns each a random vector of three variables (in whatever interval I have defined for each) and evolves a solution over successive generations/iterations. Ideally, at the end of X generations/iterations, one such solution would be x==1,y==2,z==3 as these three inputs produce the value of zero which is the minimum for this function.
Your fitness function must do something similar. Given the vector of 11 variables, it must evaluate in some manner to indicate if these variables "help" to minimize the function or not.
Just to see if we can get past the error, in the Main_Genetic_Algorithm.m file, replace
fitnessfunction = @calculation_error;
with
fitnessfunction = @(X)calculation_error(X,zeros(1,11));
This will allow you to use your calculation_error function with dummy data for the second input parameter. If the GA is just supplying the one input vector of 11 variables, then the above should work correctly...in the sense that we won't see the error, but you will get the wrong answer.
Kah Wei
el 1 de Ag. de 2014
Geoff Hayes
el 1 de Ag. de 2014
Great, Kah - glad I was able to help! I'm going to summarize some of the findings in a solution (below) so that others will know what to look for if they encounter a similar problem.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Genetic Algorithm en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!