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

📄 opt_par.m

📁 用MATLAB实现遗传算法中的好多问题
💻 M
字号:
% function [xmut]=opt_par(xmut,u,y,num_inp,options,time_out)
%
% Function for optimising a strings parameters. For use with tree_ga
%

% Author             Date     Predecessor   Modification
% ======             ====     ===========   ============
% B.McKay M.Willis   18/5/95  mutato.m      least squares extracted       
%                    24/5/95                user def. functions
%
% Function Calls:  clsq.mex, strep2.mex, strrem.m
% ===============    
%
% Last Modification: 20/1/96
% =================
% Modified to use least squares clsq.mex routine.
% Constants to be optimised are replaced by a1,a2,a3...etc
  
function [xmut]=opt_par(xmut,u,y,num_inp,options,time_out)        

%
% Find all open/close square bracket pairs and extract constants and
% replace with a variable, for performing non-linear least squares fit
% Note: The x passed to the function "leastsq" is not the x used in the
% rest of the function


% Check to see if function output is a NaN or +/- inf


for i=1:num_inp,
   eval(['u' num2str(i) '= u(:,' num2str(i) ');']);
end
              
out=['y_est =' xmut ';'];

% Remove '{', '}', '<1', '<2', '<3' and '>'  
out=strrem(out);
    
out = strep2(out);
eval(out)


if sum(finite(y_est)) == length(y),

   obr_ind=findstr(xmut,'[');
   cbr_ind=findstr(xmut,']');
   num_const=length(obr_ind);
   
   if num_const==0,
      return
   end
  
         
   [D,L]=size(xmut);
              
   x_lsq=xmut(1:obr_ind(1));       
   for i=1:num_const
      eval(['x0(' num2str(i) ')= ' xmut(obr_ind(i)+1:cbr_ind(i)-1) ';' ]);
      if i==num_const,
         x_lsq = [x_lsq 'a' num2str(i) xmut(cbr_ind(i):L)];

      else
         x_lsq = [x_lsq 'a' num2str(i) xmut(cbr_ind(i):obr_ind(i+1))];    
      end
     
    end  
              
             
    % Set up string to pass to least squares

    % Remove '{', '}', '<1', '<2', '<3' and '>'  
    x_lsq=strrem(x_lsq);
    
    x_lsq  = strep2(x_lsq);
    
   
    ndata=length(y);
    mfit=length(x0);
    
    for j=1:num_inp
        uc((((num_inp-1)*ndata)+1):(ndata*num_inp),:)=u(:,num_inp);     
    end
                 
    x_fun = 'c=clsq(y,ndata,x0,mfit,x_lsq);';
   
    eval(x_fun)
    
    % Insert least squares fit constants back into string 

    x_tmp=xmut(1:obr_ind(1));       
    for i=1:num_const
       if i==num_const,
          x_tmp = [x_tmp  num2str(c(i)) xmut(cbr_ind(i):L)];
       else
          x_tmp = [x_tmp  num2str(c(i)) xmut(cbr_ind(i):obr_ind(i+1))];
       end
    end
 
    xmut = x_tmp;  
   
end            


⌨️ 快捷键说明

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