📄 calculations.cc
字号:
#include "Calculations.h"#include "CommonIncludes.h"#include "Gear_packet_m.h"NeighbhorInfo::NeighbhorInfo(){ NeighNodeId=0; Energy=0; NeighPos=NULL;}NeighbhorInfo::NeighbhorInfo(unsigned int nodeId, Location* loc, double energy ){ char s[10]; sprintf(s,"%i",nodeId); setName(s); NeighNodeId=nodeId; NeighPos=loc; Energy=energy;}unsigned int NeighbhorInfo::getNodeId(){ return NeighNodeId;}Location* NeighbhorInfo::getNeighPos(){ return NeighPos;}double NeighbhorInfo::getEnergy(){ return Energy;}void NeighbhorInfo::setNodeId(unsigned int NodeId){ NeighNodeId=NodeId;}void NeighbhorInfo::setNeighPos(Location* loc){ NeighPos=loc;}void NeighbhorInfo::setEnergy(double energy){ Energy=energy;}//We do not need the nodeId in this methodLocation* NeighbhorInfo::getLocation(unsigned int nodeId){ return NeighPos;}Print::Print(){ }void Print::PrintHeader(Header* hdr){ //printf("SourceLocationX %d, SourceLocationY %d, SourceNodeId= %d \n ",(hdr->srcLoc).x,hdr->srcLoc.y,hdr->srcNodeId); //printf(" ttl %d, DestinationLocationX %d,DestinationLocationY %d, NextHopDestId= %d \n ",hdr->ttl,hdr->destLoc.x,hdr->destLoc.y,hdr->NextHopDestId); }Heuristics::Heuristics(){ }double Heuristics::Distance(int xPos,int yPos,int destXPos, int destYPos){ double dist=sqrt(((destXPos-xPos)*(destXPos-xPos))+((destYPos-yPos)*(destYPos-yPos))); return dist; }void Heuristics::LearnedCostEntry(Region* reg,Cost* cost ){ cArray *RegionArray=reg->getRegionCost(); RegionArray->set(cost); //simtime_t now = simTime(); //reg->setLastUpdatedTime(now);}Cost* Heuristics::EstimateCost(NeighbhorInfo * neigh,Location* RegionLoc, double maxEnergy, double minEnergy, double maxDis, double minDis){ Location* loc=neigh->getNeighPos(); double distance = Distance(loc->x, loc->y, RegionLoc->x, RegionLoc->y); //printf("Region Location XPos= %d , YPos %d Neighbor Location X %d Y %d \n\n",RegionLoc->x,RegionLoc->y,loc->x,loc->y); double cost = Distance(loc->x,loc->y,RegionLoc->x,RegionLoc->y); //double cost=((ALPHA*Distance(loc->x,loc->y,RegionLoc->x,RegionLoc->y))+((1-ALPHA)*(neigh->getEnergy()))); //double d_normalizedEnergy = (neigh->getEnergy()-minEnergy)/(maxEnergy-minEnergy); //double d_normalizedDistance = (distance - minDis)/(maxDis-minDis); //double cost = (ALPHA*d_normalizedDistance)+(1-ALPHA)*(d_normalizedEnergy); //printf("ALPHA=%f, Distance =%f, Energy=%f \n ",ALPHA,Distance(loc->x,loc->y,RegionLoc->x,RegionLoc->y),neigh->getEnergy()); Cost *c=new Cost(neigh->getNodeId(),cost); //printf("Cost Object added NodeId= %d , cost is %f \n\n",neigh->getNodeId(),cost); return c;}double Heuristics::EnergyMaxValue(cArray* a_neigh_list){ double maxEnergy=0.0; for(int i=0;i<a_neigh_list->items();i++) { //printf("Neigh_list ANKUR nodeId \n\n "); NeighbhorInfo * neigh=(NeighbhorInfo*)a_neigh_list->get(i); double tempEnergy = neigh->getEnergy(); if( tempEnergy>maxEnergy) maxEnergy = tempEnergy; } return maxEnergy;}double Heuristics::EnergyMinValue(cArray* a_neigh_list){ double minEnergy=0.0; for(int i=0;i<a_neigh_list->items();i++) { //printf("Neigh_list ANKUR nodeId \n\n "); NeighbhorInfo * neigh=(NeighbhorInfo*)a_neigh_list->get(i); double tempEnergy = neigh->getEnergy(); if( tempEnergy<minEnergy) minEnergy = tempEnergy; } return minEnergy;}double Heuristics::DisMaxValue(cArray* a_neigh_list, int RegionX, int RegionY){ double maxDis=0.0; for(int i=0;i<a_neigh_list->items();i++) { //printf("Neigh_list ANKUR nodeId \n\n "); NeighbhorInfo * neigh=(NeighbhorInfo*)a_neigh_list->get(i); int x = ((Location *)neigh->getNeighPos())->x; int y = ((Location *)neigh->getNeighPos())->y; double distance = Distance(x,y,RegionX, RegionY); if( distance>maxDis) maxDis = distance; } return maxDis;}double Heuristics::DisMinValue(cArray* a_neigh_list, int RegionX, int RegionY){ double minDis=0.0; for(int i=0;i<a_neigh_list->items();i++) { //printf("Neigh_list ANKUR nodeId \n\n "); NeighbhorInfo * neigh=(NeighbhorInfo*)a_neigh_list->get(i); double x = ((Location *)neigh->getNeighPos())->x; double y = ((Location *)neigh->getNeighPos())->y; double distance = Distance(x,y,RegionX, RegionY); if( distance<minDis) minDis = distance; } return minDis;}Region::Region(Location *location,int range){ char *name; char s[255]; sprintf(s,"%d,%d,%d",location->x,location->y,range); //strcpy(name,s); setName(s); //printf("Region::Region before strcpy........................\n\n"); strcpy(RegionName,s); //printf("Region::Region fterstrcpy........................\n\n"); loc=location; SrcNodeList=new cArray(); RegionCost=new cArray(); QueryList= new cArray();}Region::Region(){}QueryObject::QueryObject(char* id,unsigned int srcId,unsigned int destId,unsigned int finalSrc){ //QueryId=id; strcpy(queryId, id); SrcId=srcId; DestId=destId; //char s[10]; //sprintf(s,"%d",id); setName(queryId); FinalSrc=finalSrc; isAlive = false;}QueryObject::QueryObject(){ //queryId=0; SrcId=0; DestId=0; isAlive = false;}QueryObject::QueryObject(char* qid){ strcpy(queryId, qid); //char s[10]; //sprintf(s,"%d",qid); setName(qid); isAlive = false;}QueryObject::~QueryObject(){ //printf("QueryObject----I am dieing\n");}int QuId::queryID=0;char* QueryObject::getQueryId(){ return queryId;}unsigned int QueryObject::getSrcId(){ return SrcId;}unsigned int QueryObject::getDestId(){ return DestId;}unsigned int QueryObject::getFinalSrc(){ return FinalSrc; }void QueryObject::setFinalSrc(unsigned int nodeId){ FinalSrc = nodeId;} void QueryObject::setQueryId(char* qid){ strcpy(queryId, qid);}void QueryObject::setSrcId(unsigned int srcId){ SrcId=srcId;}void QueryObject::setDestId(unsigned int destId){ DestId=destId;}void QueryObject::setIsAlive(bool flag){ isAlive = flag;}bool QueryObject::getIsAlive(){ return isAlive;}Location* Region::getRegionLocation(){ return loc;}Region::~Region(){ //printf("\n\nRegion--am also dying");}char* Region::getRegionName(){ return RegionName;}cArray* Region::getRegionCost(){ return RegionCost;}cArray* Region::getSrcNodeList(){ return SrcNodeList;}cArray* Region::getQueryList(){ return QueryList;}cArray* Region::addToQueryList(QueryObject* qryObject){ QueryList->takeOwnership(false); QueryList->set(qryObject);}simtime_t Region::getDataUpdateTime(){ return DataUpdateTime;}void Region::setDataUpdateTime(simtime_t upTime){ DataUpdateTime = upTime;} Cost::Cost(unsigned int nodeId,double nodeCost){ char s[10]; sprintf(s,"%d",nodeId); setName(s); NodeId=nodeId; cost=nodeCost;}unsigned int Cost:: getNodeId(){ return NodeId;}double Cost::getNodecost(){ return cost;}void Cost::setNodeId(unsigned int nodeId){ NodeId=nodeId;}void Cost::setNodecost(double nodecost){ cost=nodecost;}void Region:: addCost(Cost *cost){ RegionCost->add(cost);}IdWrapper::IdWrapper(unsigned int nodeid){ char s[10]; sprintf(s,"%d",nodeid); setName(s); NodeId=nodeid;}unsigned int IdWrapper::getNodeId(){ return NodeId; }queryEnforceTime::queryEnforceTime(char s[50], simtime_t simT){ sTime = simT; strcpy(strQry, s); setName(s);}simtime_t queryEnforceTime::getQueryEnforceTime(){ return sTime;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -