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

📄 da_olr.m

📁 王小平《遗传算法——理论、应用与软件实现》随书光盘
💻 M
字号:
%
% da_olr
%
% On-line predictor
%
da_front;
set(w1,'NumberTitle','off','Name','OnLine model running...');
drawnow;

stop=0;

%
% Draw the prediction axes
%
if ol_pred_active == 1
	ax1=axes(...
		'Units','pixels',...
		'Position',[60 250 500 150],...
		'Box','on',...
		'Color',[0 0 0],...
		'Visible','on');
end

if ol_pca_active == 1
	ax2=axes(...
		'units','pixels',...
		'position',[60 50 150 150],...
		'box','on',...
		'color',[0 0 0],...
		'visible','on');
end
if ol_pca_active == 1 & ol_pred_active == 0
	set(ax2,'position',[60 50 330 330]);
end

%
% Print a title
%
text1=da_text(w1,200,410,200,15,'Graph of predicted output:',[1 1 1],[0 0 1]);
if ol_pred_active == 0 & ol_pca_active == 1
	set(text1,'string','Principal component plot');
end


%
% Status window
%
box1=uicontrol(w1,...
	'style','frame',...
	'position',[450 10 140 70],...
	'foregroundcolor',[1 1 1],...
	'backgroundcolor',[0 0 1]);

text4=da_text(w1,455,60,70,15,'Status:',[1 1 1],[0 0 1]);
text5=da_text(w1,455,40,120,15,'Waiting for data...',[1 1 1],[0 0 1]);

if ol_pred_active == 1
	%
	% Box that holds the value of the current estimate
	%
	box2=uicontrol(w1,...
		'style','frame',...
		'position',[450 90 140 55],...
		'backgroundcolor',[1 0 0],...
		'foregroundcolor',[1 1 1]);
	
	text6=da_text(w1,455,125,100,15,'Last prediction:',[1 1 1],[1 0 0]);
	text7=da_text(w1,460,100,120,15,'',[1 1 1],[1 0 0]);
	
	%
	% Box to diplay model type
	%
	box3=uicontrol(w1,...
		'style','frame',...
		'position',[450 155 140 40],...
		'backgroundcolor',[0 0 1],...
		'foregroundcolor',[1 1 1]);
	%
	% Model information
	%
	text2=da_text(w1,452,170,130,15,'',[1 1 1],[0 0 1]);
	
	if ol_mod_type == 1
		set(text2,'string','Linear steady-state');
	elseif ol_mod_type == 2
		set(text2,'string','Linear dynamic');
	elseif ol_mod_type == 3
		set(text2,'string','NL dynamic');
	else
		set(text2,'string','NL steady-state');
	end
end	
%
% Create a blank file
%
if ol_pred_active == 1
	[D L]=size(ol_coefs);
	temp=zeros(1,D);
	cd d:
	save transfer.dat temp -ascii
elseif ol_pred_active == 0 & ol_pca_active == 1
	[D L]=size(ol_meanx);
	temp=zeros(1,L);
	cd d:
	save transfer.dat temp -ascii
end



old=[];
dat=[];
pcdat=[];
count=1;
timed_out=0;
pcxmin=-1;
pcxmax=1;
pcymin=-1;
pcymax=1;

%
% Main model loop
%

