?? heed.m
字號:
if Node.IsClusterHeads(i) == TENTATIVE_CH % & Node.tent_CH(curentnbr)==TOS_LOCAL_ADDRESS
dist =Tr.^2+Tr.^2; % ((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2); % the distance.^2
EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
Node.EnNode(i)=Node.EnNode(i)-EntranPCH;
if Node.EnNode(i) <= 0
Node.StateNode(i)=0;
Node.Isstop(i)=0;
Node.EnNode(i) =0;
end
for j=1:Node.NumNbr(i)
curentnbr = Node.Nbr(i,j);
EnRecP=EnRec(Elec,Kbit);
Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EnRecP;
Node.ListtentCH(curentnbr,i) = i;
Node.ListtentCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
if (Node.ListtentCH(curentnbr,i)==0)
Node.n_tentCH(curentnbr)=Node.n_tentCH(curentnbr)+1;
end
if Node.EnNode(curentnbr) > 0
if (Node.ListtentCH(curentnbr,i)==0)
Node.n_tentCH(curentnbr)=Node.n_tentCH(curentnbr)+1;
end
Node.ListtentCH(curentnbr,i) = i;
Node.ListtentCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
% if Node.tent_CH(curentnbr)~=TOS_LOCAL_ADDRESS &(Node.tent_CH(curentnbr)==NON_CH | Node.tent_CH_Cost(i)< Node.tent_CH_Cost(curentnbr) | ((Node.tent_CH_Cost(i)== Node.tent_CH_Cost(curentnbr) ) & i < Node.tent_CH(curentnbr)))
if(Node.tent_CH(curentnbr)==NON_CH | Node.tent_CH_Cost(i)< Node.tent_CH_Cost(curentnbr) | ((Node.tent_CH_Cost(i)== Node.tent_CH_Cost(curentnbr) ) & i < Node.tent_CH(curentnbr)))
Node.tent_CH_Cost(curentnbr)=Node.tent_CH_Cost(i);
% if Node.tent_CH(curentnbr)==TOS_LOCAL_ADDRESS
% if Node.IsClusterHeads(curentnbr) ==TENTATIVE_CH
% Node.IsClusterHeads(curentnbr) = NON_CH;
% end
% dist =Tr.^2+Tr.^2; % ((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2); % the distance.^2
% EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
% Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EntranPCH;
% if Node.EnNode(curentnbr) <= 0
% Node.StateNode(curentnbr)=0;
% Node.Isstop(curentnbr)=0;
% Node.EnNode(curentnbr) =0;
% else
% for k=1:Node.NumNbr(curentnbr)
% tempcurentnbr = Node.Nbr(curentnbr,k);
% EnRecP=EnRec(Elec,Kbit);
% Node.EnNode(tempcurentnbr)=Node.EnNode(tempcurentnbr)-EnRecP;
% if Node.ListtentCH(tempcurentnbr,curentnbr)~=0
% Node.n_tentCH(tempcurentnbr)=Node.n_tentCH(tempcurentnbr)-1;
% Node.ListtentCH(tempcurentnbr,curentnbr)=0;
% end
% if Node.tent_CH(tempcurentnbr)==curentnbr % change tent CH
%
% if Node.n_tentCH(tempcurentnbr) <=0
% Node.tent_CH(tempcurentnbr)=NON_CH;
% Node.tent_CH_Cost(tempcurentnbr)=9999;
% else
% Node.tent_CH_Cost(tempcurentnbr)=9999;
% for ll=1:NodeNums
% if Node.ListtentCH(tempcurentnbr,ll) ~=0
% if Node.my_tent_CH_Cost(tempcurentnbr) > Node.ListtentCH_Cost(tempcurentnbr,ll)
% Node.my_tentCH(tempcurentnbr)= Node.ListtentCH(tempcurentnbr,ll);
% Node.my_tent_CH_Cost(tempcurentnbr)=Node.ListtentCH_Cost(tempcurentnbr,ll);
% end
% end
% end
% end
% end
% if Node.EnNode(tempcurentnbr) <= 0
% Node.StateNode(tempcurentnbr)=0;
% Node.Isstop(tempcurentnbr)=0;
% Node.EnNode(tempcurentnbr) =0;
% end
% end
% end
%
% end
%
Node.tent_CH(curentnbr)=i;
end % if(Node.tent_CH(curentnbr)==NON_CH | Node.tent_CH_Cost(i)< Node.tent_CH_Cost(curentnbr)
else
Node.StateNode(curentnbr)=0;
Node.Isstop(curentnbr)=0;
Node.EnNode(curentnbr)=0;
end
end
elseif Node.IsClusterHeads(i) == FINAL_CH
dist = Tr.^2+Tr.^2; %((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2); % the distance.^2
EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
Node.EnNode(i)=Node.EnNode(i)-EntranPCH;
if Node.EnNode(i) <= 0
Node.StateNode(i)=0;
Node.Isstop(i)=0;
Node.EnNode(i)=0;
end
for j=1:Node.NumNbr(i)
curentnbr = Node.Nbr(i,j);
EnRecP=EnRec(Elec,Kbit);
Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EnRecP;
if Node.EnNode(curentnbr) > 0
if (Node.ListfinalCH(curentnbr,i)==0)
Node.n_finalCH(curentnbr)=Node.n_finalCH(curentnbr)+1;
end
Node.ListfinalCH(curentnbr,i)=i;
Node.ListfinalCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
else
Node.EnNode(curentnbr)=0;
Node.StateNode(curentnbr)=0;
Node.Isstop(curentnbr)=0;
end
end
end
end
end
% recorder
fid = fopen('result\heed\iterations.txt','a');
fprintf(fid,'%6d,',iteration);
fclose(fid);
strnumnode = int2str(NodeNums);
filen = date;
type = 'ClustringEn';
strround=int2str(Rounds);
filen=['result\heed\',strround,type,strnumnode,' ',filen,'.txt'];
fid= fopen(filen,'w');
for i=1:NodeNums % the Node Id,his Cluster Head and His remain energy before TDMA . The energy after TDMA - The energy before TDMA = the consume energy TDMA
fprintf(fid,'%6d,%6d,%8.4f\r\n',i,Node.c(i),Node.EnNode(i));
end
fclose(fid);
%% TDMA
for i=1:NodeNums
if Node.StateNode(i) ~=0
if Node.IsClusterHeads(i) == NON_CH
EntranPCH=EnTran(Elec,Eamp,Kbit,Node.d(i)) ;
Node.EnNode(i)=Node.EnNode(i)-(TDMA.*EntranPCH);
if Node.EnNode(i) <= 0
Node.StateNode(i)=0;
Node.EnNode(i)=0;
end
EnRecP=EnRec(Elec,Kbit);
Node.EnNode(Node.c(i))=Node.EnNode(Node.c(i))-EnRecP;
if Node.EnNode(Node.c(i)) <= 0
Node.StateNode(Node.c(i))=0;
Node.EnNode(Node.c(i))=0;
end
else
EntranPCH=EnTran(Elec,Eamp,Kbit.*Node.csize(i).*Gathingcoefficient,Node.d(i)) ;
Node.EnNode(i)=Node.EnNode(i)-(TDMA.*EntranPCH);
if Node.EnNode(i) <= 0
Node.StateNode(i)=0;
Node.EnNode(i)=0;
end
end
end
end
%%
strnumnode = int2str(NodeNums);
filen = date;
type = 'EachRoundAllEn';
strround=int2str(Rounds);
filen=['result\heed\',strround,type,strnumnode,' ',filen,'.txt'];
fid= fopen(filen,'w');
for i=1:NodeNums % the Node Id,his Cluster Head and His remain energy after TDMA
fprintf(fid,'%6d,%6d,%8.4f\r\n',i,Node.c(i),Node.EnNode(i));
end
fclose(fid);
strnumnode = int2str(NodeNums);
filen = date;
type = 'ClusterHead';
strround=int2str(Rounds);
filen=['result\heed\',strround,type,strnumnode,' ',filen,'.txt'];
fid= fopen(filen,'w');
fprintf(fid,'%6d, ',ClusterHeadNum); % write the Num of Cluster Head,then write Id of Cluster Head
for i=1:NodeNums
if Node.IsClusterHeads(i) ~= NON_CH
fprintf(fid,'%6d, ',i);
end
end
fprintf(fid,'\r\n');
fclose(fid);
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -