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

📄 fit_fun2.m

📁 王小平《遗传算法——理论、应用与软件实现》随书光盘
💻 M
字号:
% function [fit,spop,new_size,str_len]=fit_fun2(ffunc,spop,pop_size,u,y,dt,num_data,num_inp,stdy,y_front,colons,snip_length)
%
% Function for evaluating the individual fitness of a population
% For use with tree_ga.m
%

% Author             Date     Predecessor   Modification
% ======             ====     ===========   ============
% B.McKay M.Willis   24/5/95  fit_func.m    user def. func.
% B.McKay             8/8/95  fit_fun2.m    returns string length
% 
% Function Calls:   strrep6.m, strrem.m
% ===============    
%
% Last Modification: 8/8/95
% =================
%

function [fit,spop,new_size,str_len]=fit_fun2(ffunc,spop,pop_size,u,y,num_data,num_inp,stdy,y_front,colons,snip_length) 

for i=1:num_inp,
         eval(['u' num2str(i) '= u(:,' num2str(i) ');']);
end

new_size = pop_size;
i=1;
while i<=new_size,
    ind_u=findstr(spop(i,:),'u');
    if isempty(ind_u)==1,
       if i==1,
          spop=spop(2:new_size,:);
          new_size=new_size-1;
       elseif i==new_size,
          spop=spop(1:new_size-1,:);
          new_size=new_size-1;
       else,
          spop=[spop(1:i-1,:); spop(i+1:new_size,:)];
          new_size=new_size-1;
       end       

    else
       i=i+1;
    end
end


 
%
% Modify population for matrix vector calculation
%


  pop=ad_just2(spop,new_size,y_front,colons);
clear y_front colons

x=pop';
clear pop
x=x(:);
z=x';
clear x

%
% Remove '{', '}', '<1', '<2', '<3' and '>'  
%

z=strrem(z);      

%
% Search for * and / and ^, replace with .* , ./ and .^
%
 
%  I = find(z == '*' | z == '/' | z == '^');
%  z = strrep4(z,I);
z=strep2(z);      
   
eval(z);
clear z
 
 
if ffunc == 'mse',
%
% Calculate Mean Square Error fitness function
%

   fit = sum(((ones(new_size,1)*y' - y_est').^2)')/num_data;
   fit = (ones(new_size,1)*stdy)./(fit' + ones(new_size,1)*stdy);


elseif ffunc == 'ccf',
%
% Calculate correlation coefficient fitness function
% 
   C =  corrcoef([y_est y]);
   fit = abs(C(new_size+1,1:new_size)'); 
   clear C
end
  
  
str_len=zeros(new_size,1);

for i=1:new_size,

   str_len(i,1)=length(deblank(spop(i,:)));
%
% Penalise equations that produce complex numbers, +/- inf or NaN
%
   A(i) = 1 - finite(fit(i));
 
   if isreal(y_est(:,i)) ~= 1  
      fit(i) = 0;
   end
end

I = find(A>0);
fit(I) = zeros(1,length(I));

%
% Penalise long strings
%


fit = fit.*(ones(new_size,1)./(1 + exp(0.08*(str_len - snip_length))));               


[m,I]=max(fit);

% End

⌨️ 快捷键说明

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