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

📄 andrdemo.m

📁 1、工具箱:LS_SVMlab Classification_LS_SVMlab.m - 多类分类 Regression_LS_SVMlab.m - 函数拟合 2、工具箱:OSU_SVM3.
💻 M
📖 第 1 页 / 共 2 页
字号:
      if checkdat(file.pathname,DATA_IDENT,2,[0 0])==1,
         set(h.btload,'UserData',file);
         andrdemo('loadsets',hfigure);
         andrdemo('reset',hfigure);
      else
         errordlg('This file does not contain required data.','Bad file','modal');
      end
   end


case 'algohandler'
   % == Handler for check box 'Algorithm' =======================================
   % If new algorithm is selected then prepare data for it.

   h=get(hfigure,'UserData');

   if get(h.pualgo,'UserData') ~= get(h.pualgo,'Value'),
      set(h.pualgo,'UserData',get(h.pualgo,'Value'));

      set(h.edprec,'String',num2str(DEF_PRECISION(get(h.pualgo,'Value'))) );
      set(h.txprec,'String',PREC_TITLE(get(h.pualgo,'Value'),:));

      andrdemo('loadsets',hfigure);
      andrdemo('reset',hfigure);
   end


case 'loadsets'
   % == Load sets ==================================================================
   % Get given file name and load the data set from him.

   h=get(hfigure,'UserData');                   % uicontrol handlers

   % Get file name
   file=get(h.btload,'UserData');
   if isempty(file)==0,

      % Load sets
      sets=load(file.pathname);

      % algorithm 2 (Original Anderson`s) solution find solution for two distributions only
      if get(h.pualgo,'Value')==2,
         % get only one distribution from each class
         class1=0;
         class2=0;
         NI=[];
         NMI=[];
         NSIGMA=[];
         NK=[1 1];
         i=0;
         while i<sets.K | class1==0 |class2==0,
            i=i+1;
            if sets.I(i)==1 & class1==0,
               class1=1;
               NI=[NI,1];
               NMI=[NMI,sets.MI(:,i)];
               NSIGMA=[NSIGMA,sets.SIGMA(:,(i-1)*2+1:i*2)];
            elseif sets.I(i)==2 & class2==0,
               class2=1;
               NI=[NI,2];
               NMI=[NMI,sets.MI(:,i)];
               NSIGMA=[NSIGMA,sets.SIGMA(:,(i-1)*2+1:i*2)];
            end
         end % while

         % replace old values
         sets.MI=NMI;
         sets.SIGMA=NSIGMA;
         sets.I=NI;
         sets.K=NK;
      end % if get(h.pualgo,....
   else
      % No set is loaded.
      sets=[];
   end

   % store sets
   set(h.axes1,'UserData',sets);


case 'play'
   % == Start up the adaptation process =======================================
   % Perform the adaptation step by step until the solution is found or stop
   % button is pushed down.

   h=get(hfigure,'UserData');                      % get handlers

   % get sets
   sets=get(h.axes1,'UserData');

   % if some set is loaded then perform on step
   if isempty(sets)==0,

      % Disable buttons everything axcept
      set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.btload h.pualgo ...
            h.btcreat h.editer h.edprec h.xbanim],'Enable','off');

      % Only stop button can be pushed down
      set(h.btstop,'Enable','on');

      % Stop button was not pushed down
      set(h.btstop,'UserData',0);
      play=1;

      % get arguments from dialog
      anim=get(h.xbanim,'Value');

      % Play - adaptation process
      while play==1 & get(h.btstop,'UserData')==0,

         % get arguments from dialog
         oldtheta=h.line.theta;
         oldalpha=h.line.alpha;

         % call algorithm
         [h,text,play,solution]=callalgo(h,sets);

         % appear result
         set(h.console,'String',text );
         drawnow;

         if play~=-1,
            % plot result
            if h.line.handler==-1,
               axes(h.axes1);
               h.line.handler=feval(PLOT_FCE,sets.MI,sets.SIGMA,sets.I,...
                  h.line.alpha,h.line.theta );
            else
               feval(PLOT_FCE,sets.MI,sets.SIGMA,sets.I,h.line.alpha,h.line.theta,...                  h.line.handler,anim,oldalpha,oldtheta);            end         end         % hands on control to MATLAB         drawnow;      end % while play == 1 & get(...      %  store new solution
      set(hfigure,'UserData',h);

      % enable these buttons
      set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.btload ...
         h.btcreat h.pualgo h.editer h.edprec h.xbanim],'Enable','on');

      % disable stop button
      set(h.btstop,'Enable','off');

   else % isempty(sets)==0,
      % write down description
      text=sprintf('No data loaded.\nPress Load data button.\n');
      set(h.console,'String',text );
   end


case 'step'
   % == Perform one adaptation step ================================================
   h=get(hfigure,'UserData');                     % get handlers we will need...

   % get sets
   sets=get(h.axes1,'UserData');

   % if some set is loaded then perform on step
   if isempty(sets)==0,

      % get arguments from dialog
      anim=get(h.xbanim,'Value');
      oldtheta=h.line.theta;
      oldalpha=h.line.alpha;

      % call algorithm
      [h,text,play,solution]=callalgo(h,sets);

      % appear result
      set(h.console,'String',text );
      drawnow;

      if play~=-1,
         % plot result
         if h.line.handler==-1,
            axes(h.axes1);
            h.line.handler=feval(PLOT_FCE,sets.MI,sets.SIGMA,sets.I,...
               h.line.alpha,h.line.theta );
         else
            feval(PLOT_FCE,sets.MI,sets.SIGMA,sets.I,h.line.alpha,...
               h.line.theta,h.line.handler,anim,oldalpha,oldtheta);
         end
      end

      drawnow;

      % store data
      set(hfigure,'UserData',h);

   else % isempty(sets)==0,
      % write down description
      text=sprintf('No data loaded.\nPress Load data button.\n');
      set(h.console,'String',text );
   end

case 'reset'
   % == Reset adaptation process ==================================
   % Sets t=0 and redraws axes.

   h=get(hfigure,'UserData');                     % get handlers

   % zeroize all parameters of the solution
   h.line.t=0;
   h.line.theta=0;
   h.line.alpha=[0;0];
   h.line.alpha1=[0;0];
   h.line.alpha2=[0;0];
   h.line.lambda=0;
   set(hfigure,'UserData',h);

   % No line
   h.line.handler=-1;
   set(hfigure,'UserData',h);
   %%%   cla;
   clrchild(h.axes1);
   %%%   win=axis;
   win=getaxis(h.axes1);
   %%%   axis([0 1 0 1]);
   setaxis(h.axes1,[0 1 0 1]);
   %%%   axis(win);
   setaxis(h.axes1,win);

   % Redraw points
   sets=get(h.axes1,'UserData');

   % if some points are loaded than appear it
   if isempty(sets)==0,

      % set axes according to current points MI

      if sum(sets.K) < 3,
         win=cmpwin(min(sets.MI'),max(sets.MI'),BORDER*2,BORDER*2);
      else
         win=cmpwin(min(sets.MI'),max(sets.MI'),BORDER,BORDER);
      end

      %%% axis(win);
      setaxis(h.axes1,win);

%%      pplot(sets.MI,sets.I);
      ppoints(sets.MI,sets.I);

      % comment window text
      consoletext=sprintf('Step t=0\nNo separation line');

      file=get(h.btload,'UserData');
      titletext=sprintf('File: %s, # of distributions K = %d',file.name,sum(sets.K));

   else % if isempty(sets)==0,
      % comment window text
      consoletext=sprintf('No data loaded.\nPress Load data button.\n');
      titletext='';

      pos=get(h.axes1,'Position');
      fsize=min(pos(3),pos(4))/10;

      %%%      axis([-1 1 -1 1]);
      setaxis(h.axes1,[-1 1 -1 1]);
      builtin('text',0,0,'Press ''Load data'' button.',...
         'HorizontalAlignment','center',...
         'FontUnits','normalized',...
         'Clipping','on',...
         'FontSize',fsize);
   end

   % print comment
   set(h.console,'String',consoletext );

   % print title
   pos=get(h.axes1,'Position');
   fsize=(1-pos(2)-pos(4))*1;
   title(titletext,...
      'VerticalAlignment','bottom',...
      'HorizontalAlignment','left',...
      'FontUnits','normalized',...
      'Units','normalized',...
      'Position',[0 1 0],...
      'FontSize',fsize);


case 'info'
   % == Call standard Matlab info box =========================================
   helpwin(mfilename);


end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [h,text,play,solution]=callalgo(h,sets)

% get arguments from dialog
precision=str2num(get(h.edprec,'String'));
iter=max([1,fix(str2num(get(h.editer,'String')))]);

% get parameters
t=h.line.t;
alpha=h.line.alpha;
alpha1=h.line.alpha1;
alpha2=h.line.alpha2;
lambda=h.line.lambda;
theta=h.line.theta;

% perform algorithm
switch get(h.pualgo,'Value')
case 4
   % General solution
   [nalpha,ntheta,solution,minr,nt,maxerr]=...
      ganders(sets.MI,sets.SIGMA,sets.I,iter,precision,t,alpha,theta);
   text=sprintf('Step t=%d\nLine [%f , %f]*x=%f\nMinimal r = %.15f, Max error = %f%%',...
      nt,nalpha(1),nalpha(2),ntheta,minr,maxerr*100);
case 1
   % e-Optimal solution
   [nalpha,ntheta,solution,nt,alpha1,alpha2]=...
       eanders(sets.MI,sets.SIGMA,sets.I,iter,precision/100,t,alpha1,alpha2);
    if sum(nalpha)==0,
      solution=-1;
      nalpha=alpha;
      ntheta=theta;
   end
   text=sprintf('Step t=%d\nLine [%f , %f]*x=%f',nt,nalpha(1),nalpha(2),ntheta);
case 2
   % Original Anderson`s solution
   [nalpha,ntheta,solution,nt,lambda,ni,maxerr]=...
      oanders(sets.MI,sets.SIGMA,sets.I,iter,precision,t,lambda);
   text=sprintf(...      'Step t=%d\nLine [%f , %f]*x=%f\nNi = %f, (1-Lambda)/Lambda = %f, Max error = %f%%',...
      nt,nalpha(1),nalpha(2),ntheta,ni,(1-lambda)/lambda,maxerr*100);
case 5
   % General 2
   [nalpha,ntheta,solution,minr,nt,maxerr]=...
   ganders2(sets.MI,sets.SIGMA,sets.I,iter,precision,t,alpha,theta);
   text=sprintf('Step t=%d\nLine [%f , %f]*x=%f\nMinimal r = %.15f, Max error = %f%%',...
      nt,nalpha(1),nalpha(2),ntheta,minr,100*maxerr);
case 3
   % Gradient method
   [nalpha,ntheta,solution,minr,nt,maxerr]=...
      gganders(sets.MI,sets.SIGMA,sets.I,iter,precision,t,alpha,theta);
   text=sprintf('Step t=%d\nLine [%f , %f]*x=%f\nMinimal r = %.15f, Max error = %f%%',...
      nt,nalpha(1),nalpha(2),ntheta,minr,maxerr*100);
end

if solution==-1,
    text=sprintf('Solution does not exist.\n');
    play=-1;
    return;
elseif solution==1,
   text=strvcat(text,sprintf('Solution was found in %d step(s)',nt));
   play=0;
else
   play=1;
end

% store new values
h.line.t = nt;
h.line.alpha = nalpha;
h.line.alpha1 = alpha1;
h.line.alpha2 = alpha2;
h.line.lambda = lambda;
h.line.theta = ntheta;

return

⌨️ 快捷键说明

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