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

📄 equ_gen3.m

📁 用MATLAB实现遗传算法中的好多问题
💻 M
字号:
% function x2=equ_gen3(s_length,num_inp,equ_par,fun_set,num_fun_arg_set,user_def_fun)
% Automatically generates a string for tree_ga
function x2=equ_gen3(s_length,num_inp,equ_par,fun_set,num_fun_arg_set,user_def_fun)

% p_sub_tree: probability of a sub tree
% p_const: probability of a constant
% s_length: Number of sub trees concatenated in equ-gen

p_sub_tree=equ_par(1);  %[0.5];
p_const=equ_par(2);     %[0.1];


%
% Generate sub-trees
%

for i=1:s_length,
   fun_choice=ceil(rand(1)*length(fun_set)+eps);
   fun_name=fun_set(fun_choice);
   num_fun_arg=num_fun_arg_set(fun_choice);
   
   f_ascci=real(fun_name);
   if  f_ascci<= 57 & f_ascci>= 48, % Then its a user defined function
      temp_func=deblank(user_def_fun(str2num(fun_name),:));      
      
      if num_fun_arg==0,
         sub_tree=temp_func;
      else
         obr_ind=findstr(temp_func,'<');
         cbr_ind=findstr(temp_func,'>');
         type_ind=obr_ind+1;
       
         sub_tree=temp_func(1:type_ind(1));
         for j=1:num_fun_arg,
            soc_type=str2num(temp_func(type_ind(j)));
            if soc_type==1,             % Constant Only Socket
               const_val=1;
               eval(['user_arg=''[' num2str(const_val) ']'';'])
            elseif soc_type==2,      % Input Only Socket
               inp_choice=ceil(rand(1)*(num_inp)+eps);
               eval(['user_arg=''u' num2str(inp_choice) ''';'])
            else                        % Sub Tree Socket
               if i==s_length
                  r_num=rand(1)*(1-p_sub_tree)+p_sub_tree;
                  if r_num < (1 - p_const),
                     inp_choice=ceil(rand(1)*(num_inp)+eps);
                     eval(['user_arg' '=''u' num2str(inp_choice) ''';'])
                  else
                     const_val=1;
                     eval(['user_arg' '=''[' num2str(const_val) ']'';'])
                  end                  
               else
                
                  sub_choice=ceil( rand(1)*(s_length-(i+1)) )+(i+1);
                  eval(['user_arg=''A' num2str(sub_choice) ''';'])
               end
            end
         
            % Construct the sub tree
            if j~=num_fun_arg,
               sub_tree_new=temp_func(cbr_ind(j):type_ind(j+1));
               sub_tree=[sub_tree '(' user_arg ')' sub_tree_new];
            else
               sub_tree_end=temp_func(cbr_ind(j):length(temp_func));
               sub_tree=[sub_tree '(' user_arg ')' sub_tree_end];
            end
            clear user_arg
         end
         
      end
   else                             % Its in the basic function set  
      if num_fun_arg==1,
         if i==s_length
            r_num=rand(1)*(1-p_sub_tree)+p_sub_tree;
         else
            r_num=rand(1);
         end
         
         if r_num < p_sub_tree,
            sub_choice=ceil( rand(1)*(s_length-(i+1)) )+(i+1);
            eval(['arg' '=''A' num2str(sub_choice) ''';'])
         elseif r_num < (1 - p_const),
            inp_choice=ceil(rand(1)*(num_inp)+eps);
            eval(['arg' '=''u' num2str(inp_choice) ''';'])
         else
            const_val=1;
            eval(['arg' '=''[' num2str(const_val) ']'';'])
         end
       
         sub_tree=[ fun_name '(' arg ')'];    
      
      elseif num_fun_arg==2,
         for j=1:2,
            if i==s_length
               r_num=rand(1)*(1-p_sub_tree)+p_sub_tree;
            else
               r_num=rand(1);
            end
         
            if r_num < p_sub_tree,
              sub_choice=ceil( rand(1)*(s_length-(i+1)) )+(i+1);
               eval(['arg' num2str(j) '=''A' num2str(sub_choice) ''';'])
            elseif r_num < (1 - p_const),
               inp_choice=ceil(rand(1)*(num_inp)+eps);
               eval(['arg' num2str(j) '=''(u' num2str(inp_choice) ')'';'])
            else
               const_val=1;
               eval(['arg' num2str(j) '=''([' num2str(const_val) '])'';'])
            end
         end
       
         sub_tree=[  arg1  fun_name   arg2   ]; 
      end
   
   end
   eval(['A' num2str(i) '=' '''(' sub_tree ')''' ';']);

end


%
% Back substitute to form one large tree structure
%

for i=s_length-1:-1:1,
   for j=i+1:s_length,
      s1=eval(['A' num2str(i)]);
      s2=['A' num2str(j)];
      s3=eval(['A' num2str(j)]);
      eval(['A' num2str(i) '=strrep(''' s1 ''',''' s2 ''',''' s3 ''');' ])  
   end 
end

x2=A1;

⌨️ 快捷键说明

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