Professional Documents
Culture Documents
Giải thuật di truyền GA
Giải thuật di truyền GA
if (length(e)>1900)&&(max(abs(u))<12)
Kp
Ki
Kd
J=e'*e
fitness(pop_index)=1/J;
else
J=10^100;
fitness(pop_index)=1/J;
end
6. Gii m.
Qun th thu c sau t bin s c gii m, tr v cc s thc t 01000 trong cc gen.
7. Chy th, nh gi.
Sau khi thu c qun th mi, ta s ly b s KP, KI, KD trong tng c th ri
a vo m phng kim tra. C th no cho kt qu m phng tt s c gi tr J
nh hn, tng ng l fitness-kh nng thch nghi ln hn
8. Kim tra iu kin dng.
Sau khi chy m phng kim tra ht 20 c th ta s nh gi gi tr fitness, nu
t yu cu th ta cho dng vng lp m khng cn chy ht 200 th h. Nu
khng vng lp s quay li t u, c th cho n khi 200 th h th thi.
Cui cng sau khi chy ht vng lp, ta s nh gi vng lp GA bng gi tr
th bestfit (bestfit l nhng gi tr J tt nht ca tng th h). Nu th gim
dn v sau nhiu th h m khng gim c thm na th c th coi gi tr cui
cng thu c l cc tr ca bi ton.
%cac
%nhiem sac the
while ~Terminal, %Terminal se bang 1 neu chay du 200 the he hoac trong qua
trinh chay co 1 the he con cai c bestfit thoa man sai so <epsilon
generation = generation+1;
disp(['generation #' num2str(generation) ' of maximum '
num2str(max_generation)]);
pop=Encode_Decimal_Unsigned(par,sig,dec);
parent=Select_Linear_Ranking(pop,fitness,0.2,elitism,bestchrom);
child=Cross_Twopoint(parent,cross_prob,elitism,bestchrom);
pop=Mutate_Uniform(child,mutate_prob,elitism,bestchrom);
par=Decode_Decimal_Unsigned(pop,sig,dec);
for pop_index=1:pop_size,
Kp=(par(pop_index,1)-D)/d1;
Ki=(par(pop_index,2)-D)/d2;
Kd=(par(pop_index,3)-D)/d3;
sim('SIMU.mdl');
if (length(e)>1900)&&(max(abs(u))<12)
Kp
Ki
Kd
J=e'*e
fitness(pop_index)=1/(J);
else
J=10^100;
fitness(pop_index)=1/(J);
end
end;
[bestfit(generation),bestchrom]=max(fitness);
if generation == max_generation
Terminal = 1;
elseif generation>1,
if abs(bestfit(generation)-bestfit(generation-1))<epsilon,
stall_generation=stall_generation+1;
if stall_generation == max_stall_generation, Terminal = 1;
end
else
stall_generation=0;
end;
end;
end;
plot(1./bestfit)
Kp=(par(bestchrom,1)-D)/d1
%hien thi
Ki=(par(bestchrom,2)-D)/d2
%cac nhiem sac the
Kd=(par(bestchrom,3)-D)/d3
J=1/bestfit(end) %ham tieu chuan tuong ung ca the con tot nhat do
sim('SIMU.mdl');