📄 demo_anderson.m
字号:
h=get(hfigure,'UserData'); % change path to directory%% wres=what('anderson');%% cd(wres.path); [name,path]=uigetfile('*.mat','Open file'); if name~=0, file.pathname=strcat(path,name); file.path=path; file.name=name;% if checkdat(file.pathname,DATA_IDENT,2,[0 0])==1, if check2dgauss(file.pathname), set(h.btload,'UserData',file); demo_anderson('loadsets',hfigure); demo_anderson('reset',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end endcase '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'),:)); demo_anderson('loadsets',hfigure); demo_anderson('reset',hfigure); endcase '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); sets.MI = sets.Mean; sets.SIGMA = reshape(sets.Cov,2,2*size(sets.Mean,2)); sets.I = sets.y; sets.K = zeros(1,max(sets.y)); sets.N = size(sets.Mean,1); for i=1:max(sets.y), sets.K(i) = length(find(sets.y==i)); end % 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]; N = 2; 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 or Create data button.\n', ...
'Load sample data from ../toolboxroot/data/anderson_task/' ] ); 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 or Create data button.\n', ...
'Load sample data from ../toolboxroot/data/anderson_task/' ] ); set(h.console,'String',text ); endcase '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); ppatterns(sets.MI,sets.I); % comment window text consoletext=sprintf('Press Step button or Play button.\n'); 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.\n' ...% 'Press Load button or Create data button.\n']);% consoletext=sprintf('No data loaded.\nPress Load data button.\n'); consoletext=sprintf(['No data loaded.\nPress Load data or Create data button.\n', ...
'Load sample data from ../toolboxroot/data/anderson_task/' ] ); 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 dialogprecision=str2num(get(h.edprec,'String'));iter=max([1,fix(str2num(get(h.editer,'String')))]);% get parameterst=h.line.t;alpha=h.line.alpha;alpha1=h.line.alpha1;alpha2=h.line.alpha2;lambda=h.line.lambda;theta=h.line.theta;% perform algorithmswitch get(h.pualgo,'Value')case 4 % General solution distr = sets; options.eps = precision; options.tmax = t+iter; init_model.W=alpha; init_model.b=-theta; init_model.t = t; % [nalpha,ntheta,solution,minr,nt,maxerr]=...% ganders(sets.MI,sets.SIGMA,sets.I,iter,precision,t,alpha,theta); model = ganders( distr, options, init_model); nalpha = model.W; ntheta = -model.b; solution = model.exitflag; minr = model.r; nt = model.t; maxerr = model.err; text=sprintf(['Interation(s) t=%d\nLinear rule q(x)=sgn([%f , %f]*x %+f)\n'... 'Minimal r = %.8f\nClassification error = %.4f%%'],... nt,nalpha(1),nalpha(2),-ntheta,minr,maxerr*100);case 1 distr = sets; options.err = precision/100; options.tmax = t+iter; init_model.W1=alpha1; init_model.W2=alpha2; init_model.t = t; % e-Optimal solution model = eanders( distr, options, init_model ); nalpha = model.W; ntheta = -model.b; alpha1 = model.W1; alpha2 = model.W2; solution = model.exitflag; minr = model.r; nt = model.t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -