n_simp2.m

来自「用VC++编写的遗传算法源程序」· M 代码 · 共 58 行

M
58
字号
% function y=n_simp2(x)
%
% Simplifies strings from tree_ga2 etc.
%
% Input
%       x - population matrix
% Output
%       y - simplified population martrix

function y_pop=n_simp2(x_pop)
 
[Q,R]=size(x_pop);

for i=1:Q, 
   x=x_pop(i,:);
%   x=simple(x);
   x=strrep(x,'[','');
   x=strrep(x,']','');
   x=strrep(x,'--','+');
   x=strrep(x,'-+','-');
   x=strrep(x,'+-','-');
   x=strrep(x,'++','+');
   x=strrep(x,'*-','*<-');

   % Find all <'s, index them and replace them with ('s
   I=findstr(x,'<');
   new_x=x;
   while I~=[],
      x=strrep(x,'*<-','*(-');
      new_x=x;
      R=length(x);
      % Find correct position for matching )
      for j=1:length(I),
         for k=I(j)+2:R,
            if any(x(k)==['+-/*^'])==1 & x(k-1)~=['e'],
                new_x=[new_x(1:k+j-1-1) ')' new_x(k+j-1-1+1:R+j-1)];
                break
            end
         end
      end
 %     x=simple(x);
      x=strrep(x,'--','+');
      x=strrep(x,'-+','-');
      x=strrep(x,'+-','-');
      x=strrep(x,'++','+');
      x=strrep(x,'*-','*<-');
      I=findstr(x,'<');
   end
   x=strrep(new_x,'p','exp');
   x=strrep(x,'l','log');
   x=strrep(x,'r','sqrt');
   if i==1,
      y_pop=x;
   else
      y_pop=str2mat(y_pop,x);
   end
end

⌨️ 快捷键说明

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