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

📄 genalg.m

📁 偏最小二乘算法在MATLAB中的实现
💻 M
字号:
function [ffit,gpop] = genalg(xdat,ydat,outfit,outpop,action);
%GENALG Genetic Algorithm for Variable Selection
% This function can be used for predictive variable selection with
% MLR or PLS regression models. The inputs are (xdat) a matrix of 
% of predictor variables and (ydat) a matrix of predicted variables.
% (outfit) and (outpop) are text variables that contain the names of
% the output variables. The fitness of the final members of the
% population are given in (outfit). The final population is given
% in (gpop) a matrix of ones and zeros with the same number of
% columns as (xdat) and number of rows equal to the number of 
% unique members of the final population. A 1 in (gpop) indicates
% that the original variable in xdat was selected while a zero
% indicates that it was not. For more information about GENALG
% please see the manual and the GAREADME file.
%
%I/O is genalg(xdat,ydat,outfit,outpop);
% e.g. genalg(x,y,'fitness','selectedvariables');

% Copyright
% Eigenvector Technologies
% 1995

if nargin<5&nargin>1,
   action = 'initiate';
end

if strcmp(action,'initiate')
  handl   = zeros(17,5);
  [mx,nx] = size(xdat);
  [my,ny] = size(ydat);

%Define default values for all variables
  ps = 32; mg  = 100; mr  = .005; ww  = 1; cc = 20; ft = 30;
  co = 1;  lvs = 10;  cvs = 5;    cvi = 5;
%Set figure title
  fig     = figure('Name','Genetic Algorithm for Variable Selection',...
   'NumberTitle','off','Pos',[111 19 480 320],'Resize','Off');
%Define frame around general ga control sliders
  uicontrol(fig,'Style','Frame','Pos',[11 11 220 300],'BackGroundColor',[1 0 0]);
  uicontrol(fig,'Style','Frame','Pos',[15 15 212 292]);
%Define frame around regression choice controls
  uicontrol(fig,'Style','Frame','Pos',[251 211 220 100],'BackGroundColor',[1 0 0]);
  uicontrol(fig,'Style','Frame','Pos',[255 215 212 92]);
%Define cross-validation choices frame
  uicontrol(fig,'Style','Frame','Position',[251 61 220 140],'BackGroundColor',[1 0 0]);
  uicontrol(fig,'Style','Frame','Position',[255 65 212 132]);
%Define text header for general ga controls
  uicontrol(fig,'Style','text','Pos',[21 281 200 20],'String','General GA Parameters');
%Define population size slider sli_ps
  handl(2,1) = uicontrol(fig,'Style','slider','Position',...
   [51 256 140 20],'Min',16,'Max',256,'Value',ps,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act21'');');
  handl(2,2) = uicontrol(fig,'Style','text','Pos',[161 236 30 20],...
   'String',num2str(get(handl(2,1),'Value')),'Horiz','right');
  handl(2,3) = uicontrol(fig,'Style','text','Pos',[21 256 30 20],...
   'String',num2str(get(handl(2,1),'Min')),'Horiz','left');
  handl(2,4) = uicontrol(fig,'Style','text','Pos',[191 256 30 20],...
   'String',num2str(get(handl(2,1),'Max')),'Horiz','right');
  handl(2,5) = uicontrol(fig,'Style','text','Pos',[51 236 120 20],...
   'String','Population Size','Horiz','left');
%Define maximum generations slider sli_mg
  handl(3,1) = uicontrol(fig,'Style','slider','Position',...
   [51 136 140 20],'Min',25,'Max',500,'Value',mg,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act31'');');
  handl(3,2) = uicontrol(fig,'Style','text','Pos',[161 116 30 20],...
   'String',num2str(get(handl(3,1),'Value')),'Horiz','right');
  handl(3,3) = uicontrol(fig,'Style','text','Pos',[21 136 30 20],...
   'String',num2str(get(handl(3,1),'Min')),'Horiz','left');
  handl(3,4) = uicontrol(fig,'Style','text','Pos',[191 136 30 20],...
   'String',num2str(get(handl(3,1),'Max')),'Horiz','right');
  handl(3,5) = uicontrol(fig,'Style','text','Pos',[51 116 120 20],...
   'String','Maximum Generations','Horiz','left');
