• MATLAB算法实战应用案例精讲-【优化算法】树木生长算法(TGA)(附MATLAB代码实现)


    前言

    Armin Cheraghalipour 根据树木生长的特点于2017 年提出了一种新的元启发式优化算法TGA该算将始定量的群按照解的适应度从高到低排序,分成4组具有不同功能的种群。每次迭代分别进行处理。

    算法原理

    算法流程图

     

    代码实现

    MATLAB

    TGA.m

    1. % "Tree growth algorithm (TGA): A novel approach for solving
    2. % optimization problems"
    3. function [fitG,Xgb,curve] = TGA(N, max_Iter, lb,ub ,dim, fun)
    4. % Parameters
    5. num_tree1 = 3; % size of first group
    6. num_tree2 = 5; % size of second group
    7. num_tree4 = 3; % size of fourth group
    8. theta = 0.8; % tree reduction rate of power
    9. lambda = 0.5; % control nearest tree
    10. % Limit number of N4 to N1
    11. if num_tree4 > num_tree1 + num_tree2
    12. num_tree4 = num_tree1 + num_tree2;
    13. end
    14. % Initial
    15. X = zeros(N,dim);
    16. for i = 1:N
    17. for d = 1:dim
    18. X(i,d) = lb + (ub - lb) * rand();
    19. end
    20. end
    21. % Fitness
    22. fit = zeros(1,N);
    23. fitG = inf;
    24. for i = 1:N
    25. fit(i) = fun(X(i,:));
    26. % Best
    27. if fit(i) < fitG
    28. fitG = fit(i);
    29. Xgb = X(i,:);
    30. end
    31. end
    32. % Sort tree from best to worst
    33. [fit, idx] = sort(fit,'ascend');
    34. X = X(idx,:);
    35. % Initial
    36. dist = zeros(1,num_tree1 + num_tree2);
    37. X1 = zeros(num_tree1,dim);
    38. Xnew = zeros(num_tree4,dim);
    39. Fnew = zeros(1,num_tree4);
    40. curve = zeros(1,max_Iter);
    41. curve(1) = fitG;
    42. t = 2;
    43. % Iterations
    44. while t <= max_Iter
    45. % {1} Best trees group
    46. for i = 1:num_tree1
    47. r1 = rand();
    48. for d = 1:dim
    49. % Local search (1)
    50. X1(i,d) = (X(i,d) / theta) + r1 * X(i,d);
    51. end
    52. % Boundary
    53. XB = X1(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
    54. X1(i,:) = XB;
    55. % Fitness
    56. fitT = fun(X1(i,:));
    57. % Greedy selection
    58. if fitT <= fit(i)
    59. X(i,:) = X1(i,:);
    60. fit(i) = fitT;
    61. end
    62. end
    63. % {2} Competitive for light tree group
    64. X_ori = X;
    65. for i = num_tree1 + 1 : num_tree1 + num_tree2
    66. % Neighbor tree
    67. for j = 1 : num_tree1 + num_tree2
    68. if j ~= i
    69. % Compute Euclidean distance (2)
    70. dist(j) = sqrt(sum((X_ori(j,:) - X_ori(i,:)) .^ 2));
    71. else
    72. % Solve same tree problem
    73. dist(j) = inf;
    74. end
    75. end
    76. % Find 2 trees with shorter distance
    77. [~, idx] = sort(dist,'ascend');
    78. T1 = X_ori(idx(1),:);
    79. T2 = X_ori(idx(2),:);
    80. % Alpha in [0,1]
    81. alpha = rand();
    82. for d = 1:dim
    83. % Compute linear combination between 2 shorter tree (3)
    84. y = lambda * T1(d) + (1 - lambda) * T2(d);
    85. % Move tree i between 2 adjacent trees (4)
    86. X(i,d) = X(i,d) + alpha * y;
    87. end
    88. % Boundary
    89. XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
    90. X(i,:) = XB;
    91. % Fitness
    92. fit(i) = fun(X(i,:));
    93. end
    94. % {3} Remove and replace group
    95. for i = num_tree1 + num_tree2 + 1 : N
    96. for d = 1:dim
    97. % Generate new tree by remove worst tree
    98. X(i,d) = lb + (ub - lb) * rand();
    99. end
    100. % Fitness
    101. fit(i) = fun(X(i,:) );
    102. end
    103. % {4} Reproduction group
    104. for i = 1:num_tree4
    105. % Random a best tree
    106. r = randi([1,num_tree1]);
    107. Xbest = X(r,:);
    108. % Mask operator
    109. mask = randi([0,1],1,dim);
    110. % Mask opration between new & best trees
    111. for d = 1:dim
    112. % Generate new solution
    113. Xn = lb + (ub - lb) * rand();
    114. if mask(d) == 1
    115. Xnew(i,d) = Xbest(d);
    116. elseif mask(d) == 0
    117. % Generate new tree
    118. Xnew(i,d) = Xn;
    119. end
    120. end
    121. % Fitness
    122. Fnew(i) = fun(Xnew(i,:));
    123. end
    124. % Sort population get best nPop trees
    125. XX = [X; Xnew];
    126. FF = [fit, Fnew];
    127. [FF, idx] = sort(FF,'ascend');
    128. X = XX(idx(1:N),:);
    129. fit = FF(1:N);
    130. % Global best
    131. if fit(1) < fitG
    132. fitG = fit(1);
    133. Xgb = X(1,:);
    134. end
    135. curve(t) = fitG;
    136. t = t + 1;
    137. end
    138. end

    func_plot.m

    1. % This function draw the benchmark functions
    2. function func_plot(func_name)
    3. [lb,ub,dim,fobj]=Get_Functions_details(func_name);
    4. switch func_name
    5. case 'F1'
    6. x=-100:2:100; y=x; %[-100,100]
    7. case 'F2'
    8. x=-100:2:100; y=x; %[-10,10]
    9. case 'F3'
    10. x=-100:2:100; y=x; %[-100,100]
    11. case 'F4'
    12. x=-100:2:100; y=x; %[-100,100]
    13. case 'F5'
    14. x=-200:2:200; y=x; %[-5,5]
    15. case 'F6'
    16. x=-100:2:100; y=x; %[-100,100]
    17. case 'F7'
    18. x=-1:0.03:1; y=x; %[-1,1]
    19. case 'F8'
    20. x=-500:10:500;y=x; %[-500,500]
    21. case 'F9'
    22. x=-5:0.1:5; y=x; %[-5,5]
    23. case 'F10'
    24. x=-20:0.5:20; y=x;%[-500,500]
    25. case 'F11'
    26. x=-500:10:500; y=x;%[-0.5,0.5]
    27. case 'F12'
    28. x=-10:0.1:10; y=x;%[-pi,pi]
    29. case 'F13'
    30. x=-5:0.08:5; y=x;%[-3,1]
    31. case 'F14'
    32. x=-100:2:100; y=x;%[-100,100]
    33. case 'F15'
    34. x=-5:0.1:5; y=x;%[-5,5]
    35. case 'F16'
    36. x=-1:0.01:1; y=x;%[-5,5]
    37. case 'F17'
    38. x=-5:0.1:5; y=x;%[-5,5]
    39. case 'F18'
    40. x=-5:0.06:5; y=x;%[-5,5]
    41. case 'F19'
    42. x=-5:0.1:5; y=x;%[-5,5]
    43. case 'F20'
    44. x=-5:0.1:5; y=x;%[-5,5]
    45. case 'F21'
    46. x=-5:0.1:5; y=x;%[-5,5]
    47. case 'F22'
    48. x=-5:0.1:5; y=x;%[-5,5]
    49. case 'F23'
    50. x=-5:0.1:5; y=x;%[-5,5]
    51. end
    52. L=length(x);
    53. f=[];
    54. for i=1:L
    55. for j=1:L
    56. 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
    57. f(i,j)=fobj([x(i),y(j)]);
    58. end
    59. if strcmp(func_name,'F15')==1
    60. f(i,j)=fobj([x(i),y(j),0,0]);
    61. end
    62. if strcmp(func_name,'F19')==1
    63. f(i,j)=fobj([x(i),y(j),0]);
    64. end
    65. if strcmp(func_name,'F20')==1
    66. f(i,j)=fobj([x(i),y(j),0,0,0,0]);
    67. end
    68. if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 ||strcmp(func_name,'F23')==1
    69. f(i,j)=fobj([x(i),y(j),0,0]);
    70. end
    71. end
    72. end
    73. surfc(x,y,f,'LineStyle','none');
    74. end

    Get_Functions_details.m

    1. % This function containts full information and implementations of the benchmark
    2. % functions in Table 1, Table 2, and Table 3 in the paper
    3. % lb is the lower bound: lb=[lb_1,lb_2,...,lb_d]
    4. % up is the uppper bound: ub=[ub_1,ub_2,...,ub_d]
    5. % dim is the number of variables (dimension of the problem)
    6. function [lb,ub,dim,fobj] = Get_Functions_details(F)
    7. switch F
    8. case 'F1'
    9. fobj = @F1;
    10. lb=-100;
    11. ub=100;
    12. dim=30;
    13. case 'F2'
    14. fobj = @F2;
    15. lb=-10;
    16. ub=10;
    17. dim=30;
    18. case 'F3'
    19. fobj = @F3;
    20. lb=-100;
    21. ub=100;
    22. dim=30;
    23. case 'F4'
    24. fobj = @F4;
    25. lb=-100;
    26. ub=100;
    27. dim=30;
    28. case 'F5'
    29. fobj = @F5;
    30. lb=-30;
    31. ub=30;
    32. dim=30;
    33. case 'F6'
    34. fobj = @F6;
    35. lb=-100;
    36. ub=100;
    37. dim=30;
    38. case 'F7'
    39. fobj = @F7;
    40. lb=-1.28;
    41. ub=1.28;
    42. dim=30;
    43. case 'F8'
    44. fobj = @F8;
    45. lb=-500;
    46. ub=500;
    47. dim=30;
    48. case 'F9'
    49. fobj = @F9;
    50. lb=-5.12;
    51. ub=5.12;
    52. dim=30;
    53. case 'F10'
    54. fobj = @F10;
    55. lb=-32;
    56. ub=32;
    57. dim=30;
    58. case 'F11'
    59. fobj = @F11;
    60. lb=-600;
    61. ub=600;
    62. dim=30;
    63. case 'F12'
    64. fobj = @F12;
    65. lb=-50;
    66. ub=50;
    67. dim=30;
    68. case 'F13'
    69. fobj = @F13;
    70. lb=-50;
    71. ub=50;
    72. dim=30;
    73. case 'F14'
    74. fobj = @F14;
    75. lb=-65.536;
    76. ub=65.536;
    77. dim=2;
    78. case 'F15'
    79. fobj = @F15;
    80. lb=-5;
    81. ub=5;
    82. dim=4;
    83. case 'F16'
    84. fobj = @F16;
    85. lb=-5;
    86. ub=5;
    87. dim=2;
    88. case 'F17'
    89. fobj = @F17;
    90. lb=[-5,0];
    91. ub=[10,15];
    92. dim=2;
    93. case 'F18'
    94. fobj = @F18;
    95. lb=-2;
    96. ub=2;
    97. dim=2;
    98. case 'F19'
    99. fobj = @F19;
    100. lb=0;
    101. ub=1;
    102. dim=3;
    103. case 'F20'
    104. fobj = @F20;
    105. lb=0;
    106. ub=1;
    107. dim=6;
    108. case 'F21'
    109. fobj = @F21;
    110. lb=0;
    111. ub=10;
    112. dim=4;
    113. case 'F22'
    114. fobj = @F22;
    115. lb=0;
    116. ub=10;
    117. dim=4;
    118. case 'F23'
    119. fobj = @F23;
    120. lb=0;
    121. ub=10;
    122. dim=4;
    123. end
    124. end
    125. % F1
    126. function o = F1(x)
    127. o=sum(x.^2);
    128. end
    129. % F2
    130. function o = F2(x)
    131. o=sum(abs(x))+prod(abs(x));
    132. end
    133. % F3
    134. function o = F3(x)
    135. dim=size(x,2);
    136. o=0;
    137. for i=1:dim
    138. o=o+sum(x(1:i))^2;
    139. end
    140. end
    141. % F4
    142. function o = F4(x)
    143. o=max(abs(x));
    144. end
    145. % F5
    146. function o = F5(x)
    147. dim=size(x,2);
    148. o=sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(x(1:dim-1)-1).^2);
    149. end
    150. % F6
    151. function o = F6(x)
    152. o=sum(abs((x+.5)).^2);
    153. end
    154. % F7
    155. function o = F7(x)
    156. dim=size(x,2);
    157. o=sum([1:dim].*(x.^4))+rand;
    158. end
    159. % F8
    160. function o = F8(x)
    161. o=sum(-x.*sin(sqrt(abs(x))));
    162. end
    163. % F9
    164. function o = F9(x)
    165. dim=size(x,2);
    166. o=sum(x.^2-10*cos(2*pi.*x))+10*dim;
    167. end
    168. % F10
    169. function o = F10(x)
    170. dim=size(x,2);
    171. o=-20*exp(-.2*sqrt(sum(x.^2)/dim))-exp(sum(cos(2*pi.*x))/dim)+20+exp(1);
    172. end
    173. % F11
    174. function o = F11(x)
    175. dim=size(x,2);
    176. o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;
    177. end
    178. % F12
    179. function o = F12(x)
    180. dim=size(x,2);
    181. o=(pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*...
    182. (1+10.*((sin(pi.*(1+(x(2:dim)+1)./4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4));
    183. end
    184. % F13
    185. function o = F13(x)
    186. dim=size(x,2);
    187. o=.1*((sin(3*pi*x(1)))^2+sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2))+...
    188. ((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2))+sum(Ufun(x,5,100,4));
    189. end
    190. % F14
    191. function o = F14(x)
    192. 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;,...
    193. -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];
    194. for j=1:25
    195. bS(j)=sum((x'-aS(:,j)).^6);
    196. end
    197. o=(1/500+sum(1./([1:25]+bS))).^(-1);
    198. end
    199. % F15
    200. function o = F15(x)
    201. aK=[.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];
    202. bK=[.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK;
    203. o=sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2);
    204. end
    205. % F16
    206. function o = F16(x)
    207. 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);
    208. end
    209. % F17
    210. function o = F17(x)
    211. 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;
    212. end
    213. % F18
    214. function o = F18(x)
    215. 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))*...
    216. (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)));
    217. end
    218. % F19
    219. function o = F19(x)
    220. aH=[3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2];
    221. pH=[.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 .5743 .8828];
    222. o=0;
    223. for i=1:4
    224. o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
    225. end
    226. end
    227. % F20
    228. function o = F20(x)
    229. 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];
    230. cH=[1 1.2 3 3.2];
    231. pH=[.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 .1004 .9991;...
    232. .2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 .8732 .5743 .1091 .0381];
    233. o=0;
    234. for i=1:4
    235. o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
    236. end
    237. end
    238. % F21
    239. function o = F21(x)
    240. 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];
    241. cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
    242. o=0;
    243. for i=1:5
    244. o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
    245. end
    246. end
    247. % F22
    248. function o = F22(x)
    249. 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];
    250. cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
    251. o=0;
    252. for i=1:7
    253. o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
    254. end
    255. end
    256. % F23
    257. function o = F23(x)
    258. 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];
    259. cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
    260. o=0;
    261. for i=1:10
    262. o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
    263. end
    264. end
    265. function o=Ufun(x,a,k,m)
    266. o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));
    267. end

    main.m

    1. %_________________________________________________________________________________
    2. %_________________________________________________________________________________
    3. clear all
    4. clc
    5. SearchAgents_no=30; % Number of search agents
    6. Function_name='F1'; % Name of the test function that can be from F1 to F23
    7. Max_iteration=500; % Maximum numbef of iterations
    8. % Load details of the selected benchmark function
    9. [lb,ub,dim,fobj]=Get_Functions_details(Function_name);
    10. [Best_score,Best_pos,cg_curve]=TGA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
    11. display(['The best solution obtained by OPTIMIZER is : ', num2str(Best_pos)]);
    12. display(['The best optimal value of the objective function found by OPTIMIZER is : ', num2str(Best_score)]);
    13. %Draw objective space
    14. figure,
    15. subplot(1,2,1);
    16. func_plot(Function_name);
    17. title([Function_name])
    18. xlabel('x_1');
    19. ylabel('x_2');
    20. zlabel([Function_name,'( x_1 , x_2 )'])
    21. set(gca,'color','none')
    22. grid off
    23. subplot(1,2,2);
    24. semilogy(cg_curve,'Color','b','LineWidth',4);
    25. title('Convergence curve')
    26. xlabel('Iteration');
    27. ylabel('Best fitness obtained so far');
    28. axis tight
    29. grid off
    30. box on
    31. legend('TGA')

  • 相关阅读:
    zynq pl访问ps ddr
    WPS Word自动编号转文本
    [答疑]泛化关系上的泛化集(Generalization Set)操作
    mysql主从复制搭建
    【听课笔记】复旦大学遗传学_03基因
    五步走,轻松拥有你的个性化小程序
    PEI是聚醚酰亚胺(Polyetherimide)在粘接使用时使用UV胶水的优势有哪些?要注意哪些事项?
    Java:修改Jar的源码,并上传Nexus私有仓库,替换jar版本
    MIKE水动力笔记12_数字化海图1之提取确值水深点
    按位运算符、逻辑运算符
  • 原文地址:https://blog.csdn.net/qq_36130719/article/details/133640724