📄 gaf.m
字号:
%综合优化
global yout rin time
rin=1.0;
a_1=subplot(3,2,1);
set(a_1,'XColor','white','YColor','white');
a_2=subplot(3,2,2);
set(a_2,'XColor','white','YColor','white');
a_3=subplot(3,2,3);
set(a_3,'XColor','white','YColor','white');
a_4=subplot(3,2,4);
set(a_4,'XColor','white','YColor','white');
pause(0.1);
if popsize<0
popsize=2;
end
if maxgen<1
maxgen=1;
end
codeL=17*lchrom+147;
parmax=2^lchrom-1;
maxx(1)=3;minx(1)=0;
maxx(2)=6;minx(2)=3;
maxx(3)=9;minx(3)=6;
maxl=4;minl=0;
%do something
%initial
bestinmf=inmf;
bestinmftri=inmftri';
bestoutmf=outmf;
bestoutmftri=outmftri';
bestrule=rule;
bestpark=[2 2 2];
code=round(rand(codeL,popsize));
bestobj=fruleobj(rule,inmf,outmf,bestpark,popsize,differ,gs);
x = (-7:1:7)';
for i=1:1:maxgen
timef(i)=i;
%select save cross num to indexobj
for t=1:1:popsize
kk=0;
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
par1=0;
for h=1:lchrom
par1=par1+ch(h)*2^(h-1);
end
par1=minl+par1/parmax*4;
tinmf(:,4,t)=trimf(x,[-par1 0 par1]);
tinmftri(:,4,t)=[-par1 0 par1];
for j=1:3
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
par=0;par1=0;
for h=1:lchrom
par=par+ch(h)*2^(h-1);
end
par=minx(j)+par/parmax*3;
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
for h=1:lchrom
par1=par1+ch(h)*2^(h-1);
end
par1=minl+par1/parmax*4;
tinmf(:,j+4,t)=trimf(x,[2*j-par 2*j 2*j+par1]);
tinmftri(:,j+4,t)=[2*j-par 2*j 2*j+par1];
tinmf(:,4-j,t)=trimf(x,[-2*j-par1 -2*j -2*j+par]);
tinmftri(:,4-j,t)=[-2*j-par1 -2*j -2*j+par];
end
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
par1=0;
for h=1:lchrom
par1=par1+ch(h)*2^(h-1);
end
par1=minl+par1/parmax*4;
toutmf(:,4,t)=trimf(x,[-par1 0 par1]);
toutmftri(:,4,t)=[-par1 0 par1];
for j=1:3
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
par=0;par1=0;
for h=1:lchrom
par=par+ch(h)*2^(h-1);
end
par=minx(j)+par/parmax*3;
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
for h=1:lchrom
par1=par1+ch(h)*2^(h-1);
end
par1=minl+par1/parmax*4;
toutmf(:,j+4,t)=trimf(x,[2*j-par 2*j 2*j+par1]);
toutmftri(:,j+4,t)=[2*j-par 2*j 2*j+par1];
toutmf(:,4-j,t)=trimf(x,[-2*j-par1 -2*j -2*j+par]);
toutmftri(:,4-j,t)=[-2*j-par1 -2*j -2*j+par];
end
for j=1:1:3
ch=code(lchrom*kk+1:lchrom*(kk+1),t)';
kk=kk+1;
par=0;par1=0;
for h=1:lchrom
par=par+ch(h)*2^(h-1);
end
park(j,t)=mink(j)+par/parmax*(maxk(j)-mink(j));
end
kk=0;
for j=1:7
for k=1:7
ch=code(17*lchrom+3*kk+1:17*lchrom+3*(kk+1),t)';
kk=kk+1;
par=0;
for h=1:3
par=par+ch(h)*2^(h-1);
end
trule(j,k,t)=par;
end
end
obj(t)=fruleobj(trule(:,:,t),tinmf(:,:,t),toutmf(:,:,t),park(:,t)',popsize,differ,gs);
end
[orderobj,indexobj]=sort(obj);
if(bestobj<obj(indexobj(popsize)))
bestobj=obj(indexobj(popsize));
bestinmf=tinmf(:,:,indexobj(popsize));
bestinmftri=tinmftri(:,:,indexobj(popsize));
bestoutmf=toutmf(:,:,indexobj(popsize));
bestoutmftri=toutmftri(:,:,indexobj(popsize));
bestrule=trule(:,:,indexobj(popsize));
bestpark=park(:,indexobj(popsize))';
end
beobj(i)=bestobj;
obj_sum=sum(obj);
for j=1:1:popsize
select_rand=rand;
select_p=0;
k=0;
while select_rand>select_p
k=k+1;
select_p=select_p+obj(k)/obj_sum;
end
cindex(j)=k;
end
%cross
tempcode=code(:,cindex);
for j=1:2:popsize-1
cross_rand=rand;
n=ceil(codeL*rand);
if cross_rand<pcross
tempcode(n:codeL,j)=code(n:codeL,cindex(j+1));
tempcode(n:codeL,j+1)=code(n:codeL,cindex(j));
end
end
%mutate
for j=1:1:popsize
for k=1:1:codeL
mumate_rand=rand;
if pmutation>mumate_rand %Mutation Condition
if tempcode(k,j)==0
tempcode(k,j)=1;
else
tempcode(k,j)=0;
end
end
end
end
code=tempcode;
end
fruleobj(bestrule,bestinmf,bestoutmf,bestpark,popsize,differ,gs);
a_1=subplot(3,2,1);plot(timef,beobj);
set(a_1,'XColor','white','YColor','white');
xlabel('遗传代数');ylabel('最大适应度');
a_2=subplot(3,2,2);plot(time,rin,'r',time,yout,'b');
set(a_2,'XColor','white','YColor','white');
xlabel('时间(周期)');ylabel('输入(红),输出(蓝)');
a_3=subplot(3,2,3);
xx=-7:0.1:7;
nb=trimf(xx,bestinmftri(:,1));
nm=trimf(xx,bestinmftri(:,2));
ns=trimf(xx,bestinmftri(:,3));
zo=trimf(xx,bestinmftri(:,4));
ps=trimf(xx,bestinmftri(:,5));
pm=trimf(xx,bestinmftri(:,6));
pb=trimf(xx,bestinmftri(:,7));
plot(xx, [nb' nm' ns' zo' ps' pm' pb']);
set(gca,'XColor','white','YColor','white');
xlabel('输入量隶属函数');
a_4=subplot(3,2,4);
nb=trimf(xx,bestoutmftri(:,1));
nm=trimf(xx,bestoutmftri(:,2));
ns=trimf(xx,bestoutmftri(:,3));
zo=trimf(xx,bestoutmftri(:,4));
ps=trimf(xx,bestoutmftri(:,5));
pm=trimf(xx,bestoutmftri(:,6));
pb=trimf(xx,bestoutmftri(:,7));
plot(xx, [nb' nm' ns' zo' ps' pm' pb']);
set(gca,'XColor','white','YColor','white');
xlabel('输出量隶属函数');
strrule='';
remainder=mat2str(bestrule);
remainder=strrep(remainder,'0','X ');
remainder=strrep(remainder,'1','NB');
remainder=strrep(remainder,'2','NM');
remainder=strrep(remainder,'3','NS');
remainder=strrep(remainder,'4','ZO');
remainder=strrep(remainder,'5','PS');
remainder=strrep(remainder,'6','PM');
remainder=strrep(remainder,'7','PB');
while(any(remainder))
[strt remainder]=strtok(remainder,';');
strt=strrep(strt,'[','');
strt=strrep(strt,']','');
strrule=char(strrule,strt);
end
result_rule = uicontrol('Style','text','Horiz','right','Position',[0.15 0.12 0.15 0.15],'string',strrule);
re_1 = uicontrol('Style','text','Position',[0.15 0.271 0.15 0.02],'Horiz','left','string','de NB NM NS ZO PS PM PB');
re_2 = uicontrol('Style','text','Position',[0.129 0.12 0.02 0.15],'string',char('e','NB','NM','NS','ZO','PS','PM','PB'));
t_s1 = uicontrol( 'Style','text','Position',[0.59 0.2 0.1 0.04],'fontsize',12,'String','k1, k2, k3');
t_s2 = uicontrol( 'Style','text','Position',[0.7 0.2 0.2 0.04],'fontsize',12,'String',num2str(bestpark));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -