Ga algorithm fitness value not improving

7 ビュー (過去 30 日間)
Phumelele Magagula
Phumelele Magagula 2023 年 8 月 16 日
コメント済み: Sam Chak 2023 年 8 月 19 日
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)

採用された回答

Sam Chak
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)
Optimization terminated: maximum number of generations exceeded.
k1 = 1×3
60.0037 69.9995 80.0101
best = 1.1521e-04
% 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
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
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)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
k1 = 1×3
60.0001 70.0017 80.0020
best = 7.0672e-06
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)
60.000087400197792 70.001732581595888 80.002014388590453
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)
J1 = 7.0672e-06
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])
J2 = 6.9000e-06
% 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 ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by