0% found this document useful (0 votes)
164 views5 pages

Matlab Code of GWO Minimize Constrained Objective Function PID Controller

This document describes using the grey wolf optimizer (GWO) algorithm to optimize the parameters of a constrained objective function and tune the parameters of a PID controller for a plant model. The GWO algorithm is applied to minimize an objective function with inequality constraints by updating candidate solutions based on the best, second best and third best solutions from each iteration. The GWO algorithm is then used to tune the proportional, integral and derivative parameters (KP, KI, KD) of a PID controller to minimize the integrated time absolute error (ITAE) of the closed loop response of a plant model controlled by the PID controller. The best PID parameters found by GWO are used to design a controller that regulates the plant model

Uploaded by

Merera Taresa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
164 views5 pages

Matlab Code of GWO Minimize Constrained Objective Function PID Controller

This document describes using the grey wolf optimizer (GWO) algorithm to optimize the parameters of a constrained objective function and tune the parameters of a PID controller for a plant model. The GWO algorithm is applied to minimize an objective function with inequality constraints by updating candidate solutions based on the best, second best and third best solutions from each iteration. The GWO algorithm is then used to tune the proportional, integral and derivative parameters (KP, KI, KD) of a PID controller to minimize the integrated time absolute error (ITAE) of the closed loop response of a plant model controlled by the PID controller. The best PID parameters found by GWO are used to design a controller that regulates the plant model

Uploaded by

Merera Taresa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

%% GWO minimize constrained objective function by Dr Endalew Ayenew

function out=fungwo(x)
y1=x(:,1);
y2=x(:,2);
fx= (2*sqrt(2).*y1+y2).*100;
% inequality constraints
g(:,1)=(2.*sqrt(2).*y1+y2)./(sqrt(2).*y1.^2+2.*y1.*y2)-2;
g(:,2)=(2.*y1)./(sqrt(2).*y1.^2+2.*y1.*y2)-2;
g(:,3)=(2./(y1+sqrt(2).*y2))-2;
pp=10^9; % penalty
for i=1:size(g:1)
j=1:size(g:2);
if g(i,j)>0
penalty(i,j)= pp+g(i,j);
else
penalty(i,j)=0;
end
end
%calculate objective function
out= fx+sum(penalty,2);
end

%% main program - Gwo code to find solution


format short
clear all
clc
%intializethe parameters
fun=@fungwo;
N=300;
var=2; % y1 &y2
lb=[0 0];
ub=[1 1];
itermax=100;
%generate initial position for the 1st iteration
for i=1:N
for j=1:var
pos(i,j)=lb(:,j)+rand.*(ub(:,j)-lb(:,j));
end
end
fcost=fun(pos);
[fminvalue,ind]=min(fcost);
gbest=pos(ind,:);
iter=1;
while iter<=itermax
Fgbest=fminvalue;
a=2*(1-iter/itermax);
for i=1:N
x=pos(i,:);
pos1=pos;
A1=2.*a.*rand(1,var)-a;
C1=2.*rand(1,var);
[alpha,alphaind]=min(fcost);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-x);
x1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fcost1=fun(pos1);
%%find beta position
[bet,betind]=min(fcost1);
betpos=pos1(betind,:);
A2=2.*a.*rand(1,var)-a;
C2=2.*rand(1,var);
Dbet=abs(C2.*betpos-x);
x2=betpos-A2.*Dbet;
pos1(betind,:)=[];
fcost1=fun(pos1);
%% find delta postion
[delta,deltaind]=min(fcost1);
deltapos=pos1(deltaind,:);
A3=2.*a.*rand(1,var)-a;
C3=2.*rand(1,var);
Ddelta=abs(C3.*deltapos-x);
x3=deltapos-A3.*Ddelta;
Xnew=(x1+x2+x3)/3;
%% check bound
Xnew=max(Xnew,lb);
Xnew=min(Xnew,ub);
fnew=fun(Xnew);
%% fitness Evaluation and comparision
if fnew<fcost(i)
pos(i,:)=Xnew;
fcost(i,:)=fnew;
end
end
%update Gbest
[fmin,find]=min(fcost);
if fmin<Fgbest
Fgbest=fmin;
gbest=pos(find,:);
end
%%Memorize the best
[optval,otind]=min(fcost);
BestFx(iter)=optval;
Bestx(iter,:)=pos(otind,:);
%% show iteration information
disp(['iteration' num2str(iter) ',postion =' num2str(pos(otind,:))...
',Best Cost=' num2str(BestFx(iter))]);
%% plot cost fun
plot(BestFx,'LineWidth',2);
xlabel('iteration number');
ylabel('fitness value');
title('convergenceVs iteration')
grid on
iter=iter+1;
end % end of while
%% GWO tuned PID controller parameters by Dr Endalew Ayenew
clc; close all
% plant
n=[1 1];
d=[1 2 -3 1];
G=tf(n,d);
Gf = feedback(G,1);
step(Gf) % System step response with out controller
hold on
% GWO Parameters
iter=100; pop =60; var=3; a_gwo =2;
%Search space
al=0; au=1000; c_cf=0;

