NS2之移动节点能量模型

移动节点能量函数分析代码:

#include <stdarg.h>
#include <float.h>

#include "random.h"
#include "energy-model.h"
#include "mobilenode.h"
#include "god.h"

static class EnergyModelClass : public TclClass
{
public:
	EnergyModelClass ():TclClass ("EnergyModel") {}
	TclObject *create (int argc, const char *const *argv) {
		if (argc == 8) {
			MobileNode *n=(MobileNode*)TclObject::lookup(argv[4]);
			return (new EnergyModel(n, atof(argv[5]), 
						atof(argv[6]), atof(argv[7])));
		} else {
			Tcl::instance().add_error("Wrong arguments to ErrorModel");
			return 0;
		}
	}
} class_energy_model;

void EnergyModel::DecrTxEnergy(double txtime, double P_tx) 
{
	double dEng = P_tx * txtime;
	if (energy_ <= dEng)
		energy_ = 0.0;
	else
		energy_ = energy_ - dEng;
	if (energy_ <= 0.0)
		God::instance()->ComputeRoute();
}


void EnergyModel::DecrRcvEnergy(double rcvtime, double P_rcv) 
{
	double dEng = P_rcv * rcvtime;
	if (energy_ <= dEng)
		energy_ = 0.0;
	else
		energy_ = energy_ - dEng;
	if (energy_ <= 0.0)
		God::instance()->ComputeRoute();
}

void EnergyModel::DecrIdleEnergy(double idletime, double P_idle) 
{
	double dEng = P_idle * idletime;
	if (energy_ <= dEng)
		energy_ = 0.0;
	else
		energy_ = energy_ - dEng;
	if (energy_ <= 0.0)
		God::instance()->ComputeRoute();
}

// XXX Moved from node.cc. These wireless stuff should NOT stay in the 
// base node.
void EnergyModel::start_powersaving()
{
	snh_ = new SoftNeighborHandler(this);
	snh_->start();
	
	afe_ = new AdaptiveFidelityEntity(this);
	afe_->start();

	state_ = EnergyModel::POWERSAVING;
	state_start_time_ = Scheduler::instance().clock();
}

void EnergyModel::set_node_sleep(int status)
{
	Tcl& tcl=Tcl::instance();
	//static float last_time_gosleep;
	// status = 1 to set node into sleep mode
	// status = 0 to put node back to idle mode.
	// time in the sleep mode should be used as credit to idle 
	// time energy consumption
	if (status) {
		last_time_gosleep = Scheduler::instance().clock();
		//printf("id=%d : put node into sleep at %f\n",
		// address_,last_time_gosleep);
		sleep_mode_ = status;
		if (node_->exist_namchan()) 
			tcl.evalf("%s add-mark m1 blue hexagon",node_->name());
	} else {
		sleep_mode_ = status;
		if (node_->exist_namchan()) 
			tcl.evalf("%s delete-mark m1", node_->name()); 
		//printf("id= %d last_time_sleep = %f\n",
		// address_, last_time_gosleep);
		if (last_time_gosleep) {
			total_sleeptime_ += Scheduler::instance().clock() -
				last_time_gosleep;
			last_time_gosleep = 0;
		}
	}	
}

void EnergyModel::set_node_state(int state)
{
	switch (state_) { 
	case POWERSAVING:
	case WAITING:
		state_ = state;
		state_start_time_ = Scheduler::instance().clock();
		break;
	case INROUTE:
		if (state == POWERSAVING) {
			state_ = state;
		} else if (state == INROUTE) {
			// a data packet is forwarded, needs to reset 
			// state_start_time_
			state_start_time_= Scheduler::instance().clock();
		}
		break;
	default:
		printf("Wrong state, quit...\n");
		abort();
	}
}

void EnergyModel::add_neighbor(u_int32_t nodeid)
{
	neighbor_list_item *np;
	np = neighbor_list.head;
	for (; np; np = np->next) {
		if (np->id == nodeid) {
			np->ttl = maxttl_;
			break;
		}
	}
	if (!np) {      // insert this new entry
		np = new neighbor_list_item;
		np->id = nodeid;
		np->ttl = maxttl_;
		np->next = neighbor_list.head;
		neighbor_list.head = np;
		neighbor_list.neighbor_cnt_++;
	}
}

void EnergyModel::scan_neighbor()
{
	neighbor_list_item *np, *lp;
	if (neighbor_list.neighbor_cnt_ > 0) {
		lp = neighbor_list.head;
		np = lp->next;
		for (; np; np = np->next) {
			np->ttl--;
			if (np->ttl <= 0){
				lp->next = np->next;
				delete np;
				np = lp;
				neighbor_list.neighbor_cnt_--;
			} 
			lp = np;
		}
		// process the first element
		np = neighbor_list.head;
		np->ttl--;
		if (np->ttl <= 0) {
			neighbor_list.head = np->next;
			delete np;
			neighbor_list.neighbor_cnt_--;
		}
	}
}


void SoftNeighborHandler::start()
{
	Scheduler::instance().schedule(this, &intr, CHECKFREQ);
}

void SoftNeighborHandler::handle(Event *)
{
	Scheduler &s = Scheduler::instance();
	nid_->scan_neighbor();
	s.schedule(this, &intr, CHECKFREQ);
}

void AdaptiveFidelityEntity::start()
{
	sleep_time_ = 2;
	sleep_seed_ = 2;
	idle_time_ = 10;
	nid_->set_node_sleep(0);
	Scheduler::instance().schedule(this, &intr, 
				       Random::uniform(0, idle_time_));
}

