?? gafmf.asv
字號:
%隸屬孫數優化
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;
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';
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
obj(t)=fruleobj(rule,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));
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(rule,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;
fid=fopen('nlresult.txt','w');
fprintf(fid,'輸入量隸屬函數: ');
nb=trimf(xx,bestinmftri(:,1));
fprintf(fid,'nb:%10.6f ',bestinmftri(:,1));
nm=trimf(xx,bestinmftri(:,2));
fprintf(fid,'nm:%10.6f ',bestinmftri(:,2));
ns=trimf(xx,bestinmftri(:,3));
fprintf(fid,'ns:%10.6f ',bestinmftri(:,3));
zo=trimf(xx,bestinmftri(:,4));
fprintf(fid,'zo:%10.6f ',bestinmftri(:,4));
ps=trimf(xx,bestinmftri(:,5));
fprintf(fid,'ps:%10.6f ',bestinmftri(:,5));
pm=trimf(xx,bestinmftri(:,6));
fprintf(fid,'pm:%10.6f ',bestinmftri(:,6));
pb=trimf(xx,bestinmftri(:,7));
fprintf(fid,'pb:%10.6f ',bestinmftri(:,7));
plot(xx, [nb' nm' ns' zo' ps' pm' pb']);
set(gca,'XColor','white','YColor','white');
xlabel('輸入量隸屬函數');
fprintf(fid,'輸出量隸屬函數: ');
a_4=subplot(3,2,4);
nb=trimf(xx,bestoutmftri(:,1));
fprintf(fid,'nb:%10.6f ',bestoutmftri(:,1));
nm=trimf(xx,bestoutmftri(:,2));
fprintf(fid,'nm:%10.6f ',bestoutmftri(:,2));
ns=trimf(xx,bestoutmftri(:,3));
fprintf(fid,'ns:%10.6f ',bestoutmftri(:,3));
zo=trimf(xx,bestoutmftri(:,4));
fprintf(fid,'zo:%10.6f ',bestoutmftri(:,4));
ps=trimf(xx,bestoutmftri(:,5));
fprintf(fid,'ps:%10.6f ',bestoutmftri(:,5));
pm=trimf(xx,bestoutmftri(:,6));
fprintf(fid,'pm:%10.6f ',bestoutmftri(:,6));
pb=trimf(xx,bestoutmftri(:,7));
fprintf(fid,'pb:%10.6f ',bestoutmftri(:,7));
plot(xx, [nb' nm' ns' zo' ps' pm' pb']);
set(gca,'XColor','white','YColor','white');
xlabel('輸出量隸屬函數');
%fclose(fid);
%strrule='';
%remainder=mat2str(rule);
%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 + -