Changeset 562
- Timestamp:
- 05/20/10 17:34:20 (14 years ago)
- Location:
- vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.cpp
r554 r562 22 22 #include <iostream> 23 23 24 24 25 using namespace std; 26 27 // ZERO is consider +1 sign 28 inline int signof(int nos) { return ( (nos < 0) ? -1:1 ); } 29 //inline int signof(int nos) { return (nos==0)?0:(nos<0?-1:1); } 30 25 31 26 32 … … 37 43 { 38 44 LOG("Cognitive Engine:: Generating solution.\n"); 45 46 std::cout << "Ptune value: " << optList->Ptune << std::endl; 47 // std::cout << "slope values: " << Slope[0] << std::endl; 48 // std::cout << "slope values: " << Slope[1] << std::endl; 39 49 40 50 string searchNames[radioInfo->numUtilities]; … … 81 91 82 92 /* CBR specific call */ 83 uint32_t rc = myCBR ->Search(searchNames, searchOps, searchVals,93 uint32_t rc = myCBR1->Search(searchNames, searchOps, searchVals, 84 94 radioInfo->numUtilities, returnValues); 85 95 … … 87 97 /* Adapt the returned parameters to meet the objective */ 88 98 LOG("Cognitive Engine:: Found\n"); 89 99 100 FineTune(); 101 /* 102 No need or random adaptation since it will get updated 103 using update function call 104 if(returnValues[numberColumns-1] < 0) { 105 returnValues[2] = returnValues[2] - 15; 106 returnValues[3] = returnValues[3] - 2; 107 } else { 108 returnValues[2] = returnValues[2] + 15; 109 returnValues[3] = returnValues[3] + 2; 110 } 111 */ 90 112 } else if(rc == 31337) { 91 113 LOG("Cognitive Engine:: Not Found.\n"); … … 93 115 /* Currently this is hard coded and implementation specific! */ 94 116 117 int Pt = optList->Ptune; 118 // helps to cycle from on parameter to another.. 119 95 120 for (int i = 0; i < radioInfo->numParameters; i++) { 96 returnValues[i] = currentParameters[i].value + pList[i].step; 97 if (returnValues[i] > pList[i].max) { 121 returnValues[i] = currentParameters[i].value; 122 } 123 124 returnValues[Pt] = currentParameters[Pt].value + pList[Pt].step*(optList->Slope[Pt]); 125 126 //////////////////// LIMITER ///////////////////////////// 127 if (returnValues[Pt] > pList[Pt].max) { 98 128 std::cout << "SORRY CANT EXCEED MAX VALUE" << std::endl; 99 returnValues[i] = returnValues[i] - pList[i].step; 129 returnValues[Pt] = returnValues[Pt] - pList[Pt].step; 130 optList->Ptune += 1; 100 131 } 101 if (returnValues[ i] < pList[i].min) {132 if (returnValues[Pt] < pList[Pt].min) { 102 133 std::cout << "SORRY CANT GO BELOW MIN VALUE" << std::endl; 103 returnValues[i] = returnValues[i] + pList[i].step ; 134 returnValues[Pt] = returnValues[Pt] + pList[Pt].step ; 135 optList->Ptune += 1; 104 136 } 105 } 106 107 for (int i = 0; i < radioInfo->numUtilities; i++) 137 138 if (optList->Ptune > radioInfo->numParameters) { 139 optList->Ptune = 0; 140 } 141 ///////////////////////////////////////////////////////////// 142 143 144 for (int i = 0; i < radioInfo->numUtilities; i++) { 108 145 returnValues[radioInfo->numParameters + i] = uList[i].value; 109 110 for (int i = 0; i < radioInfo->numObservables; i++) 146 } 147 148 for (int i = 0; i < radioInfo->numObservables; i++) { 111 149 returnValues[radioInfo->numParameters + radioInfo->numUtilities + i] = oList[i].value; 150 } 112 151 113 152 size_t returnValueIndex = 0; 114 for(size_t i = 0; i < radioInfo->num Utilities; i++) {153 for(size_t i = 0; i < radioInfo->numParameters; i++) { 115 154 pList[i].value = returnValues[returnValueIndex]; 116 155 returnValueIndex++; 117 156 } 118 for(size_t i = 0; i < radioInfo->num Parameters; i++) {157 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 119 158 uList[i].value = returnValues[returnValueIndex]; 120 159 returnValueIndex++; … … 143 182 144 183 // Add row to CBR. 145 myCBR ->AddRow(allNames, returnValues, returnValueIndex + 1);146 184 myCBR1->AddRow(allNames, returnValues, returnValueIndex + 1); 185 myCBR2->AddRow(allNames, returnValues, returnValueIndex + 1); 147 186 } else { 148 187 LOG("Cognitive Engine:: Search return an invalid value.\n"); 149 188 } 150 151 189 152 190 return pList; … … 162 200 163 201 uint32_t whereLen = radioInfo->numParameters; 164 uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables ;202 uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables + 1; 165 203 166 204 … … 170 208 string setList[setLen]; 171 209 210 /* 211 size_t columnObsIndex = 0; 212 for (size_t i = 0; i < radioInfo->numObservables; i++){ 213 obsList[columnObsIndex] = observables[i].name; 214 columnObsIndex++; 215 } 216 obsList[columnObsIndex] = "utility"; 217 218 size_t columnIndex = 0; 219 for (size_t i = 0; i < radioInfo->numParameters; i++){ 220 nameList[columnIndex] = parameters[i].name; 221 columnIndex++; 222 } 223 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 224 nameList[columnIndex] = uList[i].name; 225 columnIndex++; 226 } 227 228 size_t obsValueIndex = 0; 229 for(size_t i = 0; i < radioInfo->numObservables; i++) { 230 obsVals[obsValueIndex] = observables[i].value; 231 obsValueIndex++; 232 } 233 234 /// Calculate Utility 235 float newUtilityValue = 0; 236 237 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 238 newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); 239 } 240 obsVals[obsValueIndex] = newUtilityValue; 241 242 size_t returnValueIndex = 0; 243 for(size_t i = 0; i < radioInfo->numParameters; i++) { 244 valList[returnValueIndex] = parameters[i].value; 245 returnValueIndex++; 246 } 247 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 248 valList[returnValueIndex] = uList[i].target; 249 returnValueIndex++; 250 } 251 */ 252 float TotalWeight = 0; 253 int Penalty = 10; 172 254 173 255 for(size_t i = 0; i < radioInfo->numParameters; i++) { 174 256 whereList[i] = parameters[i].name; 175 257 whereValue[i] = parameters[i].value; 258 TotalWeight += parameters[i].value*(optList->Pweights[i]); 176 259 } 177 260 … … 179 262 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 180 263 setList[returnValueIndex] = utilities[i].name; 181 setValue[returnValueIndex] = utilities[i].value; 264 setValue[returnValueIndex] = utilities[i].value; 265 if ((utilities[i].value > uList[i].target && uList[i].goal == "max") || (utilities[i].value < uList[i].target && uList[i].goal == "min")) 266 TotalWeight += abs(utilities[i].value - uList[i].target)*(optList->Uweights[i]); 267 else 268 TotalWeight += abs(utilities[i].value - uList[i].target)*(optList->Uweights[i])*Penalty; 182 269 returnValueIndex++; 183 270 } … … 187 274 returnValueIndex++; 188 275 } 189 190 191 myCBR->Update(whereList,setList,whereValue,setValue,whereLen,setLen); 192 193 } 194 276 setList[returnValueIndex] = "TotalWeight"; 277 setValue[returnValueIndex] = TotalWeight; 278 279 myCBR1->Update(whereList,setList,whereValue,setValue,whereLen,setLen); 280 281 // MAIN CODE HERE // 282 283 int searchOps[radioInfo->numParameters]; 284 string searchNames[radioInfo->numParameters]; 285 float searchVals[radioInfo->numParameters]; 286 float returnValues[numberColumns]; 287 288 289 for (int L = 0; L < radioInfo->numParameters ; L++) { 290 searchOps[L] = 0; 291 searchNames[L] = parameters[L].name; 292 searchVals[L] = parameters[L].value; 293 } 294 295 296 for (int lp = 0; lp < radioInfo->numParameters; lp++ ) { 297 298 searchOps[lp] = (optList->Slope[lp] == -1)?2:4; 299 300 uint32_t rc = myCBR1->Search(searchNames, searchOps, searchVals,radioInfo->numParameters, returnValues); 301 if(rc == 0) { 302 std::cout << "case found for learning for parameter : " << lp << std::endl; 303 304 /////////// 305 306 std::cout << "CASE STUDY" << std::endl; 307 std::cout << "returnvalue ber " << returnValues[radioInfo->numParameters] << std::endl; 308 std::cout << "BER current value " << utilities[0].value << std::endl; 309 310 311 312 ///////// 313 314 for (int ul = 0; ul < radioInfo->numUtilities; ul++) { 315 int rl = radioInfo->numParameters + ul; 316 if( abs(1 - returnValues[rl]/utilities[ul].value) > optList->PoC ) { 317 if(optList->Trend[lp][ul] == 0.0) { 318 319 if ((returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] >= 0 ) { 320 optList->Status[lp][ul] += 1; 321 } 322 else if ( ((returnValues[rl]) < utilities[ul].value) && optList->Status[lp][ul] <= 0) { 323 optList->Status[lp][ul] -= 1; 324 } 325 else { 326 optList->Status[lp][ul] = 0; 327 } 328 329 if (abs(optList->Status[lp][ul]) > 2) { 330 optList->Trend[lp][ul] = signof(optList->Status[lp][ul]); 331 std::cout << "TREND GETTING UPDATED" << std::endl; 332 } 333 } 334 else if ( optList->Trend[lp][ul] == 1 && (returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] >= 0 ) { 335 optList->Status[lp][ul] += 1; 336 } 337 else if ( optList->Trend[lp][ul] == -1 && (returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] <= 0) { 338 optList->Status[lp][ul] -= 1; 339 } 340 else { 341 optList->Status[lp][ul] = 0; 342 optList->Trend[lp][ul] = 0; 343 } 344 345 346 347 } 348 } 349 } 350 else if (rc == 31337) { 351 std::cout << "No case found for parameter: " << lp << std::endl; 352 } 353 searchOps[lp] = 0; 354 355 356 } 357 358 359 // updating slope matrix.. 360 // AS OF NOW VALID ONLY FOR ONE UTILITY 361 for (int i = 0; i < radioInfo->numParameters; i++) { 362 if ( (uList[0].goal == "min" && optList->Trend[i][0] < 0) || (uList[0].goal == "max" && optList->Trend[i][0] > 0) ) { 363 optList->Slope[i] = -1; 364 } 365 else if ( (uList[0].goal == "min" && optList->Trend[i][0] >= 0) || (uList[0].goal == "max" && optList->Trend[i][0] <= 0) ) { 366 optList->Slope[i] = 1; 367 } 368 } 369 370 371 std::cout << "Status1 " << optList->Status[0][0] << std::endl; 372 std::cout << "Status2 " << optList->Status[1][0] << std::endl; 373 std::cout << "Slope1 " << optList->Slope[0] << std::endl; 374 std::cout << "Slope2 " << optList->Slope[1] << std::endl; 375 std::cout << "Trend1 " << optList->Trend[0][0] << std::endl; 376 std::cout << "Trend2 " << optList->Trend[1][0] << std::endl; 377 /// end of MAIN CODE // 378 } 195 379 196 380 void 197 381 CBR_CE::BuildCognitiveEngine() 198 382 { 199 string filename = "ex1"; 200 string filename2 = "ex2"; 201 string tablename = "data"; 383 string filename = "exdb"; 384 string tablename1 = "data1"; 385 string tablename2 = "data2"; 386 387 //// NEW SET OF VARIABLES /// 388 389 // float * Pweights = new float[radioInfo->numParameters]; 390 optList->Uweights = new float[radioInfo->numUtilities]; 391 optList->Pweights = new float[radioInfo->numParameters]; 392 393 ///// we have to assume 10 utilities maxxx!!!! VERY important 394 395 optList->Status = new float[radioInfo->numParameters][10]; 396 optList->Trend = new float[radioInfo->numParameters][10]; 397 optList->PoC = 0.01; 398 optList->Slope = new int[radioInfo->numParameters]; 399 optList->Ptune = 0; // starting with most tunable parameter.. (widest range..) 400 401 402 ////////////initializing here since initialsetting giving wierd errors///// 403 404 for (int x = 0; x < radioInfo->numParameters; x++) { 405 std::cout << x << std::endl; 406 optList->Pweights[x] = 1/(pList[x].step); 407 optList->Slope[x] = 1; 408 } 409 410 411 412 for (size_t i = 0; i < radioInfo->numUtilities; i++) { 413 optList->Uweights[i] = 1; 414 } 415 416 417 for (size_t i = 0; i < radioInfo->numParameters; i++) { 418 for (size_t k = 0; k < radioInfo->numUtilities; k++) { 419 optList->Status[i][k] = 0; 420 optList->Trend[i][k] = 0; 421 } 422 } 423 //////////////////////////////////////////////////////////////////////// 424 425 426 202 427 203 428 uint32_t numberColumns = radioInfo->numParameters + radioInfo->numUtilities \ … … 208 433 209 434 string cols[numberColumns]; 210 string Plist[numberParameters];211 string Ulist[numberUtilities];212 float Utarget[numberUtilities];213 string Ugoal[numberUtilities];214 215 435 size_t columnIndex = 0; 216 size_t Pindex = 0; // new217 size_t Uindex = 0; // new218 436 219 437 … … 221 439 cols[columnIndex] = pList[i].name; 222 440 columnIndex++; 223 Plist[Pindex] = pList[i].name; // new224 Pindex++; // new225 441 } 226 442 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 227 443 cols[columnIndex] = uList[i].name; 228 444 columnIndex++; 229 Ulist[Uindex] = uList[i].name; // new230 Utarget[Uindex] = uList[i].target; // new231 Ugoal[Uindex] = uList[i].goal; // new232 Uindex++; // new233 234 445 } 235 446 for (size_t i = 0; i < radioInfo->numObservables; i++){ … … 239 450 cols[columnIndex] = "TotalWeight"; 240 451 241 myCBR = new CBR(filename, tablename, cols, numberColumns); 452 myCBR1 = new CBR(filename, tablename1, cols, numberColumns); 453 454 myCBR2 = new CBR(filename, tablename2, cols, numberColumns); 455 242 456 243 457 } … … 399 613 } 400 614 615 void CBR_CE::InitialSetting() 616 { 617 618 std::cout << "inside initilsetting method" << std::endl; 619 620 for (int x = 0; x < radioInfo->numParameters; x++) { 621 std::cout << x << std::endl; 622 optList->Pweights[x] = 1/(pList[x].step); 623 optList->Slope[x] = 0; 624 } 625 626 627 628 for (size_t i = 0; i < radioInfo->numUtilities; i++) { 629 optList->Uweights[i] = 1; 630 } 631 632 633 for (size_t i = 0; i < radioInfo->numParameters; i++) { 634 for (size_t k = 0; k < radioInfo->numUtilities; k++) { 635 optList->Status[i][k] = 0; 636 optList->Trend[i][k] = 0; 637 } 638 } 639 640 } 641 401 642 402 643 void … … 412 653 Shutdown(); 413 654 } 414 655 656 void 657 CBR_CE::FineTune() 658 { 659 660 std::cout << "UNDER DEVELOPMENT" << std::endl; 661 662 } -
vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.h
r554 r562 37 37 38 38 39 //typedef float (*array)[radioInfo->numParameters][radioInfo->numUtilities]; 40 39 41 class CBR_CE : public CognitiveEngine 40 42 { 41 43 public: 44 42 45 /*! Default constructor. */ 43 46 CBR_CE() : CognitiveEngine(){}; … … 92 95 * cognitive engine's functionality. 93 96 */ 97 void InitialSetting(); 98 94 99 void PerformUpdatePerformance(); 95 100 void PerformRequestOptimization(); … … 97 102 void PerformResetEngineCognitive(); 98 103 void PerformShutdownEngineCognitive(); 104 void FineTune(); 99 105 100 CBR *myCBR; 106 CBR *myCBR1; 107 CBR *myCBR2; 108 /* 109 private: 110 111 float * Pweights; 112 float * Uweights; 113 114 float (*Status)[10]; 115 // this array stores the relationship between all parameters vs utilities.. 116 float (*Trend)[10]; 117 118 int * Slope; 119 120 float PoC; 121 // this term represents percentage of change.. as in if this min PoC 122 // is not detected its as good as no change.. 123 int Ptune; 124 // This is pointer to correspont to which parameter to tune.. 125 // AS OF NOW ONLY ONE AT A TIME PER CYCLE 126 */ 101 127 102 128 };