├── .gitattributes └── COA ├── initialization.m ├── license.txt ├── mainCOA.m ├── func_plot.m ├── COA.m └── Get_F.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /COA/initialization.m: -------------------------------------------------------------------------------- 1 | 2 | function X=initialization(N,Dim,UB,LB) 3 | 4 | B_no= size(UB,2); % numnber of boundaries 5 | 6 | if B_no==1 7 | X=rand(N,Dim).*(UB-LB)+LB; 8 | end 9 | 10 | % If each variable has a different lb and ub 11 | if B_no>1 12 | for i=1:Dim 13 | Ub_i=UB(i); 14 | Lb_i=LB(i); 15 | X(:,i)=rand(N,1).*(Ub_i-Lb_i)+Lb_i; 16 | end 17 | end -------------------------------------------------------------------------------- /COA/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023, Heming Jia,Raohonghua,Wen changsheng,Seyedali Mirjalili 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution 13 | 14 | * Neither the name of contributor's University nor the names of its 15 | contributors may be used to endorse or promote products derived from this 16 | software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /COA/mainCOA.m: -------------------------------------------------------------------------------- 1 | % Crayfish Optimization Algorithm(COA) 2 | % 3 | % Source codes demo version 1.0 4 | % 5 | % The 11th Gen Intel(R) Core(TM) i7-11700 processor with the primary frequency of 2.50GHz, 16GB memory, and the operating system of 64-bit windows 11 using matlab2021a. 6 | % 7 | % Author and programmer: Heming Jia,Honghua Rao,Changsheng Wen,Seyedali Mirjalili 8 | % e-Mail: jiaheminglucky99@126.com;rao12138@163.com 9 | 10 | 11 | clear all 12 | close all 13 | clc 14 | 15 | N=100; %Number of search agents 16 | F_name='F3'; %Name of the test function 17 | T=200; %Maximum number of iterations 18 | 19 | 20 | [lb,ub,dim,fobj]=Get_F(F_name); %Get details of the benchmark functions 21 | [best_fun,best_position,cuve_f,global_Cov]=COA(N,T,lb,ub,dim,fobj); 22 | 23 | 24 | figure('Position',[454 445 694 297]); 25 | subplot(1,3,1); 26 | func_plot(F_name); % Function plot 27 | title('Parameter space') 28 | xlabel('x_1'); 29 | ylabel('x_2'); 30 | zlabel([F_name,'( x_1 , x_2 )']) 31 | subplot(1,3,2); % Convergence plot 32 | semilogy(cuve_f,'LineWidth',3) 33 | subplot(1,3,3); % Convergence plot 34 | semilogy(global_Cov,'LineWidth',3) 35 | xlabel('Iteration#'); 36 | ylabel('Best fitness so far'); 37 | legend('COA'); 38 | 39 | 40 | 41 | display(['The best-obtained solution by COA is : ', num2str(best_position)]); 42 | display(['The best optimal value of the objective funciton found by COA is : ', num2str(best_fun)]); -------------------------------------------------------------------------------- /COA/func_plot.m: -------------------------------------------------------------------------------- 1 | 2 | function func_plot(func_name) 3 | 4 | [LB,UB,Dim,F_obj]=Get_F(func_name); 5 | 6 | switch func_name 7 | case 'F1' 8 | x=-100:2:100; y=x; %[-100,100] 9 | 10 | case 'F2' 11 | x=-100:2:100; y=x; %[-10,10] 12 | 13 | case 'F3' 14 | x=-100:2:100; y=x; %[-100,100] 15 | 16 | case 'F4' 17 | x=-100:2:100; y=x; %[-100,100] 18 | case 'F5' 19 | x=-200:2:200; y=x; %[-5,5] 20 | case 'F6' 21 | x=-100:2:100; y=x; %[-100,100] 22 | case 'F7' 23 | x=-1:0.03:1; y=x %[-1,1] 24 | case 'F8' 25 | x=-500:10:500;y=x; %[-500,500] 26 | case 'F9' 27 | x=-5:0.1:5; y=x; %[-5,5] 28 | case 'F10' 29 | x=-20:0.5:20; y=x;%[-500,500] 30 | case 'F11' 31 | x=-500:10:500; y=x;%[-0.5,0.5] 32 | case 'F12' 33 | x=-10:0.1:10; y=x;%[-pi,pi] 34 | case 'F13' 35 | x=-5:0.08:5; y=x;%[-3,1] 36 | case 'F14' 37 | x=-100:2:100; y=x;%[-100,100] 38 | case 'F15' 39 | x=-5:0.1:5; y=x;%[-5,5] 40 | case 'F16' 41 | x=-1:0.01:1; y=x;%[-5,5] 42 | case 'F17' 43 | x=-5:0.1:5; y=x;%[-5,5] 44 | case 'F18' 45 | x=-5:0.06:5; y=x;%[-5,5] 46 | case 'F19' 47 | x=-5:0.1:5; y=x;%[-5,5] 48 | case 'F20' 49 | x=-5:0.1:5; y=x;%[-5,5] 50 | case 'F21' 51 | x=-5:0.1:5; y=x;%[-5,5] 52 | case 'F22' 53 | x=-5:0.1:5; y=x;%[-5,5] 54 | case 'F23' 55 | x=-5:0.1:5; y=x;%[-5,5] 56 | end 57 | 58 | 59 | 60 | L=length(x); 61 | f=[]; 62 | 63 | for i=1:L 64 | for j=1:L 65 | 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 66 | f(i,j)=F_obj([x(i),y(j)]); 67 | end 68 | if strcmp(func_name,'F15')==1 69 | f(i,j)=F_obj([x(i),y(j),0,0]); 70 | end 71 | if strcmp(func_name,'F19')==1 72 | f(i,j)=F_obj([x(i),y(j),0]); 73 | end 74 | if strcmp(func_name,'F20')==1 75 | f(i,j)=F_obj([x(i),y(j),0,0,0,0]); 76 | end 77 | if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 ||strcmp(func_name,'F23')==1 78 | f(i,j)=F_obj([x(i),y(j),0,0]); 79 | end 80 | end 81 | end 82 | 83 | surfc(x,y,f,'LineStyle','none'); 84 | 85 | end 86 | 87 | -------------------------------------------------------------------------------- /COA/COA.m: -------------------------------------------------------------------------------- 1 | % Crayfish Optimization Algorithm(COA) 2 | % 3 | % Source codes demo version 1.0 4 | % 5 | % The 11th Gen Intel(R) Core(TM) i7-11700 processor with the primary frequency of 2.50GHz, 16GB memory, and the operating system of 64-bit windows 11 using matlab2021a. 6 | % 7 | % Author and programmer: Heming Jia,Raohonghua,Wen changsheng,Seyedali Mirjalili 8 | % e-Mail: jiaheminglucky99@126.com;rao12138@163.com 9 | % 10 | % 11 | %_______________________________________________________________________________________________ 12 | % You can simply define your cost function in a seperate file and load its handle to fobj 13 | % The initial parameters that you need are: 14 | %__________________________________________ 15 | % fobj = @YourCostFunction 16 | % dim = number of your variables 17 | % T = maximum number of iterations 18 | % N = number of search agents 19 | % lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n 20 | % ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n 21 | % If all the variables have equal lower bound you can just 22 | % define lb and ub as two single numbers 23 | function [best_fun,best_position,cuve_f,global_Cov] =COA(N,T,lb,ub,dim,fobj) 24 | %% Define Parameters 25 | cuve_f=zeros(1,T); 26 | X=initialization(N,dim,ub,lb); %Initialize population 27 | global_Cov = zeros(1,T); 28 | Best_fitness = inf; 29 | best_position = zeros(1,dim); 30 | fitness_f = zeros(1,N); 31 | for i=1:N 32 | fitness_f(i) = fobj(X(i,:)); %Calculate the fitness value of the function 33 | if fitness_f(i)30 49 | %% summer resort stage 50 | if rand<0.5 51 | Xnew(i,:) = X(i,:)+C*rand(1,dim).*(xf-X(i,:)); %Eq.(6) 52 | else 53 | %% competition stage 54 | for j = 1:dim 55 | z = round(rand*(N-1))+1; %Eq.(9) 56 | Xnew(i,j) = X(i,j)-X(z,j)+xf(j); %Eq.(8) 57 | end 58 | end 59 | else 60 | %% foraging stage 61 | P = 3*rand*fitness_f(i)/fobj(Xfood); %Eq.(4) 62 | if P>2 % The food is too big 63 | Xfood = exp(-1/P).*Xfood; %Eq.(12) 64 | for j = 1:dim 65 | Xnew(i,j) = X(i,j)+cos(2*pi*rand)*Xfood(j)*p_obj(temp)-sin(2*pi*rand)*Xfood(j)*p_obj(temp); %Eq.(13) 66 | end 67 | else 68 | Xnew(i,:) = (X(i,:)-Xfood)*p_obj(temp)+p_obj(temp).*rand(1,dim).*X(i,:); %Eq.(14) 69 | end 70 | end 71 | end 72 | %% boundary conditions 73 | for i=1:N 74 | for j =1:dim 75 | if length(ub)==1 76 | Xnew(i,j) = min(ub,Xnew(i,j)); 77 | Xnew(i,j) = max(lb,Xnew(i,j)); 78 | else 79 | Xnew(i,j) = min(ub(j),Xnew(i,j)); 80 | Xnew(i,j) = max(lb(j),Xnew(i,j)); 81 | end 82 | end 83 | end 84 | 85 | global_position = Xnew(1,:); 86 | global_fitness = fobj(global_position); 87 | 88 | for i =1:N 89 | %% Obtain the optimal solution for the updated population 90 | new_fitness = fobj(Xnew(i,:)); 91 | if new_fitnessa)+k.*((-x-a).^m).*(x<(-a)); 340 | end --------------------------------------------------------------------------------