Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

PostureTransition Class Reference
[mobility - modules handling the mobility of the hostsMoBAN - Classes providing a mobility model for Wireless Body Area Networks]

Class to provide spatial and temporal correlation in the posture selection process of the MoBAN mobility model. This class obtains and stores Markovian transition matrices. There is also the possibility to get a steady state vector. In this case, the closest transition matrix to the default Makov matrix is extracted which satisfies the given steady state vector. The class also receives the defined area types and time domains as well as given space-time domains during the initialization phase. During the simulation run, the class provide a functions to return the corresponding markov matrix for a given time and location. It will be used whenever a new posture is going to be selected. More...

#include <PostureTransition.h>

List of all members.

Classes

struct  AreaBound
 Data type for one instance of the area (space) boundary. More...
struct  AreaType
 Data type for one instance of area type. More...
struct  CombinationType
 Data type for one instance of space-time combination. More...
struct  TimeBound
 Data type for one instance of the time boundary. More...
struct  TimeDomainType
 Data type for one instance of time domain. More...
struct  TransMatrix
 Data type for one instance of Markov transition matrix. More...

Public Member Functions

 PostureTransition (int)
 Construct a posture transition object. The parameter is the number of postures which is the dimension of all matrices.
int addMatrix (std::string, double **, bool)
 Receives a transition matrix and add to the list.
int addSteadyState (std::string, double *)
 Receives a steady state vector, extracts the corresponding transition matrix considering the default matrix, and add to the list of given matrices.
int addAreaType (std::string)
 Adds a area type to the list with the given name and returns the index of this area type in the list.
bool setAreaBoundry (int, Coord, Coord)
 Adds the given boundary to the existing area type specified by the given ID .
int addTimeDomain (std::string)
 Adds a time domain to the list with the given name and returns the index of the this time domain in the list.
bool setTimeBoundry (int, simtime_t, simtime_t)
 Adds the given boundary to the existing time domain specified by the given ID .
bool addCombination (std::string, std::string, std::string)
 Adds a space-time combination to the list.
double ** getMatrix (simtime_t, Coord)
 Gets a time and location, and returns the corresponding Markov transition matrix.

Protected Types

typedef std::vector
< TransMatrix * > 
TransMatrixList
 Data type for a list of Markov transition matrices.
typedef std::vector< AreaType * > AreaTypeList
 Data type for the list of area types.
typedef std::vector
< TimeDomainType * > 
TimeDomainList
 Data type for the list of time domains.
typedef std::vector
< CombinationType * > 
CombinationList
 Data type for the list of space-time combinations.

Protected Member Functions

double ** extractMatrixFromSteadyState (double *)
 Gets a steady state vector and return a matrix which is as close as posible to the default matrix and satisfies the given steady state.
int findTimeDomain (simtime_t)
 Gets a time and finds the ID of the containing time domain if there is. If not, return -1.
int findAreaType (Coord)
 Gets a location and finds the ID of the containing area type if there is. If not, return -1.
bool isMarkovian (double **)
 Checks if a matrix can be a Markov transition matrix. All elements should be in the range [0,1] and elements of each column of the matrix should add up to 1.
bool isMarkovian (double *)
 Checks if a vector can be the steady state of a Markov chain. All elements should be in the range [0,1] and the sum of elements should be 1.
void multMatrix (double **, double **, double **)
 Multiplies two matrices with dimension numPos*numPose .
void addMatrix (double **, double **, double **)
 Adds two matrices with dimension numPos*numPose .
void subtractMatrix (double **, double **, double **)
 Subtracts two matrices with dimension numPos*numPose .
void multVector (double *, double **)
 Multiply a vector of size numPos with its transpose.

Protected Attributes

int numPos
 Number of postures.
int defaultMatrixID
 The index of the default (base) transition matrix. If no default is set, the first matrix is supposed as the default. Default matrix is used for the cases that a time or space domain does not lie in any given area types or time domains. It is also used for generating the transition matrix in the case that a steady state vector is given for a space-time domain.
TransMatrixList matrixList
 The list of all given transition matrices.
AreaTypeList areaTypeList
 The list of all defined area types.
TimeDomainList timeDomainList
 The list of all defined time domains.
CombinationList combinationList
 The list of all given space-time combinations.

Detailed Description

Class to provide spatial and temporal correlation in the posture selection process of the MoBAN mobility model. This class obtains and stores Markovian transition matrices. There is also the possibility to get a steady state vector. In this case, the closest transition matrix to the default Makov matrix is extracted which satisfies the given steady state vector. The class also receives the defined area types and time domains as well as given space-time domains during the initialization phase. During the simulation run, the class provide a functions to return the corresponding markov matrix for a given time and location. It will be used whenever a new posture is going to be selected.

Author:
Majid Nabi

Definition at line 62 of file PostureTransition.h.


Constructor & Destructor Documentation

PostureTransition::PostureTransition ( int  numPosture  ) 

Construct a posture transition object. The parameter is the number of postures which is the dimension of all matrices.

Constructor function of the class. It sets the value for t he number of posture. It also suppose the first given transition matrix as default. However, during parsing the xml configuration file, if a matrix has attribute type with value "default", it will be considered as the default (base) transition matrix.

Definition at line 56 of file PostureTransition.cc.

References defaultMatrixID, and numPos.

{
  numPos = numPosture;
  defaultMatrixID = 0; // if no default matrix found, the first one will be supposed as the default matrix.
}


Member Function Documentation

int PostureTransition::addAreaType ( std::string  name  ) 

Adds a area type to the list with the given name and returns the index of this area type in the list.

Creates a new area type instance and adds it to the list. The boundaries of the area type is empty now. It will be filled later. The function returns the index of the new area type in the list as its output.

Definition at line 150 of file PostureTransition.cc.

References areaTypeList.

Referenced by MoBANCoordinator::readConfigurationFile().

{

  //Check if the name is repetitive
  AreaTypeList::const_iterator areaIt;
  for (areaIt = areaTypeList.begin(); areaIt != areaTypeList.end(); areaIt++)
  {
    if ((*areaIt)->name == name )
    {
      std::string str = "There are multiple area types with the same name: " + name + " in the configuration file!";
      opp_error (str.c_str());
    }
  }

  AreaType* area = new AreaType;
  area->name = name;
  areaTypeList.push_back(area);
  return areaTypeList.size()-1;
}

bool PostureTransition::addCombination ( std::string  areaName,
std::string  timeName,
std::string  matrixName 
)

Adds a space-time combination to the list.

This function creates a new space-time combination instance and adds it to the combinations list. It checks if the given names for area type, time domain, and matrix are previously defined and exist in the corresponding lists. Note that at least area type or time domain should have been specified for a combination. Otherwise the combination is not meaningful. if for example a combination has no area type and just has specified time domain, it means that for the whole simulation area, it will be the same and the proper matrix is selected based on the time.

Definition at line 227 of file PostureTransition.cc.

References areaTypeList, combinationList, matrixList, and timeDomainList.

Referenced by MoBANCoordinator::readConfigurationFile().

{
  int thisID;
  CombinationType* comb = new CombinationType;
  comb->areaID = -1;
  comb->timeID = -1;
  comb->matrixID = -1;

  // look for matching area type name.
  thisID = 0;
  AreaTypeList::const_iterator areaIt;
  for (areaIt = areaTypeList.begin(); areaIt != areaTypeList.end(); areaIt++)
  {
    if (areaName == (*areaIt)->name )
    {
      comb->areaID = thisID;
      break;
    }
    ++thisID;
  }

  // in the input name is empty, it means that no area type is specified for this combination.
  if (comb->areaID == -1 && !areaName.empty())
  {
    std::string str = "Undefined area type name is given in a combinations: " + areaName + ", " + timeName + ", " + matrixName;
    opp_error (str.c_str());
  }


  // look for matching time domain name.
  thisID = 0;
  TimeDomainList::const_iterator timeIt;
  for (timeIt = timeDomainList.begin(); timeIt != timeDomainList.end(); timeIt++)
  {
    if (timeName == (*timeIt)->name )
    {
      comb->timeID = thisID;
      break;
    }
    ++thisID;
  }
  if (comb->timeID == -1 && !timeName.empty())
  {
    std::string str = "Undefined time domain name is given in a combinations: " + areaName + ", " + timeName + ", " + matrixName;
    opp_error (str.c_str());
  }


  if (comb->areaID == -1 && comb->timeID == -1)
    opp_error ("Both area type and time domain is unspecified in a combination." );

  // look for matching transition matrix name.
  thisID = 0;
  TransMatrixList::const_iterator matrixIt;
  for (matrixIt = matrixList.begin(); matrixIt != matrixList.end(); matrixIt++)
  {
    if (matrixName == (*matrixIt)->name )
    {
      comb->matrixID = thisID;
      break;
    }
    ++thisID;
  }
  if (comb->matrixID == -1)
    opp_error ("Undefined matrix name is given in the combinations" );

  combinationList.push_back(comb);

  return true;
}

