heed算法的matlab代码,heed.m 源代码在线查看 - 在matlab下的对无线传感器网络中heed算法仿真,相当经典! 资源下载 虫虫电子下载站...

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) 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) % 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) % 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) % 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) 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) 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)) 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) 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值