Ga algorithm fitness value not improving
7 次查看(过去 30 天)
显示 更早的评论
Hello users,
This is the first time I'm using genetic algorithms. I am attempting to tune PID gains. As I observe the results I find that the fitness value is not improving, infact it's not changing at all. I am wondering what I am doing wrong. Before running the code I first initized k1=[1 1 1]. Below is my script written as clearly as I could.
% Inner Loop Controller
%Population range
InitialPopulationRange1 = [50 50 10;inf inf inf];
var_num1=3; % number of variables
lb1=[50 50 10]; % solution lower bound
ub1=[inf inf inf]; % solution upper bound
%Initial population
PopulationSize = 10; %Population size
InitialPopulationMatrix1=rand(PopulationSize,3);
MaxGenerations=10; % max no. of generations
MaxStallGenerations=5; %max generations when solution doesn't change
% ga options
ga_opt1 = optimoptions(@ga,'MaxGenerations',MaxGenerations,'MaxStallGenerations',MaxStallGenerations,'PopulationSize',PopulationSize,'SelectionFcn',{@selectiontournament,4},'FitnessScalingFcn',@fitscalingprop,'MutationFcn',{@mutationadaptfeasible},'PlotFcn',@gaplotbestf);
% Reproduction options
ga_opt1.EliteCount = 0;
ga_opt1.CrossoverFraction=0.8;
obj_fn=@(k) optimization_PID(k1);
% ga Command
[k1,best]=ga((obj_fn),var_num1,[],[],[],[],lb1,ub1,[],ga_opt1)
0 个评论
采纳的回答
Sam Chak
2023-8-17
To observe convergence, you should consider increasing the values of 'PopulationSize' and 'MaxGenerations'. In the example provided below, it's evident from the GA Best plot that convergence takes place within 150 generations.
%Population range
InitialPopulationRange1 = [50 50 10; inf inf inf];
var_num1 = 3; % number of variables
lb1 = [50 50 10]; % solution lower bound
ub1 = [inf inf inf]; % solution upper bound
%Initial population
PopulationSize = 25; %Population size
InitialPopulationMatrix1 = rand(PopulationSize, 3);
MaxGenerations = 150; % max no. of generations
MaxStallGenerations = 5; %max generations when solution doesn't change
% ga options
ga_opt1 = optimoptions(@ga,'MaxGenerations',MaxGenerations,'MaxStallGenerations',MaxStallGenerations,'PopulationSize',PopulationSize,'SelectionFcn',{@selectiontournament,4},'FitnessScalingFcn',@fitscalingprop,'MutationFcn',{@mutationadaptfeasible},'PlotFcn',@gaplotbestf);
% Reproduction options
ga_opt1.EliteCount = 0;
ga_opt1.CrossoverFraction=0.8;
obj_fn = @optimization_PID;
% ga Command
[k1, best] = ga((obj_fn), var_num1, [], [], [], [], lb1, ub1, [], ga_opt1)
% Test multivariate convex function
function J = optimization_PID(k1)
J = (k1(1) - 60)^2 + (k1(2) - 70)^2 + (k1(3) - 80)^2;
end
4 个评论
Walter Roberson
2023-8-18
You are copying the k values as text and passing those into the functions. But with the default MATLAB output format, you are getting at most 4 digits of precision in the values... and you need higher precision in order to get accurate cost function values.
Instead of displaying k1 the way you are now, use
fprintf("%.17g %.17g %.17g\n", k1);
There are some IEEE 754 Double Precision numbers that need 17 digits to exactly reproduce.
Using the default format short will always be too few digits for accurate calculations with nonlinear functions.
Using format long g is better but only displays one few digit than is required for accurate reproduction in most cases... two fewer in some cases.
Sam Chak
2023-8-19
See my comment below.
rng default
%Population range
InitialPopulationRange1 = [50 50 10; inf inf inf];
var_num1 = 3; % number of variables
lb1 = [50 50 10]; % solution lower bound
ub1 = [inf inf inf]; % solution upper bound
%Initial population
PopulationSize = 25; %Population size
InitialPopulationMatrix1 = rand(PopulationSize, 3);
MaxGenerations = 150; % max no. of generations
MaxStallGenerations = 5; %max generations when solution doesn't change
% ga options
ga_opt1 = optimoptions(@ga,'MaxGenerations',MaxGenerations,'MaxStallGenerations',MaxStallGenerations,'PopulationSize',PopulationSize,'SelectionFcn',{@selectiontournament,4},'FitnessScalingFcn',@fitscalingprop,'MutationFcn',{@mutationadaptfeasible},'PlotFcn',@gaplotbestf);
% Reproduction options
ga_opt1.EliteCount = 0;
ga_opt1.CrossoverFraction=0.8;
obj_fn = @optimization_PID;
% ga Command
[k1, best] = ga((obj_fn), var_num1, [], [], [], [], lb1, ub1, [], ga_opt1)
If you want to assess the precision of the PID gains returned by the Genetic Algorithm, follow @Walter Roberson's advice.
fprintf("%.17g %.17g %.17g\n", k1)
If you directly plug the corresponding gains into the same cost function, 'optimization_PID()', you will obtain the same fitness value as the 'best' value.
J1 = optimization_PID(k1)
However, if you merely use the displayed 4-digit precision values, the computed fitness value () will not match the 'best' value (). If the error between the computed fitness value and the 'best' value is not significant, and if , then you can be sure that the displayed 4-digit precision values are indeed 'better' gains. This is because they are much closer to the true optimal values [60, 70, 80]."
J2 = optimization_PID([60.0001 70.0017 80.0020])
% Test multivariate convex function
function J = optimization_PID(k1)
J = (k1(1) - 60)^2 + (k1(2) - 70)^2 + (k1(3) - 80)^2;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!