内容:
clc;
clear
close all
%免疫遗传算法优化来训练RBF网络权值
%G为进化代数,Size为种群规模,CodeL为参数的二进制编码长度
G = 300;
Size = 100;
CodeL = 10;
%确定每个参数的最大最小值
for i = 1:3
MinX(i) = 0.1*ones(1);
MaxX(i) = 3*ones(1);
end
for i = 4:1:9
MinX(i) = -3*ones(1);
MaxX(i) = 3*ones(1);
end
%%
for i = 10:1:12
MinX(i) = -ones(1);
MaxX(i) = ones(1);
end
%初始化种群
E = round(rand(Size,12*CodeL));
BsJ = 0;
%进化开始
for kg = 1:1:G
time(kg) = kg ;
for s = 1:1:Size
m = E(s,:); %取出其中个体
%把二进制表示的参数转化为实数
for j = 1:1:12
y(j) = 0;
mj = m((j-1)*CodeL + 1:1:j*CodeL);
for i = 1:1:CodeL
y(j) = y(j) + mj(i)*2^(i - 1);
end
f(s,j) = (MaxX(j) - MinX(j))*y(j)/1023 + MinX(j);
end
p = f(s,:);
[p,BsJ] = RBF(p,BsJ);
BsJi(s) = BsJ; %记录每个个体的总误差
end
%对误差排序,求出最好误差
[OderJi,IndexJi] = sort(BsJi);
BestJ(kg) = OderJi(1);
BJ = BestJ(kg);
Ji = BsJi + 1e-10;
%对误差取倒数,求出适应度值
fi = 1./Ji; %适应度值
[Oderfi,Indexfi] = sort(fi);
Bestfi = Oderfi(Size); %最佳适应度值
BestS = E(Indexfi(Size),:);