nojunyunEvote2new.m.txt
上传用户:liweizzp
上传日期:2015-06-15
资源大小:4k
文件大小:16k
源码类别:

3G开发

开发平台:

Matlab

  1. %ddddddddddddddddddddd      基于选票分簇算法的能量消耗--非均匀分簇       ddddddddddddddddddddddddddd
  2. %开始每个节点广播ID号和当前能量;接收计算,每个节点广播ID号和当前选票和适宜性(或节点度)
  3. %接收,若本节点选票最多,作为簇头,并广播本节点ID号结束;簇成员节点接受簇头信息,加入并广播本节点ID号结束。
  4. %能量消耗包括两部分:簇形成阶段的能耗和数据传输阶段的能耗。
  5. %dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
  6. N=50;                    %节点个数 
  7. f=1;                     %最后结果数组下标,50-500, 每隔50取一数,共有10
  8. while N<=500
  9. g=5;                     %相同条件下的重复次数    
  10. for t=1:g
  11. %dddddddddddddddddddddddddddddd     节点配置    dddddddddddddddddddddddddddddddddddddd
  12. %   radius=25;                              %广播距离
  13. X_axis=100;                    %x轴长度 代表节点分布范围中的一维 
  14. Y_axis=100;                    %y轴长度 代表节点分布范围中的另一维 
  15. Loca=[X_axis*rand(1,N);Y_axis*rand(1,N)]';    %产生坐标矩阵 
  16. for i=1:N
  17.     e(i)=10;                  %节点的能量 
  18.     Eprog(i)=0;
  19. end  
  20. er=0.533*10^(-6);
  21. et=1.066*10^(-6);
  22. eta=3.076*10^(-9);
  23. efuse=1.8*10^(-7);
  24. es=1.75*10^(-7);
  25. D=-175;
  26. c=0.2;
  27. dopt=sqrt((er+et)/eta);
  28. radius=dopt/2;                              %广播距离
  29. dT=210;                             %门限距离,小于直接传输到网关,大于多跳到网关
  30. %ddddddddddddddddddddddddddddd         节点位置示意图    dddddddddddddddddddddddddddddddd
  31.  %plot(Loca(:,1),Loca(:,2),'o')
  32. %ddddddddddddddddddddddddddd        计算邻居节点矩阵   ddddddddddddddddddddddddddddddddddddddd
  33.                                  
  34. for i=1:N
  35.     d(i)=0;                             %节点的度 
  36. end    
  37. for i=1:N
  38.     for j=i:N
  39.         dis(i,j)=sqrt((Loca(i,1)-Loca(j,1))*(Loca(i,1)-Loca(j,1))+(Loca(i,2)-Loca(j,2))*(Loca(i,2)-Loca(j,2)));          %存放节点间距离 
  40.         dis(j,i)=dis(i,j);
  41.     end
  42. end
  43. dmax=0;
  44. dmin=250;
  45. for i=1:N
  46.     dis(i,N+1)=sqrt((Loca(i,1))*(Loca(i,1))+(Loca(i,2)-D)*(Loca(i,2)-D));          %存放节点到网关的距离 
  47.     if dis(i,N+1)>dmax
  48.        dmax=dis(i,N+1);
  49.     end
  50.     if dis(i,N+1)<dmin
  51.        dmin=dis(i,N+1);
  52.     end
  53. end
  54.   
  55. for i=1:N   
  56.     r(i)=radius*(1-c*(dmax-dis(i,N+1))/(dmax-dmin));
  57.     for j=1:N
  58.         if dis(i,j)<=r(i)&&i~=j
  59.            d(i)=d(i)+1;
  60.            neibor(i,d(i))=j;
  61.        end 
  62.     end
  63.    if d(i)==0
  64.      neibor(i,1)=0;
  65.    end  
  66. end
  67. for i=1:N
  68.     n1=ceil(dis(i,N+1)/dopt);
  69.     syms k;
  70.     n2=double(symsum(1/k,1,n1));
  71.     dn(i)=dis(i,N+1)/n2;
  72. end
  73. %ddddddddddddddddddddddddddd        节点布置后,直到所有节点能量耗尽,所经历的轮数   ddddddddddddddddddddddddddddddddddddddd
  74. lunum=0;
  75. flag2=1;
  76. Ecluz=0;
  77. Edataz=0;
  78. flag3=0;
  79. Nper=10;
  80. dienote=0;
  81. while flag2==1
  82. %ddddddddddddddddddddddddddd        计算节点初始阶段的能量消耗   ddddddddddddddddddddddddddddddddddddddd
  83. for i=1:N
  84.     Edata(i)=0;
  85.     Einit(i)=0;
  86.     Eprog(i)=0;
  87. end    
  88. for i=1:N
  89.     if e(i)~=0
  90.        Einit(i)=(6*8+8*8+8*8)*(eta*r(i)^2+et)+d(i)*(6*8+8*8+8*8)*er;
  91.     end   
  92. end    
  93.       
  94. %ddddddddddddddddddddddddddd        计算节点初始选票   ddddddddddddddddddddddddddddddddddddddd
  95. for i=1:N
  96.     if e(i)~=0
  97.        E(i)=e(i);
  98.        if d(i)~=0
  99.           for k=1:d(i)
  100.               E(i)=E(i)+e(neibor(i,k));
  101.           end
  102.        end  
  103.        v(i,i)=e(i)/E(i);
  104.        if d(i)~=0
  105.           for k=1:d(i)
  106.               v(i,neibor(i,k))=e(neibor(i,k))/E(i);
  107.           end
  108.        end   
  109.     end
  110. end    
  111. for i=1:N
  112.     if e(i)~=0
  113.        if d(i)==0
  114.           fitness(i)=e(i);
  115.        else   
  116.           fitness(i)=e(i)/d(i);           %节点的适应性
  117.        end    
  118.        curvote(i)=v(i,i);
  119.        if d(i)~=0
  120.           for k=1:d(i)
  121.               curvote(i)=curvote(i)+v(neibor(i,k),i);          %节点的总选票
  122.           end
  123.        end
  124.     end   
  125.  end
  126. %ddddddddddddddddddddddddddd  查找本节点是否是邻居集合中最多选票的节点,如是发簇头广播 ddddddddddddddddddddddddddddddddddddddd
  127. for i=1:N
  128.     CH(i)=0;
  129.     cluster_head(i)=0;
  130. end    
  131. for i=1:N
  132.     if e(i)~=0
  133.        maxvote=curvote(i);
  134.        maxvoteID(i)=i;
  135.        flag=0;
  136.        if d(i)~=0
  137.           for k=1:d(i)
  138.              if curvote(neibor(i,k))>maxvote
  139.                 flag=1;
  140.                 break
  141.              end
  142.           end
  143.        end   
  144.        if flag==0
  145.           cluster_head(i)=i;
  146.           CH(i)=1;
  147.           Eprog(i)=Eprog(i)+4*8*(eta*r(i)^2+et);         %发簇头信息
  148.        end
  149.     end   
  150. end
  151.     
  152. %ddddddddddddddddddddddddddd       处理收到的簇头信息的节点,按适宜性选择簇头  ddddddddddddddddddddddddddddddddddddddd
  153. for i=1:N
  154.     schnum(i)=0;
  155. end    
  156. for i=1:N
  157.     if CH(i)==1
  158.        if d(i)~=0 
  159.           for k=1:d(i)
  160.               schnum(neibor(i,k))=schnum(neibor(i,k))+1;
  161.               sch(neibor(i,k),schnum(neibor(i,k)))=i;
  162.               CH(neibor(i,k))=2;
  163.               Eprog(neibor(i,k))=Eprog(neibor(i,k))+4*8*er+4*8*(eta*r(neibor(i,k))^2+et);       %接收簇头信息并发撤消包
  164.               if d(neibor(i,k))~=0
  165.                  for b=1:d(neibor(i,k))
  166.                      Eprog(neibor(neibor(i,k),b))=Eprog(neibor(neibor(i,k),b))+4*8*er;       %接收撤消包
  167.                  end    
  168.               end
  169.           end
  170.        end  
  171.     end  
  172. end   
  173.      
  174.  for i=1:N
  175.      if CH(i)==2
  176.         fitness1=0;
  177.         if schnum(i)~=0
  178.            for k=1:schnum(i)           %收到的簇头集合中适宜性最高的簇头
  179.                if fitness(sch(i,k))>fitness1
  180.                   fitness1=fitness(sch(i,k));
  181.                   head=sch(i,k); 
  182.               end
  183.             end
  184.         end 
  185.         flag1=0; 
  186.         if d(i)~=0
  187.            for k=1:d(i)                     %簇头的适宜性是否在未被覆盖的节点中最高
  188.                if CH(neibor(i,k))==0
  189.                   if fitness(neibor(i,k))>fitness1
  190.                      flag1=1;
  191.                      break
  192.                  end
  193.               end
  194.            end 
  195.         end
  196.         if flag1==0
  197.            cluster_head(i)=head;
  198.            CH(i)=1;
  199.            Eprog(i)=Eprog(i)+6*8*(eta*r(i)^2+et);         %发加入信息
  200.         end
  201.     end   
  202.   end
  203.   
  204. %ddddddddddddddddddddddddddd       处理未被覆盖的节点  ddddddddddddddddddddddddddddddddddddddd 
  205. flag=1;
  206. while flag==1
  207.       flag=0;
  208.       chval=3;    
  209.       for i=1:N            %重新计算未覆盖节点的选票    
  210.           if e(i)~=0 
  211.              if CH(i)==0
  212.                 flag=1;
  213.                 E(i)=e(i);
  214.                 if d(i)~=0
  215.                    for k=1:d(i)
  216.                        if CH(neibor(i,k))==0
  217.                           E(i)=E(i)+e(neibor(i,k));
  218.                        end  
  219.                    end   
  220.                 end  
  221.                 v(i,i)=e(i)/E(i);
  222.                 if d(i)~=0
  223.                    for k=1:d(i)
  224.                        if CH(neibor(i,k))==0
  225.                           v(i,neibor(i,k))=e(neibor(i,k))/E(i);
  226.                        else   
  227.                           v(i,neibor(i,k))=0;
  228.                        end 
  229.                    end   
  230.                 end
  231.             end
  232.           end
  233.       end   
  234.          
  235.         for i=1:N
  236.             if e(i)~=0
  237.                if CH(i)==0
  238.                   Eprog(i)=Eprog(i)+6*8*(eta*r(i)^2+et);         %发新选票信息 
  239.                   curvote(i)=v(i,i);
  240.                   if d(i)~=0
  241.                      for k=1:d(i)
  242.                          if CH(neibor(i,k))==0
  243.                             curvote(i)=curvote(i)+v(neibor(i,k),i);
  244.                          end  
  245.                      end  
  246.                   end
  247.                end
  248.             end   
  249.         end
  250.         
  251.         for i=1:N               %查找本节点是否是邻居集合中最多选票的节点,如是发簇头广播
  252.             if e(i)~=0
  253.                if CH(i)==0
  254.                   maxvote=curvote(i);
  255.                   maxvoteID(i)=i;
  256.                   flag1=0;
  257.                   if d(i)~=0
  258.                      for k=1:d(i)
  259.                          if CH(neibor(i,k))==0
  260.                             if curvote(neibor(i,k))>maxvote
  261.                                flag1=1;
  262.                                break
  263.                             end
  264.                          end  
  265.                      end
  266.                   end    
  267.                   if flag1==0
  268.                      cluster_head(i)=i;
  269.                      CH(i)=chval;
  270.                      Eprog(i)=Eprog(i)+4*8*(eta*r(i)^2+et);         %发簇头信息
  271.                   end     
  272.                end
  273.             end  
  274.         end
  275.         
  276.        for i=1:N            %  处理收到的簇头信息的节点
  277.            if CH(i)==chval
  278.               if d(i)~=0 
  279.                  for k=1:d(i)
  280.                      schnum(neibor(i,k))=schnum(neibor(i,k))+1;
  281.                      sch(neibor(i,k),schnum(neibor(i,k)))=i;
  282.                      CH(neibor(i,k))=chval+1;
  283.                      Eprog(neibor(i,k))=Eprog(neibor(i,k))+4*8*er+4*8*(eta*r(neibor(i,k))^2+et);       %接收簇头信息并发撤消包
  284.                      if d(neibor(i,k))~=0
  285.                         for b=1:d(neibor(i,k))
  286.                             Eprog(neibor(neibor(i,k),b))=Eprog(neibor(neibor(i,k),b))+4*8*er;       %接收撤消包
  287.                         end    
  288.                      end
  289.                   end  
  290.               end
  291.            end  
  292.        end   
  293.      
  294.        for i=1:N                        %按适宜性选择簇头
  295.            if CH(i)~=1&&CH(i)~=chval                %CH(i)~=0&&CH(i)~=1
  296.               fitness1=0;
  297.               if schnum(i)~=0
  298.                     for k=1:schnum(i)
  299.                         if fitness(sch(i,k))>fitness1
  300.                            fitness1=fitness(sch(i,k));
  301.                            head=sch(i,k); 
  302.                         end
  303.                     end   
  304.                  end
  305.                  flag1=0; 
  306.                  if d(i)~=0
  307.                     for k=1:d(i)                     %簇头的适宜性是否在未被覆盖的节点中最高
  308.                         if CH(neibor(i,k))==0
  309.                            if fitness(neibor(i,k))>fitness1
  310.                               flag1=1;
  311.                               break
  312.                            end
  313.                         end
  314.                     end 
  315.                  end
  316.                  if flag1==0
  317.                     cluster_head(i)=head;
  318.                     CH(i)=1;
  319.                     Eprog(i)=Eprog(i)+6*8*(eta*r(i)^2+et);         %发加入信息
  320.                  end
  321.               end      
  322.            end  
  323.          chval=chval+2;
  324.      end    
  325. %ddddddddddddddddddddddddddd      簇形成阶段每个节点的能量消耗   ddddddddddddddddddddddddddddddddddddddd  
  326. for i=1:N
  327.     if e(i)~=0
  328.        Eclu(i)=Einit(i)+Eprog(i);
  329.     end   
  330. end    
  331.  
  332. %ddddddddddddddddddddddddddd       计算簇的个数:簇头和成员   ddddddddddddddddddddddddddddddddddddddd
  333. temp1=cluster_head;
  334. cluster_num=1;
  335. for i=1:N
  336.     if e(i)~=0
  337.        if temp1(i)==0
  338.           continue
  339.        end    
  340.        head=temp1(i);
  341.        temp1(i)=0;
  342.        m=1;
  343.        cluster(cluster_num,m)=head;
  344.        m=m+1;
  345.        for k=i+1:N
  346.           if head==temp1(k)
  347.              if k==head
  348.                 cluster(cluster_num,m)=i; 
  349.              else   
  350.                 cluster(cluster_num,m)=k;
  351.              end   
  352.              m=m+1;
  353.              temp1(k)=0;
  354.          end
  355.        end
  356.        node_num(cluster_num)=m-1;
  357.        cluster_num=cluster_num+1;
  358.     end  
  359. end
  360. cluster_num=cluster_num-1;  
  361. %ddddddddddddddddddddddddddd      数据传输阶段每个节点的能量消耗   ddddddddddddddddddddddddddddddddddddddd  
  362. for i=1:cluster_num
  363.     Edata(cluster(i,1))=5*(128*8*er*(node_num(i)-1)+efuse*128*8*(node_num(i)-1));  %簇头接收信息和融合信息能耗以及经多跳发送到网关的能耗
  364.     for k=2:node_num(i)
  365.         Edata(cluster(i,k))=5*128*8*(et+eta*dis(cluster(i,1),cluster(i,k))^2)+5*128*8*es;       % 成员节点采集和发送数据能耗
  366.     end
  367. end
  368. %下面计算簇头到网关的路由
  369. for i=1:cluster_num
  370.     if dis(cluster(i,1),N+1)<=dT
  371.        rout(cluster(i,1),1)=0;
  372.        rout1(cluster(i,1),1)=0;
  373.        continue
  374.     else
  375.        m=0;
  376.        for j=1:cluster_num
  377.            if dis(cluster(i,1),N+1)>dis(cluster(j,1),N+1)
  378.                m=m+1;
  379.                rout(cluster(i,1),m)=cluster(j,1);
  380.            end
  381.        end
  382.        if rout(cluster(i,1),1)==0
  383.           rout1(cluster(i,1),1)=0;
  384.       end
  385.       %求最佳中继节点的位置(x0,y0)   
  386.       x0=Loca(cluster(i,1),1)*(1-dn(cluster(i,1))/(dis(cluster(i,1),N+1)));
  387.       y0=Loca(cluster(i,1),2)*(1-dn(cluster(i,1))/(dis(cluster(i,1),N+1)))+D*dn(cluster(i,1))/(dis(cluster(i,1),N+1));
  388.       flag5=1;
  389.       rout1(cluster(i,1),1)=0;
  390.       dmin1=dn(cluster(i,1))^2;
  391.       for j=1:m
  392.           dtemp=(Loca(rout(cluster(i,1),j),1)-x0)^2+(Loca(rout(cluster(i,1),j),2)-y0)^2;
  393.           if dtemp<dmin1
  394.               dmin1=dtemp;
  395.               node=rout(cluster(i,1),j);
  396.               flag5=0;
  397.           end
  398.       end
  399.       if flag5==0
  400.          rout1(cluster(i,1),1)=node;
  401.       end
  402.   end
  403. end
  404. %下面计算簇头消耗的能量
  405. for i=1:cluster_num
  406.     if rout1(cluster(i,1),1)==0
  407.         Edata(cluster(i,1))=Edata(cluster(i,1))+128*8*(et+eta*(dis(cluster(i,1),N+1))^2);
  408.     else
  409.         temp=rout1(cluster(i,1),1);
  410.         Edata(cluster(i,1))=Edata(cluster(i,1))+128*8*(et+eta*(dis(cluster(i,1),temp))^2);
  411.         Edata(temp)=Edata(temp)+128*8*er;
  412.         for k=1:7
  413.             if rout1(temp,1)==0
  414.                 Edata(temp)=Edata(temp)+128*8*(et+eta*(dis(temp,N+1))^2);
  415.                 break
  416.             else
  417.                 temp1=rout1(temp,1);
  418.                 Edata(temp)=Edata(temp)+128*8*(et+eta*(dis(temp,temp1))^2);
  419.                 Edata(temp1)=Edata(temp1)+128*8*er;
  420.                 temp=temp1;
  421.             end
  422.         end
  423.     end
  424. end        
  425. %ddddddddddddddddddddddddddd      一轮结束后每个节点的能量消耗和剩余能量   ddddddddddddddddddddddddddddddddddddddd 
  426. for i=1:N
  427.     if e(i)~=0
  428.        e(i)=e(i)-Edata(i)-Eclu(i);
  429.        Ecluz=Ecluz+Eclu(i);
  430.        Edataz=Edataz+Edata(i);
  431.     end   
  432. end 
  433. lunum=lunum+1;
  434. if N==200
  435.    if lunum<=20 
  436.       Ehead(t,lunum)=0;
  437.       for i=1:cluster_num
  438.           if e(i)~=0
  439.              Ehead(t,lunum)=Ehead(t,lunum)+Edata(cluster(i,1))+Eclu(cluster(i,1));
  440.           end
  441.       end
  442.    end
  443. end
  444. for i=1:N
  445.     if e(i)<=1*10^(-10) &&e(i)~=0            %能量耗尽后,该节点从邻居集中删去
  446.        e(i)=0;
  447.        if N==200
  448.           dienote=dienote+1;
  449.           if dienote==Nper
  450.               die(t,Nper/10)=lunum; 
  451.               Nper=Nper+10;
  452.            end
  453.         end
  454.        if d(i)~=0
  455.           for k=1:d(i)
  456.               nei=neibor(i,k);
  457.               if d(nei)~=0  
  458.                  for j=1:d(nei)
  459.                      if neibor(nei,j)==i
  460.                         if j~=d(nei)
  461.                            for x=j:d(nei)-1 
  462.                                neibor(nei,x) =neibor(nei,x+1);
  463.                            end  
  464.                         end
  465.                         break
  466.                      end    
  467.                   end
  468.                   neibor(nei,d(nei))=0;
  469.                   if d(nei)>0
  470.                      d(nei)=d(nei)-1;
  471.                   end
  472.               end
  473.           end 
  474.        end 
  475.        d(i)=0;
  476.        if flag3==0
  477.           first_lu=lunum;
  478.           flag3=1;
  479.     %     flag2=0;
  480.     %      break
  481.        end   
  482.     end
  483. end    
  484. flag4=0;
  485. for i=1:N    
  486.     if e(i)>=1*10^(-10)
  487.        flag4=1;
  488.        break
  489.     end
  490. end    
  491.     if flag4==0
  492.        flag2=0;
  493.        last_lu=lunum;
  494.     end     
  495. end
  496. %ddddddddddddddddddddddddddd  算法结束 ddddddddddddddddddddddddddddddddddddddd
  497. first_luf(t)=first_lu;     %重复100次,每次的第一个节点死去经历的轮数
  498. last_luf(t)=last_lu;         %重复100次,每次的最后一个节点死去经历的轮数
  499. Ecluzf(t)=Ecluz;             %重复100次,每次的簇形成消耗的能量
  500. Edatazf(t)=Edataz;          %重复100次,每次的数据传输消耗的能量
  501. end                     % 对于相同的传输半径,重复100次结束
  502. %dddddddddddddddddddddd计算各参数100次的平均值dddddddddddddddddddddddd
  503. sum=0;
  504. sum1=0;
  505. sum2=0;
  506. sum3=0;
  507. if N==200
  508.    for i=1:20
  509.        sum4(i)=0;
  510.        sum5(i)=0;
  511.    end
  512. end
  513. for i=1:g
  514.     sum=sum+first_luf(i);
  515.     sum1=sum1+last_luf(i);
  516.     sum2=sum2+Ecluzf(i);
  517.     sum3=sum3+Edatazf(i);
  518. end    
  519. if N==200
  520.    for j=1:20
  521.        for i=1:g
  522.            sum4(j)=sum4(j)+Ehead(i,j);
  523.            sum5(j)=sum5(j)+die(i,j);
  524.        end
  525.     end
  526. end
  527. first_lu_z(f)=sum/g;
  528. last_lu_z(f)=sum1/g;
  529. Ecluzz(f)=sum2/g;
  530. Edatazz(f)=sum3/g;
  531. if N==200
  532.    for i=1:20
  533.        Eheadz(i)=sum4(i)/g;
  534.        diez(i)=sum5(i)/g;
  535.    end
  536. end
  537. N=N+50;
  538. f=f+1;
  539. end                    %程序结束
  540. %dddddddddddddddddddd     画图        dddddddddddddddddddddddd
  541.