00207 {
00208
00209
00210
00211
00212
00213
00214 int numHosts=100;
00215
00216
00217 double connectionThreshold=0.1;
00218
00219
00220 double sideLength_WholeAreaX=2000.0;
00221 double sideLength_WholeAreaY=2000.0;
00222
00223
00224 double radius=200.0;
00225
00226
00227 int numberOfRows=20;
00228
00229
00230 int numberOfColumns=20;
00231
00232
00233 double totalSimulationTime=1000.0;
00234
00235
00236 double stepInterval=1.0;
00237
00238
00239 double minHostSpeed=1.0;
00240 double maxHostSpeed=10.00;
00241
00242
00243
00244 double reconfigurationInterval=10000.0;
00245
00246
00247 double rewiringProb=0.1;
00248
00249
00250 int numberOfGroups=10;
00251
00252
00253 double simTime=0.0;
00254
00255
00256 int numberOfTravellers=0;
00257
00258
00259 double travellerSpeed=20.0;
00260
00261
00262 bool verbose=false;
00263
00264
00265 bool generateXML=false;
00266 string xmlFile="socMov.xml";
00267
00268
00269 bool girvanNewmanOn=true;
00270
00271
00272 bool colocationTracesOn=false;
00273
00274
00275 bool communitiesTracesOn=false;
00276
00277
00278 bool deterministicOn=true;
00279
00280
00281 float drift;
00282
00283
00284 bool firstTime=true;
00285 double timeFirstReconfiguration;
00286
00287
00288 bool read_adjacency=false;
00289 string inputFile;
00290
00291
00292 bool output_adjacency=false;
00293
00294
00295 int pauseMAX=0;
00296
00297
00298
00299
00300
00301 char ch;
00302 int inputSeed;
00303
00304 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:"))!=EOF)
00305
00306 switch(ch) {
00307 case 'h':
00308
00309 header();
00310 usage();
00311 exit(1);
00312 case 'x':
00313
00314 generateXML=true;
00315
00316 if(optarg[0]!='-') {
00317 xmlFile.assign(optarg);
00318 xmlFile.append(".xml");
00319 }
00320 else
00321 --optind;
00322 break;
00323 case 'n':
00324
00325 numHosts=atoi(optarg);
00326 break;
00327 case 's':
00328
00329 minHostSpeed=(double)atof(optarg);
00330 break;
00331 case 'S':
00332
00333 maxHostSpeed=(double)atof(optarg);
00334 break;
00335 case 'p':
00336
00337 connectionThreshold=(double)atof(optarg);
00338 break;
00339 case 'X':
00340
00341 sideLength_WholeAreaX=(double)atof(optarg);
00342 break;
00343 case 'Y':
00344
00345 sideLength_WholeAreaY=(double)atof(optarg);
00346 break;
00347 case 'R':
00348
00349 numberOfRows=atoi(optarg);
00350 break;
00351 case 'C':
00352
00353 numberOfColumns=atoi(optarg);
00354 break;
00355 case 'T':
00356
00357 radius=(double)atof(optarg);
00358 break;
00359 case 't':
00360
00361 totalSimulationTime=(double)atof(optarg);
00362 break;
00363 case 'r':
00364
00365 reconfigurationInterval=(double)atof(optarg);
00366 break;
00367 case 'w':
00368
00369 rewiringProb=(double)atof(optarg);
00370 break;
00371 case 'G':
00372
00373 numberOfGroups=atoi(optarg);
00374 break;
00375 case 'g':
00376
00377 inputSeed=atoi(optarg);
00378 setSeed(inputSeed);
00379 break;
00380 case 'c':
00381
00382 numberOfTravellers=atoi(optarg);
00383 break;
00384 case 'v':
00385
00386 travellerSpeed=(double)atof(optarg);
00387 break;
00388 case 'a':
00389
00390 if (strcmp(optarg,"on")==0)
00391 girvanNewmanOn=true;
00392 else
00393 girvanNewmanOn=false;
00394 break;
00395 case 'A':
00396
00397 if (strcmp(optarg,"on")==0)
00398 colocationTracesOn=true;
00399 else
00400 colocationTracesOn=false;
00401 break;
00402 case 'b':
00403
00404 if (strcmp(optarg,"on")==0)
00405 communitiesTracesOn=true;
00406 else
00407 communitiesTracesOn=false;
00408 break;
00409 case 'd':
00410
00411 if (strcmp(optarg,"on")==0)
00412 deterministicOn=true;
00413 else
00414 deterministicOn=false;
00415 break;
00416 case 'i':
00417
00418 read_adjacency=true;
00419 inputFile.assign(optarg);
00420 break;
00421 case 'o':
00422
00423 if (strcmp(optarg,"on")==0)
00424 output_adjacency=true;
00425 break;
00426 case 'P':
00427 pauseMAX=atoi(optarg);
00428 break;
00429 case '?':
00430
00431 cerr << "Invalid option will be discarted!" << endl;
00432 break;
00433 case ':':
00434
00435 usage();
00436 exit(1);
00437 default:
00438 break;
00439 }
00440 int commute=0;
00441 int setgoal=0;
00442 bool isConnected[numHosts][numHosts];
00443
00444 struct hostsItem{
00445 double currentX;
00446 double currentY;
00447 double relativeX;
00448 double relativeY;
00449 double goalRelativeX;
00450 double goalRelativeY;
00451 double goalCurrentX;
00452 double goalCurrentY;
00453 int cellIdX;
00454 int cellIdY;
00455 int initialCellIdX;
00456 int initialCellIdY;
00457 double speed;
00458 double absSpeed;
00459 double af;
00460 bool isATraveller;
00461 int **numberOfFriends;
00462 double pausetime;
00463 };
00464
00465 hostsItem hosts[numHosts];
00466
00467 struct cellsItem{
00468 double minX;
00469 double minY;
00470 int numberOfHosts;
00471 };
00472
00473 cellsItem cells[numberOfRows][numberOfColumns];
00474
00475 double sideLengthX;
00476 double sideLengthY;
00477
00478
00479 float CA[numberOfRows][numberOfColumns];
00480
00481
00482
00483
00484 double threshold=connectionThreshold;
00485
00486 int numberOfMembers[numHosts];
00487 int optNumberOfMembers[numHosts];
00488 int **adjacency;
00489 int **tempadjacency;
00490 double **interaction;
00491 int **groups;
00492 int **optGroups;
00493
00494
00495
00496
00497
00498 if(numHosts<numberOfGroups){
00499 cerr << "ERROR: The number of Groups was set higher than the number of Hosts! Exiting..." << endl;
00500 exit(-1);
00501 }
00502
00503
00504
00505 std::ofstream out1;
00506
00507
00508 char resultsFileName[]="results.txt";
00509
00510
00511 double lastValues[numHosts][numHosts];
00512
00513
00514 int communities[numHosts][2];
00515
00516
00517 float a[numberOfRows][numberOfColumns];
00518
00519 struct probRange {
00520 float min;
00521 float max;
00522 };
00523
00524 probRange p[numberOfRows][numberOfColumns];
00525
00526
00527
00528
00529
00530
00531
00532
00533 FILE* outputFileXML;
00534 generateXML=true;
00535 if (generateXML==true)
00536 if ((outputFileXML=fopen(xmlFile.c_str(),"wt"))==NULL)
00537 fprintf(stderr,"Cannot open %s\n",xmlFile.c_str());
00538
00539
00540
00541 FILE* outputColocFiles[numHosts];
00542 if (colocationTracesOn==true)
00543 for (int i=0;i<numHosts;i++) {
00544 static char fileName[10];
00545 static char prefix[]=".idat";
00546 sprintf(fileName,"%d%s",i,prefix);
00547 if ((outputColocFiles[i]=fopen(fileName,"wt"))==NULL)
00548 fprintf(stderr,"Cannot open %s\n",fileName);
00549 }
00550
00551
00552 FILE* outputCommunitiesFile;
00553 FILE* outputAdjacency;
00554
00555
00556 system("echo > adjacency.idat");
00557
00558
00559
00560
00561 sideLengthX=sideLength_WholeAreaX/((double)numberOfRows);
00562 sideLengthY=sideLength_WholeAreaY/((double)numberOfColumns);
00563
00564 for (int i=0;i<numberOfRows;i++)
00565 for (int j=0;j<numberOfColumns;j++) {
00566 cells[i][j].minX=((double)i)*sideLengthX;
00567 cells[i][j].minY=((double)j)*sideLengthY;
00568 cells[i][j].numberOfHosts=0;
00569 }
00570
00571
00572 for (int i=0;i<numHosts;i++) {
00573
00574 for (int l=0;l<numHosts;l++) {
00575 isConnected[i][l]=false;
00576 lastValues[i][l]=0.0;
00577
00578 }
00579 }
00580
00581
00582 for (int i=0; i<numberOfTravellers; i++) {
00583
00584 hosts[i].isATraveller=true;
00585
00586 hosts[i].speed=travellerSpeed;
00587
00588 }
00589
00590 for (int i=numberOfTravellers; i<numHosts; i++) {
00591
00592 hosts[i].isATraveller=false;
00593 hosts[i].speed=generateRandomDouble(minHostSpeed,maxHostSpeed);
00594
00595 }
00596
00597 double numberOfReconfigurations=0.0;
00598 double nextReconfigurationTime=0.0;
00599
00600
00601 int initialNumberOfGroups=numberOfGroups;
00602
00603
00604 adjacency=initialise_int_array(numHosts);
00605 interaction=initialise_double_array(numHosts);
00606 groups=initialise_int_array(numHosts);
00607 optGroups=initialise_int_array(numHosts);
00608 int optNumberOfGroups=0;
00609 double node_betw[numHosts];
00610 double** betw=initialise_double_array(numHosts);
00611
00612
00613 for (int k=0;k<numHosts;k++) {
00614 hosts[k].numberOfFriends=initialise_int_square_array(numberOfColumns, numberOfRows);
00615 }
00616
00617 for (simTime=0.0; simTime<totalSimulationTime;simTime=simTime+stepInterval) {
00618
00619
00620 if (simTime==nextReconfigurationTime) {
00621 for (int i=0;i<numberOfRows;i++) {
00622 for (int j=0;j<numberOfColumns;j++) {
00623
00624 cells[i][j].numberOfHosts=0;
00625
00626
00627 for (int k=0;k<numHosts;k++)
00628 hosts[k].numberOfFriends[i][j]=0;
00629 }
00630 }
00631
00632
00633 adjacency=initialise_int_array(numHosts);
00634 tempadjacency=initialise_int_array(numHosts);
00635 interaction=initialise_double_array(numHosts);
00636
00637
00638
00639 groups=initialise_int_array(numHosts);
00640 optGroups=initialise_int_array(numHosts);
00641 optNumberOfGroups=0;
00642
00643
00644 numberOfReconfigurations=numberOfReconfigurations+1.0;
00645 nextReconfigurationTime=reconfigurationInterval*numberOfReconfigurations;
00646
00647 bool splitted=true;
00648 double previousModth=0.0;
00649 double modth=0.0;
00650 double optModth=0.0;
00651 if (girvanNewmanOn==true) {
00652
00653 if(read_adjacency==false) {
00654 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,generateRandomDouble(0,inputSeed));
00655 generate_adjacency(interaction,adjacency,threshold,numHosts);
00656 }
00657 else {
00658 if(!readAdjacency(inputFile, interaction,adjacency,numHosts,threshold))
00659 exit(-1);
00660 }
00661
00662
00663 for (int i=0;i<numHosts; i++) {
00664 for (int j=0;j<numHosts; j++) {
00665 tempadjacency[i][j]=adjacency[i][j];
00666 }
00667 }
00668
00669
00670 do {
00671
00672 for (int i=0;i<numHosts; i++)
00673 numberOfMembers[i]=0;
00674
00675 for (int i=0;i<numHosts; i++) {
00676 for (int j=0;j<numHosts;j++) {
00677 betw[i][j]=0;
00678 }
00679 }
00680
00681 calculate_edge_betweenness(betw,tempadjacency,numHosts);
00682
00683
00684 numberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00685
00686
00687 previousModth=modth;
00688
00689
00690 modth=splitNetwork_edge_Threshold(adjacency, tempadjacency, betw, numHosts, modth);
00691
00692
00693 if(optModth<modth || optNumberOfGroups==0) {
00694 optModth=modth;
00695
00696
00697 for (int i=0;i<numHosts; i++)
00698 numberOfMembers[i]=0;
00699
00700
00701 optNumberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00702
00703
00704 for(int m=0;m<numHosts;m++) {
00705 optNumberOfMembers[m]=numberOfMembers[m];
00706 for(int n=0;n<numHosts;n++)
00707 if (m<optNumberOfGroups && n<optNumberOfMembers[m]){
00708 optGroups[m][n]=groups[m][n];
00709 }
00710 }
00711 }
00712
00713
00714 if (verbose==true && modth!=-1) {
00715
00716 for (int i=0;i<numHosts; i++)
00717 numberOfMembers[i]=0;
00718
00719
00720 numberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00721 cout<<"\nModth is equal to "<<modth<<endl;
00722 printGroups(numberOfGroups, groups, numberOfMembers, numHosts);
00723 }
00724 }
00725 while ( (previousModth<=2*modth) && (modth>-1) );
00726
00727
00728 cout<<endl<<"---------Communities Detected---------"<<endl;
00729 cout << "Optimal Modularity: " << optModth << endl;
00730 cout << "Optimal number of groups: " << optNumberOfGroups << endl;
00731 printGroups(optNumberOfGroups, optGroups, optNumberOfMembers, numHosts);
00732 cout<<"--------------------------------------"<<endl;
00733
00734
00735
00736 numberOfGroups=optNumberOfGroups;
00737
00738
00739 for(int m=0;m<numHosts;m++) {
00740 numberOfMembers[m]=optNumberOfMembers[m];
00741 for(int n=0;n<numHosts;n++)
00742 if (m<optNumberOfGroups && n<optNumberOfMembers[m]){
00743 groups[m][n]=optGroups[m][n];
00744 }
00745 }
00746
00747 }
00748 else if(girvanNewmanOn==false) {
00749
00750
00751 if(read_adjacency==true) {
00752 cout << endl << "!!!!!!!!!Adjacency input file given but Girvan Newman algorithm is turned off!!!!!!!!!" << endl;
00753 cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Creating random network...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl << endl;
00754 sleep(2);
00755 }
00756
00757 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,0,threshold,inputSeed);
00758 generate_adjacency(interaction,adjacency,threshold,numHosts);
00759
00760 for (int i=0;i<numHosts; i++)
00761 numberOfMembers[i]=0;
00762 numberOfGroups=getGroups(adjacency, groups ,numberOfMembers,numHosts);
00763 if (verbose==true)
00764 printGroups(numberOfGroups, groups, numberOfMembers, numHosts);
00765
00766 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,inputSeed);
00767 generate_adjacency(interaction,adjacency,threshold,numHosts);
00768
00769
00770 }
00771
00772
00773 calculate_node_betweenness(node_betw, adjacency, numHosts);
00774 calculate_edge_betweenness(betw,adjacency,numHosts);
00775
00776
00777 int pointer=0;
00778 for (int i=0;i<numberOfGroups;i++) {
00779
00780 int cellIdX=generateRandomInteger(0,numberOfRows);
00781 int cellIdY=generateRandomInteger(0,numberOfColumns);
00782
00783
00784 for (int k=0;k<pointer;k++) {
00785 if(hosts[communities[k][0]].cellIdX==cellIdX && hosts[communities[k][0]].cellIdY==cellIdY) {
00786 cellIdX=generateRandomInteger(0,numberOfRows);
00787 cellIdY=generateRandomInteger(0,numberOfColumns);
00788 k=0;
00789 }
00790 }
00791
00792 for (int j=0;j<numberOfMembers[i];j++) {
00793 int hostId=groups[i][j];
00794 hosts[hostId-1].cellIdX=cellIdX;
00795 hosts[hostId-1].cellIdY=cellIdY;
00796 hosts[hostId-1].initialCellIdX=cellIdX;
00797 hosts[hostId-1].initialCellIdY=cellIdY;
00798
00799 communities[pointer][0]=hostId;
00800 communities[pointer][1]=i+1;
00801 pointer++;
00802
00803
00804 cells[cellIdX-1][cellIdY-1].numberOfHosts+=1;
00805 }
00806 }
00807
00808
00809 if (output_adjacency==true) {
00810
00811 if ((outputAdjacency=fopen("adjacency.idat","a+"))==NULL)
00812 fprintf(stderr,"Cannot open adjacency.idat\n");
00813
00814 for (int i=0;i<numHosts;i++) {
00815 for (int j=0;j<numHosts;j++) {
00816 fprintf(outputAdjacency, "%d ", adjacency[i][j]);
00817 }
00818 fprintf(outputAdjacency,"\n");
00819 }
00820 fprintf(outputAdjacency,"\n");
00821 fclose(outputAdjacency);
00822
00823 }
00824
00825
00826 for (int i=0;i<numHosts;i++) {
00827
00828 hosts[i].pausetime=-1;
00829
00830 for (int j=0;j<numHosts;j++) {
00831
00832 if(adjacency[i][j]==1 && i!=j) {
00833
00834 hosts[i].numberOfFriends[hosts[j].cellIdX-1][hosts[j].cellIdY-1]++;
00835 }
00836 }
00837 }
00838
00839 if (communitiesTracesOn==true) {
00840 if ((outputCommunitiesFile=fopen("communities.idat","wt"))==NULL)
00841 fprintf(stderr,"Cannot open communities.idat\n");
00842
00843 int temp1=0;
00844 int temp2=0;
00845 for (int i=0;i<numHosts;i++) {
00846 for (int j=0;j<numHosts-1;j++) {
00847
00848 if (communities[j][0]>communities[j+1][0]) {
00849 temp1=communities[j+1][0];
00850 temp2=communities[j+1][1];
00851
00852 communities[j+1][0]=communities[j][0];
00853 communities[j+1][1]=communities[j][1];
00854
00855 communities[j][0]=temp1;
00856 communities[j][1]=temp2;
00857 }
00858 }
00859 }
00860
00861 for (int i=0;i<numHosts;i++) {
00862 fprintf(outputCommunitiesFile, "%d%s", communities[i][0]," ");
00863 fprintf(outputCommunitiesFile, "%d%s", communities[i][1],"\n");
00864
00865 }
00866
00867 fclose(outputCommunitiesFile);
00868
00869 }
00870
00871 if (firstTime==true) {
00872
00873 for (int k=0;k<numHosts;k++) {
00874 hosts[k].currentX=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minX+generateRandomDouble(0.0,sideLengthX);
00875 hosts[k].currentY=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minY+generateRandomDouble(0.0,sideLengthY);
00876 }
00877
00878 timeFirstReconfiguration=simTime;
00879 firstTime=false;
00880
00881
00882
00883
00884
00885 printf("%s","#Mobility Patterns Generator for OMNET++ simulator\n");
00886 printf("%s","#Based on the Enhanced Home Community Based Mobility Model\n");
00887 printf("%s","#Copyright (C) Nikolaos Vastardis, University of Essex\n");
00888 printf("%s","# nvasta@essex.ac.uk\n");
00889 printf("%s","#Version 1.0 May 2011\n");
00890
00891 printf("%s","set god_ [God instance]\n");
00892
00893
00894 for (int i=0;i<numHosts;i++) {
00895
00896 printf("%s%d%s%f%s","$node_(",i,") set X_ ",hosts[i].currentX,"\n");
00897 printf("%s%d%s%f%s","$node_(",i,") set Y_ ",hosts[i].currentY,"\n");
00898 printf("%s%d%s%f%s","$node_(",i,") set Z_ ",0.0,"\n");
00899
00900 }
00901
00902
00903 if (generateXML==true) {
00904
00905 generateXMLHeader(outputFileXML, sideLength_WholeAreaX, sideLength_WholeAreaY, radius, numHosts);
00906 fprintf(outputFileXML,"\t<node_settings>\n");
00907
00908 for (int i=0;i<numHosts;i++) {
00909 generateXMLSetNodePosition(outputFileXML, i, hosts[i].currentX, hosts[i].currentY,sideLength_WholeAreaX,sideLength_WholeAreaY);
00910 }
00911
00912 fprintf(outputFileXML,"\t</node_settings>\n");
00913 fprintf(outputFileXML,"\t<mobility>\n");
00914
00915 }
00916 }
00917
00918
00919 for (int k=0;k<numHosts;k++) {
00920 hosts[k].goalCurrentX=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minX+generateRandomDouble(0.0,sideLengthX);
00921 hosts[k].goalCurrentY=cells[hosts[k].cellIdX-1][hosts[k].cellIdY-1].minY+generateRandomDouble(0.0,sideLengthY);
00922 }
00923
00924
00925
00926 for (int i=0;i<numHosts;i++) {
00927
00928 hosts[i].absSpeed=hosts[i].speed;
00929
00930
00931
00932 if (generateXML==true)
00933 generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
00934 }
00935 }
00936
00937
00938
00939
00940
00941
00942 for (int i=0;i<numHosts;i++) {
00943
00944 if((hosts[i].currentX>hosts[i].goalCurrentX+hosts[i].speed)||
00945 (hosts[i].currentX<hosts[i].goalCurrentX-hosts[i].speed)||
00946 (hosts[i].currentY>hosts[i].goalCurrentY+hosts[i].speed)||
00947 (hosts[i].currentY<hosts[i].goalCurrentY-hosts[i].speed)) {
00948
00949 if (hosts[i].currentX<(hosts[i].goalCurrentX-hosts[i].speed))
00950 hosts[i].currentX=hosts[i].currentX+hosts[i].speed;
00951 if (hosts[i].currentX>(hosts[i].goalCurrentX+hosts[i].speed))
00952 hosts[i].currentX=(hosts[i].currentX)-hosts[i].speed;
00953 if (hosts[i].currentY<(hosts[i].goalCurrentY-hosts[i].speed))
00954 hosts[i].currentY=(hosts[i].currentY)+hosts[i].speed;
00955 if (hosts[i].currentY>(hosts[i].goalCurrentY+hosts[i].speed))
00956 hosts[i].currentY=(hosts[i].currentY)-hosts[i].speed;
00957 }
00958
00959 else if(pauseMAX>0 && hosts[i].pausetime==-1 ) {
00960 hosts[i].pausetime=simTime+generateRandomInteger(0,pauseMAX);
00961 hosts[i].absSpeed=0;
00962
00963
00964
00965 if (generateXML==true)
00966 generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
00967 }
00968 else if(hosts[i].pausetime<=simTime){
00969 hosts[i].pausetime=-1;
00970 int selectedGoalCellX=0;
00971 int selectedGoalCellY=0;
00972 int previousGoalCellX=hosts[i].cellIdX;
00973 int previousGoalCellY=hosts[i].cellIdY;
00974
00975 if (deterministicOn==true) {
00976
00977
00978 for (int c=0;c<numberOfRows;c++)
00979 for (int r=0;r<numberOfColumns;r++)
00980 CA[c][r]=0;
00981
00982
00983
00984 for (int n=0;n<numHosts;n++) {
00985
00986 if (n!=i) {
00987 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
00988 }
00989 }
00990
00991 for (int c=0;c<numberOfRows;c++) {
00992 for (int r=0;r<numberOfColumns;r++) {
00993 if (cells[c][r].numberOfHosts!=0) {
00994 CA[c][r]=CA[c][r]/(double)(cells[c][r].numberOfHosts);
00995 }
00996 else
00997 CA[c][r]=0;
00998 }
00999 }
01000
01001 int selectedGoalCellX2=0;
01002 int selectedGoalCellY2=0;
01003
01004 double CAMax1=0;
01005 double CAMax2=0;
01006
01007 for (int c=0;c<numberOfRows;c++)
01008 for (int r=0;r<numberOfColumns;r++)
01009 if (CA[c][r]>CAMax1) {
01010
01011
01012 selectedGoalCellX2=selectedGoalCellX;
01013 selectedGoalCellY2=selectedGoalCellY;
01014 CAMax2=CAMax1;
01015
01016 selectedGoalCellX=c+1;
01017 selectedGoalCellY=r+1;
01018
01019 CAMax1=CA[c][r];
01020
01021 }
01022 else if (CA[c][r]>CAMax2) {
01023 selectedGoalCellX2=c+1;
01024 selectedGoalCellY2=r+1;
01025 CAMax2=CA[c][r];
01026 }
01027
01028 setgoal++;
01029
01030 if ((previousGoalCellX!=selectedGoalCellX)||(previousGoalCellY!=selectedGoalCellY)) {
01031 cout << "Host "<< i+1 << " is moving from cell " << previousGoalCellX << "-" << previousGoalCellY;
01032 cout << " to cell " << selectedGoalCellX << "-" << selectedGoalCellY << endl;
01033 }
01034
01035 if ((previousGoalCellX==selectedGoalCellX)&&(previousGoalCellY==selectedGoalCellY)) {
01036 if (hosts[i].isATraveller==true) {
01037 if (selectedGoalCellX!=0)
01038 {
01039 selectedGoalCellX=selectedGoalCellX2;
01040 selectedGoalCellY=selectedGoalCellY2;
01041 }
01042 }
01043 }
01044
01045 }
01046 else {
01047
01048
01049 for (int c=0;c<numberOfRows;c++)
01050 for (int r=0;r<numberOfColumns;r++)
01051 CA[c][r]=0.0;
01052
01053
01054 for (int n=0;n<numHosts;n++) {
01055
01056 if (n!=i) {
01057 CA[hosts[n].cellIdX-1][hosts[n].cellIdY-1]+=interaction[i][n];
01058 }
01059 }
01060
01061 for (int c=0;c<numberOfRows;c++) {
01062 for (int r=0;r<numberOfColumns;r++) {
01063 if (cells[c][r].numberOfHosts!=0) {
01064 CA[c][r]=CA[c][r]/(double)(cells[c][r].numberOfHosts);
01065 }
01066 else
01067 CA[c][r]=0;
01068 }
01069 }
01070
01071
01072 float denNorm=0.00;
01073
01074
01075
01076
01077 drift=0;
01078
01079 for (int c=0;c<numberOfRows;c++) {
01080 for (int r=0;r<numberOfColumns;r++) {
01081 denNorm=denNorm+CA[c][r]+drift;
01082
01083 }
01084
01085 }
01086
01087 for (int c=0;c<numberOfRows;c++) {
01088 for (int r=0;r<numberOfColumns;r++) {
01089 if (CA[c][r]==0)
01090 a[c][r]=drift/denNorm;
01091 else
01092 a[c][r]=(CA[c][r]+drift)/(+denNorm);
01093 }
01094 }
01095
01096 float current=0.0;
01097 for (int c=0;c<numberOfRows;c++) {
01098 for (int r=0;r<numberOfColumns;r++) {
01099 p[c][r].min=current;
01100
01101 p[c][r].max=p[c][r].min+a[c][r];
01102 current=p[c][r].max;
01103 }
01104 }
01105
01106
01107 float infiniteDice = (float) generateRandomDouble(0.0,1.0);
01108
01109 for (int c=0;c<numberOfRows;c++) {
01110 for (int r=0;r<numberOfColumns;r++) {
01111 if ((infiniteDice>p[c][r].min)&&(infiniteDice<=p[c][r].max)) {
01112 selectedGoalCellX=c+1;
01113 selectedGoalCellY=r+1;
01114 setgoal++;
01115 }
01116 }
01117 }
01118 }
01119
01120
01121 if ((previousGoalCellX!=selectedGoalCellX)||(previousGoalCellY!=selectedGoalCellY)) {
01122 for (int n=0;n<numHosts;n++) {
01123
01124 if(adjacency[i][n]==1 && i!=n) {
01125
01126 hosts[n].numberOfFriends[previousGoalCellX-1][previousGoalCellY-1]-=1;
01127 hosts[n].numberOfFriends[selectedGoalCellX-1][selectedGoalCellY-1]+=1;
01128 }
01129 }
01130 commute++;
01131 }
01132
01133
01134 cells[previousGoalCellX-1][previousGoalCellY-1].numberOfHosts-=1;
01135 cells[selectedGoalCellX-1][selectedGoalCellY-1].numberOfHosts+=1;
01136
01137
01138 double newGoalRelativeX=generateRandomDouble(0,sideLengthX);
01139 double newGoalRelativeY=generateRandomDouble(0,sideLengthY);
01140
01141
01142 hosts[i].cellIdX=selectedGoalCellX;
01143 hosts[i].cellIdY=selectedGoalCellY;
01144
01145 hosts[i].goalCurrentX=cells[selectedGoalCellX-1][selectedGoalCellY-1].minX+newGoalRelativeX;
01146 hosts[i].goalCurrentY=cells[selectedGoalCellX-1][selectedGoalCellY-1].minY+newGoalRelativeY;
01147 hosts[i].speed=generateRandomDouble(minHostSpeed,maxHostSpeed);
01148 hosts[i].absSpeed=hosts[i].speed;
01149
01150
01151
01152 if (generateXML==true)
01153 generateXMLSetNewGoal(outputFileXML,i,simTime,hosts[i].goalCurrentX,hosts[i].goalCurrentY,hosts[i].absSpeed/stepInterval,sideLength_WholeAreaX,sideLength_WholeAreaY);
01154
01155 }
01156 }
01157
01158 for (int i=0;i<numHosts;i++) {
01159
01160 for (int j=0;j<numHosts;j++) {
01161
01162 if (i!=j) {
01163
01164
01165 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));
01166
01167
01168 if (currentDistance<radius) {
01169
01170 if (isConnected[i][j]==false) {
01171
01172 isConnected[i][j]=true;
01173
01174 lastValues[i][j]=simTime;
01175
01176 }
01177 }
01178
01179 else {
01180 if (isConnected[i][j]==true) {
01181 if (simTime!=0) {
01182
01183 if (colocationTracesOn)
01184 fprintf(outputColocFiles[i],"%d %ld %ld\n",j+1,(long)lastValues[i][j],(long)simTime);
01185 isConnected[i][j]=false;
01186 }
01187 }
01188 }
01189
01190 }
01191
01192 }
01193
01194 }
01195
01196 }
01197
01198
01199 for (int i=0;i<numHosts;i++) {
01200
01201 for (int j=0;j<numHosts;j++) {
01202
01203 if (i!=j) {
01204 if (isConnected[i][j]==true) {
01205 if (simTime!=0) {
01206
01207 if (colocationTracesOn)
01208 fprintf(outputColocFiles[i],"%d %ld %ld\n",j+1,(long)lastValues[i][j],(long)simTime);
01209 isConnected[i][j]=false;
01210 }
01211 }
01212
01213 }
01214
01215 }
01216 }
01217
01218
01219 if (generateXML==true) {
01220 fprintf(outputFileXML,"\t</mobility>\n");
01221 fprintf(outputFileXML,"\t<statistics>\n");
01222 fprintf(outputFileXML,"\t\t<stoptime>%f</stoptime>\n",simTime);
01223 fprintf(outputFileXML,"\t</statistics>\n");
01224 fprintf(outputFileXML,"</simulation>\n");
01225 }
01226
01227 if(setgoal!=0)
01228 cout << "Percentage of commute nodes: "<< (double)commute/(double)setgoal << endl;
01229 else
01230 cout << "Percentage of commute nodes: 0" << endl;
01231 cout << "Random Seed: " << inputSeed << endl;
01232
01233
01234 if (generateXML==true)
01235 fclose (outputFileXML);
01236
01237
01238 }