LEACH算法源代码

上传人:仙*** 文档编号:101523411 上传时间:2022-06-05 格式:DOC 页数:13 大小:56.50KB
收藏 版权申诉 举报 下载
LEACH算法源代码_第1页
第1页 / 共13页
LEACH算法源代码_第2页
第2页 / 共13页
LEACH算法源代码_第3页
第3页 / 共13页
资源描述:

《LEACH算法源代码》由会员分享,可在线阅读,更多相关《LEACH算法源代码(13页珍藏版)》请在装配图网上搜索。

1、./* * bs.cc * * Created on: 2011-4-17 * Author: syj */#include #include #include #include bs.h#include node.h#include cl_msg_m.h#include leach.hDefine_Module;/定义简单模块 直接或间接定义一个 CSimpleModule 的子类;/ 以 define_Module 或 define_Module_Like宏注册之;/*第一个执行的函数*/void BS:initialize int i;cModule* parent = getParen

2、tModule;/消息参数的访问 调用cModule 的par成员函数可以访问模块指针:/cPar& delayPar = par;cPar类是一个存储值的对象,/它支持数据类型,指针值可以这样读:/周围的复合模块可以通过 parentModule成员函数访问: cModule *parent = parentModule;/例如,父模块的参数像这样被访问: double timeout = parentModule-parmyId = par;this-xpos = par;this-ypos = par;this-nrNodes = parent-par;/?this-nrGates =

3、parent-par;/?this-nrRounds = parent-par;this-deadNodes = 0;this-roundsDone = 0;this-oldDeadNodes = 0;this-nrStatusRec = 0;/?this-halfDeadCtr = 0;/?this-halfDead = 0;/?this-calledEnd = 0;/?this-P = 0.05;/?this-cHeadsRound = 0;/每一轮簇头的个数this-roundEnergyLoss = 80001.0;/?/*记录图像用的变量的名字*加的*/roundVector.set

4、Name;/轮数deadNodesVector.setName;/死亡的节点olddeadNodesVector.setName;/所有死亡的节点nrheadVector.setName;/簇头的个数tVector.setName;/运行的时间lastrecVector.setName;/最后记录的消息cheadVector.setName;/所有的节点个数for i = 0; i advInfoi.id = 0;advInfoi.energy = 0;advInfoi.status = 0;/初始化节点信息共103项为什么是103?this-setGateSize;/需要 设置参数值或门向量

5、大小?this-setGateSize;/创建消息cMessage* cmsg = new cMessage;/创建消息/cMessage是OMNET的一个中心类。CMessage和子类的对象可以模拟一些东西:事/件;消息;包;帧;蜂窝;网络中的位或信号传输;系统中的实体传输等/等。一个cMessage对象有许多属性: cmsg-setKind;/初始化消息scheduleAtsimTime, cmsg;/立即发送给基站自己evid:myId 使用scheduleAt发送自传消息;scheduleAt;/scheduleAtsimtime+delta, msg;/*第三个执行的函数*/void

6、 BS:initNodes /初始化函数cModule* parent = getParentModule;cModule* mod;int numNodes;/总的节点个数int trRange;/总的轮数int i;numNodes = parent-par;ev BS numNodes is: numNodes par;ev BS trRange is: trRange n;/总的轮数是:fori=1;i=simulation.getLastModuleId;i+/?mod=simulation.getModule;/?ifstrcmpgetName,node=0/遍历节点,把模块指针填

7、充nodePtrmod-myId=mod;/Id标识,指针/*第六个执行的函数*/void BS:handleMessage /消息处理函数if isSelfMessage /本身的身消息ev BS: got self message type getKind n;/节点开始初始化完在initialize函数中if getKind = SMSG_INIT ev initNodes; else /其他消息no selfmessage来自节点的消息或者是簇头的消息if msg-getProto = CL_TOBS /簇头到基站的消息红色的。ev status message n;/消息状态doub

8、le energy;int cluster;/簇头int status;/各个状态int sender;/发射机int clHead;double curHEnergy;int curHStatus;double rating;double xpos, ypos;/*?*/sender = msg-getSrcAddress;/发送信息地址energy = msg-getEnergy;/发送信息能量cluster = msg-getCluster;/簇status = msg-getStatus;/状态xpos = msg-getXpos;ypos = msg-getYpos; if ener

9、gy halfDead = 0this-halfDeadCtr+;/? advInfosender.id = sender;/信息表中的发射量发射地址advInfosender.energy = energy;advInfosender.status = status;advInfosender.xpos = xpos;advInfosender.ypos = ypos;if advInfosender.energy if nodePtrsender-myStatus != STATUS_DEADthis-deadNodes+;/消息的状态advInfosender.status = STAT

10、US_DEAD;/此时的状态赋值为死亡status = STATUS_DEAD; nodePtrsender-myStatus = STATUS_DEAD;rating = energy - roundEnergyLoss;/发送功率advInfosender.rating = rating;ev BS received from sender status status rating: rating nrStatusRec+;/接收到的节点自加ev BS rec nrStatusRec nrNodes: nrNodes n;/check if doneif nrStatusRec = thi

