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