Changeset 391 for vtcross/trunk
- Timestamp:
- 07/29/09 16:10:39 (15 years ago)
- Location:
- vtcross/trunk/src/cognitive_engines/DSA_CE
- Files:
-
- 3 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 -
vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c
r390 r391 57 57 char *zErrMsg = 0; 58 58 59 printf("command: %s\n", _cbr->command);59 //printf("command: %s\n", _cbr->command); 60 60 rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 61 61 if( rc!=SQLITE_OK){ … … 93 93 } 94 94 95 // create database 96 cbr cbr_create_with_primary(char * _filename, char * _tablename, 97 char * _cols[], char * _primcols[], unsigned int _len, 98 unsigned int _primlen) 99 { 100 // cbr is a pointer to struct cbr_s 101 cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); 102 103 // create database 104 105 // copy filename 106 unsigned int i=0; 107 strcpy(_cbr->filename, _filename); 108 109 // execute create database command 110 // database handle 111 //_cbr->db = NULL; 112 OpenDatabase(_cbr); 113 114 // create table 115 116 // copy tablename 117 strcpy(_cbr->tablename, _tablename); 118 119 // number of columns in the table 120 _cbr->num_columns = _len; 121 122 // generate command 123 strcpy(_cbr->command, "CREATE TABLE "); 124 strcat(_cbr->command, _cbr->tablename); 125 strcat(_cbr->command, "("); 126 for (i=0; i<_cbr->num_columns; i++) { 127 strcat(_cbr->command, _cols[i]); 128 strcat(_cbr->command, " FLOAT"); 129 strcat(_cbr->command, ", "); 130 } 131 strcat(_cbr->command, "PRIMARY KEY ("); 132 for (i=0; i<_primlen; i++) { 133 strcat(_cbr->command, _primcols[i]); 134 if (i != _primlen-1) // not last entry 135 strcat(_cbr->command, ", "); 136 } 137 strcat(_cbr->command, "));"); 138 139 // execute create table command 140 ExecuteCommand(_cbr); 141 142 return _cbr; 143 } 95 144 96 145 // create database … … 181 230 const char * ops_str[] = { 182 231 "==", "!=", ">", ">=", "<", "<="}; 232 233 234 // cbr search for a sum 235 int cbr_search_sum( 236 cbr _cbr, 237 char *_name, 238 float *_retvals) 239 { 240 int rc; 241 // generate command 242 strcpy(_cbr->command, "select SUM( "); 243 strcat(_cbr->command, _cbr->tablename); 244 strcat(_cbr->command, "."); 245 strcat(_cbr->command, _name); 246 strcat(_cbr->command, ") from "); 247 strcat(_cbr->command, _cbr->tablename); 248 strcat(_cbr->command, ";"); 249 250 //printf("search command: %s\n", _cbr->command); 251 252 rc = ExecuteSearchCommand(_cbr, _retvals); 253 /*printf("search result: "); 254 for (int i=0; i<_cbr->num_columns; i++) 255 printf("%f, ",_retvals[i]); 256 printf("\n"); 257 */ 258 return rc; 259 } 260 261 262 // cbr search 263 int cbr_search_rand( 264 cbr _cbr, 265 char *_names[], 266 int * _ops, 267 float *_vals, 268 unsigned int _n, 269 float *_retvals) 270 { 271 int rc; 272 // generate command 273 strcpy(_cbr->command, "select "); 274 strcat(_cbr->command, _cbr->tablename); 275 strcat(_cbr->command, ".* from "); 276 strcat(_cbr->command, _cbr->tablename); 277 strcat(_cbr->command, " where "); 278 279 unsigned int i; 280 char str_buffer[64]; 281 for (i=0; i<_n; i++) { 282 // ensure valid ops value 283 if (_ops[i] < 0 || _ops[i] > 5) { 284 printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); 285 exit(1); 286 } 287 288 strcat(_cbr->command, _names[i]); 289 strcat(_cbr->command, ops_str[_ops[i]]); 290 sprintf(str_buffer, "%E", _vals[i]); 291 strcat(_cbr->command, str_buffer); 292 293 if (i<_n-1) 294 strcat(_cbr->command, " AND "); 295 else 296 strcat(_cbr->command, " order by RAND();"); 297 } 298 299 //printf("search command: %s\n", _cbr->command); 300 301 rc = ExecuteSearchCommand(_cbr, _retvals); 302 303 /*printf("search result: "); 304 for (i=0; i<_cbr->num_columns; i++) 305 printf("%f, ",_retvals[i]); 306 printf("\n"); 307 */ 308 return rc; 309 } 183 310 184 311 … … 220 347 } 221 348 222 printf("search command: %s\n", _cbr->command);349 //printf("search command: %s\n", _cbr->command); 223 350 224 351 rc = ExecuteSearchCommand(_cbr, _retvals); … … 279 406 // generate command 280 407 //printf("%s\n", _cbr->command); 281 strcpy(_cbr->command, " insertinto ");408 strcpy(_cbr->command, "replace into "); 282 409 strcat(_cbr->command, _cbr->tablename); 283 410 -
vtcross/trunk/src/cognitive_engines/DSA_CE/examples/gnuradio-examples/dsa.py
r390 r391 39 39 def main(): 40 40 41 global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag41 global stats_array, count_array, time_array, n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 42 42 n_rcvd = 0 43 43 n_right = 0 44 44 n_attempts = 5 45 45 return_flag = 0 46 47 count_array = [ 0, 0, 0, 0, 0] 48 time_array = [ 0, 0, 0, 0, 0] 49 stats_array = [ 0, 0, 0, 0, 0] 50 46 51 47 52 def send_pkt(self, payload='', eof=False): … … 63 68 return real_channel 64 69 70 def get_average_time(channel,absent_time): 71 72 global count_array, time_array 73 74 count_array[channel] = count_array[channel] + 1 75 average_time = (time_array[channel] + absent_time) / count_array[channel] 76 77 return average_time 78 65 79 def get_freq(hop_freq,probe_level,absent_time): 66 80 … … 76 90 channel = 4 77 91 78 p = Parameter(1)79 92 currentParameters = Parameter(1) 80 o = Observable(1) 81 93 currentParameters[0].name = "channel" 94 currentParameters[0].value = channel 95 96 o = Observable(2) 82 97 o[0].value = probe_level 83 98 o[0].name = "energy" 84 85 currentParameters[0].name = "channel" 86 currentParameters[0].value = channel; 87 88 p = GetOptimalParameters(o,1,currentParameters,1); 89 print p[0].value 99 100 o[1].value = absent_time 101 o[1].name = "communication_time" 102 103 # If time == 0 then we are scanning and we dont want to 104 # use this time in the averaging process. 105 106 if absent_time != 0: 107 UpdateParameterPerformance(currentParameters,1,o,1) 108 109 else: 110 # Get the average communication time 111 average_time = get_average_time(channel, absent_time) 112 o[1].value = average_time 113 o[1].name = "communication_time" 114 UpdateParameterPerformance(currentParameters,1,o,2) 115 116 p = Parameter(1) 117 p = GetOptimalParameters(o,2,currentParameters,1); 90 118 91 119 channel = get_real_channel(int(p[0].value)) … … 96 124 hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets 97 125 126 stats_array[int(p[0].value)] = stats_array[int(p[0].value)] + 1 127 print "Stats:: 1..",stats_array[1]," 2..",stats_array[2]," 3..",stats_array[3]," 4..",stats_array[4] 98 128 return channel,hop_freq #returning the channel number and hop frequency 99 129 … … 154 184 155 185 parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 156 expert_grp_tx = parser_tx.add_option_group("Expert_tx") 157 186 parser_tx.add_option("", "--vtcross", action="store_true", default=False, 187 help="Use the CROSS engine for DSA decisions: default is random.") 188 158 189 parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 159 190 default='gmsk', … … 166 197 help="set megabytes to transmit [default=%default]") 167 198 parser_tx.add_option("","--discontinuous", action="store_true", default=False, 168 help="enable discontinous transmission (bursts of 5 packets)") 199 help="enable discontinous transmission (bursts of 5 packets)") 169 200 parser_tx.add_option("","--from-file", default=None, 170 201 help="use file for packet contents") 171 202 203 expert_grp_tx = parser_tx.add_option_group("Expert_tx") 204 172 205 transmit_path.add_options(parser_tx, expert_grp_tx) 173 206 174 207 for mod in mods.values(): 175 208 mod.add_options(expert_grp_tx) 209 176 210 177 211 (options_tx, args_tx) = parser_tx.parse_args () … … 188 222 options_tx.fusb_nblocks = 16 189 223 options_tx.bitrate = 0.0125e6 190 224 ############# 191 225 192 226 if options_tx.tx_freq is None: … … 195 229 sys.exit(1) 196 230 197 if options_tx.from_file is not None:198 source_file = open(options_tx.from_file, 'r')231 #if options_tx.from_file is not None: 232 # source_file = open(options_tx.from_file, 'r') 199 233 200 234 parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 201 235 expert_grp_rx = parser_rx.add_option_group("Expert_rx") 202 parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),203 default='gmsk',204 help="Select modulation from: %s [default=%%default]"205 % (', '.join(demods.keys()),))236 #parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 237 # default='gmsk', 238 # help="Select modulation from: %s [default=%%default]" 239 # % (', '.join(demods.keys()),)) 206 240 207 241 receive_path.add_options(parser_rx, expert_grp_rx) 208 242 209 for mod in demods.values():210 mod.add_options(expert_grp_rx)243 #for mod in demods.values(): 244 # mod.add_options(expert_grp_rx) 211 245 212 246 (options_rx, args_rx) = parser_rx.parse_args () 213 247 214 if len(args_rx) != 0:215 parser_rx.print_help(sys.stderr)216 sys.exit(1)248 #if len(args_rx) != 0: 249 # parser_rx.print_help(sys.stderr) 250 # sys.exit(1) 217 251 ############# Setting some default values for rx side of the block 218 252 options_rx.rx_freq = 462.5625e6 #setting default rx_freq value … … 224 258 ############# 225 259 226 if options_rx.rx_freq is None:227 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")228 parser_rx.print_help(sys.stderr)229 sys.exit(1)260 #if options_rx.rx_freq is None: 261 # sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 262 # parser_rx.print_help(sys.stderr) 263 # sys.exit(1) 230 264 231 265 # build the graph … … 248 282 249 283 # Scan all channels first for inital data 284 time.sleep(0.1) 250 285 251 286 while running: