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

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');
then from GA Input Args the following is true for your fitness function
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?
Hello Geoff
Thank you for taking time looking into my problem which bothered me for quite some time and I am still trying to figure out what is wrong with it.
1)The two input vectors (output1 and output2) are the difference of responses between the desired system and the actual system. They are obtained via simulation in Simulink.The desired closed loop system is expressed in terms of linear transfer function and the actual nonlinear system is expressed mathematically.The simulation of the Simulink produces the outputs ----> output1 and output2 which are the error for channel 1 and channel 2 respectively. (My system is a 2 by 2 system and you can refer to the attached Simulink model)
Then the error of the system which obtained from the simulation of the Simulink model are calculated via "calculation_error" . And for the next step I want to apply genetic algorithm to minimize the error mentioned.
2)nvar is set to 11 because there are 11 controller parameters that I want the genetic algorithm to optimize and finally when applied on the nonlinear system,I can get the responses that I am aiming for.They are not the dimension of the input vectors.
3)I made a mistake by switching the 'Vectorize' function on.Thanks for pointing that out.
I hope I did answer your questions Geoff.Hope to hear from you soon and sorry for the long explanation.
I am new to MATLAB and Simulink and I hope I can get some guidance to solve this problem.
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
Kah Wei 2014년 7월 30일
Hi Geoff.
1. Yes you are right. Output_1 and output_2 are generated in the workspace from the call to sim(‘Taguchi_GA_alternative’). I attach a picture of the Simulink model that I applied on the simulation. Hope you can get a clearer picture on how the Simulink model that I am using is able to calculate the error.
2. 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.
3.I am sorry I don’t understand what you mean by GA is only supplying the first input and
for j = 1:length(output2) error2 = error2 + norm(output2(j)); end
fails since there is no output2.
4.The 11 variables are the parameters of the controller that I want to optimize by minimizing the error generated from the Simulink via the application of GA. The fitness function that I am using to perform the optimization is “calculation_error.m”. No I am not doing any kind of comparison.
Kah Wei
Kah Wei 2014년 7월 30일
Basically I am doing the followings
I have a 2 by 2 controller that I wish to optimize via GA.In this controller,there are 16 parameters.11 parameters out of the 16 parameters are the one that I need to optimized.
The fitness function for GA to be applied is constructed by measuring the error between the responses of the actual and desired system.The system that I am investigating is a 2 by 2 system.Hence "calculation_error.m" was constructed to calculate the error obtained from the simulation.
Then I apply "calculation_error.m" in GA and then I am stuck for error that I dont understand.
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
Kah Wei 2014년 8월 1일
Thanks Geoff.
I adopted your suggestion and I am not encountering the error mentioned anymore.I think I got the correct idea now on how to make the GA works in MATLAB.
I will need to modify the fitness function based on the example shown by you for my system.Thanks again Geoff for the guidance and spending time to answer my questions. :)
Geoff Hayes
Geoff Hayes 2014년 8월 1일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 1일

0 개 추천

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개)

카테고리

질문:

2014년 7월 29일

답변:

2014년 8월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by