├── ACO.m ├── CCPSOGSA.m ├── CPSOGSA.m ├── DE.m ├── GA.m ├── GSA.m ├── Gconstant.m ├── Geinitialization.m ├── Gfield.m ├── Main.m ├── README.md ├── Read _Me.txt ├── RouletteWheelSelection.m ├── bbo.m ├── benchmark_functions.m ├── benchmark_functions_details.m ├── crossover_continious.m ├── elitism.m ├── evaluateF.m ├── initialization.m ├── massCalculation.m ├── move.m ├── mutation_continious.m ├── pso.m ├── selection.m └── space_bound.m /ACO.m: -------------------------------------------------------------------------------- 1 | %The code has been taken from: 2 | % Copyright (c) 2015, Yarpiz (www.yarpiz.com) 3 | 4 | function [BestSolACO,BestAnt,BestCostACO] = ACO(Benchmark_Function_ID, N, Max_Iteration,Q,tau0,alpha,rho) 5 | [down,up,dim]=benchmark_functions_details(Benchmark_Function_ID); 6 | % %% ACO Parameters 7 | % MaxIt=500; % Maximum Number of Iterations 8 | % nAnt=50; % Number of Ants (Population Size) 9 | % Q=1; 10 | % tau0=10; % Initial Phromone 11 | % alpha=0.3; % Phromone Exponential Weight 12 | % rho=0.1; % Evaporation Rate 13 | %% Initialization 14 | tau=tau0*ones(dim,dim); 15 | % Empty Ant 16 | empty_ant.Tour=[]; 17 | empty_ant.Cost=[]; 18 | 19 | if size(up,1)>1 20 | for i=1:dim 21 | high=up(i);ll=down(i); 22 | end 23 | end 24 | % Ant Colony Matrix 25 | ant=repmat(empty_ant,N,1); 26 | for i=1:N 27 | % ant(i).tour=unifrnd(ll,high,dim); 28 | ant(i).tour=rand(1,N).*(high-ll)+ll; 29 | ant(i).Cost=benchmark_functions(ant(i).tour,Benchmark_Function_ID,dim); 30 | 31 | % if ant(i).Cost1 35 | % for i=1:dim 36 | % high=up(i);ll=low(i); 37 | % pop(i).Position=rand(1,N).*(high-ll)+ll; 38 | % end 39 | % end 40 | %% Initialization 41 | 42 | empty_individual.Position=[]; 43 | empty_individual.Cost=[]; 44 | 45 | BestSolDE.Cost=inf; 46 | 47 | pop=repmat(empty_individual,N,1); 48 | if size(up,1)>1 49 | for i=1:dim 50 | high=up(i);ll=low(i); 51 | end 52 | end 53 | for i=1:N 54 | pop(i).Position=rand(1,dim).*(high-ll)+ll; 55 | pop(i).Cost=benchmark_functions(pop(i).Position,Benchmark_Function_ID,dim); 56 | 57 | % if pop(i).Cost1 42 | for i=1:dim 43 | high=up(i);ll=down(i); 44 | sigma=0.02*(high-ll); 45 | end 46 | end 47 | % Empty Habitat 48 | habitat.Position=[]; 49 | habitat.Cost= []; 50 | % benchmark_functions(currentX,Benchmark_Function_ID,dim); 51 | 52 | % Create Habitats Array 53 | pop=repmat(habitat,N,1); 54 | 55 | % Initialize Habitats 56 | for i=1:N 57 | pop(i).Position= (high-ll).* rand(1,dim) + ll; 58 | % pop(i).Position=unifrnd(down,up,VarSize); 59 | % pop(i).Position=unifrnd(ll,high,VarSize); 60 | pop(i).Cost=benchmark_functions(pop(i).Position,Benchmark_Function_ID,dim); 61 | end 62 | 63 | % Sort Population 64 | [~, SortOrder]=sort([pop.Cost]); 65 | pop=pop(SortOrder); 66 | 67 | % Best Solution Ever Found 68 | BestSol=pop(1); 69 | 70 | % Array to Hold Best Costs 71 | BestCost=zeros(Max_Iteration,1); 72 | 73 | %% BBO Main Loop 74 | 75 | for it=1:Max_Iteration 76 | 77 | newpop=pop; 78 | for i=1:N 79 | for k=1:dim 80 | % Migration 81 | if rand<=lambda(i) 82 | % Emmigration Probabilities 83 | EP=mu; 84 | EP(i)=0; 85 | EP=EP/sum(EP); 86 | 87 | % Select Source Habitat 88 | j=RouletteWheelSelection(EP); 89 | 90 | % Migration 91 | newpop(i).Position(k)=pop(i).Position(k) ... 92 | +alpha*(pop(j).Position(k)-pop(i).Position(k)); 93 | 94 | end 95 | 96 | % Mutation 97 | if rand<=pMutation 98 | newpop(i).Position(k)=newpop(i).Position(k)+sigma*randn; 99 | end 100 | end 101 | 102 | % Apply Lower and Upper Bound Limits 103 | % newpop(i).Position = max(newpop(i).Position, down); 104 | % newpop(i).Position = min(newpop(i).Position, up); 105 | % % Apply Lower and Upper Bound Limits 106 | newpop(i).Position = max(newpop(i).Position, ll); 107 | newpop(i).Position = min(newpop(i).Position, high); 108 | 109 | % Evaluation 110 | newpop(i).Cost=benchmark_functions(newpop(i).Position,Benchmark_Function_ID,dim); 111 | end 112 | 113 | % Sort New Population 114 | [~, SortOrder]=sort([newpop.Cost]); 115 | newpop=newpop(SortOrder); 116 | 117 | % Select Next Iteration Population 118 | pop=[pop(1:nKeep) 119 | newpop(1:nNew)]; 120 | 121 | % Sort Population 122 | [~, SortOrder]=sort([pop.Cost]); 123 | pop=pop(SortOrder); 124 | 125 | % Update Best Solution Ever Found 126 | BestSol=pop(1); 127 | 128 | % Store Best Cost Ever Found 129 | BestCost(it)=BestSol.Cost; 130 | 131 | % % Show Iteration Information 132 | % disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]); 133 | 134 | end 135 | 136 | % %% Results 137 | % 138 | % figure; 139 | % %plot(BestCost,'LineWidth',2); 140 | % semilogy(BestCost,'LineWidth',2); 141 | % xlabel('Iteration'); 142 | % ylabel('Best Cost'); 143 | % grid on; 144 | 145 | 146 | -------------------------------------------------------------------------------- /benchmark_functions.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Chaotic GSA for Engineering Design Problems 4 | % 5 | % E-Mail: sajad.win8@gmail.com 6 | % 7 | % Homepage: https://github.com/SajadAHMAD1. 8 | % 9 | 10 | % Programmer: Sajad Ahmad Rather 11 | % Developed in MATLAB R2013a 12 | 13 | 14 | % This function calculates the value of objective function. 15 | function PHI=benchmark_functions(x,Benchmark_Function_ID,dim) 16 | 17 | %You can insert your own objective function with a new Benchmark_Function_ID. 18 | % dim=30; 19 | if Benchmark_Function_ID==1 20 | PHI=sum(x.^2); 21 | end 22 | 23 | if Benchmark_Function_ID==2 24 | PHI=sum(abs(x))+prod(abs(x)); 25 | end 26 | 27 | if Benchmark_Function_ID==3 28 | PHI=0; 29 | for i=1:dim 30 | PHI=PHI+sum(x(1:i))^2; 31 | end 32 | end 33 | 34 | if Benchmark_Function_ID==4 35 | PHI=max(abs(x)); 36 | end 37 | 38 | if Benchmark_Function_ID==5 39 | PHI=sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(x(1:dim-1)-1).^2); 40 | end 41 | 42 | if Benchmark_Function_ID==6 43 | PHI=sum(abs((x+.5)).^2); 44 | end 45 | 46 | if Benchmark_Function_ID==7 47 | PHI=sum([1:dim].*(x.^4))+rand; 48 | end 49 | 50 | if Benchmark_Function_ID==8 51 | PHI=sum(-x.*sin(sqrt(abs(x)))); 52 | end 53 | 54 | if Benchmark_Function_ID==9 55 | PHI=sum(x.^2-10*cos(2*pi.*x))+10*dim; 56 | end 57 | 58 | if Benchmark_Function_ID==10 59 | PHI=-20*exp(-.2*sqrt(sum(x.^2)/dim))-exp(sum(cos(2*pi.*x))/dim)+20+exp(1); 60 | end 61 | 62 | if Benchmark_Function_ID==11 63 | PHI=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1; 64 | end 65 | 66 | if Benchmark_Function_ID==12 67 | PHI=(pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*... 68 | (1+10.*((sin(pi.*(1+(x(2:dim)+1)./4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4)); 69 | end 70 | if Benchmark_Function_ID==13 71 | PHI=.1*((sin(3*pi*x(1)))^2+sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2))+... 72 | ((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2))+sum(Ufun(x,5,100,4)); 73 | end 74 | 75 | if Benchmark_Function_ID==14 76 | 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;,... 77 | -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]; 78 | for j=1:25 79 | bS(j)=sum((x'-aS(:,j)).^6); 80 | end 81 | PHI=(1/500+sum(1./([1:25]+bS))).^(-1); 82 | end 83 | 84 | if Benchmark_Function_ID==15 85 | aK=[.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246]; 86 | bK=[.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK; 87 | PHI=sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2); 88 | end 89 | 90 | if Benchmark_Function_ID==16 91 | PHI=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); 92 | end 93 | 94 | if Benchmark_Function_ID==17 95 | PHI=(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; 96 | end 97 | 98 | if Benchmark_Function_ID==18 99 | PHI=(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))*... 100 | (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))); 101 | end 102 | 103 | if Benchmark_Function_ID==19 104 | aH=[3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2]; 105 | pH=[.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 .5743 .8828]; 106 | PHI=0; 107 | for i=1:4 108 | PHI=PHI-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2)))); 109 | end 110 | end 111 | 112 | if Benchmark_Function_ID==20 113 | 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]; 114 | cH=[1 1.2 3 3.2]; 115 | pH=[.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 .1004 .9991;... 116 | .2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 .8732 .5743 .1091 .0381]; 117 | PHI=0; 118 | for i=1:4 119 | PHI=PHI-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2)))); 120 | end 121 | end 122 | 123 | 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]; 124 | cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5]; 125 | 126 | if Benchmark_Function_ID==21 127 | PHI=0; 128 | for i=1:5 129 | PHI=PHI-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1); 130 | end 131 | end 132 | 133 | if Benchmark_Function_ID==22 134 | PHI=0; 135 | for i=1:7 136 | PHI=PHI-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1); 137 | end 138 | end 139 | 140 | if Benchmark_Function_ID==23 141 | PHI=0; 142 | for i=1:10 143 | PHI=PHI-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1); 144 | end 145 | end 146 | if Benchmark_Function_ID==24 %welded design 147 | % fit=PrWf(L); 148 | %% WELDED BEAM DESIGN PROBLEM DEFINITION (all units are in british system) 149 | P = 6000; % APPLIED TIP LOAD 150 | E = 30e6; % YOUNGS MODULUS OF BEAM 151 | G = 12e6; % SHEAR MODULUS OF BEAM 152 | tem = 14; % LENGTH OF CANTILEVER PART OF BEAM 153 | PCONST = 100000; % PENALTY FUNCTION CONSTANT 154 | TAUMAX = 13600; % MAXIMUM ALLOWED SHEAR STRESS 155 | SIGMAX = 30000; % MAXIMUM ALLOWED BENDING STRESS 156 | DELTMAX = 0.25; % MAXIMUM ALLOWED TIP DEFLECTION 157 | M = P*(tem+x(2)/2); % BENDING MOMENT AT WELD POINT 158 | R = sqrt((x(2)^2)/4+((x(1)+x(3))/2)^2); % SOME CONSTANT 159 | J = 2*(sqrt(2)*x(1)*x(2)*((x(2)^2)/4+((x(1)+x(3))/2)^2)); % POLAR MOMENT OF INERTIA 160 | PHI = 1.10471*x(1)^2*x(2)+0.04811*x(3)*x(4)*(14+x(2)); % OBJECTIVE FUNCTION 161 | SIGMA = (6*P*tem)/(x(4)*x(3)^2); % BENDING STRESS 162 | DELTA = (4*P*tem^3)/(E*x(3)^3*x(4)); % TIP DEFLECTION 163 | PC = 4.013*E*sqrt((x(3)^2*x(4)^6)/36)*(1-x(3)*sqrt(E/(4*G))/(2*tem))/(tem^2); % BUCKLING LOAD 164 | TAUP = P/(sqrt(2)*x(1)*x(2)); % 1ST DERIVATIVE OF SHEAR STRESS 165 | TAUPP = (M*R)/J; % 2ND DERIVATIVE OF SHEAR STRESS 166 | TAU = sqrt(TAUP^2+2*TAUP*TAUPP*x(2)/(2*R)+TAUPP^2); % SHEAR STRESS 167 | G1 = TAU-TAUMAX; % MAX SHEAR STRESS CONSTRAINT 168 | G2 = SIGMA-SIGMAX; % MAX BENDING STRESS CONSTRAINT 169 | %G3 = L(1)-L(4); % WELD COVERAGE CONSTRAINT 170 | G3=DELTA-DELTMAX; 171 | G4=x(1)-x(4); 172 | G5=P-PC; 173 | G6=0.125-x(1); 174 | %G4 = 0.10471*L(1)^2+0.04811*L(3)*L(4)*(14+L(2))-5; % MAX COST CONSTRAINT 175 | %G5 = 0.125-L(1); % MAX WELD THICKNESS CONSTRAINT 176 | %G6 = DELTA-DELTMAX; % MAX TIP DEFLECTION CONSTRAINT 177 | %G7 = P-PC; % BUCKLING LOAD CONSTRAINT 178 | G7=1.10471*x(1)^2+0.04811*x(3)*x(4)*(14+x(2))-5; 179 | PHI = PHI + PCONST*(max(0,G1)^2+max(0,G2)^2+... 180 | max(0,G3)^2+max(0,G4)^2+max(0,G5)^2+... 181 | max(0,G6)^2+max(0,G7)^2); % PENALTY FUNCTION 182 | end 183 | 184 | if Benchmark_Function_ID==25 %Spring design 185 | 186 | %% TENSION/COMPRESSION SPRING DESIGN PROBLEM DEFINITION (all units are in british system) 187 | 188 | PCONST = 100; % PENALTY FUNCTION CONSTANT 189 | 190 | fit=(x(3)+2)*x(2)*(x(1)^2); 191 | G1=1-(x(2)^3*x(3))/(71785*x(1)^4); 192 | G2=(4*x(2)^2-x(1)*x(2))/(12566*(x(2)*x(1)^3-x(1)^4))+1/(5108*x(1)^2); 193 | G3=1-(140.45*x(1))/(x(2)^2*x(3)); 194 | G4=((x(1)+x(2))/1.5)-1; 195 | 196 | PHI = fit + PCONST*(max(0,G1)^2++max(0,G2)^2+... 197 | max(0,G3)^2+max(0,G4)^2); % PENALTY FUNCTION 198 | end 199 | 200 | if Benchmark_Function_ID==26 %Pressure vessel design 201 | %% Pressure Vessel design 202 | 203 | PCONST=10000; % PENALTY FUNCTION CONSTANT 204 | fit= 0.6224*x(1)*x(3)*x(4)+ 1.7781*x(2)*x(3)^2 + 3.1661 *x(1)^2*x(4) + 19.84 * x(1)^2*x(3); 205 | G1= -x(1)+ 0.0193*x(3); 206 | G2= -x(3) + 0.00954* x(3); 207 | G3= -pi*x(3)^2*x(4)-(4/3)* pi*x(3)^3 +1296000; 208 | G4= x(4) - 240; 209 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+... 210 | max(0,G3)^2+max(0,G4)^2); % PENALTY FUNCTION 211 | end 212 | if Benchmark_Function_ID==27 %Speed Reducer design 213 | %% Speed Reducer design 214 | PCONST=10000; % PENALTY FUNCTION CONSTANT 215 | fit=0.7854*x(1)*x(2)^2*(3.3333*x(3)^2-14.9334*x(3)-43.0934)-1.508*x(1)*(x(6)^2+x(7)^2)+7.4777*(x(6)^3+x(7)^3)+0.7854*(x(4)*x(6)^2+x(5)*x(7)^2); 216 | G1=27/(x(1)*x(2)^2*x(3))-1; 217 | G2=397.5/(x(1)*x(2)^2*x(3)^2); 218 | G3=1.93*x(4)^3/(x(2)*x(3)*x(6)^4); 219 | G4=1.93*x(5)^3/(x(2)*x(3)*x(7)^4); 220 | G5=1.0/(110*x(6)^3)*((745.0*x(4)/x(2)*x(3))^2+16.9*(10)^6)^(1/2)-1; 221 | G6=1.0/(85*x(7)^3)*((745.0*x(5)/x(2)*x(3))^2+157.5*(10)^6)^1/2-1; 222 | G7=x(2)*x(3)/40; 223 | G8=5*x(2)/(x(1))-1; 224 | G9=x(1)/(12*x(2))-1; 225 | G10=(1.5*x(6)+1.9)/(x(4))-1; 226 | G11=(1.1*x(7)+1.9)/(x(5))-1; 227 | PHI =fit+ PCONST*(max(0,G1)^2+max(0,G2)^2+... 228 | max(0,G3)^2+max(0,G4)^2+max(0,G5)^2+max(0,G6)^2+max(0,G7)^2+max(0,G8)^2+max(0,G9)^2+max(0,G10)^2+max(0,G11)^2); % PENALTY FUNCTION 229 | end 230 | if Benchmark_Function_ID==28 %Gear Train design 231 | %% gear train design 232 | %PCONST=10000; % PENALTY FUNCTION CONSTANT 233 | fit=((1/6.931)- floor (x(1))*floor (x(2))/floor (x(3))*floor (x(4)))^2; 234 | PHI=fit; 235 | end 236 | 237 | if Benchmark_Function_ID==29 %Himmelblau's Problem 238 | %% Himmelblau's Problem 239 | PCONST=10000; % PENALTY FUNCTION CONSTANT 240 | fit= 5.3578547* x(3)^2 + 0.8356891 *x(1)*x(5)+ 37.293239*x(1)-40792.141; 241 | G1= 85.334407 +0.0056858*x(1)*x(5)+0.0006262*x(1)*x(4)-0.0022053*x(3)*x(5); 242 | G2= 80.51249+ 0.0071317*x(2)*x(5)+0.0029955*x(1)*x(2)-0.0021813*x(3)^2; 243 | G3= 9.300961+0.0047026*x(3)*x(5)+0.0012547*x(1)*x(3)-0.0019085*x(3)*x(4); 244 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+ max(0,G3)^2); 245 | end 246 | if Benchmark_Function_ID==30 %Three Bar Truss Design 247 | %% Three Bar Truss Design 248 | PCONST=10000; % PENALTY FUNCTION CONSTANT 249 | P=2; 250 | RU=2; 251 | L=100; 252 | fit=(2*(2*x(1))^1/2+x(2))*L; 253 | G1=((2)^1/2*x(1)+ x(2))/ ((2)^1/2*x(1)^2+ 2*x(1)*x(2))*P-RU; 254 | G2= (x(2))/ ((2)^1/2*x(1)^2+2*x(1)*x(2))*P-RU; 255 | G3= (1)/(x(1)+ (2)^1/2*x(2))*P-RU; 256 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+ max(0,G3)^2); 257 | 258 | end 259 | if Benchmark_Function_ID==31 %Stepped Cantilever Beam Design 260 | %% Stepped Cantilever Beam Design 261 | PCONST=10000; % PENALTY FUNCTION CONSTANT 262 | L=100; 263 | P=50000; 264 | E=2*107; 265 | fit=(x(1)*x(6)+x(2)*x(7)+x(3)*x(8)+x(4)*x(9)+x(5)*x(10))*L; 266 | G1= ((600*P)/(x(5)*x(10)^2))-14000; 267 | G2= ((6*P*2*L)/(x(4)*x(9)^2))-14000; 268 | G3= ((6*P*3*L)/(x(3)*x(8)^2))-14000; 269 | G4= ((6*P*4*L)/(x(2)*x(7)^2))-14000; 270 | G5= ((6*P*5*L)/(x(1)*x(6)^2))-14000; 271 | G6= (((P*L^3)/(3*E))*(125/L))-2.7; 272 | G7=(x(10)/x(5))-20; 273 | G8=(x(9)/x(4))-20; 274 | G9=(x(8)/x(3))-20; 275 | G10=(x(7)/x(2))-20; 276 | G11=(x(6)/x(1))-20; 277 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+... 278 | max(0,G3)^2+max(0,G4)^2+max(0,G5)^2+max(0,G6)^2+max(0,G7)^2+max(0,G8)^2+max(0,G9)^2+max(0,G10)^2+max(0,G11)^2); % PENALTY FUNCTION 279 | 280 | end 281 | if Benchmark_Function_ID==32 %Multiple Disc Clutch Brake Design 282 | %% Multiple Disc Clutch Brake Design 283 | PCONST=100000; % PENALTY FUNCTION CONSTANT 284 | DR=20; 285 | % T=3; 286 | L=30; 287 | % Z=10; 288 | VSR=10; 289 | MU=0.5; 290 | S=1.5; 291 | MS=40; 292 | MF=3; 293 | n=250; 294 | P=1; 295 | I=55; 296 | T=15; 297 | % F=1000; 298 | % RI=80; 299 | % % RO=110; 300 | MH=(2/3)*MU*x(4)*x(5)*((x(2)^3-x(1)^3)/(x(2)^2-x(1)^2)); 301 | PRZ=x(4)/pi*(x(2)^2-x(1)^2); 302 | VSR1= (2/90)*pi*n*((x(2)^3-x(1)^3)/(x(2)^2-x(1)^2)); 303 | T1= (I*pi*n)/(30*(MH+MF)); 304 | fit=pi*x(3)*( x(2)^2-x(1)^2)*(x(5)+1)*8*P; 305 | G1= x(2)-x(1)-DR; 306 | G2= L-(x(5)+1)*x(3); 307 | G3= P-PRZ; 308 | G4= P*VSR-PRZ*VSR; 309 | G5=VSR-VSR1; 310 | G6= T-T1; 311 | G7= MH-S*MS; 312 | G8= T1; 313 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+... 314 | max(0,G3)^2+max(0,G4)^2+max(0,G5)^2+max(0,G6)^2+max(0,G7)^2+max(0,G8)^2); 315 | end 316 | if Benchmark_Function_ID==33 %Hydrodynamic Thrust Bearing Design 317 | %% Hydrodynamic Thrust Bearing Design 318 | PCONST=100000; % PENALTY FUNCTION CONSTANT 319 | WS=101000; 320 | PMAX=1000; 321 | DTM=50; 322 | HM=0.001; 323 | Y=0.0307; 324 | C=0.5; 325 | C1=10.04; 326 | n=-3.55; 327 | Ne=750; 328 | Ge=386.4; 329 | P= (log10(log10(8.1222*1e6*x(3)+0.8)-C1))/n; 330 | DT= 2*(10^P-560); 331 | EF= 9336* x(4)*Y*C*DT; 332 | H= ((2*pi*Ne)/60)^2*((2*pi*x(3))/EF)*((x(1)^4/4)-((x(2)^4)/4)); 333 | P0= (6*x(3)*x(4)/pi*H^3)* log(x(1)/x(2)); 334 | W=((pi*P0)/2)*((x(1)^2*x(2)^2)/log(x(1)/x(2))); 335 | fit=((x(4)*P0)/0.7)+EF; 336 | G1= W-WS; 337 | G2=PMAX-P0; 338 | G3= DTM-DT; 339 | G4= H-HM; 340 | G5=x(1)-x(2); 341 | G6= 0.001- (Y/(Ge*P0))*(x(4)/2*pi*x(1)*H); 342 | G7= 5000- W/(pi*(x(1)^2*x(2)^2)); 343 | PHI =fit + PCONST*(max(0,G1)^2+max(0,G2)^2+... 344 | max(0,G3)^2+max(0,G4)^2+max(0,G5)^2+max(0,G6)^2+max(0,G7)^2); 345 | end 346 | % function y=Ufun(x,a,k,m) 347 | % y=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a)); 348 | % return -------------------------------------------------------------------------------- /benchmark_functions_details.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Chaotic GSA for Engineering Design Problems 4 | % 5 | % E-Mail: sajad.win8@gmail.com 6 | % 7 | % Homepage: https://github.com/SajadAHMAD1. 8 | % 9 | 10 | % Programmer: Sajad Ahmad Rather 11 | % Developed in MATLAB R2013a 12 | 13 | 14 | 15 | % This function gives boundaries and dimension of search space for test functions. 16 | function [down,up,dim]=benchmark_functions_details(Benchmark_Function_ID) 17 | 18 | %If lower bounds of dimensions are the same, then 'down' is a value. 19 | %Otherwise, 'down' is a vector that shows the lower bound of each dimension. 20 | %This is also true for upper bounds of dimensions. 21 | 22 | %Insert your own boundaries with a new Benchmark_Function_ID. 23 | 24 | dim=30; 25 | if Benchmark_Function_ID==1 26 | down=-100;up=100; 27 | end 28 | 29 | if Benchmark_Function_ID==2 30 | down=-10;up=10; 31 | end 32 | 33 | if Benchmark_Function_ID==3 34 | down=-100;up=100; 35 | end 36 | 37 | if Benchmark_Function_ID==4 38 | down=-100;up=100; 39 | end 40 | 41 | if Benchmark_Function_ID==5 42 | down=-30;up=30; 43 | end 44 | 45 | if Benchmark_Function_ID==6 46 | down=-100;up=100; 47 | end 48 | 49 | if Benchmark_Function_ID==7 50 | down=-1.28;up=1.28; 51 | end 52 | 53 | if Benchmark_Function_ID==8 54 | down=-500;up=500; 55 | end 56 | 57 | if Benchmark_Function_ID==9 58 | down=-5.12;up=5.12; 59 | end 60 | 61 | if Benchmark_Function_ID==10 62 | down=-32;up=32; 63 | end 64 | 65 | if Benchmark_Function_ID==11 66 | down=-600;up=600; 67 | end 68 | 69 | if Benchmark_Function_ID==12 70 | down=-50;up=50; 71 | end 72 | 73 | if Benchmark_Function_ID==13 74 | down=-50;up=50; 75 | end 76 | 77 | if Benchmark_Function_ID==14 78 | down=-65.536;up=65.536;dim=2; 79 | end 80 | 81 | if Benchmark_Function_ID==15 82 | down=-5;up=5;dim=4; 83 | end 84 | 85 | if Benchmark_Function_ID==16 86 | down=-5;up=5;dim=2; 87 | end 88 | 89 | if Benchmark_Function_ID==17 90 | down=[-5;0];up=[10;15];dim=2; 91 | end 92 | 93 | if Benchmark_Function_ID==18 94 | down=-2;up=2;dim=2; 95 | end 96 | 97 | if Benchmark_Function_ID==19 98 | down=0;up=1;dim=3; 99 | end 100 | 101 | if Benchmark_Function_ID==20 102 | down=0;up=1;dim=6; 103 | end 104 | 105 | if Benchmark_Function_ID==21 106 | down=0;up=10;dim=4; 107 | end 108 | 109 | if Benchmark_Function_ID==22 110 | down=0;up=10;dim=4; 111 | end 112 | 113 | if Benchmark_Function_ID==23 114 | down=0;up=10;dim=4; 115 | end 116 | if Benchmark_Function_ID==24 %Welded Design 117 | down=[0.10;0.10;0.10;0.10]; 118 | up=[2;10;10;2]; 119 | dim=4; 120 | end 121 | if Benchmark_Function_ID==25 %Spring Design 122 | down=[0.05;0.25;2.00]; 123 | up=[2.00;1.30;15.0]; 124 | dim=3; 125 | 126 | end 127 | if Benchmark_Function_ID==26 %Pressure vessel 128 | 129 | down=[0;0;10;10]; % Lower Bound of Variables 130 | up= [99;99;200;200]; % Upper Bound of Variables 131 | dim=4; 132 | end 133 | if Benchmark_Function_ID==27 %Speed Reducer design 134 | down=[2.6;0.7;17;7.3;7.8;2.9;5]; % Lower Bound of Variables 135 | up= [3.6;0.8;28;8.3;8.3;3.9;5.5]; % Upper Bound of Variables 136 | dim=7; 137 | end 138 | if Benchmark_Function_ID==28 %Gear Train design 139 | down=[12;12;12;12]; % Lower Bound of Variables 140 | up=[60;60;60;60]; % Upper Bound of Variables 141 | dim=4; 142 | end 143 | if Benchmark_Function_ID==29 %Himmelblau's Problem 144 | down=[78;33;27;27;27]; % Lower Bound of Variables 145 | up=[102;45;45;45;45]; % Upper Bound of Variables 146 | dim=5; 147 | end 148 | if Benchmark_Function_ID==30 % Three Bar Truss Design 149 | down=[0;0]; % Lower Bound of Variables 150 | up=[1;1]; % Upper Bound of Variables 151 | dim=2; 152 | end 153 | if Benchmark_Function_ID==31 % Stepped Cantilever Beam Design 154 | down=[1;1;1;1;1;30;30;30;30;30]; % Lower Bound of Variables 155 | up=[5;5;5;5;5;65;65;65;65;65]; % Upper Bound of Variables 156 | dim=10; 157 | end 158 | if Benchmark_Function_ID==32 % Multiple Disc Clutch Brake Design 159 | down=[60;90;1.5;600;2]; % Lower Bound of Variables 160 | up=[80;110;3;1000;9]; % Upper Bound of Variables 161 | dim=5; 162 | end 163 | if Benchmark_Function_ID==33 % Hydrodynamic Thrust Bearing Design 164 | down=[1;1;1e6;1]; % Lower Bound of Variables 165 | up=[16;16;16e6;16]; % Upper Bound of Variables 166 | dim=4; 167 | end 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /crossover_continious.m: -------------------------------------------------------------------------------- 1 | 2 | function [child1 , child2] = crossover_continious(parent1 , parent2, Pc, Benchmark_Function_ID,dim,up,low) 3 | 4 | child1.Gene = zeros(1,dim); 5 | child2.Gene = zeros(1,dim); 6 | for k = 1 : dim 7 | beta = rand(); 8 | child1.Gene(k) = beta .* parent1.Gene(k) + (1-beta)*parent2.Gene(k); 9 | child2.Gene(k) = (1-beta) .* parent1.Gene(k) + beta*parent2.Gene(k); 10 | 11 | if child1.Gene(k) > up(k) 12 | child1.Gene(k) = up(k); 13 | end 14 | if child1.Gene(k) < low(k) 15 | child1.Gene(k) = low(k); 16 | end 17 | 18 | if child2.Gene(k) > up(k) 19 | child2.Gene(k) = up(k); 20 | end 21 | 22 | if child2.Gene(k) < low(k) 23 | child2.Gene(k) = low(k); 24 | end 25 | end 26 | 27 | R1 = rand(); 28 | 29 | if R1 <= Pc 30 | child1 = child1; 31 | else 32 | child1 = parent1; 33 | end 34 | 35 | R2 = rand(); 36 | 37 | if R2 <= Pc 38 | child2 = child2; 39 | else 40 | child2 = parent2; 41 | end 42 | 43 | end -------------------------------------------------------------------------------- /elitism.m: -------------------------------------------------------------------------------- 1 | 2 | function [ newPopulation2 ] = elitism(population , newpopulation, Er) 3 | 4 | M = length(newpopulation.Chromosomes); % number of individuals 5 | Elite_no = round(M * Er); 6 | 7 | [max_val , indx] = sort([ population.Chromosomes(:).fitness ] , 'descend'); 8 | 9 | 10 | 11 | for k = 1 : Elite_no 12 | newPopulation2.Chromosomes(k).Gene = population.Chromosomes(indx(k)).Gene; 13 | newPopulation2.Chromosomes(k).fitness = population.Chromosomes(indx(k)).fitness; 14 | end 15 | 16 | for k = Elite_no + 1 : length(newpopulation.Chromosomes) 17 | newPopulation2.Chromosomes(k).Gene = newpopulation.Chromosomes(k).Gene; 18 | newPopulation2.Chromosomes(k).fitness = newpopulation.Chromosomes(k).fitness; 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /evaluateF.m: -------------------------------------------------------------------------------- 1 | 2 | function fitness=evaluateF(X,Benchmark_Function_ID) 3 | 4 | [dim,N]=size(X); 5 | for i=1:N 6 | L=X(:,i)'; 7 | %calculation of objective function 8 | fitness(i)=benchmark_functions(L,Benchmark_Function_ID,dim); 9 | end -------------------------------------------------------------------------------- /initialization.m: -------------------------------------------------------------------------------- 1 | 2 | %This function initializes the position of the agents in the search space, randomly. 3 | function [X]=initialization(dim,N,up,down) 4 | 5 | if size(up,1)==1 6 | X=rand(dim,N).*(up-down)+down; 7 | end 8 | if size(up,1)>1 9 | for i=1:dim 10 | high=up(i);low=down(i); 11 | X(i,:)=rand(1,N).*(high-low)+low; 12 | end 13 | end -------------------------------------------------------------------------------- /massCalculation.m: -------------------------------------------------------------------------------- 1 | 2 | function [M]=massCalculation(fit,min_flag); 3 | %%%%here, make your own function of 'mass calculation' 4 | 5 | Fmax=max(fit); Fmin=min(fit); Fmean=mean(fit); 6 | [i N]=size(fit); 7 | 8 | if Fmax==Fmin 9 | M=ones(N,1); 10 | else 11 | 12 | if min_flag==1 %for minimization 13 | best=Fmin;worst=Fmax; %eq.17-18. 14 | else %for maximization 15 | best=Fmax;worst=Fmin; %eq.19-20. 16 | end 17 | 18 | M=(fit-worst)./(best-worst); %eq.15, 19 | 20 | end 21 | 22 | M=M./sum(M); %eq. 16. -------------------------------------------------------------------------------- /move.m: -------------------------------------------------------------------------------- 1 | %This function updates the velocity and position of agents. 2 | function [X,V]=move(X,a,V) 3 | 4 | %movement. 5 | [dim,N]=size(X); 6 | V=rand(dim,N).*V+a; %eq. 11. 7 | X=X+V; %eq. 12. -------------------------------------------------------------------------------- /mutation_continious.m: -------------------------------------------------------------------------------- 1 | 2 | function [child] = mutation_continious(child, Pm, Benchmark_Function_ID,up,low) 3 | 4 | Gene_no = length(child.Gene); 5 | 6 | for k = 1: Gene_no 7 | R = rand(); 8 | if R < Pm 9 | child.Gene(k) = (up(k) - low(k)) * rand() +low(k); 10 | end 11 | end 12 | 13 | end -------------------------------------------------------------------------------- /pso.m: -------------------------------------------------------------------------------- 1 | % 2 | 3 | 4 | % Chaotic GSA for Engineering Design Problems 5 | % 6 | % E-Mail: sajad.win8@gmail.com 7 | % 8 | % Homepage: https://github.com/SajadAHMAD1. 9 | % 10 | 11 | % Programmer: Sajad Ahmad Rather 12 | % Developed in MATLAB R2013a 13 | 14 | 15 | 16 | function [cgCurve,GBEST] = pso( Benchmark_Function_ID, N, Max_Iteration) 17 | 18 | 19 | % dim=30; 20 | 21 | 22 | % Define the details of the objective function 23 | %get allowable range and dimension of the test function. 24 | [down,up,dim]=benchmark_functions_details(Benchmark_Function_ID); 25 | % PSO parameters 26 | % noP = 5; 27 | % maxIter = 100; 28 | % visFlag = 0; 29 | % 30 | % RunNo = 30; 31 | % 32 | % 33 | % for k = [ 1 : 1 : RunNo ] 34 | % fit=benchmark_functions(currentX,Benchmark_Function_ID,dim); 35 | 36 | % PSO 1 37 | % [ GBEST , cgcurve1] = PSO( Benchmark_Function_ID, N, Max_Iteration) ; 38 | % BestSolutions1(k) = GBEST.O; 39 | % 40 | % disp(['Run # ' , num2str(k), 'GBEST.O: ' , num2str( GBEST.O)]); 41 | % end 42 | % % Define the details of the objective function 43 | % %get allowable range and dimension of the test function. 44 | % [down,up,dim]=benchmark_functions_details(Benchmark_Function_ID); 45 | 46 | 47 | % Define the PSO's paramters 48 | wMax = 0.9; 49 | wMin = 0.2; 50 | c1 = 2; 51 | c2 = 2; 52 | if size(up,1)==1 53 | current_position=rand(dim,N).*(up-down)+down; 54 | VelMax=0.1*(up-down); 55 | VelMin=-up; 56 | end 57 | if size(up,1)>1 58 | for i=1:dim 59 | high=up(i);ll=down(i); 60 | current_position(i,:)=rand(1,N).*(high-ll)+ll; 61 | VelMax=0.1*(high-ll); 62 | VelMin=-high; 63 | end 64 | end 65 | vMax = (up - down) .* 0.2; 66 | vMin = -vMax; 67 | 68 | 69 | % The PSO algorithm 70 | 71 | % Initialize the particles 72 | for k = 1 :N 73 | % Swarm.Particles(k).X = (up-down) .* rand(1,dim) + down; 74 | Swarm.Particles(k).X = (high-ll) .* rand(1,dim) + ll; 75 | 76 | Swarm.Particles(k).V = zeros(1, dim); 77 | Swarm.Particles(k).PBEST.X = zeros(1,dim); 78 | Swarm.Particles(k).PBEST.O = inf; 79 | 80 | Swarm.GBEST.X = zeros(1,dim); 81 | Swarm.GBEST.O = inf; 82 | end 83 | 84 | 85 | % Main loop 86 | for t = 1 : Max_Iteration 87 | 88 | % Calcualte the objective value 89 | for k = 1 : N 90 | currentX = Swarm.Particles(k).X; 91 | Swarm.Particles(k).O = benchmark_functions(currentX,Benchmark_Function_ID,dim); 92 | 93 | % Update the PBEST 94 | if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O 95 | Swarm.Particles(k).PBEST.X = currentX; 96 | Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O; 97 | end 98 | 99 | % Update the GBEST 100 | if Swarm.Particles(k).O < Swarm.GBEST.O 101 | Swarm.GBEST.X = currentX; 102 | Swarm.GBEST.O = Swarm.Particles(k).O; 103 | end 104 | end 105 | 106 | % Update the X and V vectors 107 | w = wMax - t .* ((wMax - wMin) / Max_Iteration); 108 | 109 | for k = 1 : N 110 | Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,dim) .* (Swarm.Particles(k).PBEST.X - Swarm.Particles(k).X) ... 111 | + c2 .* rand(1,dim) .* (Swarm.GBEST.X - Swarm.Particles(k).X); 112 | 113 | % Apply Velocity Limits 114 | Swarm.Particles(k).V= max(Swarm.Particles(k).V,VelMin); 115 | Swarm.Particles(k).V = min(Swarm.Particles(k).V,VelMax); 116 | % % Check velocities 117 | % index1 = find(Swarm.Particles(k).V > vMax); 118 | % index2 = find(Swarm.Particles(k).V < vMin); 119 | % % 120 | % Swarm.Particles(k).V(index1) = vMax(index1); 121 | % Swarm.Particles(k).V(index2) = vMin(index2); 122 | 123 | Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V; 124 | % Velocity Mirror Effect 125 | % IsOutside=(Swarm.Particles(k).Xup); 126 | % Swarm.Particles(k).V (IsOutside)=-Swarm.Particles(k).V (IsOutside); 127 | % Velocity Mirror Effect 128 | IsOutside=(Swarm.Particles(k).Xhigh); 129 | Swarm.Particles(k).V (IsOutside)=-Swarm.Particles(k).V (IsOutside); 130 | % % Apply Position Limits 131 | Swarm.Particles(k).X = max(Swarm.Particles(k).X,ll); 132 | Swarm.Particles(k).X= min(Swarm.Particles(k).X,high); 133 | % % Apply Position Limits 134 | % Swarm.Particles(k).X = max(Swarm.Particles(k).X,down); 135 | % Swarm.Particles(k).X= min(Swarm.Particles(k).X,up); 136 | % Check positions 137 | % index1 = find(Swarm.Particles(k).X > high); 138 | % index2 = find(Swarm.Particles(k).X < ll); 139 | % 140 | % Swarm.Particles(k).X(index1) = high(index1); 141 | % Swarm.Particles(k).X(index2) = ll(index2); 142 | % 143 | end 144 | 145 | % if dataVis == 1 146 | % outmsg = ['Iteration# ', num2str(t) , ' Swarm.GBEST.O = ' , num2str(Swarm.GBEST.O)]; 147 | % disp(outmsg); 148 | % end 149 | % 150 | cgCurve(t) = Swarm.GBEST.O; 151 | end 152 | 153 | GBEST = Swarm.GBEST; 154 | 155 | % if dataVis == 1 156 | % semilogy(cgCurve); 157 | % xlabel('Iteration#') 158 | % ylabel('Weight') 159 | % end -------------------------------------------------------------------------------- /selection.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [parent1, parent2] = selection(population) 4 | 5 | N = length(population.Chromosomes(:)); 6 | 7 | if any([population.Chromosomes(:).fitness] < 0 ) 8 | % Fitness scaling in case of negative values scaled(f) = a * f + b 9 | a = 1; 10 | b = abs( min( [population.Chromosomes(:).fitness] ) ); 11 | Scaled_fitness = a * [population.Chromosomes(:).fitness] + b; 12 | 13 | normalized_fitness = [Scaled_fitness] ./ sum([Scaled_fitness]); 14 | else 15 | normalized_fitness = [population.Chromosomes(:).fitness] ./ sum([population.Chromosomes(:).fitness]); 16 | end 17 | 18 | 19 | [sorted_fintness_values , sorted_idx] = sort(normalized_fitness , 'descend'); 20 | 21 | for i = 1 : length(population.Chromosomes) 22 | temp_population.Chromosomes(i).Gene = population.Chromosomes(sorted_idx(i)).Gene; 23 | temp_population.Chromosomes(i).fitness = population.Chromosomes(sorted_idx(i)).fitness; 24 | temp_population.Chromosomes(i).normalized_fitness = normalized_fitness(sorted_idx(i)); 25 | end 26 | 27 | 28 | cumsum = zeros(1 , N); 29 | 30 | for i = 1 : N 31 | for j = i : N 32 | cumsum(i) = cumsum(i) + temp_population.Chromosomes(j).normalized_fitness; 33 | end 34 | end 35 | 36 | 37 | R = rand(); % in [0,1] 38 | parent1_idx = N; 39 | for i = 1: length(cumsum) 40 | if R > cumsum(i) 41 | parent1_idx = i - 1; 42 | break; 43 | end 44 | end 45 | 46 | parent2_idx = parent1_idx; 47 | while_loop_stop = 0; % to break the while loop in rare cases where we keep getting the same index 48 | while parent2_idx == parent1_idx 49 | while_loop_stop = while_loop_stop + 1; 50 | R = rand(); % in [0,1] 51 | if while_loop_stop > 20 52 | break; 53 | end 54 | for i = 1: length(cumsum) 55 | if R > cumsum(i) 56 | parent2_idx = i - 1; 57 | break; 58 | end 59 | end 60 | end 61 | 62 | parent1 = temp_population.Chromosomes(parent1_idx); 63 | parent2 = temp_population.Chromosomes(parent2_idx); 64 | 65 | end -------------------------------------------------------------------------------- /space_bound.m: -------------------------------------------------------------------------------- 1 | 2 | %This function checks the search space boundaries for agents. 3 | function X=space_bound(X,up,low); 4 | 5 | [dim,mass_num]=size(X); 6 | for i=1:mass_num 7 | Tp=X(:,i)>up;Tm=X(:,i)