⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 link.cpp

📁 传感器网络的可靠路由算法
💻 CPP
字号:
#include "stdafx.h"
#include "Network.h"

extern NODE_TYPE nodeT[4000];
extern NETWORK_TYPE net;

double adistance(int i, int j);


//the quality of the link from from to to 
double linkquality(int from, int to)
{
  
  int i;
  for (i=0; i<nodeT[from].neighbornum;i++)
  {
    if(nodeT[from].neighbor[i]== to) return nodeT[from].LQI2neighbor[i];
  }
  return -1;
}


//wehther a particular link is unidirectional 

bool unidirectional(int parent, int child)
{
  if ((linkquality(parent,child)==-1)||(linkquality(child, parent)==-1))
	   return true;
  return false;
}


//physical distance 

double adistance(int i,int j)
{ return sqrt(double(((nodeT[i].x-nodeT[j].x)*(nodeT[i].x-nodeT[j].x)+(nodeT[i].y-nodeT[j].y)*(nodeT[i].y-nodeT[j].y))));
}


//link delay expected value 

double linkDelay(int from, int to)
{
      double p,q;
	      p = linkquality(from,to);
		  q = linkquality(to,from);
        assert(p>0);
		assert(q>0);
		return (1/p-1)*(SEND_TIMEOUT+1)+1;
}


//sendcost expected vlaue 

double linkCost(int from, int to,int dest){
    double p,q;

    assert(from < net.NUM_NODES && dest < net.NUM_NODES && to < net.NUM_NODES);

	switch(net.LINK_COST_MODEL){
		case EQUAL:
			if ((linkquality(from,to)==-1)||(linkquality(from, to)==-1)) return MAXCOST;
			return 1.0;
			break;
		case ETX:
		if ((linkquality(from,to)==-1)||(linkquality(to,from)==-1)) return MAXCOST;
		  p = linkquality(from,to);
		  q = linkquality(to,from);
		  return 1/(p*q)+net.LAMBDA/q;    
		break;
		case LAZY:
		if ((linkquality(from,to)==-1)||(linkquality(to,from)==-1)) return MAXCOST;
		  p = linkquality(from,to);
		  q = linkquality(to,from);
		return 1/p + (1-p)*net.LAMBDA/(p*q);
		break;
		case GF_LINK:{
		  double a,b,c;
		  a = adistance(from, to);
		  b = adistance(from, dest);
		  c = adistance(to,dest);
		  /*
		  double cos = (a*a+b*b-c*c)/(2*a*b);
		  if (cos <=0){ return -1; printf("Backward\n");}
		  assert( (b-c) >= 0);
		  */
		  if((b-c) < 0) { return MAXCOST; printf("Backward\n");}
		  
		  if (b>c)
		  /* to discuss */
		  return 1/((b-c) * linkquality(from,to));

		   if (b==c)

		  
		  break;
		}
		default: exit(-1);
	}
	return MAXCOST;
}




/* build the link quality table and cost table */
void BuildNeighborHood(){

	int i,j;

    double templink;




	for (i=0;i<net.NUM_NODES;i++)
	{ int now=0;
		for (j=0;j<net.NUM_NODES;j++){	
			if (i==j)
				continue;
		    switch(net.LINK_MODEL){
				case PERFECT:
				templink = prrdistanceSimple(adistance(i,j));
				break;
				case REAL_INDEPENDENT:
				templink = prrdistance(adistance(i,j));
				break;
				default: assert(false);
			}
			if ( templink >=CUT_OFF_THRESHOLD && (j!=i))
			{
			 nodeT[i].neighbor[now]=j;
			 nodeT[i].LQI2neighbor[now] = templink;
			 now++;
			}
		}

	    nodeT[i].neighbornum=now;

		assert(now < net.NUM_NODES);

		#ifdef GUI_ENABLED
		DrawNode(i,3,true);
		#endif
	}
	//printf("Successfully Build NeighborHood\n");
}


bool LinkSimulation(int sender, int receiver){

	double rn = double(rand())/RAND_MAX;

	if(unidirectional(sender,receiver) == true) return false;

	if( rn <= linkquality(sender, receiver))
		return true;
	else
		return false;
}



//legacy code not really used 
int TransmitSimulation(int sender, int next_id){
	
	switch(net.ROUTING_TYPE){
		case GF:
		case MH_A:
           if(LinkSimulation(sender,next_id)) return next_id;
		   else return INVALID_ID;
		   break;
		default: assert(false);	

	}
	return INVALID_ID;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -