📄 dynamic_main.asv
字号:
% the main program of the dynamic training
% -------------------------------------
clear('all');
clc;
load('matlabhia31.mat');
% record how many times have the parameters been changed
dynamic_counter=1;
max_change_time=100;
per_evo_time=20;
result3=result2;
% -------------------------------------
proo=zeros(per_evo_time+2,max_change_time);
% main loop
% -------------------------------------
while dynamic_counter<=max_change_time
% caculate the importance of each neuron
importance=judge(result2,t_x,t_y);
% find out the global-fix neurons
ss=20;
global_neuron_idx=find(importance>ss);
global_neuron_num=length(global_neuron_idx);
while(global_neuron_num<10||global_neuron_num>20)
if(global_neuron_num<10)
ss=ss-1;
end
if(global_neuron_num>20)
ss=ss+1;
end
global_neuron_idx=find(importance>ss);
global_neuron_num=length(global_neuron_idx);
end
% the number of subtle-fix neurons
subtle_neuron_num=size(result2,1)-global_neuron_num;
subtle_neuron_idx=1:size(result2,1);
% remove the global neurons from the individual
subtle_neuron_idx(global_neuron_idx)=[];
% generate the dynamic training sets
[t_x,t_y,te_x,te_y]=drosen;
winner_fitness=fitness(result2,t_x,t_y);
pro(per_evo_time+1,dynamic_counter)=winner_fitness;
% calculate the fitness value of the static individual
winner_fitness=fitness(result3,t_x,t_y);
proo(per_evo_time+1,dynamic_counter)=winner_fitness;
fprintf('dynamic counter: %d\ncurrent fitness: %f\n',dynamic_counter,winner_fitness);
% re-modify if the error exceed the tolerance
% ---------------------------------
if winner_fitness<10000
tic;
% find the samples that need further modification
ss=0.0001;
err_idx=find_error(result2,t_x,t_y,ss);
while(length(err_idx)>40)
ss=ss+0.0001;
err_idx=find_error(result2,t_x,t_y,ss);
end
err_x=t_x(:,err_idx);
if (subtle_neuron_num~=1 & subtle_neuron_num~=0 & ~isempty(err_x))
fprintf('subtle-neuron-num: %d\n',subtle_neuron_num);
% create new neurons
[index,center]=kmeans(err_x',subtle_neuron_num,'distance','sqEuclidean');
else
center=result2(subtle_neuron_idx,2:size(result2,2)-1);
end
% generate new population
% ------------------------------
% the winner is preserved
ini_pop=zeros(size(result2,1),size(result2,2),40);
ini_pop(:,1,:)=1;
for i=1:30
ini_pop(:,:,i)=result2;
end
% add the subtle-fix neurons to the winner network
ini_pop(subtle_neuron_idx,2:size(result2,2)-1,3)=center;
ini_pop(subtle_neuron_idx,size(result2,2),3)=result2(subtle_neuron_idx,size(result2,2));
for i=2:2:18
ini_pop(subtle_neuron_idx,2:size(result2,2),i)=ini_pop(subtle_neuron_idx,2:4,1)+rand(subtle_neuron_num,3,1);
end
for i=20:2:38
ini_pop(subtle_neuron_idx,2:size(result2,2),i)=ini_pop(subtle_neuron_idx,2:4,3)+rand(subtle_neuron_num,3,1);
end
ini_pop(:,2:size(result2,2)-1,29:2:37)=-2+4*rand(30,2,5);
ini_pop(:,4,29:2:37)=1+7*rand(30,1,5);
ini_pop(:,2:size(result2,2)-1,39:40)=-2+4*rand(30,2,2);
ini_pop(:,4,39:40)=1+7*rand(30,1,2);
% -----------------------------
% evolve again
[new_vault,pro2,result2]=hia1(ini_pop,t_x,t_y,vault,per_evo_time,inf);
% refresh the vault
vault=new_vault;
% ---------------------------------
timer=toc;
fprintf('evolution time: %f\n',timer);
pro(1:size(pro2,2),dynamic_counter)=pro2';
pro(per_evo_time+2,dynamic_counter)=fitness(result2,te_x,te_y);
result2=result3;
init=zeros(size(result3,1),size(result3,2),40);
init(:,1,:)=1;
for i=1:30
init(:,:,i)=result2;
end
for i=2:2:40
init(:,2:size(result2,2),i)=init(:,2:4,1)+0*rand(size(init,1),size(result2,2)-1,1);
end
[pro2,result3]=sea(init,t_x,t_y,20);
proo(1:size(pro2,2),dynamic_counter)=pro2';
proo(per_evo_time+2,dynamic_counter)=fitness(result3,te_x,te_y);
dynamic_counter=dynamic_counter+1;
end
end
save('dynamic_result.mat');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -