📄 da_ga3.m
字号:
%
% da_ga3.m
%
% Non-Linear Dynamic GA model
%
%
% Get the Parameters before clearing the screen
%
global stop;
members=ceil(get(sl1,'value'));
members=round(members/2)*2;
pmutate=get(sl2,'value');
pcross=get(sl3,'value');
maxgen=ceil(get(sl4,'value'));
presults=str2num(get(text8,'string'));
d1=str2num(get(text11,'string'));
d2=str2num(get(text13,'string'));
d3=str2num(get(text15,'string'));
d1=[d1 d2 d3]
d4=str2num(get(text17,'string'));
d5=str2num(get(text19,'string'));
d6=str2num(get(text21,'string'));
d2=[d4 d5 d6];
%
% Clear the screen
%
da_front;
drawnow;
set(w1,'NumberTitle','off','Name','Building Dynamic Non-Linear GA Model');
%
% Plot a couple of axes
%
ax1=axes(...
'Units','pixels',...
'Position',[60 270 500 130],...
'Box','on',...
'Color',[0 0 0],...
'Visible','on');
ax2=axes(...
'Units','pixels',...
'Position',[60 125 230 100],...
'Box','on',...
'Color',[0 0 0],...
'Visible','on');
box1=uicontrol(w1,...
'style','frame',...
'position',[400 20 200 30],...
'backgroundcolor',[0 0 0],...
'foregroundcolor',[1 1 1]);
box2=uicontrol(w1,...
'style','frame',...
'position',[410 25 1 20],...
'foregroundcolor',[0 0 1],...
'backgroundcolor',[0 0 1]);
box3=uicontrol(w1,...
'style','frame',...
'position',[400 60 200 30],...
'backgroundcolor',[0 0 0],...
'foregroundcolor',[1 1 1]);
box4=uicontrol(w1,...
'style','frame',...
'position',[410 65 1 20],...
'foregroundcolor',[0 0 1],...
'backgroundcolor',[0 0 1]);
text1=da_text(w1,110,28,280,15,'Percentage of current generation complete:',[1 1 1],[1 0 0]);
text2=da_text(w1,110,63,280,15,'Percentage of current generation decoded :',[1 1 1],[1 0 0]);
text3=da_text(w1,440,220,100,15,'0 - Generations',[1 1 1],[0 0 0]);
text4=da_text(w1,400,95,20,15,'0',[1 1 1],[0 0 0]);
text5=da_text(w1,560,95,40,15,'100 %',[1 1 1],[0 0 0]);
text6=da_text(w1,210,410,200,15,'GA RUNNING. Press ^C to stop',[1 1 1],[1 0 0]);
%
% Save model button
%
but1=uicontrol(w1,...
'style','push',...
'position',[400 150 200 20],...
'string','Save model description',...
'callback','da_svmod;');
but2=uicontrol(w1,...
'style','push',...
'position',[400 120 200 20],...
'string','Save model for OnLine use',...
'callback','svlin('''',include_var,sdx,mx,sdy,my,c,p,f);');
drawnow;
%
% Set up the x and y variables
%
if sum(output_var) > 1
[D L]=size(output_var);
for i = 1 : L
if output_var(i) == 1
y=data(:,i);
break;
end
end
else
y=data(:,output_var);
end
x=pcincl(data,include_var,output_var);
[x mx sdx]=mceng2(x);
[y my sdy]=mceng2(y);
%
% Determine the population size
%
d=[];
d3=[];
d4=[];
[D L]=size(x);
for i = 1:L
d3(i,:)=d1;
end
for i = 1:L
d4(i,:)=d2;
end
d=[d3;d4];
[o d b]=decode(0,d,0);
popsize=sum(b);
%
% Create the initial random population
%
chrom=newpop(members,popsize);
%
% Go for a number of generations
%
results=[0 0 0];
count=0;
c=0;
p=0;
f=0;
for i = 1:maxgen
%
% Cross and mutate
%
[D L]=size(chrom);
for j = 1 : 2 :members
chrom(j,:)=mutate(chrom(j,:),0.02);
chrom(j+1,:)=mutate(chrom(j+1,:),0.02);
[chrom(j,:) chrom(j+1,:)]=crover(chrom(j,:),chrom(j+1,:),0.02);
end
%
% Select the best
%
[chrom coefs pin]=select3(chrom,d,x,y,box2,box4);
count=count+1;
set(text3,'string',[num2str(i) ' - Generations']);
set(box2,'position',[410 25 1 20]);
set(box4,'position',[410 65 1 20]);
if count >= presults
%
% Decode this popultaion and plot results
%
results(i,:)=[min(pin) mean(pin) max(pin)];
[D L]=size(coefs);
[D1 L1]=size(x);
[c,in]=min(pin);
p=coefs(in,1:L1);
f=coefs(in,L1+1:L);
x2=pol2(x,p);
x2=fil(x2,f);
c=pinv(x2'*x2)*x2'*y;
yest=x2*c;
[c2,in]=max(pin);
axes(ax1);
plot([resclg(y,my,sdy) resclg(yest,my,sdy)]);
Xlabel('Sample number');
Ylabel('Actual & Predicted');
axes(ax2);
plot(results(:,1));
Xlabel('Generation number');
Ylabel('Error');
drawnow;
if stop == 1
break;
end
count=0;
else
results(i,:)=[min(pin) mean(pin) max(pin)];
axes(ax2)
plot(results(:,1));
Xlabel('Generation number');
Ylabel('Error');
drawnow;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -