LEACH算法源代码(12页).doc

上传人:1595****071 文档编号:35412993 上传时间:2022-08-21 格式:DOC 页数:12 大小:185.50KB
返回 下载 相关 举报
LEACH算法源代码(12页).doc_第1页
第1页 / 共12页
LEACH算法源代码(12页).doc_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《LEACH算法源代码(12页).doc》由会员分享,可在线阅读,更多相关《LEACH算法源代码(12页).doc(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、-LEACH算法源代码-第 12 页 * 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( BS);/定义简单模块 (1) 直接或间接定义一个 CSimpleModule 的子类;/(2) 以 define_Module() 或 define_Module_Like()宏注册之;/*第一个执行的函数*/void BS:initialize() int i;cModul

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

3、-ypos = par(ypos);this-nrNodes = parent-par(numNodes);/?this-nrGates = parent-par(numNodes);/?this-nrRounds = parent-par(rounds);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-cH

4、eadsRound = 0;/每一轮簇头的个数this-roundEnergyLoss = 80001.0;/?/*记录图像用的变量的名字*加的*/roundVector.setName(round);/轮数deadNodesVector.setName(nodes dead);/死亡的节点olddeadNodesVector.setName(all dead);/所有死亡的节点nrheadVector.setName(headnumber);/簇头的个数tVector.setName(T);/运行的时间lastrecVector.setName(lastrec);/最后记录的消息cheadV

5、ector.setName(all head num);/所有的节点个数for (i = 0; i setGateSize(out, nrGates + 1);/需要 设置参数值或门向量大小?this-setGateSize(in, nrGates + 1);/创建消息cMessage* cmsg = new cMessage(init);/创建消息/cMessage是OMNET的一个中心类。CMessage和子类的对象可以模拟一些东西:事/件;消息;包;帧;蜂窝;网络中的位或信号传输;系统中的实体传输等/等。一个cMessage对象有许多属性: cmsg-setKind(SMSG_INIT)

6、;/初始化消息scheduleAt(simTime(), cmsg);/立即发送给基站自己evid:myId par(numNodes);ev BS numNodes is: numNodes par(trRange);ev BS trRange is: trRange n;/总的轮数是:for(i=1;igetName(),node)=0)/遍历节点,把模块指针填充nodePtr(Node*)mod)-myId=(Node*)mod;/Id标识,指针/*第六个执行的函数*/void BS:handleMessage(cMessage* msg) /消息处理函数if (msg-isSelfMe

7、ssage() /本身的身消息ev BS: got self message type getKind() getKind() = SMSG_INIT) ev initNodes(); else /其他消息no selfmessage来自节点的消息或者是簇头的消息if (ClusterMessage*) msg)-getProto() = CL_TOBS) /簇头到基站的消息(红色的)。ev status message getSrcAddress();/发送信息地址energy = (Status2BSMessage*) msg)-getEnergy();/发送信息能量cluster = (

8、Status2BSMessage*) msg)-getCluster();/簇status = (Status2BSMessage*) msg)-getStatus();/状态xpos = (Status2BSMessage*) msg)-getXpos();ypos = (Status2BSMessage*) msg)-getYpos(); if (energy halfDead = 0)this-halfDeadCtr+;/? advInfosender.id = sender;/信息表中的发射量发射地址advInfosender.energy = energy;advInfosender

9、.status = status;advInfosender.xpos = xpos;advInfosender.ypos = ypos;if (advInfosender.energy myStatus != STATUS_DEAD)this-deadNodes+;/消息的状态advInfosender.status = STATUS_DEAD;/此时的状态赋值为死亡status = STATUS_DEAD;(Node*) nodePtrsender)-myStatus = STATUS_DEAD;rating = energy - roundEnergyLoss;/发送功率advInfos

