0% found this document useful (0 votes)
6 views9 pages

Duelist Algorithm for PID Tuning

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Duelist Algorithm for PID Tuning

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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];

Common questions

Powered by AI

Critical factors contributing to the effectiveness of the Duelist Algorithm in PID tuning include parameter constraints that ensure realistic simulation bounds, population diversity that enhances search ability, inclusion of luck in duel outcomes to prevent stagnation in suboptimal solutions, and iterative refinements through skill transfer and innovation processes that power gradual solution improvements across generations .

The winner in the Duelist Algorithm is determined by comparing the fitness vector of two duelists adjusted by a 'Luck' factor. Each duelist's fitness vector is multiplied by a luck coefficient plus a random value scaled by twice the luck coefficient. If the adjusted fitness of one duelist is less than or equal to the other, the first is declared the winner; otherwise, the second wins .

The concept of 'Luck' in the Duelist Algorithm introduces variability beyond deterministic fitness, affecting the duel's outcome by incorporating a random factor. Each duelist's fitness is adjusted by adding a product of its fitness and a random factor scaled by a luck coefficient. This ensures that less fit duelists can occasionally win, maintaining genetic diversity and avoiding premature convergence .

The PID configuration parameter dictates whether a simulation adopts a PI or PID structure. When PIDconfig is set to 0, the system configures as a PI controller, ignoring the derivative component (Td). Conversely, when set to 1, it considers all PID parameters (including Td). This configuration influences how the simulation models the controller behavior, directly affecting the outcome of each duel .

The variables K, T, D, and L within the Duelist Algorithm are constrained by upper and lower bounds based on system parameters: gain (K), time constant (T), dead time (D), and lambda (L), defined by KUB/KLB, TUB/TLB, DUB/DLB, and LUB/LLB respectively. This signifies that the algorithm operates within preset boundaries suitable for tuning the PID parameters, ensuring that simulations remain within realistic and effective control settings .

Within the Duelist Algorithm, the IAE is calculated during simulations from the difference between the system response and a setpoint over time. This metric represents the controlled system's performance, where the aim is to minimize the IAE, thus improving the fitness of the tuning parameters. The lower the IAE, the better the controller's performance, as it indicates a reduced error over time .

The 'Skill Transfer + Innovate' phase aims to combine genetic information from duelists and introduce innovations. The winner of a duel gets to replace part of its genetic code with the loser's, effectively transferring the 'skills.' Additionally, there is a probability-driven innovation step, where individual bits are randomly inverted with a probability equal to 'ProbInnovate,' allowing for the introduction of new genetic variations .

The 'Population' parameter defines the number of potential solutions (duelists) explored in parallel during the optimization process. A larger population increases the diversity of solutions, potentially improving the algorithm's ability to avoid local minima and explore the solution space more effectively but at the expense of increased computational complexity .

The main objective of the Duelist Algorithm is to optimize PID parameters by iteratively simulating various configurations and selecting the most effective solutions based on their performance metrics, specifically the minimized IAE. By organizing competing configurations into duels and evolving them across generations, the algorithm refines controller settings to enhance system stability and responsiveness .

The transition from one generation to the next in the Duelist Algorithm involves sorting duelists based on fitness, with a proportion of the best-performing ones retained. Additionally, selective replacements and innovations—guided by 'Skill Transfer + Innovate' operations—maintain genetic diversity by allowing new genetic structures and varying competitiveness among duelists .

You might also like