11、s-nrNodes - this-oldDeadNodes /接收状态的节点=所有的节点已经死亡的节点this-oldDeadNodes += this-deadNodes;/已经死亡的节点数deadNodes为本轮死亡的节点ev nodes dead deadNodes all dead oldDeadNodes n;deadNodesVector.recorddeadNodes;/记录当前死亡的节点*I*olddeadNodesVector.recordoldDeadNodes;/记录所有死亡的节点*I*this-deadRoundthis-roundsDone = this-deadNo

12、des;if halfDeadCtr nrNodes / 10 & this-halfDead= 0 this-halfDead = roundsDone;ev calledEnd = 2;simulation.endRun;/f仿真结束?if roundsDone nrRounds /已经循环的次数=ev BS call compute bestn;/调用最优算法releaseConnections;/释放连接?computeBest;/用computerBdst算法选出簇头成簇resetStatus;/重设节点状态ev roundsDone+;/轮询的次数自加。evroundsDone:r

13、oundsDonen;if roundsDone this-nrRounds this-calledEnd = 1;simulation.endRun;/仿真结束delete msg;void BS:resetStatus /从置状态this-nrStatusRec = 0;/接收状态的节点this-deadNodes = 0;/死亡节点的数目/集中式的computer best/*#define FBEST8#define LBEST6void BS:computeBest struct _adv bestFBEST;int i, j;double sum;double min = 9999

14、99999;int minPtr = 0;ev first 3 n;/put FBEST nodes in best,compute minfor i = 0; i if advInfoi + 2.rating min = this-advInfoi.rating;minPtr = i;besti.id = this-advInfoi + 2.id;besti.xpos = this-advInfoi + 2.xpos;besti.ypos = this-advInfoi + 2.ypos;besti.status = this-advInfoi + 2.status;besti.rating

15、 = this-advInfoi + 2.rating;ev Id: besti.id rating: besti.rating n;for i = FBEST; i nrNodes; i+ if advInfoi + 2.rating min&this-advInfoi+2.status=STATUS_OK min = this-advInfoi + 2.rating;bestminPtr.id = this-advInfoi + 2.id;bestminPtr.rating = this-advInfoi + 2.rating;bestminPtr.xpos = this-advInfoi

16、 + 2.xpos;bestminPtr.ypos = this-advInfoi + 2.ypos;/ recompute min and minPtrminPtr = 0;min = 9999999;for j = 0; j if bestj.rating min = bestj.rating;minPtr = j;/ now find one node with minimum distance to all other best nodes/ usually one in the middle will be removed/ do not through away solar nod

17、es minPtr = 0;min = 99999999;for i = 0; i sum = 0;for j = 0; j if sum += * + * ;/ev distance sum sum id besti.id min min n;if sum minPtr = i;min = sum;ev remove node bestminPtr.id from cluster n;if bestminPtr.id = bestFBEST - 1.id;bestminPtr.xpos = bestFBEST - 1.xpos;bestminPtr.ypos = bestFBEST - 1.

18、ypos;bestminPtr.rating = bestFBEST - 1.rating;/ print out FBEST bestev computeBest now:n;for i = 0; i ev Id: besti.id rating: besti.rating n;/ search for node closest to another node, if one of them close to/ the border, remove the one close to the borderint n1, n2, tmp;int close = 0;min = 9999999;f

19、or i = 0; i for j = 0; j if tmp = * + * ;if tmp min = tmp;n1 = i;n2 = j;ev n1: bestn1.id n2: bestn2.id n;#define BORDER 180/ now choose one of the twoif bestn1.xpos this-xMax - BORDER| bestn1.ypos this-yMax- BORDERclose = n1;elseclose = n2;/ update array, remove closeev remove close bestclose.id n;i

20、f bestclose.id = bestFBEST - 2.id;bestclose.xpos = bestFBEST - 2.xpos;bestclose.ypos = bestFBEST - 2.ypos;bestclose.rating = bestFBEST - 2.rating;/ print out FBEST bestev computeBest now:n;for i = 0; i ev Id: besti.id rating: besti.rating n;for i=0; i ev id: besti.id xpos: besti.xpos ypos: besti.ypo

21、s rating: besti.rating n;int head;double tmp;ClusterHeadMessage*cmsg = new ClusterHeadMessage;ev send message to nodes announcing cluster heads setProto;cmsg-setSrcAddressmyId;for int rec = 2; rec nrNodes + 2; rec+ Node* nPtr = nodePtrrec;head = 0;if myStatus != STATUS_DEAD min = 9999999;for j = 0;

22、j if tmp = xpos - bestj.xpos * xpos- bestj.xpos + ypos - bestj.ypos* ypos - bestj.ypos;/ev cmp with bestj.id gives tmp ;if tmp /ev new minn;min = tmp;head = bestj.id; else head = bestj.id;break;ev BS: rec rec x,y xpos , ypos has head: head setCHead;cmsg-setDestAddress;cmsg-setDisplayString;for int r

23、ec = 2; rec nrNodes + 2; rec+ ifnodePtrrec-myStatus=STATUS_OKsenddup, out, rec-1;delete cmsg;this-resetStatus;*/ distributed computer best#define FBEST 20 / best after first roundvoid BS:computeBest struct _adv bestFBEST; int i, j; int min = 999999999; double T; double rd; int bestCtr=0; double h1;