while stop ~= 1
	if timed_out~=1
		set(text5,'string','Waiting for data...');
		drawnow;
	end
	[out s]=read('transfer.dat',old,ol_read_wait*4);
	set(text5,'string','Processing...');
	timed_out=0;
	drawnow;
	if s ~= -1
	if ol_pred_active == 1
		%
		% If there is new data available
		%
		
		%
		% Set up the x-data
		%
		x=out;


		%
		% Scale the new data set
		%
		if ol_scl_opt == 1
			%
			% Perform variance scaling
			%
			x=x-ol_meanx;
			x=x./ol_stdx;

		else
			%
			% Scale 0----> 1
			%
			x=(x-ol_minx)./(ol_maxx-ol_minx);
		end

		if count == 1
			old=out;
			count=-1;
			xold=x;
		end

		%
		% Call the relevant model routine
		%
		if ol_mod_type == 1
			%
			% Steady state linear
			%
			ypred=da_olt1(x,ol_coefs);
		elseif ol_mod_type == 2
			%
			% Linear dynamic
			%
			[ypred x]=da_olt2(x,xold,ol_coefs,ol_filt);
		elseif ol_mod_type == 3
			%
			% Non-Linear dynamic
			%
			[ypred x]=da_olt3(x,xold,ol_coefs,ol_filt,ol_poly);
		else
			%
			% Non-Linear static
			%
			ypred=da_olt4(x,ol_coefs,ol_poly);
		end
		drawnow;
		%
		% Now re-scale the output
		%
		if ol_scl_opt == 1
			ypred=ypred*ol_sdy;
			ypred=ypred+ol_meany;
		else
			ypred=(ypred*(ol_maxy-ol_miny))+ol_miny;
		end

		%
		% Display the prediction
		%
		set(text7,'string',num2str(ypred));

		%
		% Put the prediction into the variable dat
		%
		[D L]=size(dat);
		if D < ol_plot_store
			dat=[dat ; ypred];
		else
			dat=[dat(2:D) ; ypred];
		end

		%
		% Update the graph scales
		%
		m=min(dat);
		if m < ol_plot_ymin
			ol_plot_ymin=m*0.99;
		end
		m=max(dat);
		if m > ol_plot_ymax
			ol_plot_ymax=m*1.01;
		end
		drawnow;
		axes(ax1);
		plot(dat);
		set(ax1,'ylim',[ol_plot_ymin ol_plot_ymax]);
		set(ax1,'xlim',[0 ol_plot_store]);
		xlabel('Sample number');
		ylabel('Predicted output');
		drawnow;
		old=out;
		xold=x;
	end
	%
	% Put on-line PCA stuff here
	%
	if ol_pca_active == 1
		%
		% If there is new data available
		%
		
		%
		% Set up the x-data
		%
		x=out;
		[D L]=size(x);
		if L ~= 0
			%
			% Scale the new data set
			%
			x=x-ol_meanx;
			x=x./ol_stdx;	
			
			%
			% Calculate the current pc data
			% 
			pcd=x*ol_pca_coefs;

			xpc=pcd(:,ol_xaxis_pc);	
			ypc=pcd(:,ol_yaxis_pc);

			%
			% update pc buffer
			%
			[D L]=size(pcdat);
			if D < ol_pc_buffer
				pcdat=[pcdat ; [xpc ypc]];
			else
				pcdat=[pcdat(2:D,:) ; [xpc ypc]];
			end
			
			%
			% Determine scaling
			%
			m=min(pcdat(:,1));
			if m<pcxmin
				pcxmin=m-abs(0.2*m);
			end
			m=max(pcdat(:,1));
			if m>pcxmax
				pcxmax=m+abs(0.2*m);
			end
			m=min(pcdat(:,2));
			if m<pcymin
				pcymin=m-abs(0.2*m);
			end
			m=max(pcdat(:,2));
			if m>pcymax
				pcymax=m+abs(0.2*m);
			end
				
			axes(ax2);
			hold on;
			cla;
			plot(pcdat(:,1),pcdat(:,2),'b');
			plot(pcdat(:,1),pcdat(:,2),'r+');
			plot(xpc,ypc,'o');
			xlabel(['principal comp: ' num2str(ol_xaxis_pc)]);
			ylabel(['principal comp: ' num2str(ol_yaxis_pc)]);
			set(ax2,'xlim',[pcxmin pcxmax],'ylim',[pcymin pcymax]);
			hold off;
			drawnow;
			old=out;
		end
	end

	else
		set(text5,'string','TIMED OUT!!');
		timed_out=1;
	end

end


⌨️ 快捷键说明

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