《LEACH算法源代码.doc》由会员分享,可在线阅读,更多相关《LEACH算法源代码.doc(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-作者xxxx-日期xxxxLEACH算法源代码【精品文档】/* * * 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:initia
2、lize() int i;cModule* parent = getParentModule();/消息参数的访问 调用cModule 的par()成员函数可以访问模块指针:/cPar& delayPar = par(delay);cPar类是一个存储值的对象,/它支持数据类型,指针值可以这样读:/周围的复合模块可以通过 parentModule()成员函数访问: cModule *parent = parentModule();/例如,父模块的参数像这样被访问: double timeout = parentModule()-par( tthis-myId = par(id);this-xp
3、os = par(xpos);this-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
4、-P = 0.05;/?this-cHeadsRound = 0;/每一轮簇头的个数this-roundEnergyLoss = 80001.0;/?/*记录图像用的变量的名字*加的*/roundVector.setName(round);/轮数deadNodesVector.setName(nodes dead);/死亡的节点olddeadNodesVector.setName(all dead);/所有死亡的节点nrheadVector.setName(headnumber);/簇头的个数tVector.setName(T);/运行的时间lastrecVector.setName(last
5、rec);/最后记录的消息cheadVector.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
6、-setKind(SMSG_INIT);/初始化消息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) /消息处
7、理函数if (msg-isSelfMessage() /本身的身消息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()
8、;/发送信息能量cluster = (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 = e
9、nergy;advInfosender.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 - roundEnerg
10、yLoss;/发送功率advInfosender.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 de
11、ad deadNodes all dead 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 (
12、this-roundsDone nrRounds) /已经循环的次数=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()
13、 /从置状态this-nrStatusRec = 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 advIn
14、foi.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 = this-advInfoi + 2.rating;ev Id: besti.id rating: besti.rating n;for (i = FBEST; i nrNodes; i+) if (this-advInfoi + 2.ra
15、ting 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 + 2.xpos;bestminPtr.ypos = this-advInfoi + 2.ypos;/ recompute min and minPtrminPtr = 0;min = 9999999;for (j = 0;
16、 j FBEST; j+) if (bestj.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 (
17、j = 0; j FBEST; j+) 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 fr
18、om cluster n;if (minPtr != 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;/ s
19、earch 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;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 -
20、bestj.ypos)* (besti.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.
21、id n;if (close != FBEST - 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 LBES
22、T; i+) ev id: besti.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* nP
23、tr = (Node*) nodePtrrec;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;
24、break;ev BS: rec rec 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
25、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; int cHeads; for (i = 0; i cHeadsRound;/当前轮数的簇头个数 for (int rec = 2; rec nrNodes + 2; rec+) Node *nPtr = (Node *) nodePtrrec; if (cHea
26、ds + this-deadNodes = 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-c
27、HeadsRound / 5);/计算簇头的百分比? 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 *) nodePtrre
28、c; rd = (double) rand() / (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 cHeadsRo
29、und+; if (bestCtr = FBEST)/? 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+) Node *nPtr = (Node *) nodePtrrec; if (nPtr-lastClusterRound = 1 & nPtr-myStatus != STATUS_DEA