void AdaptiveFidelityEntity::handle(Event *)
{
	Scheduler &s = Scheduler::instance();
	int node_state = nid_->state();
	switch (node_state) {
	case EnergyModel::POWERSAVING:
		if (nid_->sleep()) {
			// node is in sleep mode, wake it up
			nid_->set_node_sleep(0);
			adapt_it();
			s.schedule(this, &intr, idle_time_);
		} else {
			// node is in idle mode, put it into sleep
			nid_->set_node_sleep(1);
			adapt_it();
			s.schedule(this, &intr, sleep_time_);
		}
		break;
	case EnergyModel::INROUTE:
		// 100s is the maximum INROUTE time.
		if (s.clock()-(nid_->state_start_time()) < 
		    nid_->max_inroute_time()) {
			s.schedule(this, &intr, idle_time_);
		} else {
			nid_->set_node_state(EnergyModel::POWERSAVING);
			adapt_it();
			nid_->set_node_sleep(1);
			s.schedule(this, &intr, sleep_time_); 
		}
		break;
	case EnergyModel::WAITING:
		// 10s is the maximum WAITING time
		if (s.clock()-(nid_->state_start_time()) < MAX_WAITING_TIME) {
			s.schedule(this, &intr, idle_time_);
		} else {
			nid_->set_node_state(EnergyModel::POWERSAVING);
			adapt_it();
			nid_->set_node_sleep(1);
			s.schedule(this, &intr, sleep_time_); 
		}
		break;
	default:
		fprintf(stderr, "Illegal Node State!");
		abort();
	}
}

void AdaptiveFidelityEntity::adapt_it()
{
	float delay;
	// use adaptive fidelity
	if (nid_->adaptivefidelity()) {
		int neighbors = nid_->getneighbors();
		if (!neighbors) 
			neighbors = 1;
		delay = sleep_seed_ * Random::uniform(1,neighbors); 
	      	set_sleeptime(delay);
	}
}


 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实验内容1. 在上一个实验的基础上,建立ad hoc节点拓扑模型,网络中节点均具有路由转发功能; 2. 进入~/tarballs/ns-allinone-3.2x/ns-3.2x/examples/wireless$ 目录,在wireless文件夹下,有很多wifi传输的示例。在wifi-simple-adhoc-grid.cc代码基础上修改,文件拷贝到scratch文件夹下,重命名为你的姓名缩写+学号,例如学生姓名钢铁侠,学号20191314,c文件命名为gtx20191314.cc; 3. 读懂wifi-simple-adhoc-grid.cc代码,掌握在NS-3中网络拓扑布局,ad hoc传输方式、网络路由的简单配置等,成功运行wifi-simple-adhoc-grid.cc; 4. 掌握拓扑建模方法,在wifi-simple-adhoc-grid.cc代码中扩展建立到100个节点节点初始呈现网格布局; 5. 深入研究移动模型建模方法,完成对移动模型参数设置,使得节点移动在可视化界面更加明显; 6. 深入研究信道模型建模方法,在代码中完成对信道模型的替换,例如Friis、LogDistancePropagationLossModel信道模型。通过射频参数,了解发射机、路径损耗和接收机间增益的计算方法。可以根据参数配置想要的发射距离; 7. 掌握网路层路由配置方法,可以配置多种ad hoc路由模式,例如OLSR、aodv等。查阅不同路由协议的工作原理和方法; 8. 掌握统计模型的使用方法,可以统计相应节点的时延、丢包率等重要指标; 9. 掌握能耗模型的使用方法,配置节点能量,设备能量,可以统计节点能量消耗; 10. 掌握统计数据作图方法,对于获得的统计数据,图形化的形式得出结果。
ns-3中提供了三种节点移动模型:静态模型、随机游走模型移动模型(Mobility Model)。其中移动模型又分为以下几种: 1. Constant Position Model:恒定位置模型节点一直保持不动。 2. Random Walk Model:随机游走模型节点在平面上以随机步长和方向运动。 3. Gauss-Markov Model:高斯-马尔科夫模型节点在平面上以高斯分布的随机步长和方向运动。 4. Random Direction Model:随机方向模型节点在平面上以随机方向和常量速度移动。 5. Constant Velocity Model:恒定速度模型节点以恒定速度沿着一个随机方向移动。 6. Random Waypoint Model:随机航点模型节点随机选择目标点并以一定速度移动。 7. Random Walk 2D Model:二维随机游走模型节点在平面上以随机步长和方向运动。 可以通过创建相应的移动模型对象,并将其与节点关联,来实现节点移动。例如,使用Random Walk 2D Model来实现节点移动,可以按照以下步骤进行: 1. 创建一个移动模型对象: ```c++ Ptr<RandomWalk2dMobilityModel> model = CreateObject<RandomWalk2dMobilityModel> (); ``` 2. 设置节点的初始位置和速度: ```c++ model->SetPosition (Vector3D (0, 0, 0)); model->SetVelocity (Vector (5, 0, 0)); ``` 3. 将移动模型对象与节点关联: ```c++ node->AggregateObject (model); ``` 在仿真过程中,节点将按照指定的移动模型进行移动。可以使用仿真器的Schedule方法来控制节点移动时间。例如,以下代码将节点在10秒后开始移动,并在20秒后停止移动: ```c++ Simulator::Schedule (Seconds (10.0), &RandomWalk2dMobilityModel::Start, model); Simulator::Schedule (Seconds (20.0), &RandomWalk2dMobilityModel::Stop, model); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值