Changeset 230 for vtcross/trunk/src/cognitive_engines
- Timestamp:
- 04/03/09 13:05:41 (15 years ago)
- Location:
- vtcross/trunk/src/cognitive_engines
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp
r228 r230 12 12 #include <cstring> 13 13 #include <stdint.h> 14 #include <math.h> 14 15 15 16 #include "vtcross/common.h" … … 94 95 95 96 void 97 CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters) 98 { 99 LOG("Cognitive Engine:: Receiving feedback.\n"); 100 101 uint32_t numberColumns = 102 radioInfo->numParameters + 103 radioInfo->numUtilities; 104 105 uint32_t obsColumns = radioInfo->numObservables + 1; 106 107 float valList[numberColumns]; 108 float obsVals[numberColumns]; 109 char *nameList[numberColumns]; 110 char *obsList[obsColumns]; 111 112 size_t columnObsIndex = 0; 113 for (size_t i = 0; i < radioInfo->numObservables; i++){ 114 obsList[columnObsIndex] = (char*)observables[i].name.c_str(); 115 columnObsIndex++; 116 } 117 obsList[columnObsIndex] = "utility"; 118 119 size_t columnIndex = 0; 120 for (size_t i = 0; i < radioInfo->numParameters; i++){ 121 nameList[columnIndex] = (char*)parameters[i].name.c_str(); 122 columnIndex++; 123 } 124 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 125 nameList[columnIndex] = (char*)uList[i].name.c_str(); 126 columnIndex++; 127 } 128 129 size_t obsValueIndex = 0; 130 for(size_t i = 0; i < radioInfo->numObservables; i++) { 131 obsVals[obsValueIndex] = observables[i].value; 132 obsValueIndex++; 133 } 134 // Calculate Utility 135 float newUtilityValue = 0; 136 137 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 138 newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); 139 } 140 obsVals[obsValueIndex] = newUtilityValue; 141 142 size_t returnValueIndex = 0; 143 for(size_t i = 0; i < radioInfo->numParameters; i++) { 144 valList[returnValueIndex] = parameters[i].value; 145 returnValueIndex++; 146 } 147 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 148 valList[returnValueIndex] = uList[i].target; 149 returnValueIndex++; 150 } 151 152 153 cbr_update(myCBR, nameList, obsList, valList, obsVals, 154 numberColumns, obsColumns); 155 } 156 157 158 void 96 159 CognitiveEngine::WaitForSignal() 97 160 { … … 110 173 // MUCH faster since instead of donig string compares we can simply 111 174 // switch on the integer value... 112 if(strcmp(buffer, " request_optimization") == 0) {175 if(strcmp(buffer, "update_performance") == 0) { 113 176 177 /* Receive Set of current Parameters */ 178 memset(buffer, 0, 256); 179 ReadMessage(commandSocketFD,buffer); 180 uint32_t numParameters = atoi(buffer); 181 182 Parameter *p = new Parameter[numParameters]; 183 184 for(size_t i = 0; i < numParameters; i++) { 185 memset(buffer, 0, 256); 186 ReadMessage(commandSocketFD, buffer); 187 p[i].name = std::string(buffer); 188 189 memset(buffer, 0, 256); 190 ReadMessage(commandSocketFD, buffer); 191 p[i].value = atof(buffer); 192 } 193 114 194 /* Receive Set of Observables */ 115 LOG("Cognitive Engine:: Receiving Observable Parameters\n");116 117 195 memset(buffer, 0, 256); 118 196 ReadMessage(commandSocketFD,buffer); … … 131 209 } 132 210 211 ReceiveFeedback(o,p); 212 213 delete [] o; 214 delete [] p; 215 } 216 else if(strcmp(buffer, "request_optimization") == 0) { 217 218 /* Receive Set of Observables */ 219 LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); 220 221 memset(buffer, 0, 256); 222 ReadMessage(commandSocketFD,buffer); 223 uint32_t numObservables = atoi(buffer); 224 225 Observable *o = new Observable[numObservables]; 226 227 for(size_t i = 0; i < numObservables; i++) { 228 memset(buffer, 0, 256); 229 ReadMessage(commandSocketFD, buffer); 230 o[i].name = std::string(buffer); 231 232 memset(buffer, 0, 256); 233 ReadMessage(commandSocketFD, buffer); 234 o[i].value = atof(buffer); 235 } 236 133 237 /* Receive Set of current Parameters */ 134 238 LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); … … 156 260 157 261 // TODO need to actually do something with the observables here 158 262 159 263 LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); 160 264 char numParametersChar[10]; … … 453 557 454 558 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 455 searchNames[i] = (char*) uList[i].name.c_str();559 searchNames[i] = (char*)observables[i].name.c_str(); 456 560 } 457 561 … … 492 596 if(rc == 0){ 493 597 /* Adapt the returned parameters to meet the objective */ 494 598 599 WARNING("Cognitive Engine:: Found\n"); 600 601 // Should do a random adaptation.. 602 if(returnValues[numberColumns-1] < 0) { 603 returnValues[2] = returnValues[2] - 15; 604 returnValues[3] = returnValues[3] - 2; 605 } else { 606 returnValues[2] = returnValues[2] + 15; 607 returnValues[3] = returnValues[3] + 2; 608 } 495 609 } else if(rc == 31337){ 610 WARNING("Cognitive Engine:: Not Found.\n"); 496 611 /* No rows in the CBR, pick default parameters */ 497 612 // Currently this is hard coded and implementation specific! 498 //returnValues[2] = currentParameters[0].value + 5;499 //returnValues[3] = currentParameters[1].value + 10;613 returnValues[2] = currentParameters[0].value + 5; 614 returnValues[3] = currentParameters[1].value + 10; 500 615 501 616 } else { … … 516 631 returnValueIndex++; 517 632 } 633 returnValues[returnValueIndex] = 0; 634 635 char *allNames[numberColumns]; 636 size_t allNameIndex = 0; 637 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 638 allNames[allNameIndex] = (char*)uList[i].name.c_str(); 639 returnValues[allNameIndex] = uList[i].target; 640 allNameIndex++; 641 } 642 for(size_t i = 0; i < radioInfo->numParameters; i++) { 643 allNames[allNameIndex] = (char*)pList[i].name.c_str(); 644 allNameIndex++; 645 } 646 for(size_t i = 0; i < radioInfo->numObservables; i++) { 647 allNames[allNameIndex] = (char*)oList[i].name.c_str(); 648 returnValues[allNameIndex] = 0; 649 allNameIndex++; 650 } 651 allNames[allNameIndex] = "utility"; 518 652 519 653 // Add row to CBR. 654 cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 520 655 521 656 return pList; … … 535 670 void 536 671 CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ 537 Utility *utilities) 538 { 539 LOG("Cognitive Engine:: Receiving feedback.\n"); 540 } 541 542 543 void 544 CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ 545 Utility *utilities, std::string service) 672 std::string service) 546 673 { 547 674 LOG("Cognitive Engine:: Receiving feedback.\n"); -
vtcross/trunk/src/cognitive_engines/cbr.c
r228 r230 222 222 } 223 223 224 printf("search command: %s\n", _cbr->command);224 //printf("search command: %s\n", _cbr->command); 225 225 226 226 //ExecuteCommand(_cbr); … … 235 235 } 236 236 237 238 // update a row 239 int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals, 240 unsigned int _wherelen, unsigned int _setlen) 241 { 242 unsigned int i; 243 244 // generate command 245 //printf("%s\n", _cbr->command); 246 strcpy(_cbr->command, "UPDATE "); 247 strcat(_cbr->command, _cbr->tablename); 248 249 strcat(_cbr->command, " SET "); 250 for (i=0; i<_setlen; i++) { 251 strcat(_cbr->command, _set[i]); 252 strcat(_cbr->command, " = "); 253 sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]); 254 strcat(_cbr->command, " "); 255 if (i != _setlen-1) // not last entry 256 strcat(_cbr->command, ", "); 257 } 258 strcat(_cbr->command, " WHERE "); 259 260 for (i=0; i<_wherelen; i++) { 261 strcat(_cbr->command, _where[i]); 262 strcat(_cbr->command, " = "); 263 sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]); 264 strcat(_cbr->command, " "); 265 if (i != _wherelen-1) // not last entry 266 strcat(_cbr->command, "AND "); 267 } 268 strcat(_cbr->command, ";"); 269 270 //printf("search command: %s\n", _cbr->command); 271 // execute add command 272 ExecuteCommand(_cbr); 273 274 return 0; 275 } 237 276 238 277 // cbr add a row … … 264 303 strcat(_cbr->command, ");"); 265 304 305 //printf("search command: %s\n", _cbr->command); 266 306 // execute add command 267 307 ExecuteCommand(_cbr);