%Define mutation rate slider sli_mr
  handl(4,1) = uicontrol(fig,'Style','slider','Position',...
   [51 56 140 20],'Min',.001,'Max',.01,'Value',mr,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act41'');');
  handl(4,2) = uicontrol(fig,'Style','text','Pos',[146 36 45 20],...
   'String',num2str(get(handl(4,1),'Value')),'Horiz','right');
  handl(4,3) = uicontrol(fig,'Style','text','Pos',[16 56 33 20],...
   'String',num2str(get(handl(4,1),'Min')),'Horiz','left');
  handl(4,4) = uicontrol(fig,'Style','text','Pos',[191 56 30 20],...
   'String',num2str(get(handl(4,1),'Max')),'Horiz','right');
  handl(4,5) = uicontrol(fig,'Style','text','Pos',[51 36 110 20],...
   'String','Mutation Rate','Horiz','left');  
%Define window width slider sli_ww
  maxw       = round(min([nx/2,50]));
  handl(5,1) = uicontrol(fig,'Style','slider','Position',...
   [51 216 140 20],'Min',1,'Max',maxw,'Value',ww,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act51'');');
  handl(5,2) = uicontrol(fig,'Style','text','Pos',[161 196 30 20],...
    'String',num2str(get(handl(5,1),'Value')),'Horiz','right');
  handl(5,3) = uicontrol(fig,'Style','text','Pos',[21 216 30 20],...
   'String',num2str(get(handl(5,1),'Min')),'Horiz','left');
  handl(5,4) = uicontrol(fig,'Style','text','Pos',[191 216 30 20],...
   'String',num2str(get(handl(5,1),'Max')),'Horiz','right');
  handl(5,5) = uicontrol(fig,'Style','text','Pos',[51 196 120 20],...
   'String','Window Width','Horiz','left');
%Define convergence criteria slider
  handl(6,1) = uicontrol(fig,'Style','slider','Position',...
   [51 96 140 20],'Min',1,'Max',100,'Value',cc,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act61'');');
  handl(6,2) = uicontrol(fig,'Style','text','Pos',[161 76 30 20],...
   'String',num2str(get(handl(6,1),'Value')),'Horiz','right');
  handl(6,3) = uicontrol(fig,'Style','text','Pos',[21 96 30 20],...
   'String',num2str(get(handl(6,1),'Min')),'Horiz','left');
  handl(6,4) = uicontrol(fig,'Style','text','Pos',[191 96 30 20],...
   'String',num2str(get(handl(6,1),'Max')),'Horiz','right');
  handl(6,5) = uicontrol(fig,'Style','text','Pos',[51 76 120 20],...
    'String','% at Convergence','Horiz','left');
%Define fraction terms in initial population slider
  handl(7,1) = uicontrol(fig,'Style','slider','Position',...
   [51 176 140 20],'Min',10,'Max',50,'Value',ft,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act71'');');
  handl(7,2) = uicontrol(fig,'Style','text','Pos',[161 156 30 20],...
    'String',num2str(get(handl(7,1),'Value')),'Horiz','right');
  handl(7,3) = uicontrol(fig,'Style','text','Pos',[21 176 30 20],...
    'String',num2str(get(handl(7,1),'Min')),'Horiz','left');
  handl(7,4) = uicontrol(fig,'Style','text','Pos',[191 176 30 20],...
    'String',num2str(get(handl(7,1),'Max')),'Horiz','right');
  handl(7,5) = uicontrol(fig,'Style','text','Pos',[51 156 120 20],...
    'String','% Initial Terms','Horiz','left');
%Define crossover choice radio buttons
  uicontrol(fig,'Style','text','String','Crossover:',...
   'Horiz','left','Position',[31 20 75 15]);
  handl(8,1) = uicontrol(fig,'Style','radio','String',...
    'Single','Position',[95 19 75 20],'Value',1,...
	'CallBack','genalg(0,0,''w1'',''w2'',''act81'');');
  handl(8,2) = uicontrol(fig,'Style','radio','String',...
    'Double','Position',[155 19 55 20],'Value',0,...
	'CallBack','genalg(0,0,''w1'',''w2'',''act82'');');
  handl(8,3) = 1;	
%Define the regression choice radio buttons
  uicontrol(fig,'Style','text','String','Regression Choice',...
   'Position',[261 281 200 20]);
  handl(9,1) = uicontrol(fig,'Style','radio','String',...
   'MLR','Position',[291 261 100 20],'Value',0,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act91'');');
  handl(9,2) = uicontrol(fig,'Style','radio','String',...
   'PLS','Position',[361 261 100 20],'Value',1,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act92'');');
  handl(9,3) = 1;
