enMovGen-rel1.2.cc File Reference

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <fstream>
#include <istream>
#include <iostream>
#include <cstdlib>
#include <ios>
#include "socialNet.h"
#include <stack>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_multifit_nlin.h>

Defines

#define RAND_INT(l, h)   (((double)(random() * ((double)(h)-(l)+1.0))) + (l))

Functions

double getRandomDouble ()
void setSeed (unsigned int init)
double generateRandomDouble (double min, double max)
int generateRandomInteger (int min, int max)
void header ()
void usage ()
void generateXMLHeader (FILE *outputFileXML, double sideLength_WholeAreaX, double sideLength_WholeAreaY, double transmissionRange, int numberOfNodes)
void generateXMLSetNodePosition (FILE *outputFileXML, int node, double x, double y, double sideLength_WholeAreaX, double sideLength_WholeAreaY)
void generateXMLSetNewGoal (FILE *outputFileXML, int node, double time, double newGoalX, double newGoalY, double velocity, double sideLength_WholeAreaX, double sideLength_WholeAreaY)
int main (int argc, char *argv[])

Define Documentation

#define RAND_INT ( l,
 )     (((double)(random() * ((double)(h)-(l)+1.0))) + (l))

Function Documentation

double generateRandomDouble ( double  min,
double  max 
)

00104                                                      {
00105         //double number=getRandomDouble() * (max-min+1.0) + min;
00106         double number=getRandomDouble()*(max-min)+min;  
00107         
00108         //double number=min+((max-min)*getRandomDouble()/RAND_MAX);
00109         return number;
00110 }

int generateRandomInteger ( int  min,
int  max 
)

00113                                             {
00114         
00115         int number=(int)floor(generateRandomDouble((double)min,(double)max)+1);
00116         //printf("%s %d\n","The random generated integer number is",number);
00117 
00118         return number;
00119 }

void generateXMLHeader ( FILE *  outputFileXML,
double  sideLength_WholeAreaX,
double  sideLength_WholeAreaY,
double  transmissionRange,
int  numberOfNodes 
)

