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

📄 permut.m

📁 MATLAB遗传代码的源码 具体如何使用?希望能受到指点
💻 M
字号:
% function [xper]=permut(x,num_inp,fun_set,num_fun_arg_set,fun_name,...
%                           num_fun_arg,user_def_fun)
%
% 转换算子(inversion) 
function [xper]=permut(x,num_inp,fun_set,num_fun_arg_set,fun_name,...
                          num_fun_arg,user_def_fun)

% 在串中随机挑选位置检查是否是运算符,被选元素必须是有效的运算符

 [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;
 
 % 在用户自定义函数中屏蔽 (, ), 函数,算符,输入
 temp_x=x;
 
 fo_ind=findstr(x,'{');
 if any(fo_ind)==1,          
    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    
 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,'^');
 
 udf_ind=findstr(temp_x,'{');
 
 ind =  [add_ind sub_ind mult_ind div_ind pow_ind ...
         sqrt_ind sqr_ind exp_ind log_ind udf_ind];
 
 
flag=0;
[num_fun]=length(ind);

if num_fun==0,
   xper=x;
   xper=strrep(xper,'[&','[-');
   xper=strrep(xper,'e@','e+');
   xper=strrep(xper,'e#','e-');
   return
end
fun_choice=ceil(rand(1)*num_fun + eps);
i=ind(fun_choice);

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

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

   
   I=findstr(fun_set,x(i));
   num_fun_arg=num_fun_arg_set(I);

   if num_fun_arg==2,
      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
      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
      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
      
      k=length(xstart);              
      arg1=x(k+1:i-1);
      arg2=x(i+1:L-length(xend)); 
      sub_tree=['(' arg2 x(i) arg1 ')'];
      xper=[xstart sub_tree xend];

   else   
      xper=x;
      xper=strrep(xper,'[&','[-');
      xper=strrep(xper,'e@','e+');
      xper=strrep(xper,'e#','e-');
      return
   
   end

elseif x(i)=='{',
   bo_count=1;
   bc_count=0;

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

   xstart=x(1:s_point);
   sub_tree=x(s_point:e_point);
   if e_point == L,
      xend=[')']; 
   else
      xend=x(e_point:L);
   end
   if bo_count > 1,
      onest_ind=findstr(sub_tree,'{');
      cnest_ind=findstr(sub_tree,'}');
      nest_sub_tree=sub_tree(onest_ind(2):cnest_ind(length(cnest_ind)-1));
      osnest_ind=findstr(nest_sub_tree,'<') + onest_ind(1);
      csnest_ind=findstr(nest_sub_tree,'>') + onest_ind(1);
      
      temp_sub_tree=strrep(sub_tree,'<','$');
      temp_sub_tree=strrep(temp_sub_tree,'>','$');
      
   else
   
      temp_sub_tree=sub_tree;
   end
   osc_ind=findstr(temp_sub_tree,'<');
   csc_ind=findstr(temp_sub_tree,'>');
   type_ind=osc_ind+1;                
   num_fun_arg=length(osc_ind);
      if num_fun_arg > 1, 
      for j=1:num_fun_arg,      
   eval(['arg' num2str(j) '=sub_tree(type_ind(' num2str(j) ')+2:csc_ind(' num2str(j) ')-2);'])

      end
      arg_type=str2num(sub_tree(type_ind)');

      soc_choice=ceil(rand(1)*num_fun_arg + eps);
      soc_type=arg_type(soc_choice);
      arg_type(soc_choice)=0;
      
      if soc_type==1,         
         xper=x;
         xper=strrep(xper,'[&','[-');
         xper=strrep(xper,'e@','e+');
         xper=strrep(xper,'e#','e-');
         return 
      elseif soc_type==2,
         same_type=find(arg_type==2);
      else % if soc_type==3
         same_type=find(arg_type==3);
      end
      
      if same_type==[],
         xper=x;
         xper=strrep(xper,'[&','[-');
         xper=strrep(xper,'e@','e+');
         xper=strrep(xper,'e#','e-');
         return   
      end
      
      same_choice=same_type(ceil(rand(1)*length(same_type) + eps));
      eval(['temp_arg=arg' num2str(soc_choice) ';'])
      eval(['arg' num2str(soc_choice) '=arg' num2str(same_choice) ';'])
      eval(['arg' num2str(same_choice) '=temp_arg;'])
         xx=sub_tree;
         sub_tree=xx(2:osc_ind(1)+1);
         keyboard
         for j=1:num_fun_arg,       
            if j~=num_fun_arg,
               sub_tree_new=xx(csc_ind(j):type_ind(j+1));
               eval(['argument=arg' num2str(j)])
               sub_tree=[sub_tree '(' argument ')' sub_tree_new];
                     
            else
               sub_tree_end=xx(csc_ind(j):length(xx)-1);
               eval(['argument=arg' num2str(j)])
               sub_tree=[sub_tree '(' argument ')' sub_tree_end];
            end
         end
      
      xper=[xstart sub_tree xend];
      
   else
      xper=x;
      xper=strrep(xper,'[&','[-');
      xper=strrep(xper,'e@','e+');
      xper=strrep(xper,'e#','e-');
      return      
   end
   
end

xper=strrep(xper,'[&','[-');
xper=strrep(xper,'e@','e+');
xper=strrep(xper,'e#','e-');

⌨️ 快捷键说明

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