heed.m
上传用户:zglanne
上传日期:2015-11-03
资源大小:4k
文件大小:18k
源码类别:

3G开发

开发平台:

Visual C++

  1. NodeNums = 1000; % the num of node 
  2. AreaR = 2000 ;   % the area of simulate
  3. NodeTranR=10;   %  the transit Radius
  4. Elec=50 * 10^(-9); %
  5. Eamp=100*10^(-12); 
  6. Bx=0;  % The Postion of Baseation
  7. By=AreaR+400;
  8. MaxInteral = 100; % the leach simulate time
  9. Pch=0.05;  % the desired percentage of cluster heads 
  10. InitEn=4;  % the init energy of all node
  11. MaxEn=InitEn;
  12. Tr=100;  % the time of round 
  13. Kbit=2000; % the bits of a node transmiting a packet every time
  14. Gathingcoefficient=0.8;
  15. BandWitch = 1*10.^(6); %  Channel Bandwitch
  16. Threshold=0;    % the threshold of node becoming a cluster-head
  17. TDMA=400;
  18. Cprob=0.04;
  19. NON_CH = 0;% // non cluster head
  20. TENTATIVE_CH = 1; % // tentative cluster head
  21. FINAL_CH = 2;% // final cluster head
  22. sym ClusterHeadNum ;
  23. ClusterHeadNum=0;
  24. TOS_LOCAL_ADDRESS = -1;       % TOS_LOCAL_ADDRESS  must <=0
  25. for i=1:(MaxInteral)
  26.     AliveNode(i)=NodeNums;
  27. end
  28. %     Node.x=AreaR*rand(1,NodeNums);  % the position of node 
  29. %     Node.y=AreaR*rand(1,NodeNums);
  30.       sym buchang;
  31.       buchang = (AreaR/NodeNums);
  32.      % buchang=int(buchang);
  33.       Node.x=0:buchang:(buchang.*(NodeNums-1));  % the position of node 
  34.       Node.y=0:buchang:(buchang.*(NodeNums-1));
  35.     Node.IsClusterHeads=linspace(0,0,NodeNums); % NON_CH,TENTATIVE_CH,FINAL_CH
  36.     Node.IsCovered=linspace(0,0,NodeNums);      % Have Been Covered by a cluster head 1:yes 0:No
  37.     Node.c=linspace(0,0,NodeNums);              % the Cluster head of node
  38.     Node.chcost=linspace(0,0,NodeNums);              % the Cluster head of node
  39.     Node.d=linspace(0,0,NodeNums);              % the distance between cluster head and node
  40.     Node.l=zeros(1,NodeNums)+Kbit;           % the length of node i transmit packet
  41.     Node.EnNode=zeros(1,NodeNums)+InitEn;    % the init energy of all node
  42.     Node.StateNode=ones(1,NodeNums);      % the State of all node 1: alive 0:dead
  43.     Node.Listothernode=zeros(NodeNums);         % if node is a cluster head,Listothernode save the id of node belong to this cluster       
  44.     Node.csize=linspace(0,0,NodeNums);          % cluser size ,each cluster node num
  45.     Node.Nbr=zeros(NodeNums);                   % neighbor of node
  46.     Node.NumNbr=linspace(0,0,NodeNums);         % the neighbor's num of node
  47.     %Node.DistNbr=linspace(0,0,NodeNums);         % the neighbor's dist of node
  48.     Node.CHprob=zeros(1,NodeNums)+Cprob; 
  49.      Node.InitCHprob=zeros(1,NodeNums);
  50.     Node.tent_CH=zeros(1,NodeNums)+NON_CH; 
  51.     Node.tent_CH_Cost=ones(1,NodeNums)+9999;
  52.     Node.IsaddDummyRound=linspace(0,0,NodeNums);
  53.     Node.n_finalCH=linspace(0,0,NodeNums); 
  54.     Node.ListfinalCH=zeros(NodeNums);
  55.     Node.ListfinalCH_Cost=zeros(NodeNums)+9999;
  56.     Node.n_tentCH=linspace(0,0,NodeNums); 
  57.     Node.ListtentCH=zeros(NodeNums);
  58.     Node.ListtentCH_Cost=zeros(NodeNums)+9999;
  59.     Node.my_finalCH=linspace(0,0,NodeNums);
  60.     Node.my_tentCH=linspace(0,0,NodeNums);
  61.     Node.my_final_CH_Cost=ones(1,NodeNums)+9999;
  62.     Node.Isstop=ones(1,NodeNums);    % clustering is end ? 1:no,0:yes
  63.  for i=1:NodeNums
  64.      count =0 ;
  65.     for j=1:NodeNums
  66.        
  67.         if(j~=i) 
  68.         dist = ((Node.x(i)-Node.x(j)).^2)+((Node.y(i)-Node.y(j)).^2);  % the distance.^2
  69.                if dist < (Tr.^2+Tr.^2)  
  70.                    count=count+1;
  71.                    Node.Nbr(i,count)=j;
  72.                    
  73.                end
  74.                   
  75.          end
  76.          if j== NodeNums 
  77.                 Node.NumNbr(i) = count ;
  78.          end  
  79.     end 
  80.  end
  81.   
  82.  syms filen strnumnode tpye strround;
  83.      strnumnode = int2str(NodeNums);
  84.       filen = date;
  85.       type = 'NetWork';
  86.       
  87.       filen=['resultheed',type,strnumnode,' ',filen,'.txt'];
  88.       fid= fopen(filen,'w');
  89.        for i=1:NodeNums   % The Node ID ,position x,position y,The number of  neighbr node,The ID all  neighbr node
  90.        
  91.         fprintf(fid,'%6d,%10.4f,%10.4f,%6d',i,Node.x(i),Node.y(i),Node.NumNbr(i));
  92.           for j=1:Node.NumNbr(i)
  93.               fprintf(fid,',%6d',Node.Nbr(i,j));
  94.           end
  95.           fprintf(fid,'rn');
  96.       end
  97.       fclose(fid);  
  98.  sym iteration;
  99.  for Rounds = 1:MaxInteral  
  100.       
  101.        % the Setup phase of cluster
  102.       Node.CHprob=Cprob.*((Node.EnNode)./MaxEn);
  103.       Node.InitCHprob=Node.CHprob;
  104.       Node.IsaddDummyRound=0.*(Node.IsaddDummyRound);  %clear
  105.       Node.n_finalCH=Node.n_finalCH-Node.n_finalCH; 
  106.       Node.ListfinalCH=Node.ListfinalCH-Node.ListfinalCH;
  107.       Node.ListfinalCH_Cost=Node.ListfinalCH_Cost-Node.ListfinalCH_Cost+9999;
  108.       Node.my_finalCH=Node.my_finalCH-Node.my_finalCH;
  109.       Node.my_final_CH_Cost=(Node.my_final_CH_Cost-Node.my_final_CH_Cost)+9999;
  110.       Node.n_tentCH=Node.n_tentCH-Node.n_tentCH; 
  111.       Node.ListtentCH=Node.ListtentCH-Node.ListtentCH;
  112.       Node.ListtentCH_Cost=Node.ListtentCH_Cost-Node.ListtentCH_Cost+9999;
  113.       Node.csize=Node.csize-Node.csize;
  114.       Node.Isstop = Node.StateNode ;
  115.       Node.tent_CH=Node.tent_CH-Node.tent_CH+NON_CH;
  116.       Node.tent_CH_Cost=Node.tent_CH_Cost-Node.tent_CH_Cost+9999;
  117.       Node.c=Node.c-Node.c;
  118.       Node.d=Node.d-Node.d;
  119.       ClusterHeadNum=0;
  120.       Node.IsClusterHeads=Node.IsClusterHeads-Node.IsClusterHeads+NON_CH;
  121.       iteration=0;
  122.      while sum(Node.Isstop)~=0
  123.          iteration=iteration+1;
  124.       for i =1:NodeNums
  125.        if Node.Isstop(i)==1  
  126.           if Node.CHprob(i)<1
  127.              if   Node.tent_CH(i)==NON_CH
  128.               if rand(1,1)<Node.CHprob(i)
  129.                  Node.IsClusterHeads(i)=TENTATIVE_CH; 
  130.                  Node.tent_CH(i)=TOS_LOCAL_ADDRESS;
  131.                  Node.tent_CH_Cost(i)=Node.NumNbr(i);
  132.               end
  133.               %elseif  Node.tent_CH(i)==TOS_LOCAL_ADDRESS
  134.              end
  135.                Node.CHprob(i)=Node.CHprob(i).*2;
  136. %              if Node.InitCHprob(i)<0.02
  137. %               Node.CHprob(i)=Node.CHprob(i).*2;
  138. %              else
  139. %               Node.CHprob(i)=Node.CHprob(i).*(1+1000.*( Node.InitCHprob(i).^2));
  140. %              end 
  141.           else
  142.               if   Node.tent_CH(i)~=TOS_LOCAL_ADDRESS && Node.IsaddDummyRound(i)==0
  143.                Node.IsaddDummyRound(i)=1;
  144.               else
  145.                  for j=1:NodeNums   %Node.n_finalCH(i)
  146.                     if Node.ListfinalCH(i,j) ~=0
  147.                      if Node.my_final_CH_Cost(i) > Node.ListfinalCH_Cost(i,j) 
  148.                          Node.my_finalCH(i)= Node.ListfinalCH(i,j);
  149.                          Node.my_final_CH_Cost(i)=Node.ListfinalCH_Cost(i,j);
  150.                      end
  151.                     end  
  152.                  end
  153.                  % choose cluster head
  154.                   Node.Isstop(i)=0;   % diedai end   until CHprob==1
  155.                   if Node.my_finalCH(i) ~= NON_CH
  156.                      Node.IsClusterHeads(i)= NON_CH;
  157.                      Node.c(i)=Node.my_finalCH(i);
  158.                      Node.chcost=Node.my_final_CH_Cost(i);
  159.                      %join the cluster 
  160.                      dist =Tr.^2+Tr.^2; % ((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2);  % the distance.^2
  161.                      EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
  162.                      Node.d(i)=((Node.x(i)-Node.x(Node.c(i))).^2)+((Node.y(i)-Node.y(Node.c(i))).^2);  % the distance.^2
  163.                      Node.EnNode(i)=Node.EnNode(i)-EntranPCH;
  164.                      if Node.EnNode(i) <= 0
  165.                         Node.StateNode(i)=0;
  166.                         Node.Isstop(i)=0;
  167.                         Node.EnNode(i)=0;
  168.                                        
  169.                      end
  170.                     EnRecP=EnRec(Elec,Kbit);
  171.                     Node.EnNode(Node.c(i))=Node.EnNode(Node.c(i))-EnRecP;
  172.                     if Node.EnNode(Node.c(i)) <= 0
  173.                         Node.StateNode(Node.c(i))=0;
  174.                         Node.Isstop(Node.c(i))=0;
  175.                         Node.EnNode(Node.c(i))=0; 
  176.                     else                   
  177.                         Node.csize(Node.c(i))=Node.csize(Node.c(i))+1;  % cluster size add one
  178.                     end
  179.                  else
  180.                      Node.IsClusterHeads(i)= FINAL_CH;
  181.                      Node.my_finalCH(i)=TOS_LOCAL_ADDRESS;
  182.                      Node.c(i)=TOS_LOCAL_ADDRESS;
  183.                      Node.my_final_CH_Cost(i)= Node.NumNbr(i);%computeDegree(i);
  184.                      Node.chcost=Node.my_final_CH_Cost(i);
  185.                      Node.d(i)=((Node.x(i)-Bx).^2)+((Node.y(i)-By).^2);  % the distance.^2
  186.                      ClusterHeadNum=ClusterHeadNum+1;
  187.                   end
  188.              end    
  189.            end
  190.           end
  191.          end
  192.       
  193.       % compute consume energy
  194.         for i=1:NodeNums
  195.             if Node.IsClusterHeads(i) == TENTATIVE_CH  % & Node.tent_CH(curentnbr)==TOS_LOCAL_ADDRESS
  196.                 dist =Tr.^2+Tr.^2; % ((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2);  % the distance.^2
  197.                    EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
  198.                    Node.EnNode(i)=Node.EnNode(i)-EntranPCH;
  199.                    if Node.EnNode(i) <= 0
  200.                         Node.StateNode(i)=0;
  201.                         Node.Isstop(i)=0;
  202.                         Node.EnNode(i) =0;
  203.                    end
  204.                 for j=1:Node.NumNbr(i)
  205.                     curentnbr = Node.Nbr(i,j);
  206.                      EnRecP=EnRec(Elec,Kbit);
  207.                    Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EnRecP;
  208.                    Node.ListtentCH(curentnbr,i) = i;
  209.                    Node.ListtentCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
  210.                    if (Node.ListtentCH(curentnbr,i)==0)
  211.                            Node.n_tentCH(curentnbr)=Node.n_tentCH(curentnbr)+1;    
  212.                    end
  213.                    if Node.EnNode(curentnbr) > 0
  214.                     
  215.                       if (Node.ListtentCH(curentnbr,i)==0)
  216.                            Node.n_tentCH(curentnbr)=Node.n_tentCH(curentnbr)+1;    
  217.                       end 
  218.                       Node.ListtentCH(curentnbr,i) = i;
  219.                       Node.ListtentCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
  220.                        
  221.                       % 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)))
  222.                         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)))
  223.                        
  224.                                   Node.tent_CH_Cost(curentnbr)=Node.tent_CH_Cost(i);
  225.                                                            
  226. %                              if Node.tent_CH(curentnbr)==TOS_LOCAL_ADDRESS
  227. %                                    if Node.IsClusterHeads(curentnbr) ==TENTATIVE_CH
  228. %                                         Node.IsClusterHeads(curentnbr) = NON_CH;
  229. %                                     end
  230. %                                         dist =Tr.^2+Tr.^2; % ((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2);  % the distance.^2
  231. %                                     EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
  232. %                                     Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EntranPCH;
  233. %                                if Node.EnNode(curentnbr) <= 0
  234. %                                       Node.StateNode(curentnbr)=0;
  235. %                                       Node.Isstop(curentnbr)=0;
  236. %                                       Node.EnNode(curentnbr) =0;
  237. %                                 else 
  238. %                                   for k=1:Node.NumNbr(curentnbr)
  239. %                                     tempcurentnbr = Node.Nbr(curentnbr,k);
  240. %                                     EnRecP=EnRec(Elec,Kbit);
  241. %                                     Node.EnNode(tempcurentnbr)=Node.EnNode(tempcurentnbr)-EnRecP;
  242. %                                    if Node.ListtentCH(tempcurentnbr,curentnbr)~=0
  243. %                                     Node.n_tentCH(tempcurentnbr)=Node.n_tentCH(tempcurentnbr)-1;   
  244. %                                     Node.ListtentCH(tempcurentnbr,curentnbr)=0;
  245. %                                    end
  246. %                                    if Node.tent_CH(tempcurentnbr)==curentnbr     % change tent CH
  247. %                                        
  248. %                                        if  Node.n_tentCH(tempcurentnbr) <=0     
  249. %                                        Node.tent_CH(tempcurentnbr)=NON_CH;
  250. %                                        Node.tent_CH_Cost(tempcurentnbr)=9999;
  251. %                                        else
  252. %                                            Node.tent_CH_Cost(tempcurentnbr)=9999;
  253. %                                            for ll=1:NodeNums   
  254. %                                             if Node.ListtentCH(tempcurentnbr,ll) ~=0
  255. %                                                if Node.my_tent_CH_Cost(tempcurentnbr) > Node.ListtentCH_Cost(tempcurentnbr,ll) 
  256. %                                                   Node.my_tentCH(tempcurentnbr)= Node.ListtentCH(tempcurentnbr,ll);
  257. %                                                   Node.my_tent_CH_Cost(tempcurentnbr)=Node.ListtentCH_Cost(tempcurentnbr,ll);
  258. %                                                end
  259. %                                              end  
  260. %                                            end
  261. %                                        end
  262. %                                     end
  263. %                                      if Node.EnNode(tempcurentnbr) <= 0
  264. %                                        Node.StateNode(tempcurentnbr)=0;
  265. %                                        Node.Isstop(tempcurentnbr)=0;
  266. %                                        Node.EnNode(tempcurentnbr) =0;
  267. %                                      end
  268. %                                   end
  269. %                               end
  270. %                                  
  271. %                            end
  272. %                         
  273.                          Node.tent_CH(curentnbr)=i;
  274.                         end     % if(Node.tent_CH(curentnbr)==NON_CH | Node.tent_CH_Cost(i)< Node.tent_CH_Cost(curentnbr)
  275.                    else
  276.                          Node.StateNode(curentnbr)=0;
  277.                          Node.Isstop(curentnbr)=0;
  278.                          Node.EnNode(curentnbr)=0; 
  279.                    end  
  280.                    
  281.                 end
  282.             elseif  Node.IsClusterHeads(i) == FINAL_CH
  283.                 dist = Tr.^2+Tr.^2; %((Node.x(i)-Node.x(curentnbr)).^2)+((Node.y(i)-Node.y(curentnbr)).^2);  % the distance.^2
  284.                    EntranPCH=EnTran(Elec,Eamp,Kbit,dist) ;
  285.                    Node.EnNode(i)=Node.EnNode(i)-EntranPCH;
  286.                    if Node.EnNode(i) <= 0
  287.                         Node.StateNode(i)=0;
  288.                         Node.Isstop(i)=0;
  289.                         Node.EnNode(i)=0;
  290.                    end  
  291.                 for j=1:Node.NumNbr(i)
  292.                     curentnbr = Node.Nbr(i,j);
  293.                     EnRecP=EnRec(Elec,Kbit);
  294.                    Node.EnNode(curentnbr)=Node.EnNode(curentnbr)-EnRecP;
  295.                    if Node.EnNode(curentnbr) > 0
  296.                        if (Node.ListfinalCH(curentnbr,i)==0)
  297.                            Node.n_finalCH(curentnbr)=Node.n_finalCH(curentnbr)+1;    
  298.                        end
  299.                            
  300.                       Node.ListfinalCH(curentnbr,i)=i;
  301.                       Node.ListfinalCH_Cost(curentnbr,i)=Node.NumNbr(i);%Node.computeDegree(i);
  302.                    else
  303.                          Node.EnNode(curentnbr)=0; 
  304.                          Node.StateNode(curentnbr)=0;
  305.                          Node.Isstop(curentnbr)=0;
  306.                    end    
  307.                 end
  308.             end
  309.         end  
  310.       end
  311.    %  recorder 
  312.    
  313.      fid = fopen('resultheediterations.txt','a');
  314.        fprintf(fid,'%6d,',iteration);
  315.         fclose(fid);
  316.        strnumnode = int2str(NodeNums);
  317.        filen = date;
  318.        type = 'ClustringEn';
  319.        strround=int2str(Rounds);
  320.        filen=['resultheed',strround,type,strnumnode,' ',filen,'.txt'];
  321.         fid= fopen(filen,'w');
  322.    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
  323.        
  324.         fprintf(fid,'%6d,%6d,%8.4frn',i,Node.c(i),Node.EnNode(i));
  325.    end
  326.      fclose(fid);
  327.      %% TDMA 
  328.      for i=1:NodeNums
  329.        if Node.StateNode(i) ~=0
  330.          if Node.IsClusterHeads(i) == NON_CH
  331.              EntranPCH=EnTran(Elec,Eamp,Kbit,Node.d(i)) ;
  332.              Node.EnNode(i)=Node.EnNode(i)-(TDMA.*EntranPCH);
  333.              if Node.EnNode(i) <= 0
  334.                         Node.StateNode(i)=0;
  335.                         Node.EnNode(i)=0;
  336.                        
  337.              end 
  338.               EnRecP=EnRec(Elec,Kbit);
  339.               Node.EnNode(Node.c(i))=Node.EnNode(Node.c(i))-EnRecP; 
  340.                    if Node.EnNode(Node.c(i)) <= 0
  341.                         Node.StateNode(Node.c(i))=0;
  342.                         Node.EnNode(Node.c(i))=0;
  343.                     end
  344.          else
  345.              EntranPCH=EnTran(Elec,Eamp,Kbit.*Node.csize(i).*Gathingcoefficient,Node.d(i)) ;
  346.              Node.EnNode(i)=Node.EnNode(i)-(TDMA.*EntranPCH);
  347.              
  348.              if Node.EnNode(i) <= 0
  349.                         Node.StateNode(i)=0;
  350.                         Node.EnNode(i)=0;
  351.                        
  352.              end 
  353.          end
  354.       end 
  355.      end
  356.      %%
  357.      
  358.     strnumnode = int2str(NodeNums);
  359.        filen = date;
  360.        type = 'EachRoundAllEn';
  361.        strround=int2str(Rounds);
  362.        filen=['resultheed',strround,type,strnumnode,' ',filen,'.txt'];
  363.         fid= fopen(filen,'w');
  364.    for i=1:NodeNums                   %  the Node Id,his Cluster Head and His remain energy after TDMA
  365.        
  366.         fprintf(fid,'%6d,%6d,%8.4frn',i,Node.c(i),Node.EnNode(i));
  367.    end
  368.      fclose(fid);  
  369.     
  370.         strnumnode = int2str(NodeNums);
  371.        filen = date;
  372.        type = 'ClusterHead';
  373.        strround=int2str(Rounds);
  374.        filen=['resultheed',strround,type,strnumnode,' ',filen,'.txt'];
  375.         fid= fopen(filen,'w');
  376.         fprintf(fid,'%6d, ',ClusterHeadNum);   % write the Num of Cluster Head,then write Id of Cluster Head
  377.    for i=1:NodeNums
  378.         if  Node.IsClusterHeads(i) ~= NON_CH
  379.         fprintf(fid,'%6d, ',i);
  380.         end
  381.    end
  382.        fprintf(fid,'rn');
  383.      fclose(fid);  
  384.  end
  385.  
  386.  
  387.