matlab蚁群算法代码,蚁群算法(ACO)MATLAB实现

(一)蚁群算法的由来

蚁群算法(ant colony optimization)最早是由Marco Dorigo等人在1991年提出,他们在研究新型算法的过程中,发现蚁群在寻找食物时,通过分泌一种称为信息素的生物激素交流觅食信息从而能快速的找到目标,据此提出了基于信息正反馈原理的蚁群算法。

蚁群算法的基本思想来源于自然界蚂蚁觅食的最短路径原理,根据昆虫科学家的观察,发现自然界的蚂蚁虽然视觉不发达,但它们可以在没有任何提示的情况下找到从食物源到巢穴的最短路径,并在周围环境发生变化后,自适应地搜索新的最佳路径。

蚂蚁在寻找食物源的时候,能在其走过的路径上释放一种叫信息素的激素,使一定范围内的其他蚂蚁能够察觉到。当一些路径上通过的蚂蚁越来越多时,信息素也就越来越多,蚂蚁们选择这条路径的概率也就越高,结果导致这条路径上的信息素又增多,蚂蚁走这条路的概率又增加,生生不息。这种选择过程被称为蚂蚁的自催化行为。对于单个蚂蚁来说,它并没有要寻找最短路径,只是根据概率选择;对于整个蚁群系统来说,它们却达到了寻找到最优路径的客观上的效果。这就是群体智能。

(二)蚁群算法能做什么

蚁群算法根据模拟蚂蚁寻找食物的最短路径行为来设计的仿生算法,因此一般而言,蚁群算法用来解决最短路径问题,并真的在旅行商问题(TSP,一个寻找最短路径的问题)上取得了比较好的成效。目前,也已渐渐应用到其他领域中去,在图着色问题、车辆调度问题、集成电路设计、通讯网络、数据聚类分析等方面都有所应用。

(三)蚁群算法实现

优化的 函数为F(x,y)= -(x.^2+3*y.^4-0.2*cos(3*pi*x)-0.4*cos(4*pi*y)+0.6)

MATLAB

clear

clc

Ant = 300;%蚂蚁数量

Times = 80;%移动次数

Rou = 0.9;%荷尔蒙发挥系数

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_1+(Upper_2-Lower_2)*rand);

Tau(i)=F(X(i,1),X(i,2));

end

step=0.05;

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

[x,y]=meshgrid(Lower_1:step:Upper_1,Lower_2:step:Upper_2);

z=eval(f);

figure(1);

subplot(1,2,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

lamda=1/T;

[Tau_Best(T),BestIndex]=max(Tau);

for i=1:Ant

P(T,i)=(Tau(BestIndex)-Tau(i))/Tau(BestIndex);%计算转移状态概率

end

for i=1:Ant

if P(T,i)

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

temp1=Lower_1;

end

if temp1>Upper_1

temp1=Upper_1;

end

if temp2

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

subplot(1,2,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));

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

clear

clc

Ant=300;%蚂蚁数量

Times=80;%移动次数

Rou=0.9;%荷尔蒙发挥系数

P0=0.2;%转移概率

Lower_1=-1;%搜索范围

Upper_1=1;

Lower_2=-1;

Upper_2=1;

fori=1:Ant

X(i,1)=(Lower_1+(Upper_1-Lower_1)*rand);

X(i,2)=(Lower_1+(Upper_2-Lower_2)*rand);

Tau(i)=F(X(i,1),X(i,2));

end

step=0.05;

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

[x,y]=meshgrid(Lower_1:step:Upper_1,Lower_2:step:Upper_2);

z=eval(f);

figure(1);

subplot(1,2,1);

mesh(x,y,z);

holdon;

plot3(X(:,1),X(:,2),Tau,'k*')

holdon;

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

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

forT=1:Times

lamda=1/T;

[Tau_Best(T),BestIndex]=max(Tau);

fori=1:Ant

P(T,i)=(Tau(BestIndex)-Tau(i))/Tau(BestIndex);%计算转移状态概率

end

fori=1:Ant

ifP(T,i)

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

iftemp1

temp1=Lower_1;

end

iftemp1>Upper_1

temp1=Upper_1;

end

iftemp2

temp2=Lower_2;

end

iftemp2>Upper_2

temp2=Upper_2;

end

ifF(temp1,temp2)>F(X(i,1),X(i,2))%更新位置

X(i,1)=temp1;

X(i,2)=temp2;

end

end

fori=1:Ant

Tau(i)=(1-Rou)*Tau(i)+F(X(i,1),X(i,2));%更新荷尔蒙

end

end

subplot(1,2,2);

mesh(x,y,z);

holdon;

x=X(:,1);

y=X(:,2);

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

holdon;

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));

优化函数:

MATLAB

function f = F(x,y)

f = -(x.^2+3*y.^4-0.2*cos(3*pi*x)-0.4*cos(4*pi*y)+0.6);

end

1

2

3

functionf=F(x,y)

f=-(x.^2+3*y.^4-0.2*cos(3*pi*x)-0.4*cos(4*pi*y)+0.6);

end

效果:

99c5d34340189ffddc40b53799ed7dc6.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值