00212 {
00213
00214
00215
00216
00217
00218
00219 int numHosts=30;
00220
00221
00222 double connectionThreshold=0.1;
00223
00224
00225 double sideLength_WholeAreaX=2000.0;
00226 double sideLength_WholeAreaY=2000.0;
00227
00228
00229 double radius=50.0;
00230
00231
00232 int numberOfRows=10;
00233
00234
00235 int numberOfColumns=10;
00236
00237
00238 double totalSimulationTime=3000.0;
00239
00240
00241 double stepInterval=1;
00242
00243
00244 double minHostSpeed=1.0;
00245 double maxHostSpeed=5.00;
00246
00247
00248
00249 double reconfigurationInterval=10000.0;
00250
00251
00252 double rewiringProb=0.2;
00253
00254
00255 int numberOfGroups=4;
00256
00257
00258 double simTime=0.0;
00259
00260
00261 int numberOfTravellers=0;
00262
00263
00264 double travellerSpeed=20.0;
00265
00266
00267 bool verbose=false;
00268
00269
00270 bool generateXML=false;
00271 string xmlFile="socMov.xml";
00272
00273
00274 bool girvanNewmanOn=true;
00275
00276
00277 bool colocationTracesOn=false;
00278
00279
00280 bool communitiesTracesOn=false;
00281
00282
00283 bool deterministicOn=false;
00284
00285
00286 float drift;
00287
00288
00289 bool firstTime=true;
00290 double timeFirstReconfiguration;
00291
00292
00293 bool read_adjacency=false;
00294 string inputFile;
00295
00296
00297 bool output_adjacency=false;
00298
00299
00300 int pauseMAX=0;
00301
00302
00303 int numOfEpochs=0;
00304
00305
00306
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
00317 header();
00318 usage();
00319 exit(1);
00320 case 'x':
00321
00322 generateXML=true;
00323
00324 if(optarg[0]!='-') {
00325 xmlFile.assign(optarg);
00326 xmlFile.append(".xml");
00327 }
00328 else
00329 --optind;
00330 break;
00331 case 'n':
00332
00333 numHosts=atoi(optarg);
00334 break;
00335 case 's':
00336
00337 minHostSpeed=(double)atof(optarg);
00338 break;
00339 case 'S':
00340
00341 maxHostSpeed=(double)atof(optarg);
00342 break;
00343 case 'p':
00344
00345 connectionThreshold=(double)atof(optarg);
00346 break;
00347 case 'X':
00348
00349 sideLength_WholeAreaX=(double)atof(optarg);
00350 break;
00351 case 'Y':
00352
00353 sideLength_WholeAreaY=(double)atof(optarg);
00354 break;
00355 case 'R':
00356
00357 numberOfRows=atoi(optarg);
00358 break;
00359 case 'C':
00360
00361 numberOfColumns=atoi(optarg);
00362 break;
00363 case 'T':
00364
00365 radius=(double)atof(optarg);
00366 break;
00367 case 't':
00368
00369 totalSimulationTime=(double)atof(optarg);
00370 break;
00371 case 'r':
00372
00373 reconfigurationInterval=(double)atof(optarg);
00374 break;
00375 case 'w':
00376
00377 rewiringProb=(double)atof(optarg);
00378 break;
00379 case 'G':
00380
00381 numberOfGroups=atoi(optarg);
00382 break;
00383 case 'g':
00384
00385 inputSeed=atoi(optarg);
00386 setSeed(inputSeed);
00387 break;
00388 case 'c':
00389
00390 numberOfTravellers=atoi(optarg);
00391 break;
00392 case 'v':
00393
00394 travellerSpeed=(double)atof(optarg);
00395 break;
00396 case 'a':
00397
00398 if (strcmp(optarg,"on")==0)
00399 girvanNewmanOn=true;
00400 else
00401 girvanNewmanOn=false;
00402 break;
00403 case 'A':
00404
00405 if (strcmp(optarg,"on")==0)
00406 colocationTracesOn=true;
00407 else
00408 colocationTracesOn=false;
00409 break;
00410 case 'b':
00411
00412 if (strcmp(optarg,"on")==0)
00413 communitiesTracesOn=true;
00414 else
00415 communitiesTracesOn=false;
00416 break;
00417 case 'd':
00418
00419 if (strcmp(optarg,"on")==0)
00420 deterministicOn=true;
00421 else
00422 deterministicOn=false;
00423 break;
00424 case 'i':
00425
00426 read_adjacency=true;
00427 inputFile.assign(optarg);
00428 break;
00429 case 'o':
00430
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
00442 cerr << "Invalid option will be discarted!" << endl;
00443 break;
00444 case ':':
00445
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
00502 float CA[numberOfRows][numberOfColumns];
00503
00504
00505
00506
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
00524
00525
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
00533 std::ofstream out1;
00534
00535
00536 char resultsFileName[]="results.txt";
00537
00538
00539 double **lastValues;
00540 lastValues=initialise_double_array(numHosts);
00541
00542
00543 int communities[numHosts][2];
00544
00545
00546 float a[numberOfRows][numberOfColumns];
00547
00548 struct probRange {
00549 float min;
00550 float max;
00551 };
00552
00553 probRange p[numberOfRows][numberOfColumns];
00554
00555
00556
00557
00558
00559
00560
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
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 }
00578
00579
00580 FILE* outputCommunitiesFile;
00581 FILE* outputAdjacency;
00582
00583
00584 system("echo > adjacency.idat");
00585
00586
00587
00588
00589
00590 sideLengthX=sideLength_WholeAreaX/((double)numberOfColumns);
00591 sideLengthY=sideLength_WholeAreaY/((double)numberOfRows);
00592
00593
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
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
00608 }
00609 }
00610
00611
00612 for (int i=0; i<numberOfTravellers; i++) {
00613
00614 hosts[i].isATraveller=true;
00615
00616 hosts[i].speed=travellerSpeed;
00617
00618 }
00619
00620
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
00629 for (int k=0;k<numHosts;k++) {
00630 hosts[k].numberOfFriends=initialise_int_square_array(numberOfColumns, numberOfRows);
00631 }
00632
00633
00634 epochAdj=new double **[numOfEpochs];
00635 for(int i=0;i<numOfEpochs;i++) {
00636 epochAdj[i]=initialise_double_array(numHosts);
00637 }
00638
00639
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
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
00662 if (simTime==nextReconfigurationTime) {
00663
00664
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
00671 for (int k=0;k<numHosts;k++)
00672 hosts[k].numberOfFriends[i][j]=0;
00673 }
00674 }
00675
00676
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
00688
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
00702 if(epochNum<numOfEpochs || numOfEpochs==0) {
00703 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,generateRandomDouble(0,inputSeed));
00704
00705
00706
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
00727
00728
00729
00730
00731
00732
00733
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
00741 do {
00742
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
00752 calculate_edge_betweenness(betw,tempadjacency,numHosts);
00753
00754
00755 numberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00756
00757
00758 previousModth=modth;
00759
00760
00761 modth=splitNetwork_edge_Threshold(adjacency, tempadjacency, betw, numHosts, modth);
00762
00763
00764 if(optModth<modth || optNumberOfGroups==0) {
00765 optModth=modth;
00766
00767
00768 for (int i=0;i<numHosts; i++)
00769 numberOfMembers[i]=0;
00770
00771
00772 optNumberOfGroups=getGroups(tempadjacency, groups ,numberOfMembers,numHosts);
00773
00774
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
00785 if (verbose==true && modth!=-1) {
00786
00787 for (int i=0;i<numHosts; i++)
00788 numberOfMembers[i]=0;
00789
00790
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
00801
00802
00803
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
00811
00812 numberOfGroups=optNumberOfGroups;
00813
00814
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 }
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
00832 double tempseed=generateRandomDouble(0,inputSeed);
00833
00834
00835
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
00846 if(epochNum<numOfEpochs || numOfEpochs==0) {
00847 initialise_weight_array_ingroups(interaction,numHosts,initialNumberOfGroups,rewiringProb,threshold,tempseed);
00848
00849
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
00859 }
00860
00861
00862 if(epochNum<numOfEpochs || numOfEpochs==0) {
00863
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
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
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
00897 cells[cellIdX-1][cellIdY-1].numberOfHosts+=1;
00898 }
00899
00900 }
00901 }
00902
00903 else {
00904 int pointer=0;
00905
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
00919 cells[hosts[hostId-1].cellIdX-1][hosts[hostId-1].cellIdY-1].numberOfHosts+=1;
00920 }
00921 }
00922 }
00923
00924
00925 if (output_adjacency==true) {
00926
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 }
00934 fprintf(outputAdjacency,"\n");
00935 }
00936 fprintf(outputAdjacency,"\n");
00937 fclose(outputAdjacency);
00938 }
00939
00940
00941 for (int i=0;i<numHosts;i++) {
00942
00943 hosts[i].pausetime=-1;
00944
00945 for (int j=0;j<numHosts;j++) {
00946
00947 if(adjacency[i][j]==1 && i!=j) {
00948
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 }
00975 }
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 }
00986
00987 if (firstTime==true) {
00988
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
00999
01000 header();
01001
01002 printf("%s","set god_ [God instance]\n");
01003
01004
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
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 }
01027 }
01028
01029
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
01037 for (int i=0;i<numHosts;i++) {
01038
01039 hosts[i].absSpeed=hosts[i].speed;
01040
01041
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 }
01046
01047
01048 epochNum++;
01049 }
01050
01051
01052
01053
01054
01055
01056
01057 for (int i=0;i<numHosts;i++) {
01058
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
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
01074
01075 else if(pauseMAX>0 && hosts[i].pausetime==-1) {
01076 hosts[i].pausetime=simTime+generateRandomInteger(0,pauseMAX);
01077 hosts[i].absSpeed=0;
01078
01079
01080
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
01095 for (int c=0;c<numberOfRows;c++)
01096 for (int r=0;r<numberOfColumns;r++)
01097 CA[c][r]=0;
01098
01099
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
01107 for (int n=0;n<numHosts;n++) {
01108
01109 if (n!=i) {
01110
01111 for(int c=0;c<numHosts;c++) {
01112 if(communities[c][0]-1==n) {
01113
01114
01115 if (communities[c][1]==comId)
01116 CA[hosts[n].initialCellIdX-1][hosts[n].initialCellIdY-1]+=interaction[i][n];
01117
01118 else {
01119
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
01131
01132
01133
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
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 }
01193 else {
01194
01195 for (int c=0;c<numberOfRows;c++)
01196 for (int r=0;r<numberOfColumns;r++)
01197 CA[c][r]=0.0;
01198
01199
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
01207 for (int n=0;n<numHosts;n++) {
01208
01209 if (n!=i) {
01210
01211 for(int c=0;c<numHosts;c++) {
01212 if(communities[c][0]-1==n) {
01213
01214
01215 if (communities[c][1]==comId)
01216 CA[hosts[n].initialCellIdX-1][hosts[n].initialCellIdY-1]+=interaction[i][n];
01217
01218 else {
01219
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
01231
01232
01233
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
01245 float denNorm=0.00;
01246
01247
01248
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
01256 }
01257
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
01280 float infiniteDice = (float) generateRandomDouble(0.0,1.0);
01281
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 }
01292
01293 if ((previousGoalCellX!=selectedGoalCellX)||(previousGoalCellY!=selectedGoalCellY)) {
01294 for (int n=0;n<numHosts;n++) {
01295
01296 if(adjacency[i][n]==1 && i!=n) {
01297
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
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
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
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 }
01328
01329
01330 for (int i=0;i<numHosts;i++) {
01331
01332 for (int j=0;j<numHosts;j++) {
01333
01334 if (i!=j) {
01335
01336
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
01340 if (currentDistance<radius) {
01341
01342 if (isConnected[i][j]==false) {
01343
01344 isConnected[i][j]=true;
01345
01346 lastValues[i][j]=simTime;
01347
01348 }
01349 }
01350
01351 else {
01352 if (isConnected[i][j]==true) {
01353 if (simTime!=0) {
01354
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 }
01361
01362 }
01363 }
01364 }
01365
01366 }
01367
01368
01369 for (int i=0;i<numHosts;i++) {
01370
01371 for (int j=0;j<numHosts;j++) {
01372
01373 if (i!=j) {
01374 if (isConnected[i][j]==true) {
01375 if (simTime!=0) {
01376
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 }
01384 }
01385 }
01386
01387
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
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
01439 if (generateXML==true)
01440 fclose (outputFileXML);
01441
01442
01443 }