0% found this document useful (0 votes)
66 views

Program Duelist Algorithm: Lampiran A

This document contains the code for a Duelist Algorithm program used for PID tuning. The algorithm uses a population of "duelists" represented by binary strings, which are evaluated on a plant model. Duelists compete in a tournament-style process, with the winner's traits being passed on and others innovating to improve performance over generations. The best duelist from the final generation provides the optimized PID parameters.

Uploaded by

Rizal Rivaldi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Program Duelist Algorithm: Lampiran A

This document contains the code for a Duelist Algorithm program used for PID tuning. The algorithm uses a population of "duelists" represented by binary strings, which are evaluated on a plant model. Duelists compete in a tournament-style process, with the winner's traits being passed on and others innovating to improve performance over generations. The best duelist from the final generation provides the optimized PID parameters.

Uploaded by

Rizal Rivaldi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lampiran A

Program Duelist Algorithm

clear all;
close all;
clc;

systemorder = 1 %0 for 0-order, 1 for


FOPDT, 2 for SOPDT
PIDconfig = 0 %0 for PI, 1 for PID

KLB = 0.47833035 %Gain Master Lower bound


KUB = 0.47833035 %Gain Master Upper bound

%Keep Time constant all One for Non-1 order


system
TLB = 577.938 %Time constant Master Lower
bound
TUB = 577.938 %Time constant Master Upper
bound

%Keep Dead time all zero for system without


deadtime
DLB = 0 %Dead time Master Lower bound
DUB = 0 %Dead time Master Upper bound

%Keep Lambda all One for Non-2 order system


LLB = 1; %Lambda Master Lower bound
LUB = 1 %Lambda Master Upper bound

pause(1);
HasilAkhir = [];

for K = KLB:KUB