%GWO Initialization
for p =1:pop
for n=1:var
x(p,n)=al+rand*(au-al);
end
%Modelof pid controller parameters
kp=x(p,1);
ki=x(p,2);
kd=x(p,3);
% simulate model
Gc= pid(kp,ki,kd);
Gcf = feedback(Gc*G,1);
y= step(Gcf);
% ITAE (objective function)
ff1=0;
sim1=size(y);
for m1 = 1:sim1
ff1=ff1+((abs(y(m1)-1))*m1);
end
ITAE(p)=ff1;
end
% find best solution
[b_sol, b_loc]=min(ITAE);
best_ITAE=b_sol;
best_k=x(b_loc,:);
for tt=1:iter
a_gwo=2-((2*tt)/iter);
%find alph, beta,and delta
sol_cf=ITAE;
sol_var=x;
[b_sol1,loc1]=min(sol_cf);
alph_cf=b_sol1;
alph_var=sol_var(loc1,:);
sol_cf(loc1)=[];
sol_var(loc1,:)=[];
[b_sol1, loc1]=min(sol_cf);
beta_cf=b_sol1;
beta_var=sol_var(loc1,:);
sol_cf(loc1)=[];
sol_var(loc1,:)=[];
[b_sol1, loc1]=min(sol_cf);
delta_cf=b_sol1;
delta_var=sol_var(loc1,:);
c_cof=2*rand();
a_cof=a_gwo*((2*rand())-1);

for p=1:pop
for n=1:var
d_a=abs(c_cof*alph_var(n)-x(p,n));
xx1=alph_var(n)-(a_cof*d_a);
d_b=abs(c_cof*beta_var(n)-x(p,n));
xx2=beta_var(n)-(a_cof*d_b);
d_d=abs(c_cof*delta_var(n)-x(p,n));
xx3=delta_var(n)-(a_cof*d_d);
if x(p,n)>au
x(p,n)=au;
else
if x(p,n)<al
x(p,n)=al;
end
end

% Model parameters
kp=x(p,1);
ki=x(p,2);
kd=x(p,3);
% simulate model
Gc= pid(kp,ki,kd);
Gcf = feedback(Gc*G,1);
y= step(Gcf);
% ITAE (objective function)
ff1=0;
sim1=size(y);
for m1 = 1:sim1
ff1=ff1+((abs(y(m1)-1))*m1);
end
ITAE(p)=ff1;
end

%find the best solution


[b_sol, b_loc]=min(ITAE);
best2_ITAE=b_sol;
best2_k=x(b_loc,:);
if best2_ITAE<best_ITAE
best_ITAE=best2_ITAE;
best_k=best2_k;
end
c_cf=c_cf+1;
best_cf_go(c_cf)=best_ITAE;
end
end
Min_ITAE= best_ITAE;
kp=best_k(1,1);
ki=best_k(1,2);
kd=best_k(1,3);
Gc= pid(kp,ki,kd)
Gcf = feedback(Gc*G,1);
step(Gcf);

You might also like