⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga.m

📁 goat工具箱 遗传算法主函数祥解 goat工具箱 是网上流传最广的一个遗传工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
function [x,endPop,bPop,traceInfo] = ga(bounds,evalFN,evalOps,startPop,opts,...
termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)
% goat工具箱是网上流传最广的一个遗传工具箱,以下针对其主函数的注释。 

%GA run a genetic algorithm
% function [x,endPop,bPop,traceInfo]=ga(bounds,evalFN,evalOps,startPop,opts,
%                                        termFN,termOps,selectFN,selectOps,
%                                        xOverFNs,xOverOps,mutFNs,mutOps)
%                                
% Output Arguments:
%    x             - the best solution found during the course of the run 求得的最优解
%    endPop        - the final population 最终得到的种群
%    bPop          - a trace of the best population 最优种群的一个搜索轨迹
%    traceInfo     - a matrix of best and means of the ga for each generation 每一代种群的最优个体和均值
%
% Input Arguments:
%    bounds        - a matrix of upper and lower bounds on the variables 代表变量上下界的矩阵
%    evalFN        - the name of the evaluation .m function 适应度函数所在的m文件
%    evalOps       - options to pass to the evaluation function ([NULL])
%    传递给适应度函数的参数,默认为空
%    startPop      - a matrix of solutions that can be initialized 初始种群
%                   from initialize.m 
%    opts          - [epsilon prob_ops display] change required to consider two 
%                   solutions different, prob_ops 0 if you want to apply the
%                   genetic operators probabilisticly to each solution, 1 if
%                   you are supplying a deterministic number of operator
%                   applications and display is 1 to output progress 0 for
%                   quiet. ([1e-6 1 0])
%   options--选择编码形式(浮点编码或是二进制编码)[epsilon prob_ops display],如
%prob_ops-变量进行二进制编码时指定的精度 
%prob_ops--为1时选择浮点编码,否则为二进制编码,由precision指定精度) 
%display0或1控制是否输出结果;默认为[1e-6 1 0]

