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

📄 mutato3.m

📁 王小平《遗传算法——理论、应用与软件实现》随书光盘
💻 M
📖 第 1 页 / 共 2 页
字号:
% function [xmut]=mutato3(x,u,y,num_inp,p_c2u,mut_fun_wgt,...
% mut_fun_set,mut_num_fun_arg_set,fun_name,num_fun_arg,user_def_fun,equ_par)
%
% Mutate a string
%

% Author             Date     Predecessor   Modification
% ======             ====     ===========   ============
% B.McKay M.Willis   17/5/95  mutato2.m     user def. func.
% J. Elsey           31/7/95  mutato3.m     increase the number of allowable inputs
% 
% Function Calls:   
% ===============    
%
% Last Modification: 31/7/95
% =================
%

 function [xmut]=mutato3(x,u,y,num_inp,p_c2u,mut_fun_wgt,...
  mut_fun_set,mut_num_fun_arg_set,fun_name,num_fun_arg,user_def_fun,equ_par)           

% Randomly pick a position along the string
% and figure out if it is an operator. The 
% chosen element will either be a valid 
% operator, a real constant (delimited by
% square brackets), or an input.

 [D,L]=size(x);

 x=strrep(x,'[-','[&');
 x=strrep(x,'e+','e@');
 x=strrep(x,'e-','e#'); 
 
 % Find all sockets
         
 osc1_ind=findstr(x,'<');
 csc1_ind=findstr(x,'>');
 type1_ind=osc1_ind+1;
 
 % Mask (, ), functions, operators, and inputs
 % inside UDF's (but not in sockets) 

 temp_x=x;
 
 fo_ind=findstr(x,'{');
 if any(fo_ind)==1,           % If any UDF's
    for j=1:length(fo_ind),
       for k=fo_ind(j):L,
          if x(k)=='+' | x(k)=='-' | x(k)=='/' | x(k)=='*' | x(k)=='^' | ...
             x(k)=='r' | x(k)=='s' | x(k)=='l' | x(k)=='p' | x(k)=='u' | ...
             x(k)=='[' | x(k)==']',    
             temp_x(k)='$';
          elseif x(k)=='<',
             break,
          end
       end
    end
    
    mc_ind=findstr(x,'>');
    for j=1:length(mc_ind),
       for k=mc_ind(j):L,
          if x(k)=='+' | x(k)=='-' | x(k)=='/' | x(k)=='*' | x(k)=='^' | ...
             x(k)=='r' | x(k)=='s' | x(k)=='l' | x(k)=='p' | x(k)=='u' | ...
             x(k)=='[' | x(k)==']',        
             temp_x(k)='$';       
          elseif x(k)=='}' | x(k)=='<',
             break,
          end
       end
    end    
 
    % Mask [, ] in type I sockets
    % Mask u's in type II sockets
    
    for j=1:length(osc1_ind),
       for k=osc1_ind(j):csc1_ind(j),
          if x(type1_ind(j))=='1',
             if x(k)==']' | x(k)=='[',
                temp_x(k)='$';
             end
          elseif x(type1_ind(j))=='2',
             if x(k)=='u',
                temp_x(k)='$';
             end
          elseif x(type1_ind(j))=='3',
             break
          elseif x(k)=='>',
             break,
          end
       end
    end 
 end


 sqrt_ind=findstr(temp_x,'r'); sqr_ind=findstr(temp_x,'s');
 exp_ind=findstr(temp_x,'p');  log_ind=findstr(temp_x,'l');
 
 add_ind=findstr(temp_x,'+');  sub_ind=findstr(temp_x,'-');
 mult_ind=findstr(temp_x,'*'); div_ind=findstr(temp_x,'/');
 pow_ind=findstr(temp_x,'^');
 
 
 obr_ind=findstr(temp_x,'[');
 cbr_ind=findstr(temp_x,']');

 inp_ind=findstr(temp_x,'u');
 udf_ind=findstr(temp_x,'{');
 
 ind =  [add_ind sub_ind mult_ind div_ind pow_ind ...
         sqrt_ind sqr_ind exp_ind log_ind inp_ind udf_ind obr_ind];
 
 
flag=0;
[num_fun]=length(ind);

if num_fun==0,
   xmut=x;
   xmut=[xstart xnew xend] ;
   xmut=strrep(xmut,'[&','[-');
   xmut=strrep(xmut,'e@','e+');
   xmut=strrep(xmut,'e#','e-');
   return
end



% Select one of the functionals, inputs or constants at random

