
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.5 实验:WSN——节点分簇聚合模拟
2.5.1 实验目的
(1)了解Python的基本编程环境。
(2)了解Python程序的基本框架。
(3)对于Python如何实现一个WSN——对节点分簇聚合有整体认识。
(4)运行程序,查看结果。
2.5.2 实验要求
(1)熟悉Python的基本编程环境。
(2)用代码实现WSN——节点分簇聚合功能。
2.5.3 实验原理
(1)实验场景:给定WSN的节点数目,节点随机分布,根据 LEACH算法实现每轮对WSN的分簇。
(2)记录前k轮(本实验k=10)或绘制第k轮网络的分簇情况,即每个节点的角色(是簇头还是簇成员)及其关系,如果是簇成员,则标记其所属的簇头。
(3)实验中需要注意,节点数目不宜过小,本实验N=100;每轮只完成分簇,不考虑通信过程;每轮结束可以以定时器确定,也可以以完成当轮分簇为准。
(4)簇成员在寻找簇头时,以距离作为接收信号强弱的判断依据;约束条件为当选为簇头的节点在以后几轮的分簇中不再成为簇头。
2.5.4 实验步骤
本实验的实验环境为Python 2.7的环境。代码如下。
1.主程序

2.判断距离函数

3.生成随机节点集


4.根据LEACH算法选择簇头节点



5.节点分簇算法



6.绘制分类图


2.5.5 实验结果
实验运行结果如图2-16所示。

图2-16 实验运行结果