%Define # of latent variables slider
  mlvs        = min([mx,nx]);
  lvs         = min([mlvs,lvs]);
  handl(10,1) = uicontrol(fig,'Style','slider','Pos',...
   [291 236 140 20],'Min',2,'Max',mlvs,'Value',lvs,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act101'');');
  handl(10,2) = uicontrol(fig,'Style','text','Pos',[401 216 30 20],...
   'String',num2str(get(handl(10,1),'Value')),'Horiz','right');
  handl(10,3) = uicontrol(fig,'Style','text','Pos',[261 236 30 20],...
   'String',num2str(get(handl(10,1),'Min')),'Horiz','left');
  handl(10,4) = uicontrol(fig,'Style','text','Pos',[431 236 30 20],...
   'String',num2str(get(handl(10,1),'Max')),'Horiz','right');
  handl(10,5) = uicontrol(fig,'Style','text','Pos',[291 216 110 20],...
   'String','# of Latent Variables','Horiz','left');
%Define cross validation choice
  uicontrol(fig,'Style','text','Position',[261 171 200 20],...
   'String','Cross-Validation Parameters');
%Define cross-validation choice random radio button
  handl(11,1) = uicontrol(fig,'Style','radio','String',...
   'Random','Position',[291 156 100 20],'Value',1,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act111'');');
  handl(11,2) = uicontrol(fig,'Style','radio','String',...
   'Contiguous','Position',[361 156 100 20],'Value',0,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act112'');');
  handl(11,3) = 0;
%Define slider for cross-validation split
   maxs = min([20,mx]);
   vals = round(sqrt(mx));
   vals = min([cvs,vals]);
  handl(12,1) = uicontrol(fig,'Style','slider','Position',...
   [291 131 140 20],'Min',2,'Max',maxs,'Value',vals,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act121'');');
  handl(12,2) = uicontrol(fig,'Style','text','Pos',[401 111 30 20],...
   'String',num2str(get(handl(12,1),'Value')),'Horiz','right');
  handl(12,3) = uicontrol(fig,'Style','text','Pos',[261 131 30 20],...
   'String',num2str(get(handl(12,1),'Min')),'Horiz','left');
  handl(12,4) = uicontrol(fig,'Style','text','Pos',[431 131 30 20],...
   'String',num2str(get(handl(12,1),'Max')),'Horiz','right');
  handl(12,5) = uicontrol(fig,'Style','text','Pos',[291 111 120 20],...
   'String','Number of Subsets','Horiz','left');
   %Define slider for the cross-validation iterations
  handl(13,1) = uicontrol(fig,'Style','slider','Position',...
   [291 86 140 20],'Min',1,'Max',10,'Value',cvi,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act131'');');
  handl(13,2) = uicontrol(fig,'Style','text','Pos',[401 66 30 20],...
   'String',num2str(get(handl(13,1),'Value')),'Horiz','right');
  handl(13,3) = uicontrol(fig,'Style','text','Pos',[261 86 30 20],...
   'String',num2str(get(handl(13,1),'Min')),'Horiz','left');
  handl(13,4) = uicontrol(fig,'Style','text','Pos',[431 86 30 20],...
   'String',num2str(get(handl(13,1),'Max')),'Horiz','right');
  handl(13,5) = uicontrol(fig,'Style','text','Pos',[291 66 110 20],...
   'String','Number of Iterations','Horiz','left');
%Define the push buttons for routine control
  handl(14,1) = uicontrol(fig,'Style','Push','Pos',[251 11 66 37],...
   'String','Execute','CallBack','genalg(0,0,''w1'',''w2'',''garun'');',...
   'Interruptible','Yes');
  handl(15,1) = uicontrol(fig,'Style','push','Pos',[328 11 67 37],...
   'String','Stop','CallBack','genalg(0,0,''w1'',''w2'',''stop'');',...
   'Visible','Off','UserData',0);
   s1         = ['[',outfit,',',outpop,']=genalg(0,0,''w1'',''w2'',''quit'');'];
  handl(16,1) = uicontrol(fig,'Style','push','Pos',[406 11 66 37],'String',...
   'Quit','CallBack',s1,'Visible','Off');
  handl(17,1) = uicontrol(fig,'Style','Push','Pos',[251 11 66 37],...
   'String','Resume','CallBack','genalg(0,0,''w1'',''w2'',''resum'');',...
   'Interruptible','Yes','Visible','Off');  
  set(handl(2,1),'UserData',xdat);
  set(handl(3,1),'UserData',ydat);
  set(handl(5,1),'UserData',outfit);
  set(handl(5,2),'UserData',outpop);
  set(fig,'UserData',handl);