fun_choice=ceil(rand(1)*num_fun + eps);
i=ind(fun_choice);


% Pick another functional, input or constant

new_fun_choice=ceil(rand(1)*length(mut_fun_set+eps));
xnew=mut_fun_set(new_fun_choice);  


new_num_fun_arg=mut_num_fun_arg_set(new_fun_choice);

%
% Check if functional is from base set, UDF
% or is an input or constant
%
[HGH1 HGH2]=size(fun_name);

base_fun_flag=any(x(i)==fun_name(1:HGH2));

if base_fun_flag==1,  % Functional is in base set


   % Check number of arguments of functionals
   
   I=findstr(mut_fun_set,x(i));
   old_num_fun_arg=mut_num_fun_arg_set(I);


   % Compare degree of new and old functionals

   if new_num_fun_arg==old_num_fun_arg & any(xnew==fun_name(1:HGH2)), 

      x(i)=xnew;   
      xmut=x;

   else
      %
      % Extract sub tree 
      %
      
      % Search left

      bo_count=0;
      bc_count=0;

      for j=i:-1:1,
         if x(j) == ')',
            bc_count=bc_count+1;
         elseif x(j) == '(',
            bo_count=bo_count+1;
            if bo_count > bc_count;
               s_point = j;
               break;
            end
         end  
      end

      % Search right

      bo_count=0;
      bc_count=0;

      for j=i+1:L,
         if x(j) == ')',
            bc_count=bc_count+1;
            if bc_count > bo_count;
               e_point = j;
               break;
            end
         elseif x(j) == '(',
            bo_count=bo_count+1;   
         end  
      end

      % take out the relevant string
      
      xstart=x(1:s_point);
      sub_tree=x(s_point+1:e_point-1);
      if e_point == L,
         xend=[')']; 
      else
         xend=x(e_point:L);
      end
 
                      
      %
      % Identify and extract arguments
      %
      
      k=length(xstart);      
      if old_num_fun_arg==1,
         arg1=x(k+2:L-length(xend));
      else,                            % If old_num_fun_arg==2
         arg1=x(k+1:i-1);
         arg2=x(i+1:L-length(xend)); 
      end

      diff_num_fun_arg=abs(new_num_fun_arg-old_num_fun_arg);
            
      %
      % If new funtion is from the base set
      %
      
      if any(xnew==fun_name(1:HGH2)),
      
         diff_num_fun_arg=abs(new_num_fun_arg-old_num_fun_arg);
                  
         if new_num_fun_arg > old_num_fun_arg,
         
            % Generate one extra argument
            arg2=equ_gen3(1,num_inp,equ_par,mut_fun_set,...
                                   mut_num_fun_arg_set,user_def_fun); 
            % Construct sub tree
            sub_tree=['(' arg1 xnew arg2 ')']; 
 
         else

            % Randomly cut out one argument
            r_num=rand(1);
            if  r_num < 0.5,
               arg1=arg1;   
            else
               arg1=arg2;
            end
                 
            % Construct sub tree
            sub_tree=['(' xnew '(' arg1 '))'];

         end

      else   % If new funtion is a UDF
         
         % Look-up UDF
         temp_func=deblank(user_def_fun(str2num(xnew),:));
         
         % Find all sockets
         
         osc_ind=findstr(temp_func,'<');
         csc_ind=findstr(temp_func,'>');
         type_ind=osc_ind+1;
                  
         if new_num_fun_arg==0,
            sub_tree=temp_func;
         else

            % Create vector of socket types
            soc_type=str2num(temp_func(type_ind)');
       
            % Determine old argument types
            
            % First argument
            
            arg_flag=0;
            for j=1:HGH2,
               if any(fun_name(j)==arg1 | '{'==arg1 | 'u'==arg1)==0;            
                  arg_type(1)=1;
                  arg_flag=1;
                  break
               end
            end
            if arg_flag==0,
               for j=1:HGH2,
                  if any(fun_name(j)==arg1 | '{'==arg1 | '['==arg1)==0;            
                     arg_type(1)=2;
                     arg_flag=1;
                     break
                  end
               end
            end 
            if arg_flag==0,  
               arg_type(1)=3;               
            end

            if old_num_fun_arg==2,  % Second argument
               arg_flag=0;
               for j=1:HGH2,
                  if any(fun_name(j)==arg2 | '{'==arg2 | 'u'==arg2)==0;            
                     arg_type(2)=1;
                     arg_flag=1;
                     break
                  end
               end
               if arg_flag==0,

⌨️ 快捷键说明

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