leach和leach-c协议仿真

http://blog.csdn.net/codingkid/article/details/7215216

1.复制leach_test为leach-c_test,修改里面的文件夹和输出文件名。并且手动建立相应的文件夹。

很多教程说只修改文件名,没提到修改文件夹,如果同时运行两个协议会覆盖一部分实验结果的,而且是并行的就更难分解出是哪个协议的实验数据了。所以还是分开较好。

2.添加到test文件最后,后台运行的,需要等待。

3.写脚本分析实验数据。

[plain] view plain copy
 
  1. #计算不同时间剩余节点的数量  
  2.   
  3. BEGIN {  
  4.   
  5.   countcyl=0;  
  6.   totalleft=0;  
  7.   
  8.   lasttime=0;  
  9.   time[0]=0;  
  10.   node=0;  
  11.   total[0]=100;  
  12. }  
  13. {  
  14.   
  15. simtime              = $1;  
  16. nodeid               = $2;  
  17. statenode            = $3;  
  18.   
  19. if (simtime>lasttime ) {  
  20.     countcyl++;  
  21.     lasttime=simtime;  
  22.     time[countcyl]=simtime;  
  23.     totalleft=0;  
  24.       }  
  25.   
  26. if (statenode==1)  
  27.      totalleft++;  
  28.      total[countcyl]=totalleft;  
  29. }  
  30. END {  
  31. for(i=0;i<=countcyl;i++)  
  32. printf( "%f %d ",time[i],total[i]);  
  33. }  

[plain] view plain copy
 
  1. BEGIN {  
  2.   
  3. countcyl=0;  
  4.     packetsum=0;                       #当前时间发包且接收的总数  
  5.   
  6. lasttime=0;  
  7.     time[0]=0;  
  8.     sum[0]=0;  
  9. }  
  10. {  
  11.   
  12. simtime              = $1;  
  13. nodeid               = $2;  
  14. packet               = $3;             #读取当前时间,当前节点的发包且被成功接收的数目  
  15.   
  16. if (simtime>lasttime ) {                                #具体算法  
  17.      packtsum=0;  
  18.     countcyl++;  
  19.     lasttime=simtime;  
  20.     time[countcyl]=simtime;  
  21. }  
  22.   
  23. if (simtime==lasttime ) {  
  24.    packetsum=packetsum+packet;  
  25.    sum[countcyl]=packetsum;  
  26. }  
  27. }  
  28. END {                                                        #对应时段,打印发包总量。  
  29. for(i=0;i<=countcyl;i++)  
  30. printf ( "%f %f ",time[i],sum[i]);  
  31. }  

[plain] view plain copy
 
  1. BEGIN {  
  2.   
  3. countcyl=0;                           #计数器,用来记录当前的轮数 或者当前的时段数  
  4. energysum=0;                      #用来暂时储存当前时段的节点消耗能量总和  
  5.   
  6. lasttime=0;                           #记录当前最后的时间  
  7. time[0]=0;                             #存储各(轮)分段的时间  
  8. sum[0]=0;                             #存储各分段时间对应的消耗的能量  
  9. }  
  10. {  
  11.   
  12. simtime              = $1;                      #文件中第一字段的值,当前时间  
  13. nodeid               = $2;                      #。。。第二字段的值,节点ID  
  14. nodeenergy            = $3;               #。。。第三字段的值,当前节点使用的能量  
  15.   
  16. if (simtime>lasttime ) {     #这里就是一个简单的方法来提取每个时间段,节点总共消耗了多少能量  
  17.    energysum=0;  
  18.     countcyl++;  
  19.     lasttime=simtime;  
  20.     time[countcyl]=simtime;  
  21. }  
  22.   
  23. if (simtime==lasttime ) {  
  24.     if (nodeenergy<2.0) {  
  25.    energysum=energysum+nodeenergy;  
  26.    sum[countcyl]=energysum;  
  27. }  
  28. else if (nodeenergy>=2.0) {         #节点初始的总能量为2  
  29.    energysum=energysum+2.0;  
  30.    sum[countcyl]=energysum;  
  31. }  
  32. }  
  33. }  
  34. END {                                          #对应时间段,得出能量消耗量并打印  
  35. for(i=0;i<=countcyl;i++)  
  36. printf ( "%f %f ",time[i],sum[i]);  
  37. }  


4.gnuplot来画图

[plain] view plain copy
 
  1. set multiplot  
  2. set origin 0.0,0.5                          
  3. set size 0.5,0.5                           
  4. plot 'leach.alive.rst' with linespoint,'leach-c.alive.rst' with linespoint        
  5. set origin 0.5,0.5  
  6. set size 0.5,0.5  
  7. plot 'leach.data.rst' with linespoint,'leach-c.data.rst' with linespoint  
  8. set origin 0.33,0.0  
  9. set size 0.5,0.5  
  10. plot 'leach.energy.rst' with linespoint,'leach-c.energy.rst' with linespoint  

得到如下图:

明显实验结果说明leach-c死亡节点比较早,虽然bs收到数据量大,但是能量消耗是leach协议更关心的问题。

修改bs坐标为(0,0),再次计算得到如下结果:

从各方面来说c都比leach更优,有人说若bs在节点区域内部,则leach更优,若在外部则leach-c更优!

想知道为什么还需要看懂这两个协议喽。

接下来代码和理论分析。

 
0
原文地址:https://www.cnblogs.com/zkwarrior/p/5225155.html