蚁群算法代码(求函数最值)

时间:2026-01-19

蚁群算法简单应用

function [F]=F(x1,x2) %目标函数

F=-(x1.^2+2*x2.^2-0.3*cos(3*pi*x1)-0.4*cos(4*pi*x2)+0.7);

End

function [maxx,maxy,maxvalue]=antcolony

% 蚁群算法求函数最大值的程序

ant=200; % 蚂蚁数量

times=50; % 蚂蚁移动次数

rou=0.8; % 信息素挥发系数

p0=0.2; % 转移概率常数

lower_1=-1; % 设置搜索范围

upper_1=1; %

lower_2=-1; %

upper_2=1; %

for i=1 : ant

X(i,1)=(lower_1+(upper_1-lower_1)*rand);

%随机设置蚂蚁的初值位置

X(i,2)=(lower_2+(upper_2-lower_2)*rand);

tau(i)=F(X(i,1),X(i,2)); %第i只蚂蚁的信息量

end %随机初始每只蚂蚁的位置

step=0.05; %网格划分单位

f='-(x.^2+2*y.^2-0.3*cos(3*pi*x)-0.4*cos(4*pi*y)+0.7)';

[x,y]=meshgrid(lower_1:step:upper_1,lower_2:step:upper_2);

z=eval(f); %eval函数,将字符串内的内容执行再赋给对象

figure(1);

mesh(x,y,z); %网格图

hold on;

plot3(X(:,1),X(:,2),tau,'k*') %蚂蚁初始位置

hold on;

text(0.1,0.8,-0.1,'蚂蚁的初始分部位置')

xlabel('x');ylabel('y');zlabel('f(x,y)');

for t=1:times % 第t次移动

lamda=1/t; %步长系数,随移动次数增大而减少

[tau_best(t),bestindex]=max(tau); %第t次移动的最优值及其位置

for i=1:ant %第i只蚂蚁

p(t,i)=(tau(bestindex)-tau(i))/tau(bestindex); %最优值与第i只蚂蚁的值的差比

% 计算状态转移概率

end

for i=1:ant

if p(t,i)<p0 %局部搜索 小于转移概率常数

temp1=X(i,1)+(2*rand-1)*lamda; %移动距离

蚁群算法简单应用

temp2=X(i,2)+(2*rand-1)*lamda;

else %全局搜索

temp1=X(i,1)+(upper_1-lower_1)*(rand-0.5);

temp2=X(i,2)+(upper_2-lower_2)*(rand-0.5);

end

%%%%%%%%%%%%%%%%%%%%%% 越界处理

if temp1<lower_1

temp1=lower_1;

end

if temp1>upper_1

temp1=upper_1;

end

if temp2<lower_2

temp2=lower_2;

end

if temp2>upper_2

temp2=upper_2;

end

%%%%%%%%%%%%%%%%%%%%%%%

if F(temp1,temp2)>F(X(i,1),X(i,2))

% 判断蚂蚁是否移动

X(i,1)=temp1;

X(i,2)=temp2;

end

end

for i=1:ant

tau(i)=(1-rou)*tau(i)+F(X(i,1),X(i,2)); % 更新信息量 end

end

figure(2);

mesh(x,y,z);

hold on;

x=X(:,1);y=X(:,2);

plot3(x,y,eval(f),'k*')

hold on;

text(0.1,0.8,-0.1,'蚂蚁的最终分布位置')

xlabel('x');ylabel('y'),zlabel('f(x,y)');

[max_value,max_index]=max(tau);

maxx=X(max_index,1);

maxy=X(max_index,2);

maxvalue=F(X(max_index,1),X(max_index,2));

end

蚁群算法简单应用

function [F1]=F1(x) %目标函数

F1=x.^2-2*x+1;

End

%蚁群算法求解一元函数F1=x^2-2*x+1

close

clear

clc

ant=10;

times=40;

rou=0.8;

p0=0.2;

lb=-2;

ub=2;

step=0.05;

x=lb:0.05:ub;

for i=1:ant

X(i)=lb+(ub-lb)*rand;

tau(i)=F1(X(i));

end

figure(1);

plot(x,F1(x));

hold on

plot(X,tau,'r*');

for kk=1:10

for t=1:times

lamda=1/t;%转移次数的倒数

[tau_best(t),bestindex]=min(tau);

for i=1:ant

p(t,i)=(tau(bestindex)-tau(i))/tau(bestindex);%转移概率(最优-蚂蚁i)/最优

%此种概率选择易陷入局部最优解

end

for i=1:ant

if p(t,i)<p0

temp=X(i)+(2*rand-1)*lamda;%蚂蚁移动

else

temp=X(i)+(ub-lb)*(rand-0.5);

end

if temp<lb

temp=lb;

end

if temp>ub

蚁群算法简单应用

temp=ub;

end

if F1(temp)<F1(X(i)) X(i)=temp;

end

end

for i=1:ant

tau(i)=(1-rou)*tau(i)+F1(X(i)); end

end

end

figure(2);

plot(x,F1(x));

hold on

x=X(i);y=tau(i);

plot(x,y,'g*');

x1=X(length(X))

y1=tau(length(tau))

…… 此处隐藏:607字,全部文档内容请下载后查看。喜欢就下载吧 ……
蚁群算法代码(求函数最值).doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:4.9 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:19元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219