00168                                                                                                                                                      {
00169         
00170         fprintf(outputFileXML,"<?xml version=\"1.0\"?>\n");
00171         fprintf(outputFileXML,"<simulation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
00172         fprintf(outputFileXML,"xsi:noNamespaceSchemaLocation=\"http://www.i-u.de/schools/hellbrueck/ansim/xml/simulation.xsd\">\n");
00173         fprintf(outputFileXML,"\t<parameter>\n");
00174         fprintf(outputFileXML,"\t\t<area_shape>rectangle</area_shape>\n");
00175         fprintf(outputFileXML,"\t\t<xsize>%f</xsize>\n",sideLength_WholeAreaX);
00176         fprintf(outputFileXML,"\t\t<ysize>%f</ysize>\n",sideLength_WholeAreaY);
00177         fprintf(outputFileXML,"\t\t<extension_factor>1</extension_factor>\n");
00178         fprintf(outputFileXML,"\t\t<numberOfNodes>%d</numberOfNodes>\n",numberOfNodes);
00179         fprintf(outputFileXML,"\t\t<range>%f</range>\n",transmissionRange);
00180         fprintf(outputFileXML,"\t\t<mobility_model>socialFoundedMM</mobility_model>\n");
00181         fprintf(outputFileXML,"\t</parameter>\n");
00182                         
00183 }

void generateXMLSetNewGoal ( FILE *  outputFileXML,
int  node,
double  time,
double  newGoalX,
double  newGoalY,
double  velocity,
double  sideLength_WholeAreaX,
double  sideLength_WholeAreaY 
)

00199                                                                                                                                                                                       {
00200         fprintf(outputFileXML,"\t\t<position_change>\n");
00201         fprintf(outputFileXML,"\t\t\t<node_id>%d</node_id>\n",node);
00202         fprintf(outputFileXML,"\t\t\t<start_time>%f</start_time>\n",time);
00203         fprintf(outputFileXML,"\t\t\t<destination>\n");
00204         fprintf(outputFileXML,"\t\t\t\t<xpos>%f</xpos>\n",newGoalX-sideLength_WholeAreaX/2.0);
00205         fprintf(outputFileXML,"\t\t\t\t<ypos>%f</ypos>\n",newGoalY-sideLength_WholeAreaY/2.0);
00206         fprintf(outputFileXML,"\t\t\t</destination>\n");
00207         fprintf(outputFileXML,"\t\t\t<velocity>%f</velocity>\n",velocity);
00208         fprintf(outputFileXML,"\t\t</position_change>\n");
00209 }

void generateXMLSetNodePosition ( FILE *  outputFileXML,
int  node,
double  x,
double  y,
double  sideLength_WholeAreaX,
double  sideLength_WholeAreaY 
)

00187                                                                                                                                               {
00188         fprintf(outputFileXML,"\t\t<node>\n");
00189         fprintf(outputFileXML,"\t\t\t<node_id>%d</node_id>\n",node);
00190         fprintf(outputFileXML,"\t\t\t<position>\n");
00191         fprintf(outputFileXML,"\t\t\t\t<xpos>%f</xpos>\n",x-sideLength_WholeAreaX/2.0);
00192         fprintf(outputFileXML,"\t\t\t\t<ypos>%f</ypos>\n",y-sideLength_WholeAreaY/2.0);
00193         fprintf(outputFileXML,"\t\t\t</position>\n");
00194         fprintf(outputFileXML,"\t\t</node>\n");
00195 }

double getRandomDouble (  ) 

00078                           {
00079 
00080 /* input parameters*/
00081 
00082 /* static int a = 1588635695, m = 4294967291U, q = 2, r = 1117695901; */
00083 
00084 static unsigned int a = 1223106847, m = 4294967291U, q = 3, r = 625646750;
00085 
00086 /* static unsigned int a = 279470273, m = 4294967291U, q = 15, r = 102913196;*/
00087 /* static unsigned int a = 1583458089, m = 2147483647, q = 1, r = 564025558; */
00088 /* static unsigned int a = 784588716, m = 2147483647, q = 2, r = 578306215;  */
00089 /* static unsigned int a = 16807, m = 2147483647, q = 127773, r = 2836;      */
00090 /* static unsigned int a = 950706376, m = 2147483647, q = 2, r = 246070895;  */
00091 
00092    SEED = a*(SEED % q) - r*(SEED / q);
00093    return ((double)SEED / (double)m);
00094 }

void header (  ) 

00123               {
00124         printf("%s","\nMobility Patterns Generator for OMNET++ simulator\n");
00125         printf("%s","Enhanced Community Based Mobility Model\n");
00126         printf("%s","Based on the Community Based Mobility Model of Mirco Musolesi\n");
00127         printf("%s","Version 1.2 January 2012\n");         
00128         printf("%s","Copyright (C) Nikolaos Vastardis University of Essex\n");
00129         printf("%s","              nvasta@essex.ac.uk\n");
00130 }

int main ( int  argc,
char *  argv[] 
)

00212                                   {
00213 
00214         //----------------------------
00215         //Definitions of the constants
00216         //----------------------------
00217 
00218         //Number of hosts
00219         int numHosts=30;
00220 
00221         //Connection threshold
00222         double connectionThreshold=0.1;
00223 
00224         //Length of the sides of the area
00225         double sideLength_WholeAreaX=2000.0;
00226         double sideLength_WholeAreaY=2000.0;
00227 
00228         //Radius of the transmission area
00229         double radius=50.0;
00230 
00231         //Number of rows
00232         int numberOfRows=10;
00233 
00234         //Number of columns
00235         int numberOfColumns=10;
00236         
00237         //SimulationTime
00238         double totalSimulationTime=3000.0;
00239 
00240         //RefreshTime
00241         double stepInterval=1;
00242 
00243         //Low bound speed of the host
00244         double minHostSpeed=1.0;
00245         double maxHostSpeed=5.00;
00246 
00247         //Reconfiguration interval
00248         //it defines the interval between two reconfigurations
00249         double reconfigurationInterval=10000.0;
00250 
00251         //Rewiring probability
00252         double rewiringProb=0.2;
00253         
00254         //NumberOfGroups
00255         int numberOfGroups=4;
00256 
00257         //Simulation time
00258         double simTime=0.0;
00259         
00260         //Number of travellers
00261         int numberOfTravellers=0;
00262 
00263         //speed of the travellers
00264         double travellerSpeed=20.0;
00265     
00266         //Verbose on/off
00267         bool verbose=false;
00268         
00269         //generateXML on/off
00270         bool generateXML=false;
00271         string xmlFile="socMov.xml";
00272         
00273         //Girvan Newman clustering algorithm on off
00274         bool girvanNewmanOn=true;
00275         
00276         //Colocation traces on/off
00277         bool colocationTracesOn=false;
00278         
00279         //Communities traces on/off
00280         bool communitiesTracesOn=false;
00281         
00282         //Deterministic on/off
00283         bool deterministicOn=false;
00284         
00285         //Drift
00286         float drift;
00287         
00288         //Debug variables
00289         bool firstTime=true;
00290         double timeFirstReconfiguration;
00291         
00292         //Adjacency matric input
00293         bool read_adjacency=false;
00294         string inputFile;
00295 
00296         //Adjacency matric output
00297         bool output_adjacency=false;
00298 
00299         //Double pause time MAX
00300         int pauseMAX=0;
00301         
00302         //Number of the reconfiguration epochs
00303         int numOfEpochs=0;
00304                                                         
00305         //***********************/
00306         // Retrieving input values
00307         // **********************/
00308 
00309         char ch;
00310         int inputSeed=0;        
00311                 
00312         while ((ch = getopt (argc, argv, "hx:n:s:S:p:l:d:X:Y:c:C:R:G:r:T:t:w:v:g:a:d:A:b:i:P:o:e:"))!=EOF)
00313                 
00314                  switch(ch) {
00315                          case 'h':
00316                                                 //shows helps
00317                                                 header();
00318                                                 usage();
00319                                                 exit(1);
00320                          case 'x':
00321                                                 //generate XML file using ANSim format
00322                                                 generateXML=true;
00323                                                 //If there is file name following
00324                                                 if(optarg[0]!='-') {
00325                                                         xmlFile.assign(optarg);
00326                                                         xmlFile.append(".xml");
00327                                                 }
00328                                                 else
00329                                                         --optind;
00330                                                 break;
00331                          case 'n':
00332                                                 //sets the number of hosts
00333                                                 numHosts=atoi(optarg);
00334                                                 break;
00335                          case 's':
00336                                                 //sets lower bound of the speed of the hosts
00337                                                 minHostSpeed=(double)atof(optarg);
00338                                                 break;
00339                          case 'S':                                                                               
00340                                                 //sets upper bound of the speed of the hosts
00341                                                 maxHostSpeed=(double)atof(optarg);
00342                                                 break;
00343                          case 'p':
00344                                                 //sets the connection threshold
00345                                                 connectionThreshold=(double)atof(optarg);
00346                                                 break;
00347                          case 'X':
00348                                                 //sets the side length of the whole area (x coordinates)
00349                                                 sideLength_WholeAreaX=(double)atof(optarg);
00350                                                 break;
00351                          case 'Y':
00352                                                 //sets the side length of the whole area (y coordinates)
00353                                                 sideLength_WholeAreaY=(double)atof(optarg);
00354                                                 break;
00355                          case 'R':
00356                                                 //sets the number of rows
00357                                                 numberOfRows=atoi(optarg);
00358                                                 break;
00359                          case 'C':
00360                                                 //sets the number of columns
00361                                                 numberOfColumns=atoi(optarg);
00362                                                 break;  
00363                          case 'T':
00364                                                 //sets the transmission range (radius)
00365                                                 radius=(double)atof(optarg);                    
00366                                                 break;
00367                          case 't':
00368                                                 //sets total simulation time
00369                                                 totalSimulationTime=(double)atof(optarg);
00370                                                 break;
00371                          case 'r':
00372                                                 //sets the reconfiguration interval
00373                                                 reconfigurationInterval=(double)atof(optarg);
00374                                                 break;  
00375                          case 'w':      
00376                                                 //sets the rewiring probability
00377                                                 rewiringProb=(double)atof(optarg);
00378                                                 break;
00379                          case 'G':   
00380                                                 //sets the number of groups of the social network
00381                                                 numberOfGroups=atoi(optarg);
00382                                                 break;
00383                          case 'g':
00384                                                 //sets the seed of the random number generator
00385                                                 inputSeed=atoi(optarg);
00386                                                 setSeed(inputSeed);
00387                                                 break;
00388                          case 'c':      
00389                                                 //sets the number of carriers/travellers
00390                                                 numberOfTravellers=atoi(optarg);
00391                                                 break;
00392                          case 'v':  
00393                                                 //sets the speed of the travellers
00394                                                 travellerSpeed=(double)atof(optarg);
00395                                                 break;
00396                          case 'a': 
00397                                                 //sets the Girvan/Newman algorithm on/off
00398                                                 if (strcmp(optarg,"on")==0)
00399                                                         girvanNewmanOn=true;
00400                                                 else
00401                                                         girvanNewmanOn=false;
00402                                                 break;  
00403                          case 'A':  
00404                                                 //sets colocation traces on/off
00405                                                 if (strcmp(optarg,"on")==0)
00406                                                         colocationTracesOn=true;
00407                                                 else
00408                                                         colocationTracesOn=false;
00409                                                 break;
00410                          case 'b': 
00411                                                 //sets communities on/off
00412                                                 if (strcmp(optarg,"on")==0)
00413                                                         communitiesTracesOn=true;
00414                                                 else
00415                                                         communitiesTracesOn=false;
00416                                                 break;
00417                          case 'd':
00418                                                 //sets deterministic on/off
00419                                                 if (strcmp(optarg,"on")==0)
00420                                                         deterministicOn=true;
00421                                                 else
00422                                                         deterministicOn=false;
00423                                                 break;
00424                          case 'i':              
00425                                                 //sets adjecency input on/off
00426                                                 read_adjacency=true;
00427                                                 inputFile.assign(optarg);
00428                                                 break;
00429                          case 'o':
00430                                                 //output the adjacency matrix in the given text file
00431                                                 if (strcmp(optarg,"on")==0)
00432                                                         output_adjacency=true;
00433                                                 break;
00434                          case 'P':
00435                                                 pauseMAX=atoi(optarg);
00436                                                 break;
00437                          case 'e':
00438                                                 numOfEpochs=atoi(optarg);
00439                                                 break;
00440              case '?':
00441                         //Invalid option
00442                         cerr << "Invalid option will be discarted!" << endl;
00443                         break;
00444                          case ':':   
00445                                                 //fprintf(stderr,"Option -%c requires an operand\n", optopt);
00446                                                 usage();
00447                                                 exit(1);
00448                          default: 
00449                                                 break;
00450                 }
00451         int commute=0;
00452         int setgoal=0;
00453         bool **isConnected=new bool *[numHosts];
00454         for(int i=0;i<numHosts;i++)
00455                 isConnected[i]=new bool [numHosts];
00456         
00457         struct hostsItem{
00458                 double currentX;
00459                 double currentY;
00460                 double relativeX;
00461                 double relativeY;
00462                 double goalRelativeX;
00463                 double goalRelativeY;
00464                 double goalCurrentX;
00465                 double goalCurrentY;
00466                 int cellIdX;
00467                 int cellIdY;
00468                 int initialCellIdX;
00469                 int initialCellIdY;
00470                 double speed;
00471                 double absSpeed;
00472                 double af;
00473                 bool isATraveller;
00474                 int **numberOfFriends;
00475                 double pausetime;
00476         };
00477         
00478         hostsItem *hosts=new hostsItem[numHosts];
00479 
00480         struct cellsItem{
00481                 double minX;
00482                 double minY;
00483                 int numberOfHosts;
00484         };
00485         
00486         struct epochInfoItem {
00487                 int cellIdX;
00488                 int cellIdY;
00489         };
00490         
00491         cellsItem **cells;
00492         cells=new cellsItem *[numberOfRows];
00493         for(int c=0;c<numberOfRows;c++) {
00494                 cells[c]=new cellsItem [numberOfColumns];
00495         }
00496         
00497 
00498         double sideLengthX;
00499         double sideLengthY;
00500 
00501         //cell attractivity
00502         float CA[numberOfRows][numberOfColumns];
00503 
00504         //Variables used for the generation of the mobility scenario using the results of complex networks theory and in particular the Girvan-Newman algorithm
00505 
00506         //interaction->adjacency threshold
00507         double threshold=connectionThreshold;
00508 
00509         int numberOfMembers[numHosts];
00510         int optNumberOfMembers[numHosts];
00511         int **adjacency;
00512         int **tempadjacency;
00513         double **interaction;
00514         int **groups;
00515         int **optGroups;
00516         double** betw;
00517         double ***epochAdj;
00518         int optNumberOfGroups=0;
00519         int epochNum=0;
00520         epochInfoItem **epochHosts;
00521 
00522         // **********************************/
00523         //           Sanity Checks
00524         // **********************************/
00525         // Chech the number of hosts
00526         if(numHosts<numberOfGroups){
00527                 cerr << "ERROR: The number of Groups was set higher than the number of Hosts! Exiting..." << endl;
00528                 exit(-1);
00529         }
00530         // **********************************/
00531         
00532         //output stream
00533         std::ofstream out1; 
00534 
00535         //input fileName
00536         char resultsFileName[]="results.txt";
00537         
00538         //double last values registered
00539         double **lastValues;
00540         lastValues=initialise_double_array(numHosts);
00541         
00542         //array [hostId,communitiesId]
00543         int communities[numHosts][2];
00544         
00545         //probability of moving to the cell [c][r]
00546         float a[numberOfRows][numberOfColumns];
00547 
00548         struct probRange {
00549                 float min;
00550                 float max;
00551         };
00552 
00553         probRange p[numberOfRows][numberOfColumns];
00554         //probability space distribution in the range [0..1]
00555         
00556         // **********************************/
00557         // Initialization of the output files   
00558         // **********************************/
00559         
00560         //Creation of the output file XML
00561         FILE* outputFileXML;
00562         generateXML=true;
00563         if (generateXML==true)
00564                 if ((outputFileXML=fopen(xmlFile.c_str(),"wt"))==NULL)
00565                         fprintf(stderr,"Cannot open %s\n",xmlFile.c_str());
00566         
00567         
00568         //Creation of the colocation statistics files
00569         FILE* outputColocFiles[numHosts];
00570         if (colocationTracesOn==true)
00571                 for (int i=0;i<numHosts;i++) {
00572                         static char fileName[10];
00573                         static char prefix[]=".idat";
00574                         sprintf(fileName,"%d%s",i,prefix);
00575                         if ((outputColocFiles[i]=fopen(fileName,"wt"))==NULL)
00576                                 fprintf(stderr,"Cannot open %s\n",fileName);
00577                 }//end for
00578         
00579         //Creation of the communities statistics files
00580         FILE* outputCommunitiesFile;
00581         FILE* outputAdjacency;
00582         
00583         //Erasing file output adjacency file
00584         system("echo > adjacency.idat");
00585         
00586         // ***************************/
00587         // Initialization of the model
00588         //****************************/
00589         
00590         sideLengthX=sideLength_WholeAreaX/((double)numberOfColumns);
00591         sideLengthY=sideLength_WholeAreaY/((double)numberOfRows);
00592         
00593         //Set the limits of each cell
00594         for (int i=0;i<numberOfRows;i++)
00595                 for (int j=0;j<numberOfColumns;j++) {
00596                         cells[i][j].minX=((double)i)*sideLengthX;
00597                         cells[i][j].minY=((double)j)*sideLengthY;       
00598                         cells[i][j].numberOfHosts=0;
00599                 }       
00600         
00601         //setup of the links
00602         for (int i=0;i<numHosts;i++) {
00603 
00604                 for (int l=0;l<numHosts;l++) {
00605                         isConnected[i][l]=false;
00606                         lastValues[i][l]=0.0;
00607                         //nodeModule1->setIsInReach(l,false);
00608                 }
00609         }
00610         
00611         //initialization of the travellers
00612         for (int i=0; i<numberOfTravellers; i++) {
00613                 
00614                 hosts[i].isATraveller=true;
00615                 //definition of the initial speeds of the travellers    
00616                 hosts[i].speed=travellerSpeed;
00617                 
00618         }
00619 
00620         // Initialize the non-traveler hosts
00621         for (int i=numberOfTravellers; i<numHosts; i++) {
00622         
00623                 hosts[i].isATraveller=false;
00624                 hosts[i].speed=generateRandomDouble(minHostSpeed,maxHostSpeed);
00625         
00626         }
00627         
00628         //Initialize the number of friends for each host
00629         for (int k=0;k<numHosts;k++) {
00630                 hosts[k].numberOfFriends=initialise_int_square_array(numberOfColumns, numberOfRows);
00631         }
00632         
00633         //Initialize the adjacency matrices for each epoch
00634         epochAdj=new double **[numOfEpochs];
00635         for(int i=0;i<numOfEpochs;i++) {
00636                 epochAdj[i]=initialise_double_array(numHosts);
00637         }
00638         
00639         //Initialize the hosts for each epoch
00640         epochHosts=new epochInfoItem *[numOfEpochs];
00641         for(int i=0;i<numOfEpochs;i++) {
00642                 epochHosts[i]=new epochInfoItem [numHosts];
00643         }
00644 
00645         double numberOfReconfigurations=0.0;
00646         double nextReconfigurationTime=0.0;
00647         
00648         int initialNumberOfGroups=numberOfGroups;
00649         //Update of the positions
00650 
00651         adjacency=initialise_int_array(numHosts);
00652         tempadjacency=initialise_int_array(numHosts);
00653         interaction=initialise_double_array(numHosts);
00654         groups=initialise_int_array(numHosts);
00655         optGroups=initialise_int_array(numHosts);
00656         betw=initialise_double_array(numHosts);
00657         double node_betw[numHosts];
00658         
00659         for (simTime=0.0; simTime<totalSimulationTime;simTime=simTime+stepInterval) {
00660                 
00661                 //reconfiguration mechanism
00662                 if (simTime==nextReconfigurationTime) {
00663                                         
00664                         //Initialize the number of friends and hosts variables in the appropriate arrays
00665                         for (int i=0;i<numberOfRows;i++) {
00666                                 for (int j=0;j<numberOfColumns;j++) {
00667         
00668                                         cells[i][j].numberOfHosts=0;
00669 
00670                                         //Initialize the number of friend in each square for every host
00671                                         for (int k=0;k<numHosts;k++)
00672                                                 hosts[k].numberOfFriends[i][j]=0;
00673                                 }       
00674                         }
00675 
00676                         //start correction
00677                         for(int i=0;i<numHosts;i++) {
00678                                 optNumberOfMembers[i]=0;
00679                                 for(int j=0;j<numHosts;j++) {
00680                                         adjacency[i][j]=0;
00681                                         interaction[i][j]=0;
00682                                         groups[i][j]=0;
00683                                         optGroups[i][j]=0;
00684                                 }
00685                         }
00686                         optNumberOfGroups=0;                    
00687                         //print_double_array(betw,numHosts);                    
00688                         //end correction
00689                         
00690                         numberOfReconfigurations=numberOfReconfigurations+1.0;
00691                         nextReconfigurationTime=reconfigurationInterval*numberOfReconfigurations;
00692 
00693                         bool splitted=true;
00694                         double previousModth=0.0;
00695                         double modth=0.0;
00696                         double optModth=0.0;
00697                         
00698                         if (girvanNewmanOn==true) {     
00699                                 
00700                                 if(read_adjacency==false) {
00701                                         //Check the epoch, for loading the previous tie weight aray
00702                                         if(epochNum<numOfEpochs || numOfEpochs==0) {
00703                                                 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,generateRandomDouble(0,inputSeed));
00704                                 
00705                                                 //Save the interaction array if epoch information is being recorded
00706                                                 //epochAdj[epochNum]=interaction;
00707                                                 if(numOfEpochs!=0) {
00708                                                         for(int i=0;i<numHosts;i++)
00709                                                                 for(int j=0;j<numHosts;j++)
00710                                                                         epochAdj[epochNum][i][j]=interaction[i][j];
00711                                                 }
00712                                         }
00713                                         else {
00714                                                 for(int i=0;i<numHosts;i++)
00715                                                         for(int j=0;j<numHosts;j++)
00716                                                                 interaction[i][j]=epochAdj[epochNum%numOfEpochs][i][j];
00717                                         }
00718                                         generate_adjacency(interaction,adjacency,threshold,numHosts);
00719                                 }
00720                                 else {
00721                                         if(!readAdjacency(inputFile, interaction,adjacency,numHosts,threshold))
00722                                                 exit(-1);
00723                                 }
00724 
00725                                 //======================================//
00726                                 // -- Split network into communities -- //
00727                                 //======================================//
00728                                 // --> USING IGRAPH <-- //
00729                 //optModth=igraph_splitNetwork_edge_Threshold(adjacency, betw, optGroups ,optNumberOfMembers, &optNumberOfGroups, numHosts);
00730                                 // -------------------- //
00731 
00732                                 // -->  USING ECMM  <-- //
00733                                 //Creating a temporal copy of the adjacency matrix
00734                 for (int i=0;i<numHosts; i++) {
00735                     for (int j=0;j<numHosts; j++) {
00736                         tempadjacency[i][j]=adjacency[i][j];
00737                     }
00738                 }
00739                                                 
00740                                 //Clustering using the Girvan-Newman algorithm
00741                 do {
00742                     //Initialize numberOfMembers matrix
00743                     for (int i=0;i<numHosts; i++)
00744                         numberOfMembers[i]=0;
00745 
00746                     for (int i=0;i<numHosts; i++) {
00747                         for (int j=0;j<numHosts;j++) {
00748                             betw[i][j]=0;
00749                         }
00750                     }
00751                     //Calculating the edge betweenness of the current graph
00752                     calculate_edge_betweenness(betw,tempadjacency,numHosts);
00753 
00754                     //Find the initial number of Groups
00755                     numberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00756 
00757                     //Save the previous modularity for the comparison
00758                     previousModth=modth;
00759 
00760                     //Split the network according to the modularity
00761                     modth=splitNetwork_edge_Threshold(adjacency, tempadjacency, betw, numHosts, modth);
00762 
00763                     //Check for the optimum solution
00764                     if(optModth<modth || optNumberOfGroups==0) {
00765                         optModth=modth;
00766 
00767                         //Reinitialize numberOfMembers matrix
00768                         for (int i=0;i<numHosts; i++)
00769                             numberOfMembers[i]=0;
00770 
00771                         //Find the number of Groups after the split
00772                         optNumberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00773 
00774                         //Copy groups matrix
00775                         for(int m=0;m<numHosts;m++) {
00776                             optNumberOfMembers[m]=numberOfMembers[m];
00777                             for(int n=0;n<numHosts;n++)
00778                                 if (m<optNumberOfGroups && n<optNumberOfMembers[m]){
00779                                     optGroups[m][n]=groups[m][n];
00780                                 }
00781                         }
00782                     }
00783 
00784                     //Print results is asked
00785                     if (verbose==true && modth!=-1) {
00786                         //Reinitialize numberOfMembers matrix
00787                         for (int i=0;i<numHosts; i++)
00788                             numberOfMembers[i]=0;
00789 
00790                         //Find the number of Groups after the split
00791                         numberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00792                         cout<<"\nModth is equal to "<<modth<<endl;
00793                         printGroups(numberOfGroups, groups, numberOfMembers, numHosts);
00794                     }
00795                 }
00796                 while ( (previousModth<=2*modth) && (modth>-1) );
00797                 // -------------------- //
00798 
00799                 //======================================//
00800                 // --  End of the network splitting  -- //
00801                 //======================================//
00802 
00803                                 //Printing results
00804                                 cout<<endl<<"---------Communities Detected---------"<<endl;
00805                                 cout << "Optimal Modularity: " << optModth << endl;
00806                                 cout << "Optimal number of groups: " << optNumberOfGroups << endl;
00807                                 printGroups(optNumberOfGroups, optGroups, optNumberOfMembers, numHosts);
00808                                 cout<<"--------------------------------------"<<endl;
00809                                 
00810                                 //Copy results in the used variables
00811                                 //Find the number of Groups after the split
00812                                 numberOfGroups=optNumberOfGroups;
00813 
00814                                 //Copy groups matrix
00815                                 for(int m=0;m<numHosts;m++) {
00816                                         numberOfMembers[m]=optNumberOfMembers[m];
00817                                         for(int n=0;n<numHosts;n++)
00818                                                 if (m<optNumberOfGroups && n<optNumberOfMembers[m]){
00819                                                         groups[m][n]=optGroups[m][n];
00820                                                 }
00821                                 }
00822 
00823                         }//end if (girvanNewmanOn==true)
00824                         else if(girvanNewmanOn==false) {
00825                                 if(read_adjacency==true) {
00826                                         cout << endl << "!!!!!!!!!Adjacency input file given but Girvan Newman algorithm is turned off!!!!!!!!!" << endl;
00827                                         cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Creating random network...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl << endl;
00828                                         sleep(2);
00829                                 }
00830 
00831                                 //Save a temporary seed to generate the same results in the array initialization functions below
00832                                 double tempseed=generateRandomDouble(0,inputSeed);
00833                                 
00834                                 //communities based on the initial number of caves in the Caveman model         
00835                                 //i.e., w=0
00836                                 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,0,threshold,tempseed);
00837                                 generate_adjacency(interaction,adjacency,threshold,numHosts);
00838 
00839                                 for (int i=0;i<numHosts; i++)
00840                                         numberOfMembers[i]=0;
00841                                 numberOfGroups=getGroups(adjacency, groups ,numberOfMembers,numHosts);
00842                                 if (verbose==true)
00843                                         printGroups(numberOfGroups, groups, numberOfMembers, numHosts);
00844                                 
00845                                 //Check the epoch, for loading the previous tie weight aray
00846                                 if(epochNum<numOfEpochs || numOfEpochs==0) {
00847                                         initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,tempseed);
00848                                         
00849                                         //Save the interaction array if epoch information is being recorded
00850                                         if(numOfEpochs!=0)
00851                                                 epochAdj[epochNum]=interaction;
00852                                 }
00853                                 else {
00854                                         interaction=epochAdj[epochNum%numOfEpochs];
00855                                 }
00856                                 generate_adjacency(interaction,adjacency,threshold,numHosts);
00857                                 printGroups(numberOfGroups, groups, numberOfMembers, numHosts);
00858                                 //print_double_array(interaction, numHosts);
00859                         }//end else
00860 
00861                         //For the first epoch, save the configuration to the epoch Hosts array
00862                         if(epochNum<numOfEpochs || numOfEpochs==0) {
00863                                 //Choose initial cells for the hosts, randomly for each community
00864                                 int pointer=0; 
00865                                 for (int i=0;i<numberOfGroups;i++) {
00866                                         
00867                                         int cellIdX=generateRandomInteger(0,numberOfRows);
00868                                         int cellIdY=generateRandomInteger(0,numberOfColumns);
00869 
00870                                         //Check this cell hasn't been used before, and it is not next to another already occupied one
00871                                         for (int k=0;k<pointer;k++) {
00872                                                 if(sqrt(pow((double)(hosts[communities[k][0]].cellIdX-cellIdX), 2) + pow((double)(hosts[communities[k][0]].cellIdY-cellIdY), 2) )<=2) {
00873                                                         cellIdX=generateRandomInteger(0,numberOfRows);
00874                                                         cellIdY=generateRandomInteger(0,numberOfColumns);
00875                                                         k=0;
00876                                                 }
00877                                         }
00878                                         
00879                                         for (int j=0;j<numberOfMembers[i];j++) {                                
00880                                                 int hostId=groups[i][j];
00881                                                 hosts[hostId-1].cellIdX=cellIdX;
00882                                                 hosts[hostId-1].cellIdY=cellIdY;
00883                                                 hosts[hostId-1].initialCellIdX=cellIdX;
00884                                                 hosts[hostId-1].initialCellIdY=cellIdY;
00885 
00886                                                 //Save host information in the epoch host array if epochs are being counted
00887                                                 if(numOfEpochs!=0) {
00888                                                         epochHosts[epochNum][hostId-1].cellIdX=cellIdX;
00889                                                         epochHosts[epochNum][hostId-1].cellIdY=cellIdY;
00890                                                 }
00891 
00892                                                 communities[pointer][0]=hostId;
00893                                                 communities[pointer][1]=i+1;
00894                                                 pointer++;
00895                                                 
00896                                                 //increment the number of the hosts in that cell
00897                                                 cells[cellIdX-1][cellIdY-1].numberOfHosts+=1;
00898                                         }
00899                                         
00900                                 }
00901                         }
00902                         //Check the epoch, for loading the previous locations of each community
00903                         else {  
00904                                 int pointer=0; 
00905                                 //Set up the communities and cells arrays
00906                                 for (int i=0;i<numberOfGroups;i++) {
00907                                         for (int j=0;j<numberOfMembers[i];j++) {
00908                                                 int hostId=groups[i][j];
00909                                                 hosts[hostId-1].cellIdX=epochHosts[epochNum%numOfEpochs][hostId-1].cellIdX;
00910                                                 hosts[hostId-1].cellIdY=epochHosts[epochNum%numOfEpochs][hostId-1].cellIdY;
00911                                                 hosts[hostId-1].initialCellIdX=epochHosts[epochNum%numOfEpochs][hostId-1].cellIdX;
00912                                                 hosts[hostId-1].initialCellIdY=epochHosts[epochNum%numOfEpochs][hostId-1].cellIdY;
00913 
00914                                                 communities[pointer][0]=hostId;
00915                                                 communities[pointer][1]=i+1;
00916                                                 pointer++;
00917                                                 
00918                                                 //increment the number of the hosts in that cell
00919                                                 cells[hosts[hostId-1].cellIdX-1][hosts[hostId-1].cellIdY-1].numberOfHosts+=1;
00920                                         }
00921                                 }
00922                         }
00923                         
00924                         //Output the adjacency file
00925                         if (output_adjacency==true) {
00926                                 //Opening and writing into file
00927                                 if ((outputAdjacency=fopen("adjacency.idat","a+"))==NULL)
00928                                         fprintf(stderr,"Cannot open adjacency.idat\n");
00929 
00930                                 for (int i=0;i<numHosts;i++) {
00931                                         for (int j=0;j<numHosts;j++) {
00932                                                 fprintf(outputAdjacency, "%d ", adjacency[i][j]);
00933                                         }//end for (int j=0
00934                                         fprintf(outputAdjacency,"\n");
00935                                 }// end for (int i=0
00936                                 fprintf(outputAdjacency,"\n");
00937                                 fclose(outputAdjacency);
00938                         }//end communitiesTracesOn
00939 
00940                         //Set initial number of friends in each square for every host and initialize pause time to -1 (invalid)
00941                         for (int i=0;i<numHosts;i++) {
00942                                 //Also initialize pause time
00943                                 hosts[i].pausetime=-1;
00944                                 
00945                                 for (int j=0;j<numHosts;j++) {
00946                                         //If the two nodes are friends
00947                                         if(adjacency[i][j]==1 && i!=j) {
00948                                                 //Increase the number of friends in the specific square of the grid
00949                                                 hosts[i].numberOfFriends[hosts[j].cellIdX-1][hosts[j].cellIdY-1]++;
00950                                         }
00951                                 }
00952                         }
00953                         
00954                         
00955                         if (communitiesTracesOn==true) {
00956                                 if ((outputCommunitiesFile=fopen("communities.idat","wt"))==NULL)
00957                                         fprintf(stderr,"Cannot open communities.idat\n");
00958                                 
00959                                 int temp1=0;
00960                                 int temp2=0;
00961                                 for (int i=0;i<numHosts;i++) {
00962                                         for (int j=0;j<numHosts-1;j++) {
00963                                                 
00964                                                 if (communities[j][0]>communities[j+1][0]) {
00965                                                         temp1=communities[j+1][0];
00966                                                         temp2=communities[j+1][1];
00967                                                         
00968                                                         communities[j+1][0]=communities[j][0];
00969                                                         communities[j+1][1]=communities[j][1];
00970                                                         
00971                                                         communities[j][0]=temp1;
00972                                                         communities[j][1]=temp2;
00973                                                 }
00974                                         }//end for (int j=0
00975                                 }// end for (int i=0
00976                                 
00977                                 for (int i=0;i<numHosts;i++) {
00978                                         fprintf(outputCommunitiesFile, "%d%s", communities[i][0]," ");
00979                                         fprintf(outputCommunitiesFile, "%d%s", communities[i][1],"\n");
00980                         
00981                                 }
00982 
00983                                 fclose(outputCommunitiesFile);                  
00984                         
00985                         }//end communitiesTracesOn
00986                         
00987                         if (firstTime==true) {
00988                                 //definition of the initial position of the hosts
00989                                 for (int k=0;k<numHosts;k++) {                  
00990                                         hosts[k].currentX=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minX+generateRandomDouble(0.0,sideLengthX);     
00991                                         hosts[k].currentY=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minY+generateRandomDouble(0.0,sideLengthY);
00992                                 }
00993                                         
00994                                 timeFirstReconfiguration=simTime;
00995                                 firstTime=false;
00996         
00997                                 // ************************
00998                                 // Generation of the traces
00999                                 // ************************
01000                                 header();
01001 
01002                                 printf("%s","set god_ [God instance]\n");
01003         
01004                                 //Generating initial positions of the hosts
01005                                 for (int i=0;i<numHosts;i++) {
01006                                         
01007                                         printf("%s%d%s%f%s","$node_(",i,") set X_ ",hosts[i].currentX,"\n");    
01008                                         printf("%s%d%s%f%s","$node_(",i,") set Y_ ",hosts[i].currentY,"\n");    
01009                                         printf("%s%d%s%f%s","$node_(",i,") set Z_ ",0.0,"\n");
01010                                                           
01011                                 }
01012         
01013                                 //Generating initial positions of the hosts - XML code  
01014                                 if (generateXML==true) {
01015                 
01016                                         generateXMLHeader(outputFileXML, sideLength_WholeAreaX, sideLength_WholeAreaY, radius, numHosts);
01017                                         fprintf(outputFileXML,"\t<node_settings>\n");
01018                 
01019                                         for (int i=0;i<numHosts;i++) {
01020                                                 generateXMLSetNodePosition(outputFileXML, i, hosts[i].currentX, hosts[i].currentY,sideLength_WholeAreaX,sideLength_WholeAreaY);
01021                                         }
01022         
01023                                         fprintf(outputFileXML,"\t</node_settings>\n");
01024                                         fprintf(outputFileXML,"\t<mobility>\n");
01025         
01026                                 }//end generation XML code
01027                         }//end if (firstTime==true)
01028                         
01029                         //definition of the initial goals
01030                         for (int k=0;k<numHosts;k++) {
01031                                 hosts[k].goalCurrentX=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minX+generateRandomDouble(0.0,sideLengthX);
01032                                 hosts[k].goalCurrentY=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minY+generateRandomDouble(0.0,sideLengthY);                         
01033                         }       
01034                 
01035                 
01036                         //generation of the traces - setting of the goals
01037                         for (int i=0;i<numHosts;i++) {
01038                                 
01039                                 hosts[i].absSpeed=hosts[i].speed;
01040 
01041                                 //printf("%s %f %s%d%s %f %f %f%s","$ns_ at",simTime,"\"$node_(",i,") setdest",hosts[i].goalCurrentX,hosts[i].goalCurrentY,(hosts[i].absSpeed)/stepInterval,"\"\n");
01042                         
01043                          if (generateXML==true) 
01044                                         generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
01045                         } //end for (int i=0;i<numHosts;i++)
01046                         
01047                         //Get the number of the new epoch
01048                         epochNum++;
01049                 }//end reconfiguration mechanism
01050                 
01051                 // !!!!!!!!!!!!!!!!!!                                     !!!!!!!!!!!!!!!!!!  
01052                 // !!!!!!!!!!!!!!!!!! No specific speed for x and y axis  !!!!!!!!!!!!!!!!!!
01053                 // !!!!!!!!!!!!!!!!!!                                     !!!!!!!!!!!!!!!!!!
01054                 
01055                 
01056                 //Move each host according to the model
01057                 for (int i=0;i<numHosts;i++) {
01058                                 //If the host hasn't reached the target yet... move towards that direction
01059                                 if((hosts[i].currentX>hosts[i].goalCurrentX+hosts[i].speed)||
01060                                    (hosts[i].currentX<hosts[i].goalCurrentX-hosts[i].speed)||
01061                                    (hosts[i].currentY>hosts[i].goalCurrentY+hosts[i].speed)||
01062                                    (hosts[i].currentY<hosts[i].goalCurrentY-hosts[i].speed)) {
01063                                                 //move towards the goal                                 
01064                                                 if (hosts[i].currentX<(hosts[i].goalCurrentX-hosts[i].speed))
01065                                                         hosts[i].currentX=hosts[i].currentX+hosts[i].speed;
01066                                                 if (hosts[i].currentX>(hosts[i].goalCurrentX+hosts[i].speed))
01067                                                         hosts[i].currentX=(hosts[i].currentX)-hosts[i].speed;
01068                                                 if (hosts[i].currentY<(hosts[i].goalCurrentY-hosts[i].speed))
01069                                                         hosts[i].currentY=(hosts[i].currentY)+hosts[i].speed;
01070                                                 if (hosts[i].currentY>(hosts[i].goalCurrentY+hosts[i].speed))
01071                                                         hosts[i].currentY=(hosts[i].currentY)-hosts[i].speed;
01072                                 }
01073                                 //If node just finished a movement in his home square, pause for some seconds
01074                                 //else if(pauseMAX>0 && hosts[i].pausetime==-1 && hosts[i].cellIdX==hosts[i].initialCellIdX && hosts[i].cellIdY==hosts[i].initialCellIdY) {
01075                                 else if(pauseMAX>0 && hosts[i].pausetime==-1) {
01076                                         hosts[i].pausetime=simTime+generateRandomInteger(0,pauseMAX);
01077                                         hosts[i].absSpeed=0;
01078 
01079                                         //Print the info for the pausing node
01080                                         //printf("%s %f %s%d%s %f %f %f%s","$ns_ at",simTime,"\"$node_(",i,") setdest",hosts[i].goalCurrentX,hosts[i].goalCurrentY,(hosts[i].absSpeed)/stepInterval,"\"\n");
01081                                         if (generateXML==true)
01082                                                 generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
01083                                 }
01084                                 else if(hosts[i].pausetime<=simTime){
01085 
01086                                         hosts[i].pausetime=-1;  
01087                                         int previousGoalCellX=hosts[i].cellIdX;
01088                                         int previousGoalCellY=hosts[i].cellIdY;
01089                                         int selectedGoalCellX=previousGoalCellX;
01090                                         int selectedGoalCellY=previousGoalCellY;
01091                                         
01092                                         if (deterministicOn==true) {
01093 
01094                                                 //Algorithm of the selection of the new cell
01095                                                 for (int c=0;c<numberOfRows;c++)
01096                                                         for (int r=0;r<numberOfColumns;r++)
01097                                                                 CA[c][r]=0;
01098 
01099                                                 //find current node's community
01100                                                 int comId=-1;
01101                                                 for(int c=0;c<numHosts;c++){
01102                                                         if(communities[c][0]==i+1)
01103                                                                 comId=communities[c][1];
01104                                                 }
01105 
01106                                                 //Add interaction of each node to the present while the initial cell exerts attraction with all the initial nodes
01107                                                 for (int n=0;n<numHosts;n++) {
01108                                                         //For all nodes but the present one
01109                                                         if (n!=i) {
01110                                                                 //Check the communities of all nodes
01111                                                                 for(int c=0;c<numHosts;c++) {
01112                                                                         if(communities[c][0]-1==n) {
01113                                                                                 //If the checked node belongs to the same community as the currently choosing target add their
01114                                                                                 //interaction to the initial square of the current node
01115                                                                                 if (communities[c][1]==comId)
01116                                                                                         CA[hosts[n].initialCellIdX-1][hosts[n].initialCellIdY-1]+=interaction[i][n];
01117                                                                                 //Else add it to the square it currently resides
01118                                                                                 else {
01119                                                                                 //      CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01120                                                                                         if(isConnected[i][n]==true && adjacency[i][n]==1)
01121                                                                                                 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=10*interaction[i][n];
01122                                                                                         else
01123                                                                                                 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01124                                                                                 }
01125                                                                         }
01126                                                                 }
01127                                                         }
01128                                                 }
01129 
01130                                                 //Add interaction of each node to the present square
01131                                                 //for (int n=0;n<numHosts;n++) {
01132                                                 //      if (n!=i)
01133                                                 //              CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01134                                                 //}
01135 
01136                                                 for (int c=0;c<numberOfRows;c++) {
01137                                                         for (int r=0;r<numberOfColumns;r++) {
01138                                                                 if (cells[c][r].numberOfHosts!=0) {
01139                                                                         CA[c][r]=CA[c][r]/(double)(cells[c][r].numberOfHosts - hosts[i].numberOfFriends[c][r] + 1);
01140                                                                 }
01141                                                                 else
01142                                                                         CA[c][r]=0;
01143                                                         }
01144                                                 }
01145 
01146                                                 int selectedGoalCellX2=0;
01147                                                 int selectedGoalCellY2=0;
01148                                                 
01149                                                 double CAMax1=0;
01150                                                 double CAMax2=0;
01151                                                 
01152                                                 for (int c=0;c<numberOfRows;c++) {
01153                                                         for (int r=0;r<numberOfColumns;r++) {
01154                                                                 if (CA[c][r]>CAMax1) {
01155                                                                 
01156                                                                         //set the second best
01157                                                                         selectedGoalCellX2=selectedGoalCellX;
01158                                                                         selectedGoalCellY2=selectedGoalCellY;
01159                                                                         CAMax2=CAMax1;
01160                                                                 
01161                                                                         selectedGoalCellX=c+1;
01162                                                                         selectedGoalCellY=r+1;
01163                                                                 
01164                                                                         CAMax1=CA[c][r];
01165 
01166                                                                 }
01167                                                                 else if (CA[c][r]>CAMax2) {
01168                                                                         selectedGoalCellX2=c+1;
01169                                                                         selectedGoalCellY2=r+1;
01170                                                                         CAMax2=CA[c][r];
01171                                                                 }
01172                                                         }
01173                                                 }
01174 
01175                                                 setgoal++;
01176 
01177                                                 if ((previousGoalCellX!=selectedGoalCellX)||(previousGoalCellY!=selectedGoalCellY)) {
01178                                                         cout << "Host "<< i+1 << " is moving from cell " << previousGoalCellX << "-" << previousGoalCellY;
01179                                                         cout << " to cell " << selectedGoalCellX << "-" << selectedGoalCellY << endl;
01180                                                 }
01181 
01182                                                 if ((previousGoalCellX==selectedGoalCellX)&&(previousGoalCellY==selectedGoalCellY)) {
01183                                                         if (hosts[i].isATraveller==true) {
01184                                                                 if (selectedGoalCellX!=0) 
01185                                                                 {
01186                                                                         selectedGoalCellX=selectedGoalCellX2;
01187                                                                         selectedGoalCellY=selectedGoalCellY2;
01188                                                                 }
01189                                                         }
01190                                                 }
01191 
01192                                         }//end deterministic
01193                                         else {//probabilistic
01194                                                 //Algorithm of the selection of the new cell
01195                                                 for (int c=0;c<numberOfRows;c++)
01196                                                         for (int r=0;r<numberOfColumns;r++)
01197                                                                 CA[c][r]=0.0;
01198                                                 
01199                                                 //find current node's community
01200                                                 int comId=-1;
01201                                                 for(int c=0;c<numHosts;c++){
01202                                                         if(communities[c][0]==i+1)
01203                                                                 comId=communities[c][1];
01204                                                 }
01205 
01206                                                 //Add interaction of each node to the present while the initial cell exerts attraction with all the initial nodes
01207                                                 for (int n=0;n<numHosts;n++) {
01208                                                         //For all nodes but the present one
01209                                                         if (n!=i) {
01210                                                                 //Check the communities of all nodes
01211                                                                 for(int c=0;c<numHosts;c++) {
01212                                                                         if(communities[c][0]-1==n) {
01213                                                                                 //If the checked node belongs to the same community as the currently choosing target add their
01214                                                                                 //interaction to the initial square of the current node
01215                                                                                 if (communities[c][1]==comId)
01216                                                                                         CA[hosts[n].initialCellIdX-1][hosts[n].initialCellIdY-1]+=interaction[i][n];
01217                                                                                 //Else add it to the square it currently resides
01218                                                                                 else {
01219                                                                                 //      CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01220                                                                                         if(isConnected[i][n]==true && adjacency[i][n]==1)
01221                                                                                                 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=10*interaction[i][n];
01222                                                                                         else
01223                                                                                                 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01224                                                                                 }
01225                                                                         }
01226                                                                 }
01227                                                         }
01228                                                 }
01229 
01230                                                 //Add interaction of each node to the present square
01231                                                 //for (int n=0;n<numHosts;n++) {
01232                                                 //      if (n!=i)
01233                                                 //              CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01234                                                 //}
01235                                                 for (int c=0;c<numberOfRows;c++) {
01236                                                         for (int r=0;r<numberOfColumns;r++) {
01237                                                                 if (cells[c][r].numberOfHosts!=0) {
01238                                                                         CA[c][r]=CA[c][r]/(double)(cells[c][r].numberOfHosts - hosts[i].numberOfFriends[c][r] + 1);
01239                                                                 }
01240                                                                 else
01241                                                                         CA[c][r]=0;
01242                                                         }
01243                                                 }
01244                                                 //Denonmiantor for the normalization of the values
01245                                                 float denNorm=0.00;
01246                                                 
01247                                                 //used for simulations
01248                                                 //drift=0.01;
01249                                                 
01250                                                 drift=0;
01251                                                 
01252                                                 for (int c=0;c<numberOfRows;c++) {
01253                                                         for (int r=0;r<numberOfColumns;r++) {
01254                                                                 denNorm=denNorm+CA[c][r]+drift;
01255                                                                 //printf ("%f ",CA[c][r]);
01256                                                         }
01257                                                         //cout << endl;
01258                                                 }
01259                                                 
01260                                                 for (int c=0;c<numberOfRows;c++) {
01261                                                         for (int r=0;r<numberOfColumns;r++) {
01262                                                                 if (CA[c][r]==0)
01263                                                                         a[c][r]=drift/denNorm;
01264                                                                 else
01265                                                                         a[c][r]=(CA[c][r]+drift)/(+denNorm);
01266                                                         }
01267                                                 }
01268                                                 
01269                                                 float current=0.0;
01270                                                 for (int c=0;c<numberOfRows;c++) {
01271                                                         for (int r=0;r<numberOfColumns;r++) {
01272                                                                 p[c][r].min=current;
01273                                                                 
01274                                                                 p[c][r].max=p[c][r].min+a[c][r];
01275                                                                 current=p[c][r].max;
01276                                                         }
01277                                                 }
01278 
01279                                                 //float infiniteDice= (float) random() / (float) 0x7fffffff;;
01280                                                 float infiniteDice = (float) generateRandomDouble(0.0,1.0);
01281                                                 //printf ("%f %s",infiniteDice,"\n");
01282                                                 for (int c=0;c<numberOfRows;c++) {
01283                                                         for (int r=0;r<numberOfColumns;r++) {
01284                                                                 if ((infiniteDice>p[c][r].min)&&(infiniteDice<=p[c][r].max)) {
01285                                                                         selectedGoalCellX=c+1;
01286                                                                         selectedGoalCellY=r+1;
01287                                                                         setgoal++;
01288                                                                 }
01289                                                         }
01290                                                 }
01291                                         }//end probabilistic
01292                                         //Since the goal square changed change the number of friends for this node's friends
01293                                         if ((previousGoalCellX!=selectedGoalCellX)||(previousGoalCellY!=selectedGoalCellY)) {
01294                                                 for (int n=0;n<numHosts;n++) {
01295                                                         //If the two nodes are friends
01296                                                         if(adjacency[i][n]==1 && i!=n) {
01297                                                                 //Increase the number of friends in the specific square of the grid
01298                                                                 hosts[n].numberOfFriends[previousGoalCellX-1][previousGoalCellY-1]-=1;
01299                                                                 hosts[n].numberOfFriends[selectedGoalCellX-1][selectedGoalCellY-1]+=1;
01300                                                         }
01301                                                 }
01302                                                 commute++;
01303                                         }
01304 
01305                                         //Re-definition of the number of hosts in each cell
01306                                         cells[previousGoalCellX-1][previousGoalCellY-1].numberOfHosts-=1;
01307                                         cells[selectedGoalCellX-1][selectedGoalCellY-1].numberOfHosts+=1;               
01308                                         
01309                                         double newGoalRelativeX=generateRandomDouble(0,sideLengthX);
01310                                         double newGoalRelativeY=generateRandomDouble(0,sideLengthY);                                            
01311                                         
01312                                         //refresh of the information
01313                                         hosts[i].cellIdX=selectedGoalCellX;
01314                                         hosts[i].cellIdY=selectedGoalCellY;
01315                                                 
01316                                         hosts[i].goalCurrentX=cells[selectedGoalCellX-1][selectedGoalCellY-1].minX+newGoalRelativeX;
01317                                         hosts[i].goalCurrentY=cells[selectedGoalCellX-1][selectedGoalCellY-1].minY+newGoalRelativeY;
01318                                         hosts[i].speed=generateRandomDouble(minHostSpeed,maxHostSpeed);                                         
01319                                         hosts[i].absSpeed=hosts[i].speed;
01320                                         
01321                                         //printf("%s %f %s%d%s %f %f %f%s","$ns_ at",simTime,"\"$node_(",i,") setdest",hosts[i].goalCurrentX,hosts[i].goalCurrentY,(hosts[i].absSpeed)/stepInterval,"\"\n");
01322 
01323                                         if (generateXML==true)
01324                                                 generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
01325                                 }
01326                                 
01327                 }// end for (int i=0;i<numHosts;i++) 
01328                 
01329                 //Update the connectivity for each host
01330                 for (int i=0;i<numHosts;i++) {
01331                         //update connectivity
01332                         for (int j=0;j<numHosts;j++) {
01333                                 //I am connected with me so the following condition must hold...:)
01334                                 if (i!=j) {
01335 
01336                                         //calculation of the current distance
01337                                         double currentDistance=sqrt((hosts[i].currentX-hosts[j].currentX)*(hosts[i].currentX-hosts[j].currentX)+(hosts[i].currentY-hosts[j].currentY)*(hosts[i].currentY-hosts[j].currentY));
01338                                 
01339                                         //if currentDistance<=radius then the hosts are connected
01340                                         if (currentDistance<radius) {
01341                                                 //if the hosts has been previously disconnected, then they must be connected
01342                                                 if (isConnected[i][j]==false) {
01343 
01344                                                         isConnected[i][j]=true;
01345 
01346                                                         lastValues[i][j]=simTime;
01347                                                 
01348                                                 }
01349                                         }
01350                                         //else they are disconnected
01351                                         else {
01352                                                 if (isConnected[i][j]==true) {
01353                                                                 if (simTime!=0) {
01354                                                                         //if the hosts has been previously connected, then they must be disconnected
01355                                                                         if (colocationTracesOn)
01356                                                                                 fprintf(outputColocFiles[i],"%d %ld %ld\n",j+1,(long)lastValues[i][j],(long)simTime);
01357                                                                         isConnected[i][j]=false;
01358                                                                 }
01359                                                         }
01360                                         }//end else
01361 
01362                                 }//end if (i!=j)
01363                         }
01364                 }
01365                                 
01366         }
01367         
01368         //Print the colocation traces for the nodes who were connected when simulation finished
01369         for (int i=0;i<numHosts;i++) {
01370                 //update connectivity
01371                 for (int j=0;j<numHosts;j++) {
01372                         //I am connected with me so the following condition must hold...:)
01373                         if (i!=j) {
01374                                         if (isConnected[i][j]==true) {
01375                                                 if (simTime!=0) {
01376                                                                 //if the hosts has been previously connected, then they must be disconnected
01377                                                                 if (colocationTracesOn)
01378                                                                         fprintf(outputColocFiles[i],"%d %ld %ld\n",j+1,(long)lastValues[i][j],(long)simTime);
01379                                                                 isConnected[i][j]=false;
01380                                                         }
01381                                         }
01382 
01383                         }//end if (i!=j)
01384                 }
01385         }
01386 
01387         //generation of the XML file - generation of the last lines
01388         if (generateXML==true)  {       
01389                 fprintf(outputFileXML,"\t</mobility>\n");       
01390                 fprintf(outputFileXML,"\t<statistics>\n");
01391                 fprintf(outputFileXML,"\t\t<stoptime>%f</stoptime>\n",simTime);
01392                 fprintf(outputFileXML,"\t</statistics>\n");
01393                 fprintf(outputFileXML,"</simulation>\n");
01394         }
01395         
01396         if(setgoal!=0)
01397                 cout << "Percentage of commute nodes: "<< (double)commute/(double)setgoal << endl;
01398         else
01399                 cout << "Percentage of commute nodes: 0" << endl;
01400         cout << "Random Seed: " << inputSeed << endl;
01401         
01402         //Delete allocated pointers
01403         for(int r=0;r<numberOfRows;r++)
01404                 delete[] cells[r];
01405                 
01406         for(int i=0;i<numOfEpochs;i++){
01407                 for(int j=0;j<numHosts;j++)
01408                         delete[] epochAdj[i][j];
01409                 delete[] epochAdj[i];
01410                 delete[] epochHosts[i];
01411         }
01412         
01413         for(int i=0;i<numHosts;i++){
01414                 for(int j=0;j<numberOfRows;j++)
01415                         delete[] hosts[i].numberOfFriends[j];
01416                 delete[] hosts[i].numberOfFriends;
01417                 delete[] lastValues[i];
01418                 delete[] isConnected[i];
01419                 delete[] betw[i];
01420                 delete[] interaction[i];
01421                 delete[] adjacency[i];
01422                 delete[] groups[i];
01423                 delete[] optGroups[i];
01424         }
01425         
01426         delete[] cells;
01427         delete[] hosts;
01428         delete[] epochAdj;
01429         delete[] epochHosts;
01430         delete[] lastValues;
01431         delete[] isConnected;
01432         delete[] betw;
01433         delete[] interaction;
01434         delete[] adjacency;
01435         delete[] groups;
01436         delete[] optGroups;
01437 
01438         // close output file and outputFileXML
01439         if (generateXML==true) 
01440                 fclose (outputFileXML);
01441         
01442         
01443 }

void setSeed ( unsigned int  init  ) 

00098                                    {
00099         if (init != 0.0) SEED = init;
00100 }

void usage (  ) 

00134              {
00135         printf("%s","Usage: movGen [-options] \n");
00136         printf("%s","Input parameters:\n");
00137         printf("%s","-h                           shows help\n");
00138         printf("%s","-x <output_XML_file>         generates XML - The ouput file name can also be given\n");
00139     printf("%s","-n <numberOfHosts>           sets the number of hosts\n");
00140         printf("%s","-t <totalSimulationTime>     sets the total simulation time\n");
00141         printf("%s","-r <reconfigurationInterval> sets the reconfiguration interval\n");
00142         printf("%s","-e <numOfEpochs>             sets the number of the epochs\n");
00143         printf("%s","-s <lowerBoundSpeedHost>     sets lower bound of the speed of the hosts (in m/s)\n");
00144         printf("%s","-S <upperBoundSpeedHost>     sets upper bound of the speed of the hosts (in m/s)\n");
00145         printf("%s","-p <connectionThreshold>     sets the connection threshold\n");
00146         printf("%s","-X <sideLengthXcoordinates>  sets the side length of the the simulation area - x coordinates\n");
00147         printf("%s","-Y <sideLengthYcoordinates>  sets the side length of the the simulation area - y coordinates\n");
00148         printf("%s","-R <numberOfRows>            sets the number of rows\n");
00149         printf("%s","-C <numberOfColumns>         sets the number of columns\n");
00150         printf("%s","-T <transmissionRange>       sets the transmission range (in m)\n");
00151         printf("%s","-w <rewiring probability>    sets the rewiring probability\n");
00152         printf("%s","-G <numberOfGroups>          sets the initial number of groups for the Caveman model\n");
00153         printf("%s","-g <seedRNG>                 sets the seed of the Random Number Generator\n");
00154         printf("%s","-c <numberOfTravellers>      sets the number of travellers\n");
00155         printf("%s","-v <travellersSpeed>         sets the speed of the travellers\n");
00156         printf("%s","-P <pauseTime>               sets the maximum pause time duration\n");
00157         printf("%s","-a <on/off>                  sets the Girvan-Newman algorithm on/off\n");
00158         printf("%s","-d <on/off>                  sets the deterministic selection of the nodes on/off. If true the selection is deterministic, if false is probabilistic\n");
00159         printf("%s","-i <on/off>                  sets the the name of the input adjacency file\n");
00160         printf("%s","-o <on/off>                  sets the output adjacency file generation on/off\n");
00161         printf("%s","-P <pauseTime>               sets the maximum pause time duration\n");
00162         printf("%s","-A <on/off>                  sets the colocation traces on/off\n");
00163         printf("%s","-b <on/off>                  sets the communities traces on/off\n");
00164 }

Generated on Mon Aug 13 18:29:03 2012 for ECMM by  doxygen 1.6.3