声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4706|回复: 3

[工具箱] 求遗传算法的工具箱的initializega函数

[复制链接]
发表于 2007-3-10 10:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
哪位仁兄有遗传算法的工具箱的initializega函数,能否共享一下?谢谢!!

[ 本帖最后由 eight 于 2007-9-1 16:58 编辑 ]
回复
分享到:

使用道具 举报

发表于 2007-3-12 01:05 | 显示全部楼层
  1. function [pop] = initializega(num, bounds, evalFN,evalOps,options)
  2. % function [pop]=initializega(populationSize, variableBounds,evalFN,
  3. %                           evalOps,options)
  4. %    initializega creates a matrix of random numbers with
  5. %    a number of rows equal to the populationSize and a number
  6. %    columns equal to the number of rows in bounds plus 1 for
  7. %    the f(x) value which is found by applying the evalFN.
  8. %    This is used by the ga to create the population if it
  9. %    is not supplied.
  10. %
  11. % pop            - the initial, evaluated, random population
  12. % populatoinSize - the size of the population, i.e. the number to create
  13. % variableBounds - a matrix which contains the bounds of each variable, i.e.
  14. %                  [var1_high var1_low; var2_high var2_low; ....]
  15. % evalFN         - the evaluation fn, usually the name of the .m file for
  16. %                  evaluation
  17. % evalOps        - any options to be passed to the eval function defaults []
  18. % options        - options to the initialize function, ie.
  19. %                  [type prec] where eps is the epsilon value
  20. %                  and the second option is 1 for float and 0 for binary,
  21. %                  prec is the precision of the variables defaults [1e-6 1]

  22. % Binary and Real-Valued Simulation Evolution for Matlab GAOT V2
  23. % Copyright (C) 1998 C.R. Houck, J.A. Joines, M.G. Kay
  24. %
  25. % C.R. Houck, J.Joines, and M.Kay. A genetic algorithm for function
  26. % optimization: A Matlab implementation. ACM Transactions on Mathmatical
  27. % Software, Submitted 1996.
  28. %
  29. % This program is free software; you can redistribute it and/or modify
  30. % it under the terms of the GNU General Public License as published by
  31. % the Free Software Foundation; either version 1, or (at your option)
  32. % any later version.
  33. %
  34. % This program is distributed in the hope that it will be useful,
  35. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37. % GNU General Public License for more details. A copy of the GNU
  38. % General Public License can be obtained from the
  39. % Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  40. if nargin<5
  41.   options=[1e-6 1];
  42. end
  43. if nargin<4
  44.   evalOps=[];
  45. end

  46. if any(evalFN<48) %Not a .m file
  47.   if options(2)==1 %Float GA
  48.     estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';'];  
  49.   else %Binary GA
  50.     estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';'];
  51.   end
  52. else %A .m file
  53.   if options(2)==1 %Float GA
  54.     estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);'];
  55.   else %Binary GA
  56.     estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ...
  57.         '(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];'];  
  58.     end
  59. end


  60. numVars     = size(bounds,1);                 %Number of variables
  61. rng         = (bounds(:,2)-bounds(:,1))'; %The variable ranges'

  62. if options(2)==1 %Float GA
  63.   xZomeLength = numVars+1;                 %Length of string is numVar + fit
  64.   pop         = zeros(num,xZomeLength);         %Allocate the new population
  65.   pop(:,1:numVars)=(ones(num,1)*rng).*(rand(num,numVars))+...
  66.     (ones(num,1)*bounds(:,1)');
  67. else %Binary GA
  68.   bits=calcbits(bounds,options(1));
  69.   xZomeLength = sum(bits)+1;                 %Length of string is numVar + fit
  70.   pop = round(rand(num,sum(bits)+1));
  71. end

  72. for i=1:num
  73.   eval(estr);
  74. end
复制代码

评分

1

查看全部评分

发表于 2007-8-31 09:09 | 显示全部楼层

谢谢happy教授!!巨大帮助!

发表于 2009-6-3 10:07 | 显示全部楼层
非常感谢!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-17 02:58 , Processed in 0.057078 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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