24、int cHeads; for i = 0; i besti.id = 0;/数组用来存放地址,先初始化。 reset:/goto语句的相应跳转标示符。重新设置相应的状态 cHeads = this-cHeadsRound;/当前轮数的簇头个数 for int rec = 2; rec nrNodes + 2; rec+ Node *nPtr = nodePtrrec; if deadNodes = 50/簇头的个数加死亡的节点的个数 ev cHeadsRound = 0; nPtr-lastClusterRound = 0; if roundsDone % 20 = 0/执行20轮之后,有点

25、问题的程序。 nPtr-lastClusterRound = 0; ev cHeadsRound = 0; T = this-P / P * roundsDone % 20;/阈值的设定? h1 = P * cHeadsRound / 5;/计算簇头的百分比? if 1/全部是簇头则错误了。 ev ERROR h1 h1 ERROR h1 cHeadRound cHeadsRound P: P calledEnd = 5; endSimulation; ev round roundsDone cHeadsRound: cHeadsRound deadNodes deadNodes T: T P

26、 P n;/?/for int rec = 2; rec nrNodes + 2; rec+/从第二个节点开始轮询所有的节点。 Node *nPtr = nodePtrrec; rd = rand / ;/产生随机数函数,赋值给rd。 ev rd rec: rec cHeadsRound cHeadsRound T: T rd: rd lCR lastClusterRound n; if myStatus != STATUS_DEAD & rd lastClusterRound = 0 / 簇头选择,并输出簇头。 bestbestCtr.xpos = nPtr-xpos; bestbestCt

27、r.ypos = nPtr-ypos; bestbestCtr.id = rec; ev cHead: rec cHeadsRound+; if /? break; nPtr-lastClusterRound = this-roundsDone; this-nrHeadsthis-roundsDone = bestCtr; if /? int allDone = 1; if cHeadsRound + this-deadNodes = this-numNodes/? ev BS: go to resetn; goto reset;/ / check if not all are already

28、 done! for int rec = 2; rec nrNodes + 2; rec+ Node *nPtr = nodePtrrec; if lastClusterRound = 1 & nPtr-myStatus != STATUS_DEAD allDone = 0; break; if ev BS: restartn; goto restart;/ ev calledEnd = 5; endSimulation;/? if roundsDone = 250 ev calledEnd = 0; endSimulation; /发送消息给所有节点宣布簇头 int head; int tm

29、p; /char str32; ClusterHeadMessage*cmsg=new ClusterHeadMessage; evsend message to nodes announcing cluster headssetProto; cmsg-setSrcAddressmyId; for int rec = 2; rec nrNodes + 2; rec+/节点选择簇头形成簇。 Node *nPtr = nodePtrrec; head = 0; if myStatus != STATUS_DEAD /ev rec: rec; min = 9999999; for j = 0; j

30、if tmp = xpos - bestj.xpos * xpos - bestj.xpos + ypos - bestj.ypos * ypos - bestj.ypos; /ev cmp with bestj.id gives tmp ; if tmp /ev new minn; min = tmp; head = bestj.id; else head = bestj.id; break; ev BS: rec rec x,y xpos , ypos has head: head myHead = head; /簇形成 cmsg-setCHead; cmsg-setDestAddress

31、; cmsg-setDisplayString; for int rec = 2; rec nrNodes + 2; rec+ /* ClusterHeadMessage *cmsg = new ClusterHeadMessage; Node *nPtr = nodePtrrec; if myStatus != STATUS_DEAD /ev setProto; / XXX new name cmsg-setSrcAddressmyId; cmsg-setCHeadmyHead; cmsg-setDestAddress; /ev BS sendHEAD for rec : myHead n;

32、 if / no head when dead sprintf; if findGate 1 cGate *g = gate; if isConnected /ev myId : sToCHEAD to rec n; send cmsg, str; else ev DEAD DEAD rec n;*/ ifnodePtrrec-myStatus=STATUS_OK senddup,out,rec-1; / here we can reset everything delete cmsg; this-resetStatus;/void BS:releaseConnections cGate* g;cModule* mod;int i, j;for i = 1; i = simulation.getLastModuleId; i+ mod = simulation.getModule;if strcmpgetName, node = 0 ifmod-energy0getDisplayString.parse;for j = 1; j g = mod-gate;g-disconnect;void BS:finish evdeadnodes: oldDeadNodesn;.

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!