helieshan
2009-05-13, 11:37
clear all;
close all;
Size=30;CodeL=10;G=100;
Minx(1)=zeros(1);
Maxx(1)=20*zeros(1);
Minx(2)=zeros(1);
Maxx(2)=1.0*zeros(1)
Minx(3)=zeros(1);
Maxx(3)=1.0*zeros(1);
E=round(rand(Size,3*CodeL));
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;y3=0;
m1=m(1:1:CodeL)
y1=y1+m1(i)*2^(i-1);
end
kp=(Maxx(1)-Minx(1))*y1/1023+Minx(1);
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
ki=(Maxx(2)-Minx(2))*y2/1023+Minx(2);
m3=m(2*CodeL+1:1:3*CodeL);
for i=1:1:CodeL
y3=y3+m3(i)*2^(i-1);
end
kd=(Maxx(3)-Minx(3))*y3/1023+Minx(3);
end
end
a=zeros(1,30);
for i=1:30;
a(i,1)=pid(kp(i),ki,(i),kd(i));
end
function F=pid(kp,ki,kd)
num1=[500*ki,500*kp,500*kd];
den1[1 400 0 0];
[mum,den]=cloop(num1,den1,-1);
t=0:0.1:10;
[y,x,t]=step(num,den,t);
e=abs(1-y);
jn=t*e;
F=1/jn;
cm=max(jn);
F=cm-jn;
[0derfi,indexfi]=sort(F);
Bestfi=oderfi(Size);
BestS=E(indexfi(Size),:);
fi_S=floor(fi_Size);
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i)
TempE(kk,:)=E(indexf(i),:);
kk=kk+1
end
end
pc=0.6;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if pc>temp
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;
pm=0.001-[1:1:Size]*(0.001)/Size;
for i=1:1:Size
for j=1:1:3*CodeL
temp=rand;
if pm>temp
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end
TempE(Size,:)=BestS;
E=TempE;
end
BestF
BestS
kp
ki
kd
plot(t,y);
close all;
Size=30;CodeL=10;G=100;
Minx(1)=zeros(1);
Maxx(1)=20*zeros(1);
Minx(2)=zeros(1);
Maxx(2)=1.0*zeros(1)
Minx(3)=zeros(1);
Maxx(3)=1.0*zeros(1);
E=round(rand(Size,3*CodeL));
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;y3=0;
m1=m(1:1:CodeL)
y1=y1+m1(i)*2^(i-1);
end
kp=(Maxx(1)-Minx(1))*y1/1023+Minx(1);
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
ki=(Maxx(2)-Minx(2))*y2/1023+Minx(2);
m3=m(2*CodeL+1:1:3*CodeL);
for i=1:1:CodeL
y3=y3+m3(i)*2^(i-1);
end
kd=(Maxx(3)-Minx(3))*y3/1023+Minx(3);
end
end
a=zeros(1,30);
for i=1:30;
a(i,1)=pid(kp(i),ki,(i),kd(i));
end
function F=pid(kp,ki,kd)
num1=[500*ki,500*kp,500*kd];
den1[1 400 0 0];
[mum,den]=cloop(num1,den1,-1);
t=0:0.1:10;
[y,x,t]=step(num,den,t);
e=abs(1-y);
jn=t*e;
F=1/jn;
cm=max(jn);
F=cm-jn;
[0derfi,indexfi]=sort(F);
Bestfi=oderfi(Size);
BestS=E(indexfi(Size),:);
fi_S=floor(fi_Size);
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i)
TempE(kk,:)=E(indexf(i),:);
kk=kk+1
end
end
pc=0.6;
n=ceil(20*rand);
for i=1:2:(Size-1)
temp=rand;
if pc>temp
for j=n:1:20
TempE(i,j)=E(i+1,j);
TempE(i+1,j)=E(i,j);
end
end
end
TempE(Size,:)=BestS;
E=TempE;
pm=0.001-[1:1:Size]*(0.001)/Size;
for i=1:1:Size
for j=1:1:3*CodeL
temp=rand;
if pm>temp
if TempE(i,j)==0
TempE(i,j)=1;
else
TempE(i,j)=0;
end
end
end
end
TempE(Size,:)=BestS;
E=TempE;
end
BestF
BestS
kp
ki
kd
plot(t,y);