n_simp2.m

来自「王小平《遗传算法——理论、应用与软件实现》随书光盘」· M 代码 · 共 76 行

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

% Author             Date     Predecessor     Modification
% ======             ====     ===========     ============
% B.McKay            15/9/95  disp_res.m      extracted from
% B.McKay            5/10/95  n_simp          accepts a population matrix
% B.McKay            5/10/95      -           inserts brackets for '-*' case
%
% Function Calls: 
% ==============  
%    
% Last Modification: 5/10/95
% =================
% 

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

%
% End
%

⌨️ 快捷键说明

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