⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ant_ant_new.m

📁 基于蚁群算法的神经网络。对神经网络算法进行优化。ANT_object_func_ant是基于神经网络的优化目标函数。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 + -