for T = TLB:TUB
for D = DLB:DU
for L = LLB:LUB
Hasiliterate = [];
Hasilmax=[];
TReb1 = [];
fitnessvector =[];
XDueler=[];
minmax = 'min';
Population = 20; %Jumlah populasi
MaxGeneration = 15; %Maximum generasi
FightCapabilities = 20;
Champion = 0.1;
ProbLearning = 0.9;
ProbInnovate = 0.1;
Luckcoeff = 0.01;
LuckA = 0.01;
LuckB = 0.01;
Duelist = [];
DuelistInteger = [];
eIntSc = [];
esched = [];
Datafit = [];
Data1fit = [];
DataSort = [];
ElitDuelist = [];
HMI = [];
DataFDAfit = [];
Datafitplot = [];
maxall = [];
Dimension = 2;
UB = [2.54 0.99];%Upper bound PID, with [P I]
LB = [ 0 0];%Lower bound PID, with [P I]
RangeB = UB - LB;
if
(strcmp(minmax,'max'))
mm = 1;
else
mm = -1;
end
%=====Registrasi Duelist=====
Duelist =
floor(rand(Population,(FightCapabilities*Dimen
sion))+rand());
%=====Binary to Int=====
for i = 1:Dimension
for j = 1:Population
DuelistInteger(j,i) =
bi2de(Duelist(j,((i*FightCapabilities-
FightCapabilities)+1):(i*FightCapabilities)),'
left-msb');
end
end
Datafit = [];
for k = 1:Population
for ii=1:Dimension
X0(ii,k) =
(((DuelistInteger(k,ii)+1)/(2^FightCapabilitie
s))*RangeB(ii))+LB(ii);
end
end
for jj = 1:Population
Kp = X0(1,jj);
Ti = X0(2,jj);
if PIDconfig == 1
Td = X0(3,jj);
else
Td = 0;
end
if (systemorder == 1)
sim('PlantCas');

end
response = simout;
setpoint = 1;
IAE = 0;
IAEt = 0;
for i = 20:201

IAEt = abs(response(i) - 1);


IAE = IAE + IAEt;
end
IAEF = mm*IAE;
cost = IAE
fitness = cost;
Datafit = [Datafit fitness]
[fitemax,nmax] = max(Datafit)
end
disp('DA Processing');
for Generasi = 1:MaxGeneration
%=====DA Processing===
if (Generasi > 1)
clc
Generasi

fitnessmax

%=====sortir=====

sort_fit = sortrows(sort,
(FightCapabilities*Dimension) + 1);
Duelist1 =
sort_fit(randperm(size(sort_fit,1)),:);
Remain = sort_fit(round((1-
Champion)*Population) + 1:Population, :);
Winner = [];
X = Duelist1;
N = size(X,1);
if mod(N,2) == 0
M=N;
else
M=N-1;
end
for i=1:M
fitnessvector(i) =
X(i,(FightCapabilities*Dimension) + 1);
end
fitnessvector = fitnessvector';

%=====Setting Duelist=====
for i=1:M
XDueler = X;
end

%=====Setting Duel Arena=====

for i=1:2:M-1
LuckA = (fitnessvector(i)*(Luckcoeff +
rand*2*Luckcoeff));

LuckB = (fitnessvector(i+1)*(Luckcoeff +
rand*2*Luckcoeff));
if fitnessvector(i)+LuckA <=
fitnessvector(i+1)+LuckB

Winner(i) = 0;

Winner(i+1) = 1;

elseif fitnessvector(i)+LuckA >


fitnessvector(i+1)+LuckB

Winner(i) = 1;

Winner(i+1) = 0;

end
end

%=====Skill Transfer + Innovate=====

[M,d] = size(XDueler);

XAftermatch = XDueler; for i=1:2:M-1


if (Winner(i)==1)
p = ceil(((d/2)-1)*rand);
str = ceil(p+1+(((d/2)-2-p)*rand));
XAftermatch(i,:) = [XDueler(i,1:p)
XDueler(i+1,p+1:str) XDueler(i,str+1:d)];

for j=1:d

p = rand;

if (p<=ProbInnovate)
XAftermatch(i+1,j) = abs(1-XDueler(i+1,j));
end

end

else

p = ceil(((d/2)-1)*rand);

str = ceil(p+1+(((d/2)-2-p)*rand));

XAftermatch(i+1,:) = [XDueler(i+1,1:p)
XDueler(i,p+1:str) XDueler(i+1,str+1:d)];

XAftermatch(i,:) = XDueler(i,:);

for j=1:d

p = rand;

if (p<=ProbInnovate)

XAftermatch(i,j) = abs(1-XDueler(i,j));

end

end

end
Xnew = XAftermatch;
sort_fitnew = sortrows(Xnew,
(FightCapabilities*Dimension) + 1);
Duelistnew =
sort_fitnew(round((Champion)*Population)+1:Pop
ulation,:)
Duelist =
[Duelistnew(:,1:(FightCapabilities*Dimension))
;Remain(:,1:(FightCapabilities*Dimension))];
end;
ElitDuelist = [ElitDuelist; Duelist];
for i = 1:Dimension
for j = 1:Population
DuelistInteger(j,i) =
bi2de(Duelist(j,((i*FightCapabilities-
FightCapabilities)+1):(i*FightCapabilities)),'
left-msb');
end
end
Datafit = [];
for po = 1:Population
for ii=1:Dimension
X0(ii,po) =
(((DuelistInteger(po,ii)+1)/(2^FightCapabiliti
es))*RangeB(ii))+LB(ii);
end
end
for jj = 1:Populatio
Kp = X0(1,jj);
Ti = X0(2,jj);
if PIDconfig == 1
Td = X0(3,jj);
else
Td = 0;
end
if (systemorder == 1)

sim('PlantCas');
end

response = simout;

setpoint = 1;
IAE = 0;
IAEt = 0;
for i = 20:201
IAEt = abs(response(i) - 1);
IAE = IAE + IAEt;
end
IAEF = mm*IAE;
cost = IAEF;
fitness = cost;
Datafit = [Datafit fitness];
[fitemax,nmax] = max(Datafit);
End
Data1fit = Datafit;
[fitnessmax, nmax] = max(Data1fit)
DataFDAfit = [DataFDAfit;fitnessmax];
Datafitplot = [Datafitplot;fitnessmax];
BalikData = Datafitplot;
h = plot (BalikData);
hold on
refreshdata (h, 'caller')
drawnow;
hold off
DuelistMax = Duelist(nmax,:);
DuelistMaxLast = DuelistMax;
Hasilmax = DuelistMax;
sort = [Duelist Datafit'];
maxall = [maxall; sort];
for i = 1:Dimension
HasilMaxInt(1,i) =
bi2de(Hasilmax(1,(((i*FightCapabilities)-
FightCapabilities)+1):(i*FightCapabilities)),'
left-msb');
end
HMIt = [];
for ij=1:Dimension HMIt = [HMIt,
HasilMaxInt(1,ij)];
end
HMI = [HMI; HMIt];
end
plot(DataFDAfit);
hold on
[fitnessmaxf, nmaxf] = max(DataFDAfit);
for ik=1:Dimension
X0maxfix(ik) =
(((HMI(nmaxf,ik)+1)/(2^FightCapabilities))*Ran
geB(ii))+LB(ii)
end
X0maxfix
[fitnessmaxf, nmaxf] = max(DataFDAfit)
Hasiliterate =[];
if (PIDconfig == 1)
Hasiliterate = [K, T, D, L, X0maxfix(1),
X0maxfix(2), X0maxfix(3), IAE]
else
Hasiliterate = [K, T, D, L, X0maxfix(1),
X0maxfix(2), 0 , IAE]
end
HasilAkhir = [HasilAkhir; Hasiliterate];

You might also like