10、ender.rating = rating;ev BS received from sender status status rating: rating nrStatusRec+;/接收到的节点自加ev BS rec nrStatusRec nrNodes: nrNodes nrStatusRec = this-nrNodes - this-oldDeadNodes) /接收状态的节点=所有的节点已经死亡的节点this-oldDeadNodes += this-deadNodes;/已经死亡的节点数deadNodes为本轮死亡的节点ev nodes dead deadNodes all de

11、ad oldDeadNodes deadNodes);/记录当前死亡的节点*I*olddeadNodesVector.record(this-oldDeadNodes);/记录所有死亡的节点*I*this-deadRoundthis-roundsDone = this-deadNodes;if (this-halfDeadCtr (this-nrNodes / 10) & this-halfDead= 0) this-halfDead = roundsDone;ev calledEnd = 2;simulation.endRun();/f仿真结束?if (this-roundsDone nrR

12、ounds) /已经循环的次数=ev BS call compute bestn;/调用最优算法releaseConnections();/释放连接?computeBest();/用computerBdst算法选出簇头成簇resetStatus();/重设节点状态ev roundsDone+;/轮询的次数自加。evroundsDone:roundsDoneroundsDone this-nrRounds) this-calledEnd = 1;simulation.endRun();/仿真结束delete msg;void BS:resetStatus() /从置状态this-nrStatus

13、Rec = 0;/接收状态的节点this-deadNodes = 0;/死亡节点的数目/集中式的computer best/*#define FBEST8#define LBEST6void BS:computeBest() struct _adv bestFBEST;int i, j;double sum;double min = 999999999;int minPtr = 0;ev first 3 n;/put FBEST nodes in best,compute minfor (i = 0; i advInfoi + 2.rating advInfoi.rating;minPtr =

14、 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 = this-advInfoi + 2.rating;ev Id: besti.id rating: besti.rating n;for (i = FBEST; i nrNodes; i+) if (this-advInfoi + 2.rating min&this-advIn

15、foi+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 + 2.xpos;bestminPtr.ypos = this-advInfoi + 2.ypos;/ recompute min and minPtrminPtr = 0;min = 9999999;for (j = 0; j FBEST; j+) if (b

16、estj.rating min) 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 nodes (if not all are solar nodes)minPtr = 0;min = 99999999;for (i = 0; i FBEST; i+) sum = 0;for (j = 0; j FBEST; j+)

17、 if (j != i&besti.status=STATUS_OK&bestj.status=STATUS_OK)sum += (besti.xpos - bestj.xpos) * (besti.xpos- bestj.xpos) + (besti.ypos - bestj.ypos)* (besti.ypos - bestj.ypos);/ev distance sum sum id besti.id min min n;if (sum min) minPtr = i;min = sum;ev remove node bestminPtr.id from cluster n;if (mi

18、nPtr != FBEST - 1) bestminPtr.id = bestFBEST - 1.id;bestminPtr.xpos = bestFBEST - 1.xpos;bestminPtr.ypos = bestFBEST - 1.ypos;bestminPtr.rating = bestFBEST - 1.rating;/ print out FBEST bestev computeBest now:n;for (i = 0; i FBEST - 1; i+) ev Id: besti.id rating: besti.rating n;/ search for node clos

19、est 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;for (i = 0; i FBEST - 1; i+) for (j = 0; j FBEST - 1; j+) if (j != i) tmp = (besti.xpos - bestj.xpos) * (besti.xpos- bestj.xpos) + (besti.ypos - bestj.ypos)* (besti

20、.ypos - bestj.ypos);if (tmp min) 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- BORDER)close = n1;elseclose = n2;/ update array, remove closeev remove close bestclose.id n;if (close != F

21、BEST - 2) 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 FBEST - 2; i+) ev Id: besti.id rating: besti.rating n;for (i=0; i LBEST; i+) ev id: besti

22、.id xpos: besti.xpos ypos: besti.ypos rating: besti.rating n;int head;double tmp;ClusterHeadMessage*cmsg = new ClusterHeadMessage();ev send message to nodes announcing cluster heads setProto(CL_TOHEAD);cmsg-setSrcAddress(this-myId);for (int rec = 2; rec nrNodes + 2; rec+) Node* nPtr = (Node*) nodePt

23、rrec;head = 0;if (nPtr-myStatus != STATUS_DEAD) min = 9999999;for (j = 0; j xpos - bestj.xpos) * (nPtr-xpos- bestj.xpos) + (nPtr-ypos - bestj.ypos)* (nPtr-ypos - bestj.ypos);/ev cmp with bestj.id gives tmp ;if (tmp min) /ev new minn;min = tmp;head = bestj.id; else head = bestj.id;break;ev BS: rec re

24、c x,y xpos , ypos has head: head setCHead(rec,head);cmsg-setDestAddress(BROADCAST);cmsg-setDisplayString(b=,green,);for (int rec = 2; rec nrNodes + 2; rec+) if(Node*)nodePtrrec)-myStatus=STATUS_OK)send(cmsg-dup(), out, rec-1);delete cmsg;this-resetStatus();/ distributed computer best#define FBEST 20

25、 / best after first roundvoid BS:computeBest() struct _adv bestFBEST; int i, j; int min = 999999999; double T; double rd; int bestCtr=0; double h1; int cHeads; for (i = 0; i cHeadsRound;/当前轮数的簇头个数 for (int rec = 2; rec nrNodes + 2; rec+) Node *nPtr = (Node *) nodePtrrec; if (cHeads + this-deadNodes

26、= 50)/簇头的个数加死亡的节点的个数 ev cHeadsRound = 0; nPtr-lastClusterRound = 0; if (this-roundsDone % 20) = 0)/执行20轮之后,有点问题的程序。 nPtr-lastClusterRound = 0; ev cHeadsRound = 0; T = (double) this-P / (double) (1 - this-P * (double) (this-roundsDone) % 20);/阈值的设定? h1 = (this-P * (double) (this-cHeadsRound / 5);/计算簇

27、头的百分比? if (h1 1)/全部是簇头则错误了。 ev ERROR h1 h1 ERROR h1 cHeadRound cHeadsRound P: P calledEnd = 5; endSimulation(); ev round roundsDone cHeadsRound: cHeadsRound deadNodes deadNodes T: T P P n;for (int rec = 2; rec nrNodes + 2; rec+)/从第二个节点开始轮询所有的节点。 Node *nPtr = (Node *) nodePtrrec; rd = (double) rand()

28、 / (RAND_MAX + 1.0);/产生随机数函数,赋值给rd。 ev rd rec: rec cHeadsRound cHeadsRound T: T rd: rd lCR lastClusterRound myStatus != STATUS_DEAD & rd lastClusterRound = 0) / 簇头选择,并输出簇头。 bestbestCtr.xpos = nPtr-xpos; bestbestCtr.ypos = nPtr-ypos; bestbestCtr.id = rec; ev cHead: rec cHeadsRound+; if (bestCtr = FBE

29、ST)/? break; nPtr-lastClusterRound = this-roundsDone; this-nrHeadsthis-roundsDone = bestCtr; if (bestCtr = 0)/? int allDone = 1; if (this-cHeadsRound + this-deadNodes = this-numNodes)/? ev BS: go to resetn; goto reset;/ / check if not all are already done! for (int rec = 2; rec nrNodes + 2; rec+) No

30、de *nPtr = (Node *) nodePtrrec; if (nPtr-lastClusterRound = 1 & nPtr-myStatus != STATUS_DEAD) allDone = 0; break; if(allDone = 0) ev BS: restartn; goto restart;/ ev calledEnd = 5; endSimulation();/? if (this-roundsDone = 250) ev calledEnd = 0; endSimulation(); /发送消息给所有节点宣布簇头 int head; int tmp; /char str32; ClusterHeadMessage*cmsg=new ClusterHeadMessage(); evse

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 单元课程

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com