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

📄 fit_fun2.m

📁 遗传算法的matlab程序集(内有很多问题的具体解决程序)
💻 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)
%
% 计算群体中个体的适应度
% 调用:   strrep6.m, strrem.m

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

% 修改群体适应向量计算
  pop=ad_just2(spop,new_size,y_front,colons);
  clear y_front colons

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

% 去掉 '{', '}', '<1', '<2', '<3' and '>'  
z=strrem(z);      

% 搜索 *  /  ^并代之以 .*  ./  .^
%  I = find(z == '*' | z == '/' | z == '^');
%  z = strrep4(z,I);
z=strep2(z);      
eval(z);
clear z

if ffunc == 'mse',

% 计算适应度函数误差均方值
   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',

% 计算适应度函数相关系数
   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,:)));

   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));

%对长串处罚

fit = fit.*(ones(new_size,1)./(1 + exp(0.08*(str_len - snip_length))));               
[m,I]=max(fit);

⌨️ 快捷键说明

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