else
  fig     = gcf;
  handl   = get(fig,'UserData');
  xdat    = get(handl(2,1),'UserData');
  ydat    = get(handl(3,1),'UserData');
  [mx,nx] = size(xdat);
  [my,ny] = size(ydat);
  outfit  = get(handl(5,1),'UserData');
  outpop  = get(handl(5,2),'UserData');
  
  if strcmp(action,'act21')
    ps  = 4*round(get(handl(2,1),'Val')/4);
    set(handl(2,2),'String',num2str(ps));
  elseif strcmp(action,'act31')
    mg  = round(get(handl(3,1),'Val'));
    set(handl(3,2),'String',num2str(mg));
  elseif strcmp(action,'act41')
    mr = 1e-3*round(get(handl(4,1),'Val')/1e-3);
    set(handl(4,2),'String',num2str(mr));
  elseif strcmp(action,'act51')
    ww = round(get(handl(5,1),'Val'));
    set(handl(5,2),'String',num2str(ww));
  elseif strcmp(action,'act61')
    cc = round(get(handl(6,1),'Val'));
    set(handl(6,2),'String',num2str(cc));
  elseif strcmp(action,'act71')
    ft = round(get(handl(7,1),'Val'));
    set(handl(7,2),'String',num2str(ft));
  elseif strcmp(action,'act81')
    set(handl(8,1),'Value',1);
	set(handl(8,2),'Value',0);
	handl(8,3) = 1;
  elseif strcmp(action,'act82')
    set(handl(8,1),'Value',0);
	set(handl(8,2),'Value',1);
	handl(8,3) = 2;
  elseif strcmp(action,'act91')
    set(handl(9,1),'Value',1);
	set(handl(9,2),'Value',0);
	set(handl(10,1:5),'Visible','off');
	handl(9,3) = 0;
  elseif strcmp(action,'act92')
    set(handl(9,1),'Value',0);
	set(handl(9,2),'Value',1);
	set(handl(10,1:5),'Visible','on');
	handl(9,3) = 1;
  elseif strcmp(action,'act101')
  	lvs = round(get(handl(10,1),'Val'));
    set(handl(10,2),'String',num2str(lvs));
  elseif strcmp(action,'act111')
    set(handl(11,1),'Value',1);
	set(handl(11,2),'Value',0);
	handl(11,3) = 0;
  elseif strcmp(action,'act112')
    set(handl(11,1),'Value',0);
	set(handl(11,2),'Value',1);
	handl(11,3) = 1;
  elseif strcmp(action,'act121')
  	cvs = round(get(handl(12,1),'Val'));
    set(handl(12,2),'String',num2str(cvs));
  elseif strcmp(action,'act131')
  	cvi = round(get(handl(13,1),'Val'));
    set(handl(13,2),'String',num2str(cvi));
  elseif strcmp(action,'garun')
    set(handl([2:7 10 12 13],[1 3 4]),'Visible','Off');
    for i = 1:2
	  if get(handl(8,i),'Value') == 0
	    set(handl(8,i),'Visible','Off');
	  end
	  if get(handl(9,i),'Value') == 0
	    set(handl(9,i),'Visible','Off');
	  end
	  if get(handl(11,i),'Value') == 0
	    set(handl(11,i),'Visible','Off');
	  end
	end
	set(handl(14,1),'Visible','Off');
    handl(1,1) = figure('Name','GA for Variable Selection Results','Pos',...
	  [401 169 480 320],'Resize','On','NumberTitle','Off');
	set(handl(15,1),'Visible','On','UserData',0);
	set(fig,'UserData',[handl]);
    drawnow
	gaselect(fig,'gogas');
  elseif strcmp(action,'stop')
    set(handl(15,1),'Visible','Off','UserData',1);
  elseif strcmp(action,'resum')
    set(handl(17,1),'Visible','Off');
	set(handl([3 4 6 10 12 13],[1 3 4]),'Visible','Off');
	set(handl(15,1),'Visible','On','UserData',0);
	gaselect(fig,'resum');
  end  
  set(fig,'UserData',[handl]);
  if strcmp(action,'quit')
    ffit = get(handl(4,2),'UserData');
	gpop = get(handl(4,5),'UserData');
    close;
  end
end

⌨️ 快捷键说明

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