Advanced Optimization Using MATLAB by Endalew
Advanced Optimization Using MATLAB by Endalew
%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%% Q2 Minmization of constrained fitness function using GA
% by Dr Endalew Ayenew
% objective function
function y=myFitness(x)
y= 100*(x(1)^2-x(2))^2+(1-x(1))^2
end
% Constraint function
function [C, Ceq]= myConstraints(x)
C= [1.5+x(1)*x(2)+x(1)-x(2);10-x(1)*x(2)]; %linear equality
constraints:
Ceq=[];% No nonlinear equality constraints:
end
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
function f = confun(x)
c0= [];
% Q6 is changed to maximization
% Maximization of objective function
of= 10*(x(1)-1)^2+20*(x(2)-2)^2+30*(x(3)-3)^2;
% constraints (all constraints must be converted into ,= 0
type)
% if there is no constraints then comments all c0 lines below
c0(1) = x(1)+x(2)+x(3)-5; % <=0 type constaint
c0(2) = x(1)^2+2*x(2)-x(3); % <=0 type constaint
%define penalty for each constraint
w= [500 20]; % penalty weight on each constraint vaiolation
p= 0;
for i = 1:length(c0)
if c0(i)>0
p= p+w(i)*abs(c0(i)); % penalty
end
end
f = of + p; % objective function + penalty
end
% main PSO program for maximization of constrained problem
tic
clear all
close all
LB= [0 0 0]; % Lower bound
UB= [10 10 10];% upper bound
VLB= -abs(UB-LB); VUB= abs(UB-LB); % Lower/Upper
bound of velocity
% PSO parameters values
m = 3; % no. of variables or swarms
n= 100; % population size
wmax = 0.9; % max inertia
wmin = 0.4; % min inertia
c1 = 2; c2 = 2; % acceleration factors
% PSO main program
maxite = 1000; % max no. of iteration
maxrun = 1; % max no. of runs or populations
for run = 1:maxrun
% PSO intialization
for i = 1:n % max no. of runs or populations
for j = 1:m % no. of variables
x0(i,j) = LB(j)+rand*(UB(j)-LB(j));
end
end
x = x0; % initial population
v = 0.1*x0; % initial velocity
for i = 1:n
f0(i)= confun(x0(i,:));
end
[fmin0,index0]=min(f0);
pbest =x0; % initial personel best
gbest =x0(index0,:); % initial global best
% PSO Algorithm stated
ite = 1;
tolerance =1;
while ite <= maxite && tolerance> 10^-12
w = wmax-(wmax-wmin)*ite/maxite; % update inertia
% PSO velocity updates
for i = 1:n
for j = 1:m
v(i,j) = w*v(i,j)+c1*rand()*(pbest(i,j)-x(i,j))
+c2*rand()*(gbest(j)-x(i,j));
v(i,j)= min(max(VLB(j),v(i,j)),VUB(j)); % velocity
clamping
end
end
% PSO position update
for i = 1:n
for j = 1:m
x(i,j) = x(i,j)+v(i,j);
x(i,j)= min(max(LB(j),x(i,j)),UB(j)); % position
clamping
end
end
% evaluating fitness
for i = 1:n
f(i) = confun(x(i,:));
end
% updateing pbest and fitness
for i = 1:n
if f(i)>f0(i) % < for minimization is changed to > for
maximization
pbest(i,:)= x(i,:);
f0(i)= f(i);
end
end
[fmin,index]=min(f0); % Finding out the best particle
ffmin(ite, run)=fmin; % storing best fitness
ffite(run)=ite; % storing iteration count
% updating gbest and best fitness
if fmin>fmin0 % < for minimization is changed to > for
maximization
gbest = pbest(index,:);
fmin0 =fmin;
end
% calculating tolerance
if ite>100
tolerance = abs(ffmin(ite-100,run)-fmin0);
end
%displaying relative results
if ite == 1
fprintf('Iteration Best particle Objective function\n');
end
fprintf('%8g %8g %8.4f\n', ite, index, fmin0);
ite = ite+1;
end
% PSO algorithm end
gbest;
fvalue=10*(gbest(1)-1)^2+20*(gbest(2)-2)^2+30*(gbest(3)-
3)^2;
fff(run)=fvalue;
rgbest(run,:)=gbest;
fprintf('-----------------------------------------\n');
%end
end
% PSO main program is ended
fprintf('\n');
fprintf('*********************************************
*\n');
fprintf('Final result\n');
[bestfun, bestrun]=min(fff)
best_variables=rgbest(bestrun,:)
fprintf('*********************************************
*\n');
toc
% PSO Convergence characteritics
plot(ffmin(1:ffite(bestrun),bestrun),'-k');
xlabel('Fitness function value');
title('PSO Convergence characteritics')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
function J= EvalObj_PSOPID(x)
% Retrieve PID controller parameters
Kp= x(1); Ki= x(2); Kd= x(3);
% Plant
s= tf('s');
Gc= Kp+Ki/s+Kd*s; % PID controller TF
K= 1; T= 5; L= 2;
Gp= K*exp(-L*s)/(T*s+1); % Plant TF
% closed loop TF
G= feedback(Gp*Gc,1); % Overall TF from yr to y
% Unit step response for 40seconds
h= 0.01; t= 0:h:40; r= 1;
y= step(G,t);
% Calculate IAE using the trapezoidal integration rule
e= abs(r-y); % IAE
% e= (r-y).^2; % ISE
% e= t'.*abs(r-y); % ITAE (t must be transposed)
J= 0.5*h*(e(1)+2*sum(e(2:end-1))+e(end));
end
% main function
rng default % Initialize the random number generator
options = optimoptions('particleswarm');
options.PlotFcn= 'pswplotbestf';
options.MaxIterations= 100; % default 200 x nvar
nvar= 3;
LB= [0 0 0];
UB= [10 10 10];
[x,fval]=
particleswarm(@EvalObj_PSOPID,nvar,LB,UB,options)
% Simulate the PID control system tuned by PSO
clf
Kp= 2.2560; Ki= 0.3856; Kd= 1.8355;
s= tf('s'); K= 1; T= 5; L= 2; h= 0.01;
Gc= Kp+Ki/s+Kd*s;
Gp= K*exp(-L*s)/(T*s+1);
G= Gc*Gp/(1+Gc*Gp);
t= 0:h:40;
figure
y= step(G,t);
plot(t,y,'linewidth',2), axis([0 40 0 1.5])
xlabel('t'), ylabel('y(t)')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%