📄 遗传rbf网络.m
字号:
%种群个体数目 20 nind 变量数 1 nvar
echo off
clear
clc
NIND = 20 %个体数目
NVAR = 1; %变量维数
Range = [0;5]; %变量范围
GGAP = .8; % Generation gap, how many new individuals are created。代沟
INSR = .9; % Insertion rate, how many of the offspring are inserted。插入率
XOVR = 1; % Crossover rate 交叉率
SP = 2; % Selective Pressure
SUBPOP = 12 % Number of subpopulation 子种群数
MUTR = 1; % Mutation rate; only a factor; 变异概率
MIGR = 0.2; % Migration rate between subpopulations 迁移率
MIGGEN = 20; % Number of generations between migration (isolation time) 在子种群与迁移之间20代
MAXGEN = 500 ; % Maximal number of generations 最大遗传代数
TERMEXACT = 1e-4; % Value for termination if minimum reached
SEL_F = 'sus'; % Name of selection function
XOV_F = 'recdis'; % Name of recombination function for individuals
MUT_F = 'mutbga'; % Name of mutation function
FieldDD=rep(Range,[1,NVAR]) % 每个变量的范围
OBJ_F = 'rbfrmse'; % Name of function for objective values
% Create real population
Chrom = crtrp(SUBPOP*NIND,FieldDD);
trace=zeros(MAXGEN,2);
% reset count variables
gen = 0;
% Calculate objective function for population
ObjV = feval(OBJ_F,Chrom);
% count number of objective function evaluations
% Iterate subpopulation till termination or MAXGEN
while ((gen < MAXGEN) ),
% Fitness assignement to whole population
FitnV = ranking(ObjV,[2 0],SUBPOP);
% Select individuals from population
SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP);
% Recombine selected individuals
SelCh=recombin(XOV_F, SelCh, XOVR, SUBPOP);
% Mutate offspring
SelCh=mutate(MUT_F, SelCh, FieldDD, [MUTR], SUBPOP);
% Calculate objective function for offsprings
ObjVOff = feval(OBJ_F,SelCh);
% Insert best offspring in population replacing worst parents
[Chrom, ObjV] = reins(Chrom, SelCh, SUBPOP, [1 INSR], ObjV, ObjVOff);
gen=gen+1;
[trace(gen,1),I]=min(ObjV);
trace(gen,2)=mean(ObjV);
% Plot some results, rename title of figure for graphic output
if ((rem(gen,MAXGEN) == 0) ),
[Chrom, ObjV] = migrate(Chrom, SUBPOP, [MIGR, 1, 1], ObjV);
end
end
[Y,I]=min(ObjV);
figure(1);plot(Chrom(I,:));
hold on; grid;
plot(Chrom(I,:),'bo');
% figure(2);plot(-trace(:,1));
% hold on;
% plot(-trace(:,2),'-.');
% legend(
function ObjVal=rbfrmse(Chrom)
load DATA01
load REALMFR1
[NIND,NVAR] = size(Chrom);
ObjVal=zeros(NIND,1);
Set=[a210a' f130a' f141a' t210' p210' l210' f211' f213' f217' realmfr1];
Gset=Set(77:85,:);
Set(77:85,:)=[];
VerifySet=Set(1:4:76,:);%验证集 19个样本
Set(1:4:76,:)=[];
TrainSet=Set;%训练集 57个样本
TrainX=TrainSet(:,1:9);
TrainY=TrainSet(:,10);
VerX=VerifySet(:,1:9);
VerY=VerifySet(:,10);
%训练
[pn meanp stdp]=prestd(TrainX'); %数据标准化
[pca,transmat]=prepca(pn,0.05); %主元分析,提取95%的方差
%数据标准化
[R C]=size(VerX);
for i=1:R
pn2(:,i) = (VerX(i,:)'-meanp)./stdp;
end
pca2=transmat*pn2; %验证数据的主元提取
for i=1:NIND
net=newrb(pca,TrainY',0.01,Chrom(i)); %训练神经网络
V=sim(net,pca2); %验证数据的输出
ObjVal(i)=1/rmse(V-VerY');
end
function perf=rmse(e)
%
% calculate the root mean squared error of the given errors
%
% 'perf = rmse(E);'
%
% see also:
% mae, linf, trimmedmse
%
% Copyright (c) 2002, KULeuven-ESAT-SCD, License & help @ http://www.esat.kuleuven.ac.be/sista/lssvmlab
perf = sqrt(sum(sum(e.^2)) / prod(size(e)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -