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

📄 genalg.m

📁 PLS_Toolbox是用于故障检测与诊断方面的matlab工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
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 inputs
%  that contain the names of the output variables. The fitness
%  of the final members of the population are given in (outfit)
%  and (outpop) contains the final populations: a 1 means a
%  variable was included, and a 0 means that it was not included.
%
%I/O: genalg(xdat,ydat,outfit,outpop);
%
%Example: genalg(x,y,'fitness','selectedvariables');
%
%See also: GASELCTR

%Copyright Eigenvector Research, Inc. 1995-2000
%Modified 2/95, 1/97, 2/97 NBG
%Modified 4/30/98 BMW
% 10/27/00, nbg change in main loop to check if pop(i,:) is all zeros

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

if strcmp(action,'initiate')
  handl   = zeros(17,5);
  [mx,nx] = size(xdat);
  [my,ny] = size(ydat);
  bgc     = ['BackGroundColor'];
  bgc1    = [.8 .8 .8];
  bgc2    = [1 0 1]*0.6;

%Define default values for all variables
  ps = 64; mg  = 100; mr  = .005; ww  = 1; cc = 50; ft = 30;
  lvs = 10;  cvs = 5;    cvi = 1; %co = 1;
%Set figure title
  fig     = figure('Name','Genetic Algorithm for Variable Selection',...
   'NumberTitle','Off','Pos',[111 19 480 320],'Resize','Off','Color',[0 0 0]);
%Define frame around general ga control sliders
  uicontrol(fig,'Style','Frame','Pos',[11 11 220 300],bgc,bgc2);
  uicontrol(fig,'Style','Frame','Pos',[15 15 212 292],bgc,bgc1);
%Define frame around regression choice controls
  uicontrol(fig,'Style','Frame','Pos',[251 211 220 100],bgc,bgc2);
  uicontrol(fig,'Style','Frame','Pos',[255 215 212 92],bgc,bgc1);
%Define cross-validation choices frame
  uicontrol(fig,'Style','Frame','Position',[251 61 220 140],bgc,bgc2);
  uicontrol(fig,'Style','Frame','Position',[255 65 212 132],bgc,bgc1);
%Define text header for general ga controls
  uicontrol(fig,'Style','text','Pos',[21 281 200 20],'String',...
  'GA Parameters',bgc,bgc1);
%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],bgc,bgc1,...
   'String',num2str(get(handl(2,1),'Value')),'Horiz','right');
  handl(2,3) = uicontrol(fig,'Style','text','Pos',[21 256 30 20],bgc,bgc1,...
   'String',num2str(get(handl(2,1),'Min')),'Horiz','left');
  handl(2,4) = uicontrol(fig,'Style','text','Pos',[191 256 30 20],bgc,bgc1,...
   'String',num2str(get(handl(2,1),'Max')),'Horiz','right');
  handl(2,5) = uicontrol(fig,'Style','text','Pos',[51 236 120 20],bgc,bgc1,...
   '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],bgc,bgc1,...
   'String',num2str(get(handl(3,1),'Value')),'Horiz','right');
  handl(3,3) = uicontrol(fig,'Style','text','Pos',[21 136 30 20],bgc,bgc1,...
   'String',num2str(get(handl(3,1),'Min')),'Horiz','left');
  handl(3,4) = uicontrol(fig,'Style','text','Pos',[191 136 30 20],bgc,bgc1,...
   'String',num2str(get(handl(3,1),'Max')),'Horiz','right');
  handl(3,5) = uicontrol(fig,'Style','text','Pos',[51 116 120 20],bgc,bgc1,...
   'String','Max 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],bgc,bgc1,...
   'String',num2str(get(handl(4,1),'Value')),'Horiz','right');
  handl(4,3) = uicontrol(fig,'Style','text','Pos',[16 56 33 20],bgc,bgc1,...
   'String',num2str(get(handl(4,1),'Min')),'Horiz','left');
  handl(4,4) = uicontrol(fig,'Style','text','Pos',[191 56 30 20],bgc,bgc1,...
   'String',num2str(get(handl(4,1),'Max')),'Horiz','right');
  handl(4,5) = uicontrol(fig,'Style','text','Pos',[51 36 110 20],bgc,bgc1,...
   '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],bgc,bgc1,...
    'String',num2str(get(handl(5,1),'Value')),'Horiz','right');
  handl(5,3) = uicontrol(fig,'Style','text','Pos',[21 216 30 20],bgc,bgc1,...
   'String',num2str(get(handl(5,1),'Min')),'Horiz','left');
  handl(5,4) = uicontrol(fig,'Style','text','Pos',[191 216 30 20],bgc,bgc1,...
   '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',bgc,bgc1);
%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],bgc,bgc1,...
   'String',num2str(get(handl(6,1),'Value')),'Horiz','right');
  handl(6,3) = uicontrol(fig,'Style','text','Pos',[21 96 30 20],bgc,bgc1,...
   'String',num2str(get(handl(6,1),'Min')),'Horiz','left');
  handl(6,4) = uicontrol(fig,'Style','text','Pos',[191 96 30 20],bgc,bgc1,...
   'String',num2str(get(handl(6,1),'Max')),'Horiz','right');
  handl(6,5) = uicontrol(fig,'Style','text','Pos',[51 76 120 20],bgc,bgc1,...
    '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],bgc,bgc1,...
    'String',num2str(get(handl(7,1),'Value')),'Horiz','right');
  handl(7,3) = uicontrol(fig,'Style','text','Pos',[21 176 30 20],bgc,bgc1,...
    'String',num2str(get(handl(7,1),'Min')),'Horiz','left');
  handl(7,4) = uicontrol(fig,'Style','text','Pos',[191 176 30 20],bgc,bgc1,...
    'String',num2str(get(handl(7,1),'Max')),'Horiz','right');
  handl(7,5) = uicontrol(fig,'Style','text','Pos',[51 156 120 20],bgc,bgc1,...
    'String','% Initial Terms','Horiz','left');
%Define crossover choice radio buttons
  uicontrol(fig,'Style','text','String','Crossover:',bgc,bgc1,...
   'Horiz','left','Position',[21 20 75 15]);
  handl(8,1) = uicontrol(fig,'Style','radio','String',...
    'Single','Position',[80 19 60 20],'Value',0,bgc,bgc1,...
	'CallBack','genalg(0,0,''w1'',''w2'',''act81'');');
  handl(8,2) = uicontrol(fig,'Style','radio','String',...
    'Double','Position',[155 19 60 20],'Value',1,bgc,bgc1,...
	'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],bgc,bgc1);
  handl(9,1) = uicontrol(fig,'Style','radio','String',...
   'MLR','Position',[281 261 50 20],'Value',0,bgc,bgc1,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act91'');');
  handl(9,2) = uicontrol(fig,'Style','radio','String',...
   'PLS','Position',[371 261 50 20],'Value',1,bgc,bgc1,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act92'');');
  handl(9,3) = 1;
%Define # of latent variables slider
  mlvs        = min([mx,nx,25]);
  lvs         = min([mlvs,lvs]);
  handl(10,1) = uicontrol(fig,'Style','slider','Pos',...
   [291 236 140 20],'Min',1,'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',bgc,bgc1);
  handl(10,3) = uicontrol(fig,'Style','text','Pos',[261 236 30 20],...
   'String',num2str(get(handl(10,1),'Min')),'Horiz','left',bgc,bgc1);
  handl(10,4) = uicontrol(fig,'Style','text','Pos',[431 236 30 20],...
   'String',num2str(get(handl(10,1),'Max')),'Horiz','right',bgc,bgc1);
  handl(10,5) = uicontrol(fig,'Style','text','Pos',[291 216 110 20],...
   'String','# of LVs','Horiz','left',bgc,bgc1);
%Define cross validation choice
  uicontrol(fig,'Style','text','Position',[261 171 200 20],...
   'String','Cross-Validation Parameters',bgc,bgc1);
%Define cross-validation choice random radio button
  handl(11,1) = uicontrol(fig,'Style','radio','String',...
   'Random','Position',[281 156 58 20],'Value',1,bgc,bgc1,...
   'CallBack','genalg(0,0,''w1'',''w2'',''act111'');');
  handl(11,2) = uicontrol(fig,'Style','radio','String',...
   'Contiguous','Position',[371 156 80 20],'Value',0,bgc,bgc1,...
   '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],bgc,bgc1,...
   'String',num2str(get(handl(12,1),'Value')),'Horiz','right');
  handl(12,3) = uicontrol(fig,'Style','text','Pos',[261 131 30 20],bgc,bgc1,...
   'String',num2str(get(handl(12,1),'Min')),'Horiz','left');
  handl(12,4) = uicontrol(fig,'Style','text','Pos',[431 131 30 20],bgc,bgc1,...
   'String',num2str(get(handl(12,1),'Max')),'Horiz','right');
  handl(12,5) = uicontrol(fig,'Style','text','Pos',[291 111 120 20],bgc,bgc1,...
   'String','# 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],bgc,bgc1,...
   'String',num2str(get(handl(13,1),'Value')),'Horiz','right');
  handl(13,3) = uicontrol(fig,'Style','text','Pos',[261 86 30 20],bgc,bgc1,...
   'String',num2str(get(handl(13,1),'Min')),'Horiz','left');
  handl(13,4) = uicontrol(fig,'Style','text','Pos',[431 86 30 20],bgc,bgc1,...
   'String',num2str(get(handl(13,1),'Max')),'Horiz','right');
  handl(13,5) = uicontrol(fig,'Style','text','Pos',[291 66 110 20],bgc,bgc1,...
   'String','# of Iterations','Horiz','left');
%Define the push buttons for routine control
  handl(14,1) = uicontrol(fig,'Style','Push','Pos',[251 11 66 37],bgc,bgc1,...
   'String','Execute','CallBack','genalg(0,0,''w1'',''w2'',''garun'');',...
   'Interruptible','On');
  handl(15,1) = uicontrol(fig,'Style','push','Pos',[328 11 67 37],bgc,bgc1,...
   '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',bgc,bgc1);
  handl(17,1) = uicontrol(fig,'Style','Push','Pos',[251 11 66 37],bgc,bgc1,...
   'String','Resume','CallBack','genalg(0,0,''w1'',''w2'',''resum'');',...
   'Interruptible','On','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

function gaselect(fig,action)
%GASELECT Subroutine of GENALG for Variable Selection
%  Please see GENALG

%Copyright Eigenvector Research, Inc. 1995-98
%Modified 2/9/97,2/10/98 NBG

% Variables set by GUI
handl     = get(fig,'userdata'); 
nopop     = 4*round(get(handl(2,1),'Value')/4);       %Size of population
maxgen    = round(get(handl(3,1),'value'));           %Max number of generations
mut       = 1e-3*round(get(handl(4,1),'value')/1e-3); %Mutation Rate
window    = round(get(handl(5,1),'value'));           %Window width for spectral channels
converge  = round(get(handl(6,1),'value'));           % % of pop the same at convergence
begfrac   = round(get(handl(7,1),'value'))/100;       %Fraction of terms included in beginning
cross     = handl(8,3);                               %Double or single cross over, 1 = single
reg       = handl(9,3);                               %Regression method, 0 = MLR, 1 = PLS
maxlv     = round(get(handl(10,1),'value'));          %No. LVs, only needed with reg = 1
cvopt     = handl(11,3);                              %CV option, 0 = random, 1 = contiguous
split     = round(get(handl(12,1),'value'));          %No. subsets to divide data into
iter      = round(get(handl(13,1),'value'));          %No. iterations of CV at each generation
x         = get(handl(2,1),'UserData');               % x-block data
y         = get(handl(3,1),'UserData');               % y-block data
[m,n]     = size(x);
if strcmp(action,'gogas')
  gcount    = 1;
  specsplit = ceil(n/window);
  set(handl(2,2),'UserData',specsplit);
  %Check to see that nopop is divisible by 4
  dp        = nopop/4;
  if ceil(dp) ~= dp
    nopop   = ceil(dp)*4;
    disp('Population size not divisible by 4')
    s       = sprintf('Resizing to a population of %g',nopop);
    disp(s)
  end
  %Generate initial population
  pop     = rand(nopop,specsplit);
  for i = 1:nopop
    for j = 1:specsplit
      if pop(i,j) < begfrac
        pop(i,j) = 1;
      else
        pop(i,j) = 0;
      end
    end
	if sum(pop(i,:)')<0.5
	  colm        = round(rand(1)*specsplit);
	  if colm <0.5
	    colm      = 1;
	  end
	  pop(i,colm) = 1;

⌨️ 快捷键说明

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