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

📄 basestation.cpp

📁 Bubble Oscillation Algorithm. It is used to implement balancing load traffic, which is similar to wh
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
//  Title:        A Cooperative Negotiation Approach for Cellular Coverage Control
//
//  Description:  Using cooperative negotiation to dynamically control the cell size and 
//                shapes according to different call traffic distributions. 
//
//  Copyright:    Copyright (c) 2002
//  Company:      Elec. Eng., Queen Mary, University of London
//  @author       Lin Du
//  @version      1.0
//
//////////////////////////////////////////////////////////////////////

#include "BaseStation.h"

/*
 * Default constructor and de-constructor.
 */
BaseStation::BaseStation() {
  capacity = 0;
  utilization = 0;
  avgTransPower = 1.0;
  this->ID = 0;
  this->Color = 0;
  for (int i=0; i<6; i++) {
    Neighbour[i] = NULL;
    nbrDemand[i] = 0;
  }
  agent = NULL;
}

/**
 * Initial the BS by X and Y in new coordinate.
 */
BaseStation::BaseStation(double X, double Y, double cap, int ID, int Color, MAS_DF *df) {
  utilization = 0;
  avgTransPower = 1.0;
  for (int i=0; i<6; i++) {
    Neighbour[i] = NULL;
    nbrDemand[i] = 0;
  }

  this->set(X, Y);
  this->capacity = cap;
  this->ID = ID;
  this->Color = Color;
  agent = new BSAgent(this, df);
}

BaseStation::~BaseStation() {
  if ( agent != NULL ) 
    delete agent;
}

/**
 * Get information of nearby TU
 */
void BaseStation::findPossibleTU( TrafficUnit *tu[TU_NUMBER], double simTime) {
  P_TU_MAP tu_m;
  unsigned long i;
  double dist;
	// calculate the average distance, for decision of w0
//	double avg = 0;
  
  // Reset the nbrDemand
  for (i=0; i<6; i++) {
    nbrDemand[i] = 0;
  }

  // Find the possible TU, and sort the possible TU by distance
  for(i=0; i<TU_NUMBER; i++) {
    if ( tu[i]->isTalking(simTime) && this->distance(*tu[i]) <= MAX_DISTANCE) {
      dist = this->distance(*tu[i]);
//			avg += dist;
      tu_m.insert( P_TU_MAP::value_type(dist, tu[i]) );
    }
  }

  // put them to possibleTU
  i = 0;
  possibleTU.clear();
  for(P_TU_MAP::iterator iter=tu_m.begin(); iter!=tu_m.end(); iter++ ) {
    dist = iter->first;
    possibleTU.push_back(iter->second);
    i++;
  }

// for debug
_ASSERT(i==possibleTU.size());
//avg /= (double)i;
//cout << "avg_dist=" << avg << endl;
// for debug

  // Calculate the local prospective TUs, and save them into prospectiveLoad.
  double tmp_load = 0;

  for(i=0; i<possibleTU.size(); i++) {
    double dist = this->distance( *possibleTU[i] );
    double ang = this->angle( *possibleTU[i] );
    int nbrID = TestBed::ang2index(ang);
    double dem = possibleTU[i]->getDemand();
    if (dist < SHARE_DISTANCE ) {
	    // increase the prospectiveLoad
      tmp_load += dem;
    }
		// Increase the nbrDemand (estimate value of the load of neighbour)
		nbrDemand[nbrID] += dem;
  }
  // Save it, so no need to re-calculate next time
  prospectiveLoad = tmp_load;

}

int BaseStation::getID() const {
  return ID;
}

/**
 * Return the color of this BS
 */
int BaseStation::getColor() const {
  return Color;
}

/**
 * Return the base station agent associated with this base station
 */
BSAgent *BaseStation::getAgent() const {
  return agent;
}

/**
 * Return the number of the blocked MS near this BS
 */
double BaseStation::getBlocked() {
  double blocked = 0;

  TrafficUnit *p_tu;
  for (unsigned int i = 0; i < possibleTU.size(); i++) {
    p_tu = possibleTU[i];
    if( !p_tu->isServed() ) {
      blocked++;
    }
  } 

  return blocked;  
}

/**
 * Return the number of possible TUs
 */
double BaseStation::getProspectiveLoad() {
  return prospectiveLoad;
}

/**
 * Return the local perceived demand of a neighbour
 */
double BaseStation::getNbrDemand(int nbr) {
  return this->nbrDemand[nbr];
}

/**
 * Reset this base station
 */
void BaseStation::reset() {
  utilization = 0;
}

/** 
 * Read currScheme from BSAgent, and apply it to base station
 */
void BaseStation::applyCurrScheme() {
	TrafficUnit *p_tu;
  long len;
  // Apply scheme stored in BSAgent
  P_TU_V &curr = agent->getCurrScheme();
  len = curr.size();
  // Calculate the utilization, and assign tu
  for(int j=0; j<len; j++) {    
		p_tu = curr[j];
    p_tu->assigned(this);
    utilization += p_tu->getDemand();
  }

// for debug
_ASSERT(utilization <= capacity);
}

/**
 * Perform the assignment, only used for conventional allocation schemes (circular)
 */
void BaseStation::assignment() {
  double dist, demand;
  TrafficUnit *p_tu;
  unsigned int i;

  // perform the assignment
  for(i=0; i<possibleTU.size(); i++ ) {
    p_tu = possibleTU[i];
    dist = this->distance(*p_tu);
    demand = p_tu->getDemand();
    if( dist <= SHARE_DISTANCE && utilization+demand < capacity) {
			p_tu->assigned(this);
			utilization += demand;
    }
  }

// for debug
  _ASSERT(utilization <= capacity);
}

/**
 * save the assignment results into currentScheme, only used for the first time.
 * For the rest times, it is copied from BSAgent to BS.
 */
void BaseStation::saveAssignment() {
	P_TU_V &curr = agent->getCurrScheme();
  // Check if current LAS is empty or not, if empty, save the assignment to it
  if( curr.size() == 0 ) {
    // copy the served tu from possibleTU to currScheme
    for(unsigned long i=0; i<possibleTU.size(); i++ ) {
			TrafficUnit *p_tu = possibleTU[i];
      if( p_tu->isServed (this) ) 
        curr.push_back(p_tu);
    }
  }
}

/**
 * Return the physical capacity of this BS
 */
double BaseStation::getCapacity() const {
  return capacity;
}

/**
 * Return the utilization of this BS
 */
double BaseStation::getUtilization() const {
  return utilization;
}

/**
 * Find the adjacent base stations. used for initialsation
 */
void BaseStation::findNeighbour(BaseStation *bs[BS_NUMBER]) {
  double ang;

  for (int i=0; i<BS_NUMBER; i++) {
    if ( fabs(this->distance(*bs[i]) - 1.732 * CELL_RADIUS) < 0.2 * CELL_RADIUS ) {
      ang = this->angle(*bs[i]);
      Neighbour[TestBed::ang2index(ang)] = bs[i];
    }
  }
}

/*
 * friend functions
 */
bool operator==(const BaseStation &bs1, const BaseStation &bs2) {
  return bs1.getID() == bs2.getID();
}

bool operator==(const BaseStation &bs1, int ID) {
  return bs1.getID() == ID;
}

bool operator!=(const BaseStation &bs1, const BaseStation &bs2) {
  return bs1.getID() != bs2.getID();
}

bool operator!=(const BaseStation &bs1, int ID) {
  return bs1.getID() != ID;
}

⌨️ 快捷键说明

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