int PostureTransition::addMatrix ( std::string  name,
double **  matrix,
bool  thisDefault 
)

Receives a transition matrix and add to the list.

This function initiates a new instance of markov matrix with the given matrix. Note that it copies the matrix into the created matrix. The function first verifies if the given matrix can be a Markov transition matrix.

Definition at line 66 of file PostureTransition.cc.

References defaultMatrixID, isMarkovian(), matrixList, and numPos.

{

  //check if the name is repetitive
  TransMatrixList::const_iterator matrixIt;
  for (matrixIt = matrixList.begin(); matrixIt != matrixList.end(); matrixIt++)
  {
    if ((*matrixIt)->name == name )
    {
      std::string str = "There are multiple matrices with the same name: " + name + " in the configuration file!";
      opp_error (str.c_str());
    }
  }


  // verify if the given matrix is Markovian
  if ( !isMarkovian(matrix) )
  {
    std::string str = "Given transition matrix " + name + " is not Markovian!";
    opp_error (str.c_str());
  }

  TransMatrix* mat = new TransMatrix;

  mat->name = name;
  mat->matrix = new double* [numPos];
  for (int i=0;i<numPos;++i)
  {
    mat->matrix[i] = new double [numPos];
    for (int j=0;j<numPos;++j)
      mat->matrix[i][j] = matrix[i][j];
  }

  matrixList.push_back(mat);

  if (thisDefault)
    defaultMatrixID = matrixList.size()-1;

  return 0;
}

void PostureTransition::addMatrix ( double **  mat1,
double **  mat2,
double **  res 
) [protected]

Adds two matrices with dimension numPos*numPose .

Function to add two matrix with the known dimensions as number of postures.

Definition at line 442 of file PostureTransition.cc.

References numPos.

Referenced by extractMatrixFromSteadyState(), and MoBANCoordinator::readConfigurationFile().

{
  int i,j;
  for(i=0; i < numPos; i++)
  {
    for(j=0; j < numPos ; j++)
      res[i][j] = mat1[i][j] + mat2[i][j];
  }

}

int PostureTransition::addSteadyState ( std::string  name,
double *  iVector 
)

Receives a steady state vector, extracts the corresponding transition matrix considering the default matrix, and add to the list of given matrices.

This function creates a new instance of markov matrix to be filled with a derived matrix from the given steady state vector. The function first verifies if the given vector can be a steady state vector. Then extracts a markov matrix based on that and adds it to the list of given matrices.

Definition at line 112 of file PostureTransition.cc.

References extractMatrixFromSteadyState(), isMarkovian(), matrixList, and numPos.

Referenced by MoBANCoordinator::readConfigurationFile().

{
  //check if the name is repetitive
  TransMatrixList::const_iterator matrixIt;
  for (matrixIt = matrixList.begin(); matrixIt != matrixList.end(); matrixIt++)
  {
    if ((*matrixIt)->name == name )
    {
      std::string str = "There are multiple matrices with the same name: " + name + " in the configuration file!";
      opp_error (str.c_str());
    }
  }

  // check if the given matrix is Markovian
  if ( !isMarkovian(iVector) )
  {
    std::string str = "Given steady state vector " + name + " cannot be true!";
    opp_error (str.c_str());
  }

  // make a local copy of the input steady state vector
  double *steady = new double[numPos];
  for (int i=0; i < numPos; ++i)
    steady[i] = iVector[i];

  TransMatrix* mat = new TransMatrix;
  mat->name = name;
  mat->matrix = extractMatrixFromSteadyState(steady);

  matrixList.push_back(mat);

  return 0;
}

int PostureTransition::addTimeDomain ( std::string  name  ) 

Adds a time domain to the list with the given name and returns the index of the this time domain in the list.

Creates a new time domain instance and adds it to the list. The boundaries of the time domain is empty now. It will be filled later. The function returns the index of the time domain in the list as its output.

Definition at line 188 of file PostureTransition.cc.

References timeDomainList.

Referenced by MoBANCoordinator::readConfigurationFile().

{
  //Check if the name is repetitive
  TimeDomainList::const_iterator timeIt;
  for (timeIt = timeDomainList.begin(); timeIt != timeDomainList.end(); timeIt++)
  {
    if ((*timeIt)->name == name )
    {
      std::string str = "There are multiple time domains with the same name: " + name + " in the configuration file!";
      opp_error (str.c_str());
    }
  }

  TimeDomainType* time = new TimeDomainType;
  time->name = name;
  timeDomainList.push_back(time);
  return timeDomainList.size()-1;
}

double ** PostureTransition::extractMatrixFromSteadyState ( double *  vec  )  [protected]

Gets a steady state vector and return a matrix which is as close as posible to the default matrix and satisfies the given steady state.

This function receives a steady state vector and extracts a Markovian matrix which is as close as possible to the default markov matrix and satisfies the given steady state vector.

Definition at line 485 of file PostureTransition.cc.

References addMatrix(), defaultMatrixID, matrixList, multMatrix(), multVector(), numPos, and subtractMatrix().

Referenced by addSteadyState().

{
  int i,j;
  double** dafaultMat;

  //make output matrix and an identity matrix and a temp
  double** mat= new double* [numPos];
  double** temp1= new double* [numPos];
  double** temp2= new double* [numPos];
  double** temp3= new double* [numPos];
  double** identity = new double* [numPos];
  int** change = new int* [numPos];
  for (int i=0;i<numPos;++i)
  {
    mat[i] = new double [numPos];
    temp1[i] = new double [numPos];
    temp2[i] = new double [numPos];
    temp3[i] = new double [numPos];
    identity[i] = new double [numPos];
    change[i] = new int [numPos];

  }

  for(i=0; i < numPos; i++)
    for(j=0; j < numPos ; j++)
      if (i==j)
        identity[i][j] = 1;
      else
        identity[i][j] = 0;


  double* sum= new double [numPos];
  int* changeSum= new int [numPos];


  dafaultMat = matrixList.at(defaultMatrixID)->matrix;


  for (int numTry=0;numTry<400;++numTry)
  {
    subtractMatrix(identity,dafaultMat,temp1);
    multVector(vec,temp2);
    multMatrix(temp1,temp2,temp3);
    addMatrix(dafaultMat,temp3,mat);

    //remember if it has not changed
    for(i=0; i < numPos; i++)
      for(j=0; j < numPos ; j++)
        change[i][j] = 1;

    for(j=0; j < numPos; j++)
      for(i=0; i < numPos ; i++)
      {
        if ( mat[i][j] < 0 ){
          mat[i][j] = 0;
          change[i][j]=0;
        }
        if ( mat[i][j] > 1 ){
          mat[i][j] = 1;
          change[i][j]=0;
        }
      }


    for(j=0; j < numPos; j++)
    {
      sum[j] = 0;
      changeSum[j]=0;
      for(i=0; i < numPos ; i++)
      {
        sum[j] += mat[i][j];
        changeSum[j] += change[i][j];
      }
    }

    for(j=0; j < numPos; j++)
      for(i=0; i < numPos ; i++)
      {
        if (change[i][j] == 1)
          mat[i][j] = mat[i][j]+ (1-sum[j])/changeSum[j];
      }

    dafaultMat = mat;
  }

  for(j=0; j < numPos; j++)
    for(i=0; i < numPos ; i++)
    {
      if ( mat[i][j] < 0 )
        mat[i][j] = 0;
      if ( mat[i][j] > 1 )
        mat[i][j] = 1;
    }


  EV << "Generated Markov matrix from the steady state: "<< endl;
  for (int k=0;k < numPos; ++k)
  {
    for (int f=0; f<numPos ;++f)
      EV << mat[k][f]<<"       ";
    EV << endl;
  }

  for (int i=0;i<numPos;++i)
  {
    delete temp1[i]; delete temp2[i]; delete temp3[i];
    delete identity[i];
    delete change[i];
  }
  delete temp1; delete temp2; delete temp3;
  delete identity;
  delete change;
  delete sum;
  delete changeSum;


  return mat;
}

int PostureTransition::findAreaType ( Coord  iLocation  )  [protected]

Gets a location and finds the ID of the containing area type if there is. If not, return -1.

Looks for the first containing area type for the given location. It return the Id of the found area type. If no area type is found which contains the given location, it returns -1.

Definition at line 356 of file PostureTransition.cc.

References areaTypeList, and Coord::isInBoundary().

Referenced by getMatrix().

{
  int locationID=0;
  AreaTypeList::const_iterator areaIt;
  for (areaIt = areaTypeList.begin(); areaIt != areaTypeList.end(); areaIt++)
  {
    std::vector<AreaBound*> boundList = (*areaIt)->boundries;

    std::vector<AreaBound*>::const_iterator bound;
    for (bound = boundList.begin(); bound != boundList.end(); bound++)
    {
      if ( iLocation.isInBoundary( (*bound)->low ,(*bound)->high ) )
        return locationID;
    }
    ++locationID;
  }
  EV << "Area Type not found" << endl;
  return -1;
}

int PostureTransition::findTimeDomain ( simtime_t  iTime  )  [protected]

Gets a time and finds the ID of the containing time domain if there is. If not, return -1.

Looks for the first containing time domain for the given time instance. It return the Id of the found time domain. If no time domain is found which contains the given time instance, it returns -1.

Definition at line 332 of file PostureTransition.cc.

References timeDomainList.

Referenced by getMatrix().

{
  int timeID=0;
  TimeDomainList::const_iterator timeIt;
  for (timeIt = timeDomainList.begin(); timeIt != timeDomainList.end(); timeIt++)
  {
    std::vector<TimeBound*> boundList = (*timeIt)->boundries;

    std::vector<TimeBound*>::const_iterator bound;
    for (bound = boundList.begin(); bound != boundList.end(); bound++)
    {
      if ( iTime >= (*bound)->low && iTime < (*bound)->high)
        return timeID;
    }
    ++timeID;
  }
  EV << "Time domain not found" << endl;
  return -1;
}

double ** PostureTransition::getMatrix ( simtime_t  iTime,
Coord  iLocation 
)

Gets a time and location, and returns the corresponding Markov transition matrix.

This function is actually the main usage of this class. It gets a time instance and a location within the simulation area, and then looks for the first fitting combination. If found, it returns the specified Markov transition matrix for that combination as its output. If no combination is found, it returns the default matrix.

Definition at line 303 of file PostureTransition.cc.

References combinationList, defaultMatrixID, findAreaType(), findTimeDomain(), Coord::info(), and matrixList.

Referenced by MoBANCoordinator::selectPosture().

{
  int timeID,locationID,matrixID;

  timeID = findTimeDomain(iTime);
  locationID = findAreaType(iLocation);


  matrixID = defaultMatrixID;

  CombinationList::const_iterator combIt;
  for (combIt = combinationList.begin(); combIt != combinationList.end(); combIt++)
  {
    if ( (*combIt)->timeID == timeID && (*combIt)->areaID == locationID)
    {
      matrixID = (*combIt)->matrixID;
      break;
    }
  }

  EV << "The corresponding Markov matrix for time" << iTime.dbl() <<" and location " << iLocation.info() << " is: " << matrixList.at(matrixID)->name << endl;

  return matrixList.at(matrixID)->matrix;
}

bool PostureTransition::isMarkovian ( double **  matrix  )  [protected]

Checks if a matrix can be a Markov transition matrix. All elements should be in the range [0,1] and elements of each column of the matrix should add up to 1.

Verifies if a matrix can be a Markovian transition matrix. Each element of the matrix should be in the range [0 1]. Further, all elements of each column should adds up to one.

Definition at line 380 of file PostureTransition.cc.

References FWMath::close(), and numPos.

Referenced by addMatrix(), and addSteadyState().

{
  double sumCol;
  for (int j=0;j<numPos;++j)
  {
    sumCol = 0;
    for (int i=0;i<numPos;++i)
    {
      if (matrix[i][j] < 0 || matrix[i][j] > 1)
        return false;
      sumCol += matrix[i][j];
    }

    if (!FWMath::close(sumCol , 1.0 ))
      return false;
  }

  return true;
}

bool PostureTransition::isMarkovian ( double *  vec  )  [protected]

Checks if a vector can be the steady state of a Markov chain. All elements should be in the range [0,1] and the sum of elements should be 1.

Verifies if a vector can be the steady state of a Markov model. Each element of the matrix should be in the range [0 1]. Further, the sum of all elements should be one.

Definition at line 404 of file PostureTransition.cc.

References FWMath::close(), and numPos.

{
  double sumCol=0;
  for (int i=0;i<numPos;++i)
  {
    if (vec[i] < 0 || vec[i]> 1)
      return false;
    sumCol += vec[i];
  }

  if ( !FWMath::close(sumCol , 1.0 ) )
    return false;
  else
    return true;
}

void PostureTransition::multMatrix ( double **  mat1,
double **  mat2,
double **  res 
) [protected]

Multiplies two matrices with dimension numPos*numPose .

Function to multiply two matrix with the known dimensions as number of postures.

Definition at line 423 of file PostureTransition.cc.

References numPos.

Referenced by extractMatrixFromSteadyState().

{

  int i,j,l;
  for(i=0; i < numPos; i++)
  {
    for(j=0; j < numPos ; j++)
    {
      res[i][j]=0;
      for(l=0; l < numPos ; l++)
        res[i][j] += mat1[i][l] * mat2[l][j];
    }
  }

}

void PostureTransition::multVector ( double *  vec,
double **  res 
) [protected]

Multiply a vector of size numPos with its transpose.

Function to multiply a vector by its transpose (pi . pi^T). The size in equal to the number of postures.

Definition at line 470 of file PostureTransition.cc.

References numPos.

Referenced by extractMatrixFromSteadyState().

{
  int i,j;
  for(i=0; i < numPos; i++)
  {
    for(j=0; j < numPos ; j++)
      res[i][j] = vec[i] * vec[j];
  }

}

bool PostureTransition::setAreaBoundry ( int  id,
Coord  lowBound,
Coord  highBound 
)

Adds the given boundary to the existing area type specified by the given ID .

This function gets an index of an existing area type and adds the given boundary to the boundary list of that area type.

Definition at line 173 of file PostureTransition.cc.

References areaTypeList.

Referenced by MoBANCoordinator::readConfigurationFile().

{
  AreaBound* bound=new AreaBound;
  bound->low = lowBound;
  bound->high = highBound;

  areaTypeList.at(id)->boundries.push_back(bound);

  return true;
}

bool PostureTransition::setTimeBoundry ( int  id,
simtime_t  lowBound,
simtime_t  highBound 
)

Adds the given boundary to the existing time domain specified by the given ID .

This function gets an index of an existing time domain and adds the given boundary to the boundary list of that time domain.

Definition at line 210 of file PostureTransition.cc.

References timeDomainList.

Referenced by MoBANCoordinator::readConfigurationFile().

{
  TimeBound* bound=new TimeBound;
  bound->low = lowBound;
  bound->high = highBound;

  timeDomainList.at(id)->boundries.push_back(bound);

  return true;
}

void PostureTransition::subtractMatrix ( double **  mat1,
double **  mat2,
double **  res 
) [protected]

Subtracts two matrices with dimension numPos*numPose .

Function to subtract two matrix with the known dimensions as number of postures.

Definition at line 456 of file PostureTransition.cc.

References numPos.

Referenced by extractMatrixFromSteadyState().

{
  int i,j;
  for(i=0; i < numPos; i++)
  {
    for(j=0; j < numPos ; j++)
      res[i][j] = mat1[i][j] - mat2[i][j];
  }

}


The documentation for this class was generated from the following files: