?? gafrule.m
字號:
global yout rin time
rin=1.0;
a_1=subplot(2,2,1);
set(a_1,'XColor','white','YColor','white');
a_2=subplot(2,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=3*lchrom+147;
parmax=2^lchrom-1;
%initial
code=round(rand(codeL,popsize));
bestrule=rule;
bestpark=[2 2 2];
bestobj=fruleobj(rule,inmf,outmf,bestpark,popsize,differ,gs);
for i=1:1:maxgen
timef(i)=i;
%select save cross num to indexobj
for t=1:1:popsize
kk=0;
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(3*lchrom+3*kk+1:3*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),inmf,outmf,park(:,t)',popsize,differ,gs);
end
[orderobj,indexobj]=sort(obj);
if(bestobj<obj(indexobj(popsize)))
bestobj=obj(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,inmf,outmf,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);
%nb=inmf(:,1);
%nm=inmf(:,2);
%ns=inmf(:,3);
%zo=inmf(:,4);
%ps=inmf(:,5);
%pm=inmf(:,6);
%pb=inmf(:,7);
%plot(x, [nb nm ns zo ps pm pb]);
%set(gca,'XColor','white','YColor','white');
%xlabel('輸入量隸屬函數');
%a_4=subplot(3,2,4);
%nb=outmf(:,1);
%nm=outmf(:,2);
%ns=outmf(:,3);
%zo=outmf(:,4);
%ps=outmf(:,5);
%pm=outmf(:,6);
%pb=outmf(:,7);
%plot(x, [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.45 0.32 0.15 0.15],'string',strrule);
re_1 = uicontrol('Style','text','Position',[0.45 0.471 0.15 0.02],'Horiz','left','string','de NB NM NS ZO PS PM PB');
re_2 = uicontrol('Style','text','Position',[0.429 0.32 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 + -