%    termFN        - name of the .m termination function (['maxGenTerm'])
%    终止函数所在的m文件,maxGenTerm为求最大值时的终止函数
%    termOps       - options string to be passed to the termination function 传递个终止函数的参数,一般为需要遗传的代数
%                   ([100]).maxGenTerm将在这里面取得最大值
%    selectFN      - name of the .m selection function (['normGeomSelect']) 选择函数的名称
%    selectOpts    - options string to be passed to select after 传递给选择函数的参数,一般为变异概率
%                   select(pop,#,opts) ([0.08])
%    xOverFNS      - a string containing blank seperated names of Xover.m 交叉函数名称表,以空格分开
%                   files (['arithXover maxGenTermXover simpleXover']) 
%    xOverOps      - A matrix of options to pass to Xover.m files with the 传递给交叉函数的参数表
%                   first column being the number of that xOver to perform
%                   similiarly for mutation ([2 0;2 3;2 0])
%    mutFNs        - a string containing blank seperated names of mutation.m 变异函数表%                   files (['boundaryMutation multiNonUnifMutation ...
%                            nonUnifMutation unifMutation'])
%    mutOps        - A matrix of options to pass to Xover.m files with the 传递给变异函数的参数表
%                   first column being the number of that xOver to perform
%                   similiarly for mutation ([4 0 0;6 100 3;4 100 3;4 0 0])




%怎样建立适应度函数?
% 例1: function [x, val] = gaDemo1Eval(sol,options)
% x=sol(1);
% val = x + 10*sin(5*x)+7*cos(4*x);
%             Sol 参量X(种群个体)。
 %                             
%选择 options是   [当前代值evalOps ]

%函数必须反回两个值, val 和串sol 。这做以便 您的评估的修理或改进。
%例二:f(x1,x2)=-20*exp(-0.2*sqrt(0.5*(x1.^2+x2.^2)))-exp(0.5*(cos(2*pi*x1)+cos(2*pi*x2)))+22.71282的最小值
%先建原函数文件 
 %  function [eval]=f(sol)
  %   numv=size(sol,2);
   %  x=sol(1:numv);
    % eval=-20*exp(-0.2*sqrt(sum(x.^2)/numv)))-exp(sum(cos(2*pi*x))/numv)+22.71282;
%再建  适应度函数的matlab代码文件
%  function [sol,eval]=fitness(sol,options)
%       numv=size(sol,2)-1;
%       x=sol(1:numv);
%        eval=f(x);
%         eval=-eval;

%遗传算法的matlab代码
%bounds=ones(2,1)*[-5 5];
%[p,endPop,bestSols,trace]=ga(bounds,'fitness')

%画图查看
%  fplot('f',[0 9]);

%plot (endPop(:,1),endPop(:,2),'ro')
%

%如果没有给定初始种群,则随机生成
   %startPop=zeros(80,size(bounds,1)+1);This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
% GNU General Public License for more details. A copy of the GNU 
% General Public License can be obtained from the 
% Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

%%$Log: ga.m,v $
%Revision 1.10   1996/02/02   15:03:00   jjoine
% Fixed the ordering of imput arguments in the comments to match
% the actual order in the ga function.
%
%Revision 1.9   1995/08/28   20:01:07   chouck
% Updated initialization parameters, updated mutation parameters to reflect
% b being the third option to the nonuniform mutations
%
%Revision 1.8   1995/08/10   12:59:49   jjoine
%Started Logfile to keep track of revisions
%


n=nargin;%nargin得到函数的参数个数
if n<2 | n==6 | n==10 | n==12
   disp('Insufficient arguements') 
end
if n<3 %Default evalation opts.
   evalOps=[];%适应度函数的参数为空   

%     其它默认值        为迭代100次   传递给选择函数的参数,一般为变异概率0.05

%             交叉算子(['arithXover heuristicXover simpleXover'])参数为([2 0;2 3;2 0])
%            变异算子(['boundaryMutation multiNonUnifMutation    nonUnifMutation unifMutation']])
%              参数为[4 0 0;6 100 3;4 100 3;4 0 0]
end
if n<5
   opts = [1e-6 1 0];%进行二进制编码,并指定精度
end
if isempty(opts)
   opts = [1e-6 1 0];%默认情况下为二进制编码
end

if any(evalFN<48) %Not using a .m file
   if opts(2)==1 %Float ga,opts函数的第二个参数为1,则使用浮点数编码
     e1str=['x=c1; c1(xZomeLength)=', evalFN ';'];  
     e2str=['x=c2; c2(xZomeLength)=', evalFN ';'];  
   else %Binary ga,opts函数的第二个参数为0,则使用二进制编码
     e1str=['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=',...
evalFN ';'];
   end
else %Are using a .m file
   if opts(2)==1 %Float ga,opts函数的第二个参数为1,则使用浮点数编码   c1:个体 ;  c1(xZomeLength):个体适应度
     e1str=['[c1 c1(xZomeLength)]=' evalFN '(c1,[gen evalOps]);'];  
     e2str=['[c2 c2(xZomeLength)]=' evalFN '(c2,[gen evalOps]);'];  
   else %Binary ga,opts函数的第二个参数为0,则使用二进制编码
     e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' evalFN ...
'(x,[gen evalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];  
   end
end


if n<6 %Default termination information,                 默认终止函数信息
   termOps=[100];%默认遗传100代
   termFN='maxGenTerm';
end
if n<12 %Default muatation information,默认变异信息
   if opts(2)==1 %Float GA,浮点数编码
   mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'];
     mutOps=[4 0 0;6 termOps(1) 3;4 termOps(1) 3;4 0 0];
   else %Binary GA,二进制编码
     mutFNs=['binaryMutation'];%采用二进制变异
     mutOps=[0.05];%默认变异率为0.05
   end
end
if n<10 %Default crossover information,默认交叉信息
   if opts(2)==1 %Float GA,浮点数编码
     xOverFNs=['arithXover heuristicXover simpleXover'];
     xOverOps=[2 0;2 3;2 0];
   else %Binary GA,              二进制编码
     xOverFNs=['simpleXover'];
     xOverOps=[0.6];
   end
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -