




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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( BS);/定義簡單模塊 (1) 直接或間接定義一個 CSimpleModule 的子類;/(2) 以 define_Module() 或 define_Module_Like()宏注冊之;/*第一個執(zhí)行的函數*/void BS:initialize() int i;cModule*
2、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-yp
3、os = 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-cHead
4、sRound = 0;/每一輪簇頭的個數this-roundEnergyLoss = 80001.0;/?/*記錄圖像用的變量的名字*加的*/roundVector.setName(round);/輪數deadNodesVector.setName(nodes dead);/死亡的節(jié)點olddeadNodesVector.setName(all dead);/所有死亡的節(jié)點nrheadVector.setName(headnumber);/簇頭的個數tVector.setName(T);/運行的時間lastrecVector.setName(lastrec);/最后記錄的消息cheadVect
5、or.setName(all head num);/所有的節(jié)點個數for (i = 0; i setGateSize(out, nrGates + 1);/需要 設置參數值或門向量大小?this-setGateSize(in, nrGates + 1);/創(chuàng)建消息cMessage* cmsg = new cMessage(init);/創(chuàng)建消息/cMessage是OMNET的一個中心類。CMessage和子類的對象可以模擬一些東西:事/件;消息;包;幀;蜂窩;網絡中的位或信號傳輸;系統(tǒng)中的實體傳輸等/等。一個cMessage對象有許多屬性: cmsg-setKind(SMSG_INIT);/初
6、始化消息scheduleAt(simTime(), cmsg);/立即發(fā)送給基站自己evid:myId par(numNodes);ev BS numNodes is: numNodes par(trRange);ev BS trRange is: trRange n;/總的輪數是:for(i=1;igetName(),node)=0)/遍歷節(jié)點,把模塊指針填充nodePtr(Node*)mod)-myId=(Node*)mod;/Id標識,指針/*第六個執(zhí)行的函數*/void BS:handleMessage(cMessage* msg) /消息處理函數if (msg-isSelfMessa
7、ge() /本身的身消息ev BS: got self message type getKind() getKind() = SMSG_INIT) ev initNodes(); else /其他消息no selfmessage來自節(jié)點的消息或者是簇頭的消息if (ClusterMessage*) msg)-getProto() = CL_TOBS) /簇頭到基站的消息(紅色的)。ev status message getSrcAddress();/發(fā)送信息地址energy = (Status2BSMessage*) msg)-getEnergy();/發(fā)送信息能量cluster = (Sta
8、tus2BSMessage*) msg)-getCluster();/簇status = (Status2BSMessage*) msg)-getStatus();/狀態(tài)xpos = (Status2BSMessage*) msg)-getXpos();ypos = (Status2BSMessage*) msg)-getYpos(); if (energy halfDead = 0)this-halfDeadCtr+;/? advInfosender.id = sender;/信息表中的發(fā)射量發(fā)射地址advInfosender.energy = energy;advInfosender.st
9、atus = status;advInfosender.xpos = xpos;advInfosender.ypos = ypos;if (advInfosender.energy myStatus != STATUS_DEAD)this-deadNodes+;/消息的狀態(tài)advInfosender.status = STATUS_DEAD;/此時的狀態(tài)賦值為死亡status = STATUS_DEAD;(Node*) nodePtrsender)-myStatus = STATUS_DEAD;rating = energy - roundEnergyLoss;/發(fā)送功率advInfosend
10、er.rating = rating;ev BS received from sender status status rating: rating nrStatusRec+;/接收到的節(jié)點自加ev BS rec nrStatusRec nrNodes: nrNodes nrStatusRec = this-nrNodes - this-oldDeadNodes) /接收狀態(tài)的節(jié)點=所有的節(jié)點已經死亡的節(jié)點this-oldDeadNodes += this-deadNodes;/已經死亡的節(jié)點數deadNodes為本輪死亡的節(jié)點ev nodes dead deadNodes all dead
11、oldDeadNodes deadNodes);/記錄當前死亡的節(jié)點*I*olddeadNodesVector.record(this-oldDeadNodes);/記錄所有死亡的節(jié)點*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 nrRoun
12、ds) /已經循環(huán)的次數=ev BS call compute bestn;/調用最優(yōu)算法releaseConnections();/釋放連接?computeBest();/用computerBdst算法選出簇頭成簇resetStatus();/重設節(jié)點狀態(tài)ev roundsDone+;/輪詢的次數自加。evroundsDone:roundsDoneroundsDone this-nrRounds) this-calledEnd = 1;simulation.endRun();/仿真結束delete msg;void BS:resetStatus() /從置狀態(tài)this-nrStatusRec
13、 = 0;/接收狀態(tài)的節(jié)點this-deadNodes = 0;/死亡節(jié)點的數目/集中式的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 = i;
14、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-advInfoi
15、+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 (best
16、j.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+) if
17、 (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 (minPt
18、r != 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 closest
19、 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.yp
20、os - 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 != FBES
21、T - 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.id
22、 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*) nodePtrre
23、c;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 rec x
24、,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)/簇頭的個數加死亡的節(jié)點的個數 ev cHeadsRound = 0; nPtr-lastClusterRound = 0; if (this-roundsDone % 20) = 0)/執(zhí)行20輪之后,有點問題的程序。 nPtr-lastClusterRound = 0; ev cHeadsRound = 0; restart:/goto跳轉語句的標識符.重新設置開始的狀態(tài) T = (double) this-P / (double) (1 - this-P * (double) (this-roundsDone) % 20);/閾值的設定? h1 = (this-P * (doubl
27、e) (this-cHeadsRound / 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+)/從第二個節(jié)點開始輪詢所有的節(jié)點。 Node *nPtr = (Node *)
28、 nodePtrrec; 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: re
29、c cHeadsRound+; 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 r
30、ec = 2; rec nrNodes + 2; rec+) Node *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(); /發(fā)送消息給
31、所有節(jié)點宣布簇頭 int head; int tmp; /char str32; ClusterHeadMessage*cmsg=new ClusterHeadMessage(); evsend message to nodes announcing cluster headssetProto(CL_TOHEAD); cmsg-setSrcAddress(this-myId); for (int rec = 2; rec nrNodes + 2; rec+)/節(jié)點選擇簇頭形成簇。 Node *nPtr = (Node *) nodePtrrec; head = 0; if (nPtr-myStatus != STATUS_DEAD) /ev rec: rec; 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
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 45206-2025道地藥材生產技術規(guī)程丹參
- 幾分包合同范本
- 農村耕地流轉合同范本
- 產品免責合同范本
- 倉儲臨時合同范本
- 化妝產品合同范本
- 信息驗收合同范例
- 書法裝裱售賣合同范本
- 農村集體資源招租合同范本
- 免除追償工傷合同范本
- 2024年-ITSS新標準培訓學習材料
- 第2課《讓美德照亮幸福人生》第2框《做守家庭美德的好成員》-【中職專用】《職業(yè)道德與法治》同步課堂課件
- (正式版)SHT 3227-2024 石油化工裝置固定水噴霧和水(泡沫)噴淋滅火系統(tǒng)技術標準
- 2024屆廣東省深圳市中考物理模擬試卷(一模)(附答案)
- 前庭功能鍛煉科普知識講座
- 供應鏈戰(zhàn)略布局與區(qū)域拓展案例
- 上海話培訓課件
- 注塑車間績效考核方案
- 初中英語閱讀理解專項練習26篇(含答案)
- 誦讀經典傳承文明課件
- 高中數學選擇性必修3 教材習題答案
評論
0/150
提交評論