📄 aco_peng_v4.m
字号:
%**************************************************************************
% Ant Colony Optimization for continuous problems
% Peng Yu
% ECE@UNB
% April,19 2007
% function ACO_Peng_(ff)
%**************************************************************************
clear all
clc
global N
Nvar=3;
for ii=1:Nvar
Up(ii)=5.12; Lo(ii)=-5.12;
end
maxit=10;Q=10;ee=0.001;
N=120;% x(i,j)=L(i)+(U(i)-L(i))*(j-1)/N,discrete continuous into N parts,
%so there are N+1 nodes for each xi
dettt=ceil(N/4);Nants=500; aa=1;
tt(N+1,Nvar)=0.01;pp=0.99;% initialize phemosome 'tt' and decay rate 'pp'
% |x(1,1),x(1,2),x(1,3),....x(1,Nvar)|
% |x(2,1),x(2,2),x(2,3),....x(2,Nvar)|
% |x(3,1),x(3,2),x(3,3),....x(3,Nvar)|
% |..................................|
% |x(N,1),x(N,2),x(N,3),....x(N,Nvar)|
ff= @(x)(x(1)^2+x(2)^2+x(3)^2);% testing function
bestxx=Lo;ffbest=feval(ff,bestxx);
xx(1,Nvar)=0;sumtt(N+1,1)=0;P(N+1,Nvar)=0;
for it=1:maxit
for i0=1:Nants % xx(1)=L(1)+(U(1)-L(1))*(i0-1)/(N);%
tour = ceil((N+1)*rand(1,Nvar)); % randomly creat the trail for ants from 1 to Nvar layer
for i1=1:Nvar
Dist(i1)=Up(i1)-Lo(i1);
hh(i1)=Dist(i1)/N;
xx(i1)=Lo(i1)+(tour(i1)-1)*hh(i1);
end%i1
ffvalue = feval(ff,xx);
if ffvalue < ffbest
for j1=1:Nvar
tt(tour(j1),j1)=(1-pp)*tt(tour(j1),j1)+Q/ffvalue;
end
for i2=1:Nvar
sumtt(i2)=sum(tt(:,Nvar).^aa);
end%i2
for i3=1:N+1
for j3=1:Nvar
P(i3,j3)=tt(i3,j3)^aa/sumtt(j3);
%
end%j3
end%i3
end % if ffvalue < ffbest
end%i0
% Sort the costs and associated parameters
for i4=1:Nvar
Pvars=P(:,i4);
[Pbestxx,ind] = sort(Pvars);
ssss(i4)=ind(N+1);
bestxx(i4)=Lo(i4)+(Up(i4)-Lo(i4))*(ind(N+1))/N;
end
ffvalue=feval(ff,bestxx);
if ffvalue < ffbest
ffbest=ffvalue;
bestxx= xx;
end
bestbest(it)=ffbest;
bbestxx(it,:)=bestxx;
for iii=1:Nvar
Lo_new(iii)=Lo(iii)+(ssss(iii)-dettt)*((Up(iii)-Lo(iii))/N);
Up_new(iii)=Lo(iii)+(ssss(iii)+dettt)*((Up(iii)-Lo(iii))/N);
if Lo_new(iii)<Lo(iii)
Lo_new(iii)=Lo(iii);
end
if Up_new(iii)>Up(iii)
Up_new(iii)=Up(iii);
end
Lo(iii)=Lo_new(iii);
Up(iii)=Up_new(iii);
end
% bestmean(it)=mean()
end %it
ffbest
bestxx
figure(1) %f value
plot([1:maxit], bestbest,'b-')
figure(2) %x values
for k=1:Nvar
subplot(Nvar,1,k);
plot([1:maxit],bbestxx(:,k),'-')
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -