正弦余弦算法[1],英文名称为Sine Cosine Algorithm,简称SCA。这是即将开始的研究生生活接触的第一个智能优化算法,现在作为学习笔记的形式分享给大家,后续也会写更多关于优化算法方面的文章。
01
—
SCA算法内容介绍
正弦余弦算法是由澳大利亚学者 S. Mi rja lili于2016年 提出,该算法的出现极大地扩宽了开发新的智能算法的思路。为什么这么说呢?以往的智能算法的思想往往来源于自然界的一些现象和生物习性,比如根据生物进化提出的遗传算法(GA),根据飞鸟集群活动提出的粒子群算法(PSO)等等。正弦余弦算法则是根据正弦和余弦函数的周期性和波动性进行设计,采用的是数学函数方面的思想,并没有采用仿生的思路,这也是正弦余弦算法的创新所在。 正弦余弦算法的执行过程还是较为简单的,首先就是对种群X进行随机初始化,其中种群X包含m个个体,每个个体有n个维度。然后利用适应度函数对每个个体进行评价,选出当前所有个体中最好的个体,记为Pb。然后,根据预先设置的迭代次数进行迭代。在迭代的过程中,更新个体的位置,更新的公式如下:其中t代表着当前迭代次数,r2∈[0,2Π]、r3∈[0,2]和r4∈[0,1]为3个随机数,r1为控制参数,更新公式如下:
其中a为一个常数,通常设置为2,T为最大迭代次数。
r1、r2、r3和r4这四个重要的参数在算法迭代寻优的过程中分别发挥了不同的作用:参数r1作为一个控制参数,决定着下一次迭代时个体位置的移动方向,平衡着算法的探索(exploration)和开发(exploitation)两个阶段;r2决定了下一次迭代时移动的距离;r3作为一个随机权重,影响着最优个体在下一次迭代中的作用;参数r4使得算法等概率的在正弦和余弦部分进行迭代更新个体位置。这里的 探索( exp loration )对应着算法的全局寻优能力,开发(exploitation)则对应着算法的局部开发能力。这两个词可以用一个春晚小品《装修》来解释,大锤用来控制一个大体的装修范围,小锤则对大锤留下的范围进行修饰。算法中的探索就是找不同的范围,开发就是寻找留下的范围中有价值的区域。如何平衡算法中探索和开发这两个阶段,影响着算法的最终寻优能力。在SCA算法中,当r1sin(r2)或者r1cos(r2)函数值大于1或者小于-1时,算法进行全局探索;当r1sin(r2)或者r1cos(r2)函数值介于-1到1之间时,算法进行局部开发。02
—
SCA算法代码实现
这里也分享一下SCA算法的实现代码,其中func_plot.m和 Get_Functions_details.m为测试函数文件,initialization.m为初始化文件,SCA.m为算法实现部分,main.m用来调用相关函数和绘图。实现代码如下: func _plot.m
% Sine Cosine Algorithm (SCA) %% Source codes demo version 1.0 % % Developed in MATLAB R2011b(7.13) % % Author and programmer: Seyedali Mirjalili % % e-Mail: ali.mirjalili@gmail.com % seyedali.mirjalili@griffithuni.edu.au % % Homepage: http://www.alimirjalili.com % % Main paper: % S. Mirjalili, SCA: A Sine Cosine Algorithm for solving optimization problems% Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.12.022% This function draws the benchmark functionsfunction 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 endendsurfc(x,y,f,'LineStyle','none');end
Get_Functions_details.m
% Sine Cosine Algorithm (SCA) %% Source codes demo version 1.0 % % Developed in MATLAB R2011b(7.13) % % Author and programmer: Seyedali Mirjalili % % e-Mail: ali.mirjalili@gmail.com % seyedali.mirjalili@griffithuni.edu.au % % Homepage: http://www.alimirjalili.com % % Main paper: % S. Mirjalili, SCA: A Sine Cosine Algorithm for solving optimization problems% Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.12.022% This function containts full information and implementations of the benchmark % functions in Table 1, Table 2, and other test functins from the literature % lb is the lower bound: lb=[lb_1,lb_2,...,lb_d]% up is the uppper bound: ub=[ub_1,ub_2,...,ub_d]% dim is the number of variables (dimension of the problem)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; endend% F1function o = F1(x)o=sum(x.^2);end% F2function o = F2(x)o=sum(abs(x))+prod(abs(x));end% F3function o = F3(x)dim=size(x,2);o=0;for i=1:dim o=o+sum(x(1:i))^2;endend% F4function o = F4(x)o=max(abs(x));end% F5function 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% F6function o = F6(x)o=sum(abs((x+.5)).^2);end% F7function o = F7(x)dim=size(x,2);o=sum([1:dim].*(x.^4))+rand;end% F8function o = F8(x)o=sum(-x.*sin(sqrt(abs(x))));end% F9function o = F9(x)dim=size(x,2);o=sum(x.^2-10*cos(2*pi.*x))+10*dim;end% F10function 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% F11function o = F11(x)dim=size(x,2);o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;end% F12function 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% F13function 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% F14function 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);endo=(1/500+sum(1./([1:25]+bS))).^(-1);end% F15function 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% F16function 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% F17function 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% F18function 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% F19function 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))));endend% F20function 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))));endend% F21function 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);endend% F22function 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);endend% F23function 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);endendfunction o=Ufun(x,a,k,m)o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(xend
initialization.m
% Sine Cosine Algorithm (SCA) %% Source codes demo version 1.0 % % Developed in MATLAB R2011b(7.13) % % Author and programmer: Seyedali Mirjalili % % e-Mail: ali.mirjalili@gmail.com % seyedali.mirjalili@griffithuni.edu.au % % Homepage: http://www.alimirjalili.com % % Main paper: % S. Mirjalili, SCA: A Sine Cosine Algorithm for solving optimization problems% Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.12.022% This function creates the first random population of mothsfunction 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 lbif Boundary_no==1 X=rand(SearchAgents_no,dim).*(ub-lb)+lb;end% If each variable has a different lb and ubif 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; endend
SCA.m
% Sine Cosine Algorithm (SCA) %% Source codes demo version 1.0 % % Developed in MATLAB R2011b(7.13) % % Author and programmer: Seyedali Mirjalili % % e-Mail: ali.mirjalili@gmail.com % seyedali.mirjalili@griffithuni.edu.au % % Homepage: http://www.alimirjalili.com % % Main paper: % S. Mirjalili, SCA: A Sine Cosine Algorithm for solving optimization problems% Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.12.022%_______________________________________________________________________________________________% You can simply define your cost function in a seperate file and load its handle to fobj % The initial parameters that you need are:%__________________________________________% fobj = @YourCostFunction% dim = number of your variables% Max_iteration = maximum number of iterations% SearchAgents_no = number of search agents% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n% If all the variables have equal lower bound you can just% define lb and ub as two single numbers% To run SCA: [Best_score,Best_pos,cg_curve]=SCA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)%______________________________________________________________________________________________function [Destination_fitness,Destination_position,Convergence_curve]=SCA(N,Max_iteration,lb,ub,dim,fobj)display('SCA is optimizing your problem');%Initialize the set of random solutionsX=initialization(N,dim,ub,lb);Destination_position=zeros(1,dim);Destination_fitness=inf;Convergence_curve=zeros(1,Max_iteration);Objective_values = zeros(1,size(X,1));% Calculate the fitness of the first set and find the best onefor i=1:size(X,1) Objective_values(1,i)=fobj(X(i,:)); if i==1 Destination_position=X(i,:); Destination_fitness=Objective_values(1,i); elseif Objective_values(1,i) Destination_position=X(i,:); Destination_fitness=Objective_values(1,i); end All_objective_values(1,i)=Objective_values(1,i);end%Main loopt=2; % start from the second iteration since the first iteration was dedicated to calculating the fitnesswhile t<=Max_iteration % Eq. (3.4) a = 2; Max_iteration = Max_iteration; r1=a-t*((a)/Max_iteration); % r1 decreases linearly from a to 0 % Update the position of solutions with respect to destination for i=1:size(X,1) % in i-th solution for j=1:size(X,2) % in j-th dimension % Update r2, r3, and r4 for Eq. (3.3) r2=(2*pi)*rand(); r3=2*rand; r4=rand(); % Eq. (3.3) if r4<0.5 % Eq. (3.1) X(i,j)= X(i,j)+(r1*sin(r2)*abs(r3*Destination_position(j)-X(i,j))); else % Eq. (3.2) X(i,j)= X(i,j)+(r1*cos(r2)*abs(r3*Destination_position(j)-X(i,j))); end end end for i=1:size(X,1) % Check if solutions go outside the search spaceand bring them back Flag4ub=X(i,:)>ub; Flag4lb=X(i,:) X(i,:)=(X(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb; % Calculate the objective values Objective_values(1,i)=fobj(X(i,:)); % Update the destination if there is a better solution if Objective_values(1,i) Destination_position=X(i,:); Destination_fitness=Objective_values(1,i); end end Convergence_curve(t)=Destination_fitness; % Display the iteration and best optimum obtained so far if mod(t,50)==0 display(['At iteration ', num2str(t), ' the optimum is ', num2str(Destination_fitness)]); end % Increase the iteration counter t=t+1;end
main.m
% Sine Cosine Algorithm (SCA) %% Source codes demo version 1.0 % % Developed in MATLAB R2011b(7.13) % % Author and programmer: Seyedali Mirjalili % % e-Mail: ali.mirjalili@gmail.com % seyedali.mirjalili@griffithuni.edu.au % % Homepage: http://www.alimirjalili.com % % Main paper: % S. Mirjalili, SCA: A Sine Cosine Algorithm for solving optimization problems% Knowledge-Based Systems, DOI: http://dx.doi.org/10.1016/j.knosys.2015.12.022%_______________________________________________________________________________________________% You can simply define your cost function in a seperate file and load its handle to fobj % The initial parameters that you need are:%__________________________________________% fobj = @YourCostFunction% dim = number of your variables% Max_iteration = maximum number of iterations% SearchAgents_no = number of search agents% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n% If all the variables have equal lower bound you can just% define lb and ub as two single numbers% To run SCA: [Best_score,Best_pos,cg_curve]=SCA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)%______________________________________________________________________________________________clear all clcSearchAgents_no=30; % Number of search agentsFunction_name='F1'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)Max_iteration=1000; % Maximum numbef of iterations% Load details of the selected benchmark function[lb,ub,dim,fobj]=Get_Functions_details(Function_name);[Best_score,Best_pos,cg_curve]=SCA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);figure('Position',[284 214 660 290])%Draw search spacesubplot(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%Draw objective spacesubplot(1,2,2);semilogy(cg_curve,'Color','b')title('Convergence curve')xlabel('Iteration');ylabel('Best flame (score) obtained so far');axis tightgrid offbox onlegend('SCA')display(['The best solution obtained by SCA is : ', num2str(Best_pos)]);display(['The best optimal value of the objective funciton found by SCA is : ', num2str(Best_score)]);
[1] Mirjalili S. SCA: a sine cosine algorithm for solving optimization problems[J]. Knowledge-based systems, 2016, 96: 120-133.