?? ant_ant_new.m
字號:
%clear;
%clc;
%%%%%%%生成輸入值
%xl=0; %變量x的左域
%xr=1; %變量x的右域
%x=(xl:0.1:xr)'; %輸入變量要轉化為列向量
%y=exp(-x).*sin(2.*pi.*x);
%%%%%生成輸入值
%%%%初始化參數
%countMax=100; %最大迭代數
%Input_Num=1;
%Hidden_Num=4;
%Output_Num=1;
%N=20; %區域劃分
%bounds=[-1,1;-1,1;-1,1;-1,1;;-1,1;-1,1;-1,1;-1,1];
%ant_Num=20; %蟻群數量
%Q=50;
%p=0.6;
%樣本量
function [error_output,W,forecast,bounds_new]=ANT_ant_new(ant_Num,Q,p,N,bounds,Input_Num,Output_Num,Hidden_Num,x,y,countMax,line)
vNum=size(bounds,1);
Sample_Num_x=size(x,1);
Sample_Num_y=size(y,1);
literation_N=1;
if Sample_Num_x==Sample_Num_y
Sample_Num=Sample_Num_x;
else
Information='error'
end
for lit=1:literation_N;
%%%程序參數初始化
if lit==1
bounds=bounds;
end
if lit>1
bounds=bounds_new;
end
pt=zeros(vNum,N);
e=zeros(1,ant_Num);
T=ones(vNum,N);
count=0;
Pr=(1/N).*ones(vNum,N);
%%%%%
f_max_ant=[];W_max_ant=[];ptm=[];W_max_a=[];foreca=[];
while (count<=countMax)
W_ant=[];
%%%生成隨機數
L=bounds(:,2)-bounds(:,1);
n=size(bounds,1);
%%%%%
PrCum=cumsum(Pr,2); %為輪盤算準備
temp=rand(vNum,ant_Num); %隨機數生成器,輪盤算法。
for n=1:vNum
for i=1:ant_Num
ant_rand=temp(n,i);
pt(n,i)=min(find(ant_rand<=PrCum(n,:))); %position螞蟻在本次循環中選擇的位置
W_ant(n,i)=(((pt(n,i)+rand(1,1)-1).*L(n)))./N+bounds(n,1); %本次螞蟻選擇的權值
end
end
%%%%%
%%%將權值輸入神經網路,得到誤差值
for i=1:ant_Num
[error(i),f(i),o(:,i)]=ANT_object_func_ant(Input_Num,Hidden_Num,Output_Num,W_ant(:,i),x,y,Sample_Num,line);
if count==0
f_max_ant(i)=f(i);
W_max_ant(:,i)=W_ant(:,i);
ptm=pt;
foreca(:,i)=o(:,i);
end
if count>0
if f(i)>f_max_ant(i)
f_max_ant(i)=f(i);
W_max_ant(:,i)=W_ant(:,i);
ptm(:,i)=pt(:,i);
foreca(:,i)=o(:,i);
end
end
end
%將信息素更新
T=(1-p).*T;
for n=1:vNum
for k=1:ant_Num
T(n,ptm(n,k))=T(n,ptm(n,k))+Q.*f(k);
end
end
CumT=sum(T,2);
for n=1:vNum
Pr(n,:)=T(n,:)/CumT(n);
end
count=count+1;
eMin(count)=min(error);
eMean(count)=mean(error);
fMax(count)=max(f_max_ant);
fMean(count)=mean(f_max_ant);
for ii=1:ant_Num
if f_max_ant(ii)==fMax(count)
W_max_a(:,count)=W_max_ant(:,ii);
foreca_best(:,count)=foreca(:,ii);
end
end
end
for iii=1:countMax+1
if fMax(iii)==max(fMax)
W=W_max_a(:,iii);
forecast=foreca_best(:,iii);
end
end
%error_output=eMin(count-1);
%plot(Num,eMin);
%計算下一輪bounds
spaces=(L./N)*(ones(1,N));
spaces_Cum=cumsum(spaces,2)+bounds(:,1)*ones(1,N);
for i=1:vNum
for j=2:N
if W(i)<spaces_Cum(i,j)
bounds_new(i,1)=spaces_Cum(i,j-1);
bounds_new(i,2)=spaces_Cum(i,j);
break
end
end
end
eMin_last(lit,:)=eMin;
eMean_last(lit,:)=eMean;
fMax_last(lit,:)=fMax;
fMean_last(lit,:)=fMean;
bounds_new=bounds_new;
Pr=Pr;
end
e_Mean_last=reshape(eMean_last,1,[]);
e_Min_last=reshape(eMin_last,1,[]);
f_Max_last=reshape(fMax_last,1,[]);
f_Mean_last=reshape(fMean_last,1,[]);
Num_count=1:size(e_Mean_last,2);
error_output=min(e_Min_last);
%plot(Num_count,e_Mean_last);hold on;plot(Num_count,e_Min_last);hold on;plot(Num_count,f_Mean_last);hold on;plot(Num_count,f_Max_last);hold off;
%plot(Num_count,e_Mean_last);hold on;plot(Num_count,e_Min_last);hold off;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -