1.软件版本
MATLAB2017b
2.算法理论
整体算法流程图:
步骤一、网络节点的状态初始化,随机产生不同的网络节点,以及对各个网络节点赋值初始的状态信息。
步骤二、对每个需要检测的节点,初始该节点的相邻节点的集合N,并初始化每个节点的是否故障矩阵F=1,如果为1,说明有故障,如果为0,说明没有故障;然后初始阈值参数。
步骤三、对于每一个节点进行异常检测,检测主要通过在节点和其邻节点之间具有空间的相似性,即网络中无故障的相邻传感器节点之间具有相同或相似的测量值。
假设任意的两个节点的状态信息为x和y;
如果该两个节点之间的间距在节点最长通信距离范围之内,那么进入检测状态,
从该公式中可知,如果两个节点相似,那么返回0,否则返回1。
然后统计任意一个节点,其相邻节点相似的个数,即Ni
同理,对于时间相似性,就是对于任意一个节点,不同时刻(通过不同循环次数实现)的信息状态为:和,做相似的处理,即
这个优化目标函数的含义是,如果每次迭代更新后的检测器所检测得到故障和真实的故障检测结果的差,如果这个差接近0,那么说明此事检测器越来越接近成熟检测器,否则进一步迭代。
步骤六、筛选与抗原亲和度较好的个体进行克隆复制、变异,替代亲和度较低的个体;
步骤七、对亲和度较高的生成成熟的检测器;
步骤八、在每次迭代过程中,需要对节点的免疫信息进行更新。
每次迭代完成之后,对所有节点免疫状态进行更新。
步骤九、利用成熟的检测器发现外来的新的非自体(抗原),跳转到步骤6;否则,判定已知故障,则算法结束。
3.部分matlab程序
clc;
clear;
close all;
warning off;
addpath 'func\'
%n个传感器节点
Note = 512;
%网络场景的范围
SCALE = 512;
%%节点距离在30m相连
Dis_R = 37;
epss = 0.008;
%故障概率
%
Per = [0.05,0.10,0.15,0.20,0.25,0.30];
%免疫算法的参数初始值
Iter = 6;%免疫迭代次数
%抗体个数和编码长度
Pop = 12;
Lc = 32;
n = 10;
pm = 0.1;
fat = 0.2;
%初始
P = func_init(Pop,Lc);
xmin = 0;
xmax = 1;
for ki = 1:length(Per);
ki
for jj = 1:50
jj
%对网络的各种权值信息进行随机的赋值
X = SCALE*rand(1,Note);
Y = SCALE*rand(1,Note);
xk_ij = zeros(Note,Note);
%初始状态均为0,表示没有故障
F = zeros(1,Note);
F_real = ones(1,Note);
%定义节点状态测量值
State = zeros(1,Note);
State0= zeros(1,Note);
for j1 = 1:Note
for j2 = 1:Note
dist = sqrt((X(j1)-X(j2))^2 + (Y(j1)-Y(j2))^2);
if dist <= Dis_R & j1~=j2
xk_ij(j1,j2) = 1;
end
end
%模拟4种不同类型的故障
if j1 <= Note/4
if rand < Per(ki)
State(j1) = 2+0.05*randn(1);
State0(j1) = 2+0.05*randn(1);
F_real(j1) = 1;
else
State(j1) = 2+0.0001*randn(1);
State0(j1)= 2+0.0001*randn(1);
F_real(j1)= 0;
end
end
if j1 <= 2*Note/4 & j1 > Note/4
if rand < Per(ki)
State(j1) = 2+0.1*randn(1);
State0(j1) = 2+0.1*randn(1);
F_real(j1) = 2;
else
State(j1) = 2+0.0001*randn(1);
State0(j1)= 2+0.0001*randn(1);
F_real(j1)= 0;
end
end
if j1 <= 3*Note/4 & j1 > 2*Note/4
if rand < Per(ki)
State(j1) = 2+0.2*randn(1);
State0(j1) = 2+0.2*randn(1);
F_real(j1) = 2;
else
State(j1) = 2+0.0001*randn(1);
State0(j1)= 2+0.0001*randn(1);
F_real(j1)= 0;
end
end
if j1 <= 4*Note/4 & j1 > 3*Note/4
if rand < Per(ki)
State(j1) = 2+0.3*randn(1);
State0(j1) = 2+0.3*randn(1);
F_real(j1) = 2;
else
State(j1) = 2+0.0001*randn(1);
State0(j1)= 2+0.0001*randn(1);
F_real(j1)= 0;
end
end
end
%计算网络指标
[Cc,Cc_avg] = func_Cluster_Coeff(xk_ij);
% disp(['聚类系数为:',num2str(Cc_avg)]);
[Dds,Dds_avg,M,P_Dds]= func_Degree_Distribution(xk_ij);
% disp(['平均度为:',num2str(Dds_avg)]);
%状态cij计算
c_ij = zeros(Note,Note);
for j1 = 1:Note
for j2 = 1:Note
if xk_ij(j1,j2) == 1;
if abs(State(j1)-State(j2))<=epss
c_ij(j1,j2) = 0;
else
c_ij(j1,j2) = 1;
end
else
c_ij(j1,j2) = 0;
end
end
end
%计算相似节点的相似程度,变量名字为ci
ci = zeros(1,Note);
for j1 = 1:Note
tmp=0;
for j2 = 1:Note
if xk_ij(j1,j2) == 1;
if c_ij(j1,j2) == 0;
tmp=tmp+1;
end
end
end
ci(j1) = tmp;
end
% %根据门限获得故障点
% for j1 = 1:Note
% if ci(j1)<5-theta1
% F(j1)=1;
% end
% end
%基于时空特性的节点免疫算法
fix_obj = [];
fit_obj = [];
gens = 0;
%开始迭代
while gens<Iter
gens
gens = gens+1;
x = func_decode(P(:,1:Lc),xmin,xmax,Lc);
fit = func_obj(State0,F_real,x);
T = [];
[a,ind] = sort(fit);
valx = x(ind(end-n+1:end));
fx = a(end-n+1:end);
fix_obj = [fix_obj fx(end)];
[T,pcs] = reprod(n,fat,Pop,ind,P,T);
T = func_Hypermut(T,Lc,pm,xmax,xmin);
T(pcs,:)= P(fliplr(ind(end-n+1:end)),:);
x = func_decode(T(:,1:Lc),xmin,xmax,Lc);
fit = func_obj(State0,F_real,x);
pcs =[0 pcs];
fit_obj = [fit_obj mean(fit)];
for i=1:n
[out(i),bcs(i)] = max(fit(pcs(i)+1:pcs(i+1)));
bcs(i) = bcs(i)+pcs(i);
end
P(fliplr(ind(end-n+1:end)),:) = T(bcs,:);
%规范化操作
nodeIPV = State0/(max(State0));
%更新节点运算
for jk = 1:length(State0)
State0(jk) = State0(jk) + mean(mean(x)*nodeIPV)/Note/Note;
end
%停止迭代条件
if mod(gens,2) == 0
nodeIPV_1 =nodeIPV;
else
nodeIPV_2 =nodeIPV;
end
if gens >= 2
if sum(nodeIPV_1 - nodeIPV_2) < 0.0005
break;
end
end
end
[aa,bb]=max(fit);
%获得优化后的分类器
Net_ini = newgrnn(State0,F_real,x(bb));
F = round(Net_ini(State));
%计算正确率
L(jj) = length(find(F==F_real))/length(F_real);
%计算虚警
CNT = 0;
for i = 1:length(F)
if F_real(i)==0 & F(i)==1
CNT = CNT + 1;
end
end
LL(jj) = CNT/length(F);
end
L2(ki)= mean(L);
LL2(ki)= mean(LL);
end
figure;
plot(Per,L2,'b-o');
xlabel('节点故障率');
ylabel('故障诊断精度');
axis([0.05,0.3,0.5,1]);
figure;
plot(Per,LL2,'b-o');
xlabel('节点故障率');
ylabel('虚警率');
axis([0.05,0.3,0,1]);
save R0.mat Per L2 LL2
figure;
bar(ci);
xlabel('节点数');
ylabel('c_i');
figure;
subplot(211);
bar([1:Note],Dds);
xlabel('节点编号');
ylabel('节点的度');
subplot(212);
bar([0:M],P_Dds,'r');
xlabel('节点的度');
ylabel('节点度的概率');
figure;
plot(X,Y,'bo');
hold on
hold on
for j1 = 1:Note
for j2 = 1:Note
if xk_ij(j1,j2) == 1;
plot([X(j1),X(j2)],[Y(j1),Y(j2)],'r','linewidth',1); hold on
end
end
end
%显示故障点位置
figure;
plot(X,Y,'b.');
hold on
%真实的点
indx1 = find(F_real==1);
plot(X(indx1),Y(indx1),'rs');
%检测到的点
hold on
indx1 = find(F==1);
plot(X(indx1),Y(indx1),'k*');
legend('网络节点','真实故障点','检测到的故障点');
4.仿真结论
A05-47