设为首页收藏本站
开启辅助访问
切换到窄版

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2220|回复: 0

遗传算法函数Matlab程序

[复制链接]
发表于 2011-10-6 00:16:57 | 显示全部楼层 |阅读模式

  1. function [x,endPop,bPop,traceInfo] = ga(bounds,eevalFN,eevalOps,startPop,opts,...
  2. termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)
  3. n=nargin;
  4. if n<2 | n==6 | n==10 | n==12
  5. disp('Insufficient arguements')
  6. end
  7. if n<3 %Default eevalation opts.
  8. eevalOps=[];
  9. end
  10. if n<5
  11. opts = [1e-6 1 0];
  12. end
  13. if isempty(opts)
  14. opts = [1e-6 1 0];
  15. end
  16. if any(eevalFN<48) %Not using a .m file
  17. if opts(2)==1 %Float ga
  18. e1str=['x=c1; c1(xZomeLength)=', eevalFN ';'];
  19. e2str=['x=c2; c2(xZomeLength)=', eevalFN ';'];
  20. else %Binary ga
  21. e1str=['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=',...
  22. eevalFN ';'];
  23. end
  24. else %Are using a .m file
  25. if opts(2)==1 %Float ga
  26. e1str=['[c1 c1(xZomeLength)]=' eevalFN '(c1,[gen eevalOps]);'];
  27. e2str=['[c2 c2(xZomeLength)]=' eevalFN '(c2,[gen eevalOps]);'];
  28. else %Binary ga
  29. e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' eevalFN ...
  30. '(x,[gen eevalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];
  31. end
  32. end

  33. if n<6 %Default termination information
  34. termOps=[100];
  35. termFN='maxGenTerm';
  36. end
  37. if n<12 %Default muatation information
  38. if opts(2)==1 %Float GA
  39. mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'];
  40. mutOps=[4 0 0;6 termOps(1) 3;4 termOps(1) 3;4 0 0];
  41. else %Binary GA
  42. mutFNs=['binaryMutation'];
  43. mutOps=[0.05];
  44. end
  45. end
  46. if n<10 %默认的交叉信息
  47. if opts(2)==1 %浮点编码
  48. xOverFNs=['arithXover heuristicXover simpleXover'];
  49. xOverOps=[2 0;2 3;2 0];
  50. else %Binary GA
  51. xOverFNs=['simpleXover'];
  52. xOverOps=[0.6];
  53. end
  54. end
  55. if n<9 %Default select opts only i.e. roullete wheel.
  56. selectOps=[];
  57. end
  58. if n<8 %Default select info
  59. selectFN=['normGeomSelect'];
  60. selectOps=[0.08];
  61. end
  62. if n<6 %默认的算法终止准则
  63. termOps=[100];
  64. termFN='maxGenTerm';
  65. end
  66. if n<4 %初始种群为空
  67. startPop=[];
  68. end
  69. if isempty(startPop) %随机生成初始种群
  70. startPop=initializega(80,bounds,eevalFN,eevalOps,opts(1:2));
  71. end
  72. if opts(2)==0 %二进制编码
  73. bits=calcbits(bounds,opts(1));
  74. end
  75. xOverFNs=parse(xOverFNs);
  76. mutFNs=parse(mutFNs);
  77. xZomeLength = size(startPop,2); %Length of the xzome=numVars+fittness
  78. numVar = xZomeLength-1; %变量数目
  79. popSize = size(startPop,1); %种群中个体数目
  80. endPop = zeros(popSize,xZomeLength); %次种群矩阵
  81. c1 = zeros(1,xZomeLength); %个体
  82. c2 = zeros(1,xZomeLength); %个体
  83. numXOvers = size(xOverFNs,1); %交叉操作次数
  84. numMuts = size(mutFNs,1); %变异操作次数
  85. epsilon = opts(1); %适应度门限值
  86. oeval = max(startPop(:,xZomeLength)); %初始种群中的最优值
  87. bFoundIn = 1;
  88. done = 0;
  89. gen = 1;
  90. collectTrace = (nargout>3);
  91. floatGA = opts(2)==1;
  92. display = opts(3);
  93. while(~done)
  94. [beval,bindx] = max(startPop(:,xZomeLength)); %当前种群的最优值
  95. best = startPop(bindx,:);
  96. if collectTrace
  97. traceInfo(gen,1)=gen; %当前代
  98. traceInfo(gen,2)=startPop(bindx,xZomeLength); %最优适应度
  99. traceInfo(gen,3)=mean(startPop(:,xZomeLength)); %平均适应度
  100. traceInfo(gen,4)=std(startPop(:,xZomeLength));
  101. end
  102. if ( (abs(beval - oeval)>epsilon) | (gen==1))
  103. if display
  104. fprintf(1,'\n%d %f\n',gen,beval);
  105. end
  106. if floatGA
  107. bPop(bFoundIn,:)=[gen startPop(bindx,:)];
  108. else
  109. bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits)...
  110. startPop(bindx,xZomeLength)];
  111. end
  112. bFoundIn=bFoundIn+1;
  113. oeval=beval;
  114. else
  115. if display
  116. fprintf(1,'%d ',gen);
  117. end
  118. end
  119. endPop = feeval(selectFN,startPop,[gen selectOps]); %选择操作
  120. if floatGA
  121. for i=1:numXOvers,
  122. for j=1:xOverOps(i,1),
  123. a = round(rand*(popSize-1)+1); %一个父代个体
  124. b = round(rand*(popSize-1)+1); %另一个父代个体
  125. xN=deblank(xOverFNs(i,:)); %交叉函数
  126. [c1 c2] = feeval(xN,endPop(a,:),endPop(b,:),bounds,[gen… xOverOps(i,:)]);
  127. if c1(1:numVar)==endPop(a,(1:numVar))
  128. c1(xZomeLength)=endPop(a,xZomeLength);
  129. elseif c1(1:numVar)==endPop(b,(1:numVar))
  130. c1(xZomeLength)=endPop(b,xZomeLength);
  131. else
  132. eeval(e1str);
  133. end
  134. if c2(1:numVar)==endPop(a,(1:numVar))
  135. c2(xZomeLength)=endPop(a,xZomeLength);
  136. elseif c2(1:numVar)==endPop(b,(1:numVar))
  137. c2(xZomeLength)=endPop(b,xZomeLength);
  138. else
  139. eeval(e2str);
  140. end
  141. endPop(a,:)=c1;
  142. endPop(b,:)=c2;
  143. end
  144. end
  145. for i=1:numMuts,
  146. for j=1:mutOps(i,1),
  147. a = round(rand*(popSize-1)+1);
  148. c1 = feeval(deblank(mutFNs(i,:)),endPop(a,:),bounds,[gen mutOps(i,:)]);
  149. if c1(1:numVar)==endPop(a,(1:numVar))
  150. c1(xZomeLength)=endPop(a,xZomeLength);
  151. else
  152. eeval(e1str);
  153. end
  154. endPop(a,:)=c1;
  155. end
  156. end
  157. else %遗传操作的统计模型
  158. for i=1:numXOvers,
  159. xN=deblank(xOverFNs(i,:));
  160. cp=find(rand(popSize,1) if rem(size(cp,1),2) cp=cp(1:(size(cp,1)-1)); end
  161. cp=reshape(cp,size(cp,1)/2,2);
  162. for j=1:size(cp,1)
  163. a=cp(j,1); b=cp(j,2);
  164. [endPop(a,:) endPop(b,:)] = feeval(xN,endPop(a,:),endPop(b,:), bounds,[gen xOverOps(i,:)]);
  165. end
  166. end
  167. for i=1:numMuts
  168. mN=deblank(mutFNs(i,:));
  169. for j=1:popSize
  170. endPop(j,:) = feeval(mN,endPop(j,:),bounds,[gen mutOps(i,:)]);
  171. eeval(e1str);
  172. end
  173. end
  174. end
  175. gen=gen+1;
  176. done=feeval(termFN,[gen termOps],bPop,endPop); %
  177. startPop=endPop; %更新种群
  178. [beval,bindx] = min(startPop(:,xZomeLength));
  179. startPop(bindx,:) = best;
  180. end
  181. [beval,bindx] = max(startPop(:,xZomeLength));
  182. if display
  183. fprintf(1,'\n%d %f\n',gen,beval);
  184. end
  185. x=startPop(bindx,:);
  186. if opts(2)==0 %binary
  187. x=b2f(x,bounds,bits);
  188. bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits), startPop(bindx,xZomeLength)];
  189. else
  190. bPop(bFoundIn,:)=[gen startPop(bindx,:)];
  191. end
  192. if collectTrace
  193. traceInfo(gen,1)=gen;
  194. traceInfo(gen,2)=startPop(bindx,xZomeLength); %Best fittness
  195. traceInfo(gen,3)=mean(startPop(:,xZomeLength)); %Avg fittness
  196. end
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|生物信息学论坛 ( 蜀ICP备09031721号  

GMT+8, 2017-3-28 18:09 , Processed in 0.096686 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表