【Matlab】智能优化算法_蚁狮优化算法ALO
- 1.背景介绍
- 2.基本思想
- 3.公式推导
- 3.1 ALO算法的运算符
- 3.2 蚂蚁的随机游动
- 3.3 困在蚂蚁坑里
- 3.4 修建陷阱
- 3.5 蚂蚁划向蚁狮
- 3.6 捕捉猎物并重建坑
- 3.7 精英主义
- 3.8 ALO算法
- 4.算法流程图
- 5.文件结构
- 6.伪代码
- 7.详细代码及注释
- 7.1 ALO.m
- 7.2 func_plot.m
- 7.3 Get_Functions_details.m
- 7.4 initialization.m
- 7.5 main.m
- 7.6 Random_walk_around_antlion.m
- 7.7 RouletteWheelSelection.m
- 8.运行结果
- 9.参考文献
1.背景介绍
- 蚁狮优化算法(Ant Lion Optimization,ALO)是一种模拟自然界中蚁狮捕食蚂蚁的行为的优化算法。该算法由Mirjalili于2015年提出,是一个比较新的算法,也有很多相关的论文。
- 蚁狮是一种昆虫幼虫,它们会在沙土中挖出漏斗状的陷阱,并藏在底部等待蚂蚁或其他昆虫掉入。当有猎物掉入陷阱时,蚁狮会用它们巨大的下颚将其捕捉并吞噬。
- ALO算法利用了这种捕食机制来寻找最优解。算法中有两种主要的个体:蚁狮和蚂蚁。每个个体都代表了一个可行解。初始时,随机生成一定数量的蚁狮和蚂蚁,并分别放置在搜索空间中不同位置。
- 在每一次迭代中,每个蚂蚁都会随机选择一个陷阱(即一个最优或次优的解),并向其移动。移动过程中,它们会受到陷阱周围环境和自身惯性的影响。同时,每个陷阱也会根据其深度和吸引力进行更新。
- 当所有的迭代完成后,最深的陷阱所对应的解就是最优解。
2.基本思想
- 蚁狮优化算法的基本思想是模拟自然界中蚁狮捕猎蚂蚁的行为来寻找最优解。算法中有两种主要的个体:蚁狮和蚂蚁。每个个体都代表了一个可行解。初始时,随机生成一定数量的蚁狮和蚂蚁,并分别放置在搜索空间中不同位置。
- 它们的名字来源于它们独特的狩猎行为和它们最喜欢的猎物。一只蚂蚁幼虫沿着圆形路径移动,并用其巨大的下颚抛出沙子,从而在沙子中挖出一个锥形坑。图1(a)显示了几个不同尺寸的锥形凹坑。挖好陷阱后,幼虫躲在锥体底部下方(作为坐等捕食者),等待昆虫(最好是蚂蚁)被困在坑中,如图1(b)所示。锥体的边缘足够锋利,昆虫很容易落入陷阱底部。一旦蚂蚁意识到猎物在陷阱中,它就会试图捕捉它。然而,昆虫通常不会立即被捕捉并试图从陷阱中逃脱。在这种情况下,蚂蚁会聪明地将沙子扔到坑边,将猎物滑入坑底。当猎物被抓进下颚时,它会被拉到土壤下并被吃掉。蚂蚁吃掉猎物后,将剩下的食物扔到坑外,并修正坑,以便下次狩猎。
- 在蚂蚁的生活方式中观察到的另一个有趣的行为是陷阱的大小和两件事的相关性:饥饿程度和月亮的形状。当蚂蚁变得更饿时[54]和/或当月亮满月时,它们往往会挖出更大的陷阱。它们已经进化并适应了这种方式,以提高生存的机会。人们还发现,蚂蚁不会直接观察月球形状来决定陷阱的大小,但它有一个内部的月钟来做出这样的决定。
- ALO算法的主要灵感来自蚂蚁幼虫的衰老行为。在下一小节中,首先对自然界中蚂蚁及其猎物的行为进行数学建模。然后基于数学模型提出了一种优化算法。
3.公式推导
3.1 ALO算法的运算符
ALO算法模拟了诱捕器中蚂蚁和蚂蚁之间的相互作用。为了模拟这样的互动,蚂蚁需要在搜索空间中移动,蚂蚁被允许捕猎它们,并使用陷阱变得更健康。由于蚂蚁在寻找食物时在自然界中随机移动,因此选择随机行走来模拟蚂蚁的移动,如下所示:
其中t表示随机游动的步骤(本研究中的迭代),rand是在[0,1]区间内均匀分布生成的随机数。
为了获得这种随机行走的图像,图2显示了500次迭代中的三次随机行走。该图显示,所使用的随机行走可能会在原点周围剧烈波动(红色1曲线),具有增加趋势(黑色曲线),或具有下降行为(蓝色曲线)。
蚂蚁的位置在优化过程中保存并利用在以下矩阵中:
其中,MAnt是用于保存每个蚂蚁位置的矩阵,Aij表示第i个蚂蚁的第j个变量(维度)的值,n是蚂蚁的数量,d是变量的数量。
需要注意的是,蚂蚁与粒子群算法中的粒子或遗传算法中的个体相似。蚂蚁的位置是指特定解决方案的参数。矩阵MAnt被认为是在优化过程中保存所有蚂蚁(所有解的变量)的位置。
为了评估每只蚂蚁,在优化过程中使用适应度(目标)函数,以下矩阵存储所有蚂蚁的适应度值:
其中,MOA是用于保存每个蚂蚁的适应度的矩阵,Aij表示第i个蚂蚁的第j个维度的值,n是蚂蚁的数量,f是目标函数。
除了蚂蚁,我们假设蚂蚁也隐藏在搜索空间的某处。为了保存它们的位置和适应度值,使用以下矩阵:
其中,MAntlion是用于保存每个蚂蚁的位置的矩阵,ALij表示第i个蚂蚁的第j个维度的值,n是蚂蚁的数量,d是变量的数量(维度)。
其中MOAL是用于保存每个蚂蚁的适应度的矩阵,ALij表示第i个蚂蚁的第j个维度的值,n是蚂蚁的数量,f是目标函数。
在优化过程中,应用以下条件:
- 蚂蚁使用不同的随机游动在搜索空间中移动。
- 随机行走应用于蚂蚁的所有维度。
- 蚂蚁的陷阱会影响随机行走。
- 蚂蚁可以建造与其适应度成比例的坑(适应度越高,坑越大)。
- 具有较大凹坑的蚂蚁捕捉蚂蚁的概率较高。
- 每只蚂蚁都可以在每次迭代中被一只蚂蚁和精英(最适者蚂蚁)捕获。
- 自适应地减小随机行走的范围,以模拟蚂蚁向蚂蚁滑动。
- 如果一只蚂蚁变得比一只蚂蚁更健康,这意味着它被蚂蚁抓住并拉到沙子下面。
- 一只蚂蚁重新定位到最新捕获的猎物,并建造一个坑,以改善每次狩猎后捕捉另一个猎物的变化。
3.2 蚂蚁的随机游动
随机游动都基于等式(2.1)。蚂蚁在优化的每一步都用随机游动来更新它们的位置。然而,由于每个搜索空间都有一个边界(变量范围),等式(2.1)不能直接用于更新蚂蚁的位置。为了将随机游动保持在搜索空间内,使用以下等式(最小–最大归一化)对其进行归一化:
其中ai是第i个变量的随机游动的最小值,bi是第i变量中随机游动的最大值,ct i是第t次迭代时第i个变数的最小值;dt i表示第t次循环时第i变量的最大值。
- 应在每次迭代中应用等式(2.7),以确保搜索空间内出现随机游动。
3.3 困在蚂蚁坑里
如上所述,蚂蚁的随机游动受到蚂蚁陷阱的影响。为了对这一假设进行数学建模,提出了以下方程:
其中ct是第t次迭代时所有变量的最小值,dt表示包含第t次循环时所有变量最大值的向量,ctj是第i次蚂蚁的所有变量的最大值,dtj是第j次蚂蚁所有变量的最值,Antlion tj表示第t次重复时所选第j次蚁群的位置。
等式(2.8)和(2.9)表明,蚂蚁在由向量c和d定义的超球体中围绕选定的蚂蚁随机行走。
这种行为的概念模型如图3所示。图3显示了二维搜索空间。可以观察到,蚂蚁需要在选定蚂蚁周围的超球体内移动。
3.4 修建陷阱
为了模拟蚂蚁的狩猎能力,采用了轮盘赌。如图3所示,假设蚂蚁只被一只选定的蚂蚁困住。ALO算法需要在优化过程中根据蚂蚁的适应度来使用轮盘赌轮算子来选择蚂蚁。这一机制为更健康的蚂蚁捕捉蚂蚁提供了很大的机会。
3.5 蚂蚁划向蚁狮
根据目前提出的机制,蚂蚁能够根据自己的适应能力建立陷阱,蚂蚁需要随机移动。然而,一旦蚂蚁意识到陷阱中有蚂蚁,它们就会向坑中央射出沙子。这种行为会顺着试图逃跑的被困蚂蚁滑下来。为了对这种行为进行数学建模,自适应地减小了蚂蚁随机游动超球的半径。在这方面提出了以下方程式:
其中I是比率,ct是第t次迭代时所有变量的最小值,dt表示包括第t次循环时所有变量最大值的向量。
在等式(2.10)和(2.11)中,I=10^w t/T,其中t是当前迭代,t是最大迭代次数,w是基于当前迭代定义的常数(当t>0.1T时w=2,当t>0.5T时w=3,当t>0.75T时w=4,当t>0.9T时w=5,且当t>0.95T时w=6)。基本上,常数w可以调整开发的精度水平。
图4还显示了使用方程(2.10)和(2.11)的递减行为。这些方程缩小了更新蚂蚁位置的半径,并模拟了蚂蚁在坑内的滑动过程。这保证了搜索空间的利用。
3.6 捕捉猎物并重建坑
狩猎的最后阶段是当一只蚂蚁到达坑底并被蚂蚁的下颚抓住时。在这个阶段之后,蚂蚁会把蚂蚁拉到沙子里,并吃掉它的身体。为了模拟这一过程,人们假设当蚂蚁变得比其对应的蚂蚁更适合(进入沙地)时,就会捕捉猎物。
然后,蚂蚁需要将其位置更新到被猎杀蚂蚁的最新位置,以增加其捕捉新猎物的机会。
在这方面提出了以下方程式:
其中t表示当前迭代,Antlion tj表示第t次迭代时选定的第j个蚂蚁的位置,Antt i表示第t个迭代时第i个蚂蚁的的位置。
3.7 精英主义
精英主义是进化算法的一个重要特征,它允许它们保持在优化过程的任何阶段获得的最佳解。在这项研究中,迄今为止在每次迭代中获得的最佳蚂蚁被保存下来,并被视为精英。
由于精英是最适者的蚂蚁,它应该能够在迭代期间影响所有蚂蚁的运动。因此,假设每只蚂蚁通过轮盘和精英同时随机绕着选定的蚂蚁行走,如下所示:
其中,R tA是在第t次迭代时轮盘选择的蚂蚁周围的随机游走,R tE是在第t次迭代时精英周围的随机漫步,Ant ti表示第t次循环时第i只蚂蚁的位置。
3.8 ALO算法
利用前面小节中提出的算子,现在可以定义ALO优化算法。ALO算法定义为三元组函数,其近似于优化问题的全局最优值,如下所示:
其中A是生成随机初始解的函数,B操纵由函数A提供的初始总体,并且C在满足结束标准时返回真。函数A、B和C的定义如下:
其中,M Ant是蚂蚁位置的矩阵,M Antlion包括蚂蚁的位置,M OA包含蚂蚁的相应适应度,M OAL具有蚂蚁的适应度。
4.算法流程图
5.文件结构
ALO.m % 蚁狮优化算法
func_plot.m % 绘制的基准函数
Get_Functions_details.m % 基准的全部信息和实现
initialization.m % 初始化
main.m % 主函数
Random_walk_around_antlion.m % 创建随机游走
RouletteWheelSelection.m % 规定轮盘选择
6.伪代码
7.详细代码及注释
7.1 ALO.m
function [Elite_antlion_fitness,Elite_antlion_position,Convergence_curve]=ALO(N,Max_iter,lb,ub,dim,fobj)
% Initialize the positions of antlions and ants
antlion_position=initialization(N,dim,ub,lb);
ant_position=initialization(N,dim,ub,lb);
% Initialize variables to save the position of elite, sorted antlions,
% convergence curve, antlions fitness, and ants fitness
Sorted_antlions=zeros(N,dim);
Elite_antlion_position=zeros(1,dim);
Elite_antlion_fitness=inf;
Convergence_curve=zeros(1,Max_iter);
antlions_fitness=zeros(1,N);
ants_fitness=zeros(1,N);
% Calculate the fitness of initial antlions and sort them
for i=1:size(antlion_position,1)
antlions_fitness(1,i)=fobj(antlion_position(i,:));
end
[sorted_antlion_fitness,sorted_indexes]=sort(antlions_fitness);
for newindex=1:N
Sorted_antlions(newindex,:)=antlion_position(sorted_indexes(newindex),:);
end
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=sorted_antlion_fitness(1);
% Main loop start from the second iteration since the first iteration
% was dedicated to calculating the fitness of antlions
Current_iter=2;
while Current_iter<Max_iter+1
% This for loop simulate random walks
for i=1:size(ant_position,1)
% Select ant lions based on their fitness (the better anlion the higher chance of catching ant)
Rolette_index=RouletteWheelSelection(1./sorted_antlion_fitness);
if Rolette_index==-1
Rolette_index=1;
end
% RA is the random walk around the selected antlion by rolette wheel
RA=Random_walk_around_antlion(dim,Max_iter,lb,ub, Sorted_antlions(Rolette_index,:),Current_iter);
% RA is the random walk around the elite (best antlion so far)
[RE]=Random_walk_around_antlion(dim,Max_iter,lb,ub, Elite_antlion_position(1,:),Current_iter);
ant_position(i,:)= (RA(Current_iter,:)+RE(Current_iter,:))/2; % Equation (2.13) in the paper
end
for i=1:size(ant_position,1)
% Boundar checking (bring back the antlions of ants inside search
% space if they go beyoud the boundaries
Flag4ub=ant_position(i,:)>ub;
Flag4lb=ant_position(i,:)<lb;
ant_position(i,:)=(ant_position(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
ants_fitness(1,i)=fobj(ant_position(i,:));
end
% Update antlion positions and fitnesses based of the ants (if an ant
% becomes fitter than an antlion we assume it was cought by the antlion
% and the antlion update goes to its position to build the trap)
double_population=[Sorted_antlions;ant_position];
double_fitness=[sorted_antlion_fitness ants_fitness];
[double_fitness_sorted,I]=sort(double_fitness);
double_sorted_population=double_population(I,:);
antlions_fitness=double_fitness_sorted(1:N);
Sorted_antlions=double_sorted_population(1:N,:);
% Update the position of elite if any antlinons becomes fitter than it
if antlions_fitness(1)<Elite_antlion_fitness
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=antlions_fitness(1);
end
% Keep the elite in the population
Sorted_antlions(1,:)=Elite_antlion_position;
antlions_fitness(1)=Elite_antlion_fitness;
% Update the convergence curve
Convergence_curve(Current_iter)=Elite_antlion_fitness;
% Display the iteration and best optimum obtained so far
if mod(Current_iter,50)==0
display(['At iteration ', num2str(Current_iter), ' the elite fitness is ', num2str(Elite_antlion_fitness)]);
end
Current_iter=Current_iter+1;
end
7.2 func_plot.m
function func_plot(func_name)
[lb,ub,dim,fobj]=Get_Functions_details(func_name);
switch func_name
case 'F1'
x=-100:2:100; y=x; %[-100,100]
case 'F2'
x=-100:2:100; y=x; %[-10,10]
case 'F3'
x=-100:2:100; y=x; %[-100,100]
case 'F4'
x=-100:2:100; y=x; %[-100,100]
case 'F5'
x=-200:2:200; y=x; %[-5,5]
case 'F6'
x=-100:2:100; y=x; %[-100,100]
case 'F7'
x=-1:0.03:1; y=x %[-1,1]
case 'F8'
x=-500:10:500;y=x; %[-500,500]
case 'F9'
x=-5:0.1:5; y=x; %[-5,5]
case 'F10'
x=-20:0.5:20; y=x;%[-500,500]
case 'F11'
x=-500:10:500; y=x;%[-0.5,0.5]
case 'F12'
x=-10:0.1:10; y=x;%[-pi,pi]
case 'F13'
x=-5:0.08:5; y=x;%[-3,1]
case 'F14'
x=-100:2:100; y=x;%[-100,100]
case 'F15'
x=-5:0.1:5; y=x;%[-5,5]
case 'F16'
x=-1:0.01:1; y=x;%[-5,5]
case 'F17'
x=-5:0.1:5; y=x;%[-5,5]
case 'F18'
x=-5:0.06:5; y=x;%[-5,5]
case 'F19'
x=-5:0.1:5; y=x;%[-5,5]
case 'F20'
x=-5:0.1:5; y=x;%[-5,5]
case 'F21'
x=-5:0.1:5; y=x;%[-5,5]
case 'F22'
x=-5:0.1:5; y=x;%[-5,5]
case 'F23'
x=-5:0.1:5; y=x;%[-5,5]
end
L=length(x);
f=[];
for i=1:L
for j=1:L
if strcmp(func_name,'F15')==0 && strcmp(func_name,'F19')==0 && strcmp(func_name,'F20')==0 && strcmp(func_name,'F21')==0 && strcmp(func_name,'F22')==0 && strcmp(func_name,'F23')==0
f(i,j)=fobj([x(i),y(j)]);
end
if strcmp(func_name,'F15')==1
f(i,j)=fobj([x(i),y(j),0,0]);
end
if strcmp(func_name,'F19')==1
f(i,j)=fobj([x(i),y(j),0]);
end
if strcmp(func_name,'F20')==1
f(i,j)=fobj([x(i),y(j),0,0,0,0]);
end
if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 ||strcmp(func_name,'F23')==1
f(i,j)=fobj([x(i),y(j),0,0]);
end
end
end
surfc(x,y,f,'LineStyle','none');
end
7.3 Get_Functions_details.m
function [lb,ub,dim,fobj] = Get_Functions_details(F)
switch F
case 'F1'
fobj = @F1;
lb=-100;
ub=100;
dim=10;
case 'F2'
fobj = @F2;
lb=-10;
ub=10;
dim=10;
case 'F3'
fobj = @F3;
lb=-100;
ub=100;
dim=10;
case 'F4'
fobj = @F4;
lb=-100;
ub=100;
dim=10;
case 'F5'
fobj = @F5;
lb=-30;
ub=30;
dim=10;
case 'F6'
fobj = @F6;
lb=-100;
ub=100;
dim=10;
case 'F7'
fobj = @F7;
lb=-1.28;
ub=1.28;
dim=10;
case 'F8'
fobj = @F8;
lb=-500;
ub=500;
dim=10;
case 'F9'
fobj = @F9;
lb=-5.12;
ub=5.12;
dim=10;
case 'F10'
fobj = @F10;
lb=-32;
ub=32;
dim=10;
case 'F11'
fobj = @F11;
lb=-600;
ub=600;
dim=10;
case 'F12'
fobj = @F12;
lb=-50;
ub=50;
dim=10;
case 'F13'
fobj = @F13;
lb=-50;
ub=50;
dim=10;
case 'F14'
fobj = @F14;
lb=-65.536;
ub=65.536;
dim=2;
case 'F15'
fobj = @F15;
lb=-5;
ub=5;
dim=4;
case 'F16'
fobj = @F16;
lb=-5;
ub=5;
dim=2;
case 'F17'
fobj = @F17;
lb=[-5,0];
ub=[10,15];
dim=2;
case 'F18'
fobj = @F18;
lb=-2;
ub=2;
dim=2;
case 'F19'
fobj = @F19;
lb=0;
ub=1;
dim=3;
case 'F20'
fobj = @F20;
lb=0;
ub=1;
dim=6;
case 'F21'
fobj = @F21;
lb=0;
ub=10;
dim=4;
case 'F22'
fobj = @F22;
lb=0;
ub=10;
dim=4;
case 'F23'
fobj = @F23;
lb=0;
ub=10;
dim=4;
end
end
% F1
function o = F1(x)
o=sum(x.^2);
end
% F2
function o = F2(x)
o=sum(abs(x))+prod(abs(x));
end
% F3
function o = F3(x)
dim=size(x,2);
o=0;
for i=1:dim
o=o+sum(x(1:i))^2;
end
end
% F4
function o = F4(x)
o=max(abs(x));
end
% F5
function o = F5(x)
dim=size(x,2);
o=sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(x(1:dim-1)-1).^2);
end
% F6
function o = F6(x)
o=sum(abs((x+.5)).^2);
end
% F7
function o = F7(x)
dim=size(x,2);
o=sum([1:dim].*(x.^4))+rand;
end
% F8
function o = F8(x)
o=sum(-x.*sin(sqrt(abs(x))));
end
% F9
function o = F9(x)
dim=size(x,2);
o=sum(x.^2-10*cos(2*pi.*x))+10*dim;
end
% F10
function o = F10(x)
dim=size(x,2);
o=-20*exp(-.2*sqrt(sum(x.^2)/dim))-exp(sum(cos(2*pi.*x))/dim)+20+exp(1);
end
% F11
function o = F11(x)
dim=size(x,2);
o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;
end
% F12
function o = F12(x)
dim=size(x,2);
o=(pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*...
(1+10.*((sin(pi.*(1+(x(2:dim)+1)./4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4));
end
% F13
function o = F13(x)
dim=size(x,2);
o=.1*((sin(3*pi*x(1)))^2+sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2))+...
((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2))+sum(Ufun(x,5,100,4));
end
% F14
function o = F14(x)
aS=[-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32;,...
-32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32];
for j=1:25
bS(j)=sum((x'-aS(:,j)).^6);
end
o=(1/500+sum(1./([1:25]+bS))).^(-1);
end
% F15
function o = F15(x)
aK=[.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];
bK=[.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK;
o=sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2);
end
% F16
function o = F16(x)
o=4*(x(1)^2)-2.1*(x(1)^4)+(x(1)^6)/3+x(1)*x(2)-4*(x(2)^2)+4*(x(2)^4);
end
% F17
function o = F17(x)
o=(x(2)-(x(1)^2)*5.1/(4*(pi^2))+5/pi*x(1)-6)^2+10*(1-1/(8*pi))*cos(x(1))+10;
end
% F18
function o = F18(x)
o=(1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*(x(1)^2)-14*x(2)+6*x(1)*x(2)+3*x(2)^2))*...
(30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*(x(1)^2)+48*x(2)-36*x(1)*x(2)+27*(x(2)^2)));
end
% F19
function o = F19(x)
aH=[3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2];
pH=[.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 .5743 .8828];
o=0;
for i=1:4
o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end
% F20
function o = F20(x)
aH=[10 3 17 3.5 1.7 8;.05 10 17 .1 8 14;3 3.5 1.7 10 17 8;17 8 .05 10 .1 14];
cH=[1 1.2 3 3.2];
pH=[.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 .1004 .9991;...
.2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 .8732 .5743 .1091 .0381];
o=0;
for i=1:4
o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end
% F21
function o = F21(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;
for i=1:5
o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
% F22
function o = F22(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;
for i=1:7
o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
% F23
function o = F23(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;
for i=1:10
o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
function o=Ufun(x,a,k,m)
o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));
end
7.4 initialization.m
function X=initialization(SearchAgents_no,dim,ub,lb)
Boundary_no= size(ub,2); % numnber of boundaries
% If the boundaries of all variables are equal and user enter a signle
% number for both ub and lb
if Boundary_no==1
X=rand(SearchAgents_no,dim).*(ub-lb)+lb;
end
% If each variable has a different lb and ub
if Boundary_no>1
for i=1:dim
ub_i=ub(i);
lb_i=lb(i);
X(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;
end
end
7.5 main.m
clc
clear
close all
SearchAgents_no=40; % 个体数
Function_name='F1'; % 测试函数
Max_iteration=500; % 迭代次数
% 获取测试函数的相关信息
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
figure('Position',[500 500 660 290])
% 画搜索空间
subplot(1,2,1);
func_plot(Function_name);
title('Test function')
xlabel('x_1');
ylabel('x_2');
zlabel([Function_name,'( x_1 , x_2 )'])
grid off
% 画收敛曲线
subplot(1,2,2);
semilogy(cg_curve,'Color','r')
title('Convergence curve')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid off
box on
legend('ALO')
display(['The best solution obtained by ALO is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by ALO is : ', num2str(Best_score)]);
7.6 Random_walk_around_antlion.m
function [RWs]=Random_walk_around_antlion(Dim,max_iter,lb, ub,antlion,current_iter)
if size(lb,1) ==1 && size(lb,2)==1 %Check if the bounds are scalar
lb=ones(1,Dim)*lb;
ub=ones(1,Dim)*ub;
end
if size(lb,1) > size(lb,2) %Check if boundary vectors are horizontal or vertical
lb=lb';
ub=ub';
end
I=1; % I is the ratio in Equations (2.10) and (2.11)
if current_iter>max_iter/10
I=1+100*(current_iter/max_iter);
end
if current_iter>max_iter/2
I=1+1000*(current_iter/max_iter);
end
if current_iter>max_iter*(3/4)
I=1+10000*(current_iter/max_iter);
end
if current_iter>max_iter*(0.9)
I=1+100000*(current_iter/max_iter);
end
if current_iter>max_iter*(0.95)
I=1+1000000*(current_iter/max_iter);
end
% Dicrease boundaries to converge towards antlion
lb=lb/(I); % Equation (2.10) in the paper
ub=ub/(I); % Equation (2.11) in the paper
% Move the interval of [lb ub] around the antlion [lb+anlion ub+antlion]
if rand<0.5
lb=lb+antlion; % Equation (2.8) in the paper
else
lb=-lb+antlion;
end
if rand>=0.5
ub=ub+antlion; % Equation (2.9) in the paper
else
ub=-ub+antlion;
end
% This function creates n random walks and normalize accroding to lb and ub
% vectors
for i=1:Dim
X = [0 cumsum(2*(rand(max_iter,1)>0.5)-1)']; % Equation (2.1) in the paper
%[a b]--->[c d]
a=min(X);
b=max(X);
c=lb(i);
d=ub(i);
X_norm=((X-a).*(d-c))./(b-a)+c; % Equation (2.7) in the paper
RWs(:,i)=X_norm;
end
7.7 RouletteWheelSelection.m
function choice = RouletteWheelSelection(weights)
accumulation = cumsum(weights);
p = rand() * accumulation(end);
chosen_index = -1;
for index = 1 : length(accumulation)
if (accumulation(index) > p)
chosen_index = index;
break;
end
end
choice = chosen_index;
8.运行结果
9.参考文献
[1]Seyedali Mirjalili. The Ant Lion Optimizer[J]. Advances in Engineering Software,2015,83.