- Timestamp:
- 07/29/09 16:10:39 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp
r390 r391 29 29 30 30 #define INTERFERENCE 0 31 32 #define CHANNEL 1 33 34 #define ENERGY 0 31 35 #define COMMUNICATION_TIME 1 32 #define CHANNEL 2 33 #define ENERGY 3 34 #define FITNESS 4 36 37 #define UTILITY 4 35 38 36 39 … … 111 114 112 115 uint32_t numberColumns = 113 radioInfo->numParameters + 114 radioInfo->numUtilities; 116 radioInfo->numParameters; 115 117 116 118 uint32_t obsColumns = radioInfo->numObservables + 1; 119 uint32_t numberTotalColumns = radioInfo->numUtilities + 120 radioInfo->numParameters + 121 radioInfo->numObservables + 1; 117 122 118 123 float valList[numberColumns]; … … 120 125 char *nameList[numberColumns]; 121 126 char *obsList[obsColumns]; 127 char *searchNames[1]; 128 float searchVals[1]; 129 int searchOps[1]; 130 float returnValues[numberTotalColumns]; 122 131 123 132 size_t columnObsIndex = 0; … … 126 135 columnObsIndex++; 127 136 } 128 obsList[columnObsIndex] = "utility"; 137 138 std::string utility_name = "utility"; 139 obsList[columnObsIndex] = (char*) utility_name.c_str(); 129 140 130 141 size_t columnIndex = 0; … … 133 144 columnIndex++; 134 145 } 135 for (size_t i = 0; i < radioInfo->numUtilities; i++){136 nameList[columnIndex] = (char*)uList[i].name.c_str();137 columnIndex++;138 }139 146 140 147 size_t obsValueIndex = 0; … … 144 151 } 145 152 153 /* Make sure we do not return any entries for the current channel */ 154 std::string channel_name = "channel"; 155 searchNames[0] = (char *) channel_name.c_str(); 156 searchOps[0] = EQ; 157 searchVals[0] = parameters[0].value; 158 159 /* Execute CBR search and put output into returnValues */ 160 cbr_search(myCBR, searchNames, searchOps, searchVals, 161 1, returnValues); 162 146 163 /* Calculate Utility */ 147 float newUtilityValue = 0; 148 149 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 150 newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); 151 } 164 float oldUtilityValue = returnValues[UTILITY]; 165 166 // Set DSA utility to take into account both the previously sensed 167 // energy and the average communication time. 168 169 170 171 float newUtilityValue = oldUtilityValue + ( -observables[ENERGY].value + observables[COMMUNICATION_TIME].value); 172 173 // If communication time value is set, we know we need to change channels because of PU. 174 // So we should lower the utility for this channel. 175 176 if(observables[COMMUNICATION_TIME].value != 0) { 177 newUtilityValue = newUtilityValue - 100; 178 } else { 179 newUtilityValue = newUtilityValue + 20; 180 } 181 182 183 if(newUtilityValue <= 0) 184 newUtilityValue = 0; 185 152 186 obsVals[obsValueIndex] = newUtilityValue; 153 187 … … 155 189 for(size_t i = 0; i < radioInfo->numParameters; i++) { 156 190 valList[returnValueIndex] = parameters[i].value; 157 returnValueIndex++;158 }159 for(size_t i = 0; i < radioInfo->numUtilities; i++) {160 valList[returnValueIndex] = uList[i].target;161 191 returnValueIndex++; 162 192 } … … 641 671 /* Put together the CBR search array */ 642 672 673 uint32_t channel = 0; 643 674 char *searchNames[1]; 675 char *sumSearchName; 644 676 float searchVals[1]; 677 float utilArray[(int)pList[0].max]; 645 678 int searchOps[1]; 646 679 uint32_t numberColumns = radioInfo->numUtilities + … … 648 681 radioInfo->numObservables + 1; 649 682 650 /* Make sure we do not return any entries for the current channel */651 searchNames[0] = "channel";652 searchOps[0] = NE;653 searchVals[0] = currentParameters[0].value;654 655 683 float returnValues[numberColumns]; 656 657 /* Execute CBR search and put output into returnValues */ 658 uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 684 float sumRetVals[numberColumns]; 685 686 // Total sum of utilities in sumRetVals[0] 687 688 std::string channel_name = "channel"; 689 std::string utility_name = "utility"; 690 691 for( int i = 0 ; i < pList[0].max ; i++ ) { 692 693 searchNames[0] = (char*) pList[0].name.c_str(); 694 searchOps[0] = EQ; 695 searchVals[0] = i+1; 696 697 uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 659 698 1, returnValues); 660 699 661 if(rc == 0){ 662 /* Similiar environment was found. Adapt the returned parameters to 663 meet the objective */ 664 665 LOG("Cognitive Engine:: Previous similar environment found\n"); 666 667 /* generate random channel */ 668 returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; 700 if(rc == 31337) { 701 // No entry - must add 702 703 char *rowNames[numberColumns]; 704 size_t rowIndex = 0; 705 for(size_t j = 0; j < radioInfo->numUtilities; j++) { 706 rowNames[rowIndex] = (char*)uList[j].name.c_str(); 707 rowIndex++; 708 } 709 for(size_t j = 0; j < radioInfo->numParameters; j++) { 710 rowNames[rowIndex] = (char*)pList[j].name.c_str(); 711 if(pList[j].name == "channel") 712 returnValues[rowIndex] = i+1; 713 rowIndex++; 714 } 715 for(size_t j = 0; j < radioInfo->numObservables; j++) { 716 rowNames[rowIndex] = (char*)oList[j].name.c_str(); 717 rowIndex++; 718 } 719 720 rowNames[rowIndex] = (char *) utility_name.c_str(); 721 returnValues[rowIndex] = 500; 722 723 /* Add the new optimized set to the CBR database */ 724 725 cbr_add_row(myCBR, rowNames, returnValues, numberColumns); 726 } 727 728 utilArray[i] = returnValues[UTILITY]; 729 } 730 731 printf("1: %f\t2: %f\t3: %f\t4: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]); 732 // Get sum of all the channel utilities. 733 sumSearchName = (char *) utility_name.c_str(); 734 uint32_t rc = cbr_search_sum(myCBR, sumSearchName, sumRetVals); 735 736 // Psuedo random channel selection based upon utility. 737 int k = rand() % (int)sumRetVals[0]; 738 int cdf_total(0); 739 740 for ( int i = 0; i < pList[0].max; i++ ) { 741 cdf_total += utilArray[i]; 742 if(k < cdf_total) { 743 channel = i + 1; 744 break; 745 } 746 } 669 747 670 if(returnValues[numberColumns-1] < 0) { 671 672 } else { 673 674 } 675 } else if(rc == 31337) { 676 /* No previous similar solution was found, use default. */ 677 LOG("Cognitive Engine:: No previous similar environment found.\n"); 678 679 /* generate random channel */ 680 returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; 681 } else { 682 LOG("Cognitive Engine:: Search return an invalid value.\n"); 683 } 684 685 748 searchNames[0] = (char*) pList[0].name.c_str(); 749 searchOps[0] = EQ; 750 searchVals[0] = channel; 751 752 rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 753 1, returnValues); 754 755 756 //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; 757 returnValues[CHANNEL] = channel; 758 686 759 /* Package up the new set of parameters in order to add 687 760 the new entry into the CBR database. */ … … 700 773 returnValueIndex++; 701 774 } 702 returnValues[returnValueIndex] = 0;775 //returnValues[returnValueIndex] = 0; 703 776 704 777 char *allNames[numberColumns]; … … 715 788 for(size_t i = 0; i < radioInfo->numObservables; i++) { 716 789 allNames[allNameIndex] = (char*)oList[i].name.c_str(); 717 returnValues[allNameIndex] = 0;790 // returnValues[allNameIndex] = 0; 718 791 allNameIndex++; 719 792 } 720 allNames[allNameIndex] = "utility"; 793 794 allNames[allNameIndex] = (char *) utility_name.c_str(); 721 795 722 796 /* Add the new optimized set to the CBR database */ 723 797 cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 798 724 799 725 800 /* Return the set of new parameter values. */ … … 764 839 columnIndex++; 765 840 } 841 842 char *paramCols[radioInfo->numParameters]; 843 size_t paramColumnIndex = 0; 844 // Also need to make parameters the unique key 766 845 for (size_t i = 0; i < radioInfo->numParameters; i++){ 767 846 cols[columnIndex] = (char*)pList[i].name.c_str(); 847 paramCols[paramColumnIndex] = (char*)pList[i].name.c_str(); 768 848 columnIndex++; 769 } 849 paramColumnIndex++; 850 } 851 770 852 for (size_t i = 0; i < radioInfo->numObservables; i++){ 771 853 cols[columnIndex] = (char*)oList[i].name.c_str(); 772 854 columnIndex++; 773 855 } 774 cols[columnIndex] = "utility"; 775 776 myCBR = cbr_create(filename, tablename, cols, numberColumns); 777 } 778 856 857 std::string utility_name = "utility"; 858 cols[columnIndex] = (char *)utility_name.c_str(); 859 860 myCBR = cbr_create_with_primary(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); 861 } 862