Changeset 228
- Timestamp:
- 04/02/09 17:35:03 (15 years ago)
- Location:
- vtcross/trunk/src
- Files:
-
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp
r224 r228 19 19 #include "vtcross/error.h" 20 20 #include "vtcross/socketcomm.h" 21 21 #include "vtcross/cbr.h" 22 23 #include "cbr.c" 24 25 #include <sqlite3.h> 26 #include <sqlite3ext.h> 27 28 29 30 static cbr myCBR; 22 31 23 32 CognitiveEngine::CognitiveEngine() … … 31 40 CognitiveEngine::~CognitiveEngine() 32 41 { 42 cbr_free(myCBR); 33 43 delete [] pList; 34 44 delete [] oList; … … 82 92 } 83 93 } 84 85 94 86 95 void … … 101 110 // MUCH faster since instead of donig string compares we can simply 102 111 // switch on the integer value... 103 if(strcmp(buffer, " get_solution") == 0) {112 if(strcmp(buffer, "request_optimization") == 0) { 104 113 105 114 /* Receive Set of Observables */ … … 122 131 } 123 132 133 /* Receive Set of current Parameters */ 134 LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); 135 136 memset(buffer, 0, 256); 137 ReadMessage(commandSocketFD,buffer); 138 uint32_t numCurrentParameters = atoi(buffer); 139 140 Parameter *cp = new Parameter[numCurrentParameters]; 141 142 for(size_t i = 0; i < numCurrentParameters; i++) { 143 memset(buffer, 0, 256); 144 ReadMessage(commandSocketFD, buffer); 145 cp[i].name = std::string(buffer); 146 147 memset(buffer, 0, 256); 148 ReadMessage(commandSocketFD, buffer); 149 cp[i].value = atof(buffer); 150 } 124 151 LOG("Cognitive Engine:: Processing parameters....\n"); 152 153 Parameter *solutionSet; 154 155 solutionSet = GetSolution(o,cp); 125 156 126 157 // TODO need to actually do something with the observables here 127 158 128 159 LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); 129 130 SendMessage(commandSocketFD,"1"); 131 SendMessage(commandSocketFD,"txPower"); 132 SendMessage(commandSocketFD,"10"); 160 char numParametersChar[10]; 161 char solutionValue[50]; 162 sprintf(numParametersChar,"%i",radioInfo->numParameters); 163 SendMessage(commandSocketFD,numParametersChar); 164 for(size_t i = 0; i < radioInfo->numParameters; i++) { 165 SendMessage(commandSocketFD,solutionSet[i].name.c_str()); 166 memset(solutionValue, 0, 50); 167 sprintf(solutionValue,"%f",solutionSet[i].value); 168 SendMessage(commandSocketFD,solutionValue); 169 } 133 170 134 171 delete [] o; 172 delete [] cp; 135 173 } 136 174 else if(strcmp(buffer, "query_component_type") == 0) { … … 295 333 ReadMessage(commandSocketFD,buffer); 296 334 uList[i].name = std::string(buffer); 297 335 298 336 memset(buffer, 0, 256); 299 337 ReadMessage(commandSocketFD,buffer); … … 386 424 387 425 SendMessage(commandSocketFD, "receive_config_ack"); 426 427 BuildCognitiveEngine(); 388 428 } 389 429 … … 405 445 } 406 446 407 void408 CognitiveEngine::GetSolution(Observable *observables )447 Parameter* 448 CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) 409 449 { 410 450 LOG("Cognitive Engine:: Generating solution.\n"); 411 } 412 413 void 414 CognitiveEngine::GetSolution(Observable *observables, std::string service) 451 452 char *searchNames[radioInfo->numUtilities]; 453 454 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 455 searchNames[i] = (char*)uList[i].name.c_str(); 456 } 457 458 float searchVals[radioInfo->numUtilities]; 459 460 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 461 searchVals[i] = uList[i].target; 462 } 463 464 uint32_t numberColumns = 465 radioInfo->numUtilities + 466 radioInfo->numParameters + 467 radioInfo->numObservables + 1; 468 469 float returnValues[numberColumns]; 470 471 int searchOps[radioInfo->numUtilities]; 472 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 473 474 /* If the goal is to maximum, set the search operation to 475 * return values greater than the target. 476 * 477 * If the goal is to minimize, set the search operation to 478 * return values less than the target. 479 */ 480 481 if(strcmp(uList[i].goal.c_str(),"max") == 0) { 482 searchOps[i] = GT; 483 } else if(strcmp(uList[i].goal.c_str(),"min") == 0) { 484 searchOps[i] = LT; 485 } 486 } 487 488 /* CBR specific call */ 489 uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 490 radioInfo->numUtilities, returnValues); 491 492 if(rc == 0){ 493 /* Adapt the returned parameters to meet the objective */ 494 495 } else if(rc == 31337){ 496 /* No rows in the CBR, pick default parameters */ 497 // Currently this is hard coded and implementation specific! 498 //returnValues[2] = currentParameters[0].value + 5; 499 //returnValues[3] = currentParameters[1].value + 10; 500 501 } else { 502 WARNING("Cognitive Engine:: Search return an invalid value.\n"); 503 } 504 505 size_t returnValueIndex = 0; 506 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 507 uList[i].value = returnValues[returnValueIndex]; 508 returnValueIndex++; 509 } 510 for(size_t i = 0; i < radioInfo->numParameters; i++) { 511 pList[i].value = returnValues[returnValueIndex]; 512 returnValueIndex++; 513 } 514 for(size_t i = 0; i < radioInfo->numObservables; i++) { 515 oList[i].value = returnValues[returnValueIndex]; 516 returnValueIndex++; 517 } 518 519 // Add row to CBR. 520 521 return pList; 522 523 } 524 525 526 Parameter* 527 CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters, std::string service) 415 528 { 416 529 LOG("Cognitive Engine:: Generating solution for %s service.\n",service.c_str()); 417 } 530 531 return pList; 532 } 533 418 534 419 535 void … … 424 540 } 425 541 542 426 543 void 427 544 CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ … … 430 547 LOG("Cognitive Engine:: Receiving feedback.\n"); 431 548 } 549 550 551 void 552 CognitiveEngine::BuildCognitiveEngine() 553 { 554 char filename[] = {"ex1"}; 555 char tablename[] = {"data"}; 556 557 uint32_t numberColumns = 558 radioInfo->numUtilities + 559 radioInfo->numParameters + 560 radioInfo->numObservables + 1; 561 562 char *cols[numberColumns]; 563 564 size_t columnIndex = 0; 565 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 566 cols[columnIndex] = (char*)uList[i].name.c_str(); 567 columnIndex++; 568 } 569 for (size_t i = 0; i < radioInfo->numParameters; i++){ 570 cols[columnIndex] = (char*)pList[i].name.c_str(); 571 columnIndex++; 572 } 573 for (size_t i = 0; i < radioInfo->numObservables; i++){ 574 cols[columnIndex] = (char*)oList[i].name.c_str(); 575 columnIndex++; 576 } 577 cols[columnIndex] = "utility"; 578 579 myCBR = cbr_create(filename, tablename, cols, numberColumns); 580 } 581 -
vtcross/trunk/src/cognitive_engines/cbr.c
r161 r228 60 60 rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 61 61 if( rc!=SQLITE_OK){ 62 fprintf(stderr, "SQL error: %s \n", zErrMsg);62 fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command); 63 63 sqlite3_free(zErrMsg); 64 64 } else{ … … 72 72 int ExecuteSearchCommand(cbr _cbr, float *_retvals){ 73 73 int rc; 74 //char *zErrMsg = 0;75 74 unsigned int i; 76 75 77 //printf("command: %s\n", _cbr->command);78 76 sqlite3_stmt * pStatement; 79 77 rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL); 80 //if (rc == SQLITE_OK && sqlite3_step(pStatement) == SQLITE_ROW){81 78 if (rc == SQLITE_OK){ 82 79 if (sqlite3_step(pStatement) == SQLITE_ROW){ 83 printf("there is search return data\n"); 84 for (i=0; i<_cbr->num_columns; ++i) 85 _retvals[i] = sqlite3_column_double(pStatement, i); 80 for (i=0; i<_cbr->num_columns; ++i) 81 _retvals[i] = sqlite3_column_double(pStatement, i); 86 82 } else { 87 printf("no matched rearch results. use default values\n"); 88 89 _retvals[0] = 100; // throughput 90 _retvals[1] = 1; // spectral_efficiency 91 _retvals[2] = -2; // log10_ber 92 _retvals[3] = 1; // mod_scheme 93 _retvals[4] = -10; // tx_power 94 _retvals[5] = 10; // snr 95 _retvals[6] = 0.5; // utility 96 } 83 printf("CBR:: No matched results returning default.\n"); 84 rc=31337; 85 } 86 } else { 87 printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command); 97 88 } 98 89 99 90 sqlite3_finalize(pStatement); 100 101 /*rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg); 102 if( rc!=SQLITE_OK){ 103 fprintf(stderr, "SQL error: %s\n", zErrMsg); 104 sqlite3_free(zErrMsg); 105 } else{ 106 printf("command executed.\n"); 107 }*/ 108 91 109 92 return rc; 110 93 } … … 185 168 // execute print (select all) command 186 169 ExecuteCommand(_cbr); 187 //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);170 printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); 188 171 } 189 172 … … 209 192 float *_retvals) 210 193 { 194 int rc; 211 195 // generate command 212 196 strcpy(_cbr->command, "select "); … … 231 215 strcat(_cbr->command, str_buffer); 232 216 217 233 218 if (i<_n-1) 234 219 strcat(_cbr->command, " AND "); … … 237 222 } 238 223 239 //printf("search command: %s\n", _cbr->command);224 printf("search command: %s\n", _cbr->command); 240 225 241 226 //ExecuteCommand(_cbr); 242 ExecuteSearchCommand(_cbr, _retvals);227 rc = ExecuteSearchCommand(_cbr, _retvals); 243 228 244 229 /*printf("search result: "); … … 247 232 printf("\n");*/ 248 233 249 return 0;234 return rc; 250 235 } 251 236 -
vtcross/trunk/src/include/vtcross/components.h
r222 r228 260 260 * and active. 261 261 */ 262 void GetSolution(Observable *observables);263 void GetSolution(Observable *observables, std::string service);262 Parameter *GetSolution(Observable *observables, Parameter *currentParameters); 263 Parameter *GetSolution(Observable *observables, Parameter *currentParameters, std::string service); 264 264 265 265 /* Receive a feedback from the radio regarding the performance of a … … 275 275 void ReceiveFeedback(Observable *observables, \ 276 276 Parameter *parameters, Utility *utilities, std::string service); 277 278 279 /* BuildCognitiveEngine performs the CE implementation specific work 280 * that defines the internals of a CE. For example, a CBR CE engine 281 * would build the case-base reasoner or create the database, a neural 282 * network based CE may perform the initial training, a GA based CE 283 * may build the chromosome structure. 284 */ 285 void BuildCognitiveEngine(); 277 286 278 287 /* The SML_present bool reflects whether or not the remote component -
vtcross/trunk/src/include/vtcross/libvtcross.h
r222 r228 76 76 * use this? Shouldn't this be internal to the radio operation? 77 77 */ 78 Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables); 78 Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, 79 Parameter *currentParameters, uint32_t numCurrentParameters); 79 80 80 81 … … 103 104 bool Shutdown(); 104 105 106 /* Return total number of currently recognized transmission parameters. 107 */ 108 uint32_t GetNumParameters(); 109 110 105 111 #endif -
vtcross/trunk/src/libvtcross/libvtcross.cpp
r227 r228 27 27 } 28 28 29 uint32_t 30 GetNumParameters() 31 { 32 uint32_t socketfd, numParameters; 33 char buffer[256]; 34 35 socketfd = ConnectToRemoteComponent(); 36 SendMessage(socketfd,"get_number_parameters"); 37 38 memset(buffer, 0, 256); 39 ReadMessage(socketfd, buffer); 40 numParameters = atoi(buffer); 41 42 return numParameters; 43 } 29 44 30 45 /* Given a certain set of observables, ask the radio to find the optimum radio … … 38 53 * memory when it is done using it. 39 54 */ 40 Parameter* 41 GetOptimalParameters(Observable *radioObservables, uint32_t numObservables) 42 { 55 Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, 56 Parameter *currentParameters, uint32_t numCurrentParameters) { 57 uint32_t i,socketfd,numParameters; 43 58 char var[50]; 44 59 char counter[55]; … … 61 76 } 62 77 78 // Send Observables 79 memset(counter, 0, 55); 80 sprintf(counter,"%i",numCurrentParameters); 81 SendMessage(socketfd,counter); 82 for(i = 0; i < numCurrentParameters; i++) { 83 SendMessage(socketfd,currentParameters[i].name.c_str()); 84 sprintf(var,"%f",currentParameters[i].value); 85 SendMessage(socketfd,var); 86 } 63 87 /* Receive Set of Parameters */ 64 88 memset(buffer, 0, 256); -
vtcross/trunk/src/libvtcross/libvtcross_demo.cpp
r225 r228 23 23 main(int32_t argc, char *argv[]) 24 24 { 25 Observable * o = new Observable[1]; 25 Observable *o = new Observable[1]; 26 Parameter *currentParameters = new Parameter[2]; 26 27 Parameter * p; 27 28 28 o[0].name = "Test123"; 29 o[0].value = 1243.00; 29 uint32_t numParameters; 30 30 31 p = GetOptimalParameters(o,1); 31 o[0].name = "PER"; 32 o[0].value = 12.00; 33 34 currentParameters[0].name = "tx_power"; 35 currentParameters[0].value = 10.0; 36 currentParameters[1].name = "bandwidth"; 37 currentParameters[1].value = 300.0; 38 39 p = GetOptimalParameters(o,1,currentParameters,2); 40 numParameters = GetNumParameters(); 32 41 33 42 printf("Application:: Received the following parameters.\n"); 34 printf("%s:: %f\n",p[0].name.c_str(),p[0].value); 43 44 for(size_t i = 0; i < numParameters; i++) { 45 printf("%s:: %f\n",p[i].name.c_str(),p[i].value); 46 } 35 47 36 delete [] p; 48 // Try them out! Do they work? 49 50 Utility *resultingUtility = new Utility[2]; 51 52 // UpdateParameterPerformance(p,o,resultingUtility); 53 54 55 delete [] p; 37 56 delete [] o; 38 57 -
vtcross/trunk/src/shell/CognitiveRadioShell.cpp
r227 r228 454 454 { 455 455 char buffer[256]; 456 uint32_t numObservables,numParameters,numCurrentParameters; 456 457 char counter[55]; 457 458 char var[50]; … … 476 477 } 477 478 479 /* Receive Set of Current Parameters */ 480 memset(buffer, 0, 256); 481 ReadMessage(commandSocketFD,buffer); 482 numCurrentParameters = atoi(buffer); 483 484 LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters); 485 Parameter * cp = new Parameter[numCurrentParameters]; 486 487 for (i = 0; i < numCurrentParameters; i++){ 488 memset(buffer, 0, 256); 489 ReadMessage(commandSocketFD,buffer); 490 cp[i].name = std::string(buffer); 491 492 memset(buffer, 0, 256); 493 ReadMessage(commandSocketFD,buffer); 494 cp[i].value = atof(buffer); 495 } 478 496 /* Send to Cognitive Engine 479 497 * TODO: With multiple CEs we need to make a decision about where … … 481 499 */ 482 500 483 LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n"); 484 SendMessage(ceSocketFD, "get_solution"); 485 sprintf(counter, "%i", numObservables); 486 SendMessage(ceSocketFD, counter); 487 for(size_t i = 0; i < numObservables; i++) { 488 SendMessage(ceSocketFD, o[i].name.c_str()); 489 sprintf(var, "%f", o[i].value); 490 SendMessage(ceSocketFD, var); 491 } 492 501 if(!SML_present) { 502 LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n"); 503 SendMessage(ceSocketFD,"request_optimization"); 504 sprintf(counter,"%i",numObservables); 505 SendMessage(ceSocketFD,counter); 506 for(i = 0; i < numObservables; i++) { 507 SendMessage(ceSocketFD,o[i].name.c_str()); 508 sprintf(var,"%f",o[i].value); 509 SendMessage(ceSocketFD,var); 510 } 511 512 LOG("Cognitive Radio Shell:: Passing on current parameters to Cognitive Engine\n"); 513 sprintf(counter,"%i",numCurrentParameters); 514 SendMessage(ceSocketFD,counter); 515 for(i = 0; i < numCurrentParameters; i++) { 516 SendMessage(ceSocketFD,cp[i].name.c_str()); 517 sprintf(var,"%f",cp[i].value); 518 SendMessage(ceSocketFD,var); 519 } 520 } 493 521 494 522 LOG("Cognitive Radio Shell:: Receiving optimized parameters from Cognitive Engine.\n"); … … 546 574 } else if(strcmp(buffer, "deregister_sml") == 0) { 547 575 DeregisterSML(socketFD); 548 } else if(strcmp(buffer, "request_optimization") == 0) { 576 } else if(strcmp(buffer,"get_number_parameters") == 0) { 577 char numParameters[20]; 578 sprintf(numParameters,"%i",radio_info->numParameters); 579 SendMessage(commandSocketFD,numParameters); 580 } else if(strcmp(buffer,"request_optimization") == 0) { 549 581 /* Receive optimization request and current environment */ 550 582 GetOptimalParameters(socketFD); 583 } else if(strcmp(buffer,"request_optimization_service") == 0) { 584 /* Receive optimization request and current environment */ 585 //GetOptimalParametersService(socketFD); 551 586 } 552 587 }