主题:遗传算法、模糊PID
请教大家两个问题,1、用模糊控制和PID控制结合,输入偏差和偏差的微分,当大于某一个值时用模糊,小于的时候用PID,我计划的是用max和min模型与那个值做比较,请问那个值用哪个模型给定哦? 有人说用一个模型就可以完成,请问那个模型是什么呢?
2、遗传算法。我看了个程序是求最大值的,自己写了些注释,不知道对不对。我想改成求最小值,但是中间不明白,请教高手,谢谢
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
%parameters
Size=100; %种群个体100个
G=50; %代数50代
CodeL=10; %染色体长度20/2
umax=2.048;
umin=-2.048; %2^(CodeL+1)/1000
E=round(rand(Size,2*CodeL)); %编码初始化.rand产生矩阵,size为行,codel为列
%Main Program
for k=1:1:G
time(k)=k;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;
%Uncoding
m1=m(1:1:CodeL);
for i=1:1:CodeL
y1=y1+m1(i)*2^(i-1);
end
x1=(umax-umin)*y1/1023+umin;
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
x2=(umax-umin)*y2/1023+umin; %轮盘赌
F(s)=100*(x1^2-x2)^2+(1-x1)^2; %目标函数
end
Ji=1./F;
%------1.Evaluate BestJ------求Evaluate的值
BestJ(k)=min(Ji);
fi=F; %适应度
[Oderfi,Indexfi]=sort(fi); %适应度从小到大排列
Bestfi=Oderfi(Size); %把最好的赋值给Bestfi
BestS=E(Indexfi(Size),:); %let BestS=E(m),m is the Indexfi belong to max(fi)
bfi(k)=Bestfi;
%--------------2.Select and Reproduct Operation----------选择、重生成
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=ceil(fi_Size); %选择较大的一组(5个)
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i) %选择、重生成
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1;
end
end
%-------------交叉--------------
pc=0.90; %交叉概率90%
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;
%pm=0.001-[1:1:Size]*(0.001)/Size; %fi更大,pm更小
%pm=0.0; %没有变异
pm=0.05; %变异概率5%
for i=1:1:Size
for j=1:1:2*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
Max_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best F');
2、遗传算法。我看了个程序是求最大值的,自己写了些注释,不知道对不对。我想改成求最小值,但是中间不明白,请教高手,谢谢
%Generic Algorithm for function f(x1,x2) optimum
clear all;
close all;
%parameters
Size=100; %种群个体100个
G=50; %代数50代
CodeL=10; %染色体长度20/2
umax=2.048;
umin=-2.048; %2^(CodeL+1)/1000
E=round(rand(Size,2*CodeL)); %编码初始化.rand产生矩阵,size为行,codel为列
%Main Program
for k=1:1:G
time(k)=k;
for s=1:1:Size
m=E(s,:);
y1=0;y2=0;
%Uncoding
m1=m(1:1:CodeL);
for i=1:1:CodeL
y1=y1+m1(i)*2^(i-1);
end
x1=(umax-umin)*y1/1023+umin;
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
y2=y2+m2(i)*2^(i-1);
end
x2=(umax-umin)*y2/1023+umin; %轮盘赌
F(s)=100*(x1^2-x2)^2+(1-x1)^2; %目标函数
end
Ji=1./F;
%------1.Evaluate BestJ------求Evaluate的值
BestJ(k)=min(Ji);
fi=F; %适应度
[Oderfi,Indexfi]=sort(fi); %适应度从小到大排列
Bestfi=Oderfi(Size); %把最好的赋值给Bestfi
BestS=E(Indexfi(Size),:); %let BestS=E(m),m is the Indexfi belong to max(fi)
bfi(k)=Bestfi;
%--------------2.Select and Reproduct Operation----------选择、重生成
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=ceil(fi_Size); %选择较大的一组(5个)
kk=1;
for i=1:1:Size
for j=1:1:fi_S(i) %选择、重生成
TempE(kk,:)=E(Indexfi(i),:);
kk=kk+1;
end
end
%-------------交叉--------------
pc=0.90; %交叉概率90%
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;
%pm=0.001-[1:1:Size]*(0.001)/Size; %fi更大,pm更小
%pm=0.0; %没有变异
pm=0.05; %变异概率5%
for i=1:1:Size
for j=1:1:2*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
Max_Value=Bestfi
BestS
x1
x2
figure(1);
plot(time,BestJ);
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best F');