- Timestamp:
- 07/28/09 23:23:14 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp
r375 r389 27 27 #include "sqlite3.h" 28 28 #include "sqlite3ext.h" 29 30 #define INTERFERENCE 0 31 #define COMMUNICATION_TIME 1 32 #define CHANNEL 2 33 #define ENERGY 3 34 #define FITNESS 4 35 29 36 30 37 … … 632 639 } 633 640 641 642 /* The core of the CE is this function */ 643 634 644 Parameter* 635 645 CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) … … 637 647 LOG("Cognitive Engine:: Generating solution.\n"); 638 648 639 char *searchNames[radioInfo->numUtilities]; 649 /* Put together the CBR search array */ 650 651 char *searchNames[radioInfo->numUtilities + 1]; 652 float searchVals[radioInfo->numUtilities + 1]; 653 int searchOps[radioInfo->numUtilities + 1]; 654 uint32_t numberColumns = radioInfo->numUtilities + 655 radioInfo->numParameters + 656 radioInfo->numObservables + 1; 640 657 641 658 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 642 searchNames[i] = (char*)observables[i].name.c_str(); 643 } 644 645 float searchVals[radioInfo->numUtilities]; 646 647 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 648 searchVals[i] = uList[i].target; 649 } 650 651 uint32_t numberColumns = 652 radioInfo->numUtilities + 653 radioInfo->numParameters + 654 radioInfo->numObservables + 1; 655 656 float returnValues[numberColumns]; 657 658 int searchOps[radioInfo->numUtilities]; 659 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 660 661 /* If the goal is to maximum, set the search operation to 662 * return values greater than the target. 663 * 664 * If the goal is to minimize, set the search operation to 665 * return values less than the target. 666 */ 659 searchNames[i] = (char*)uList[i].name.c_str(); 660 667 661 if(strcmp(uList[i].goal.c_str(), "max") == 0) { 668 662 searchOps[i] = GT; … … 670 664 searchOps[i] = LT; 671 665 } 672 } 673 674 /* CBR specific call */ 666 667 /* Set search target to utility target */ 668 /* This may be bad if no target is set */ 669 searchVals[i] = uList[i].target; 670 } 671 672 673 /* Make sure we do not return any entries for the current channel */ 674 searchNames[radioInfo->numUtilities+1] = "channel"; 675 searchOps[radioInfo->numUtilities+1] = NE; 676 searchVals[radioInfo->numUtilities+1] = currentParameters[0].value; 677 678 float returnValues[numberColumns]; 679 680 /* Execute CBR search and put output into returnValues */ 675 681 uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 676 682 radioInfo->numUtilities, returnValues); 677 683 678 684 if(rc == 0){ 679 /* Adapt the returned parameters to meet the objective */ 680 LOG("Cognitive Engine:: Found\n"); 681 682 /* Should do a random adaptation.. */ 685 /* Similiar environment was found. Adapt the returned parameters to 686 meet the objective */ 687 688 LOG("Cognitive Engine:: Previous similar environment found\n"); 689 683 690 if(returnValues[numberColumns-1] < 0) { 691 684 692 returnValues[2] = returnValues[2] - 15; 685 693 returnValues[3] = returnValues[3] - 2; 694 686 695 } else { 696 687 697 returnValues[2] = returnValues[2] + 15; 688 698 returnValues[3] = returnValues[3] + 2; 699 689 700 } 690 701 } else if(rc == 31337) { 691 LOG("Cognitive Engine:: Not Found.\n"); 692 /* No rows in the CBR, pick default parameters */ 693 /* Currently this is hard coded and implementation specific! */ 694 returnValues[2] = currentParameters[0].value + 5; 695 returnValues[3] = currentParameters[1].value + 10; 696 702 /* No previous similar solution was found, use default. */ 703 LOG("Cognitive Engine:: No previous similar environment found.\n"); 704 705 /* In the DSA case, the default is to select a random channel */ 706 707 /* initialize random seed: */ 708 srand ( time(NULL) ); 709 710 /* generate random channel */ 711 //returnValues[CHANNEL] = rand() % currentParameters[0].max + currentParameters[0].min; 712 returnValues[CHANNEL] = rand() % 5; 713 714 printf("One: %i Two: %i\n",(int)currentParameters[0].max,(int)currentParameters[0].min); 715 697 716 } else { 698 717 LOG("Cognitive Engine:: Search return an invalid value.\n"); 699 718 } 719 720 721 /* Package up the new set of parameters in order to add 722 the new entry into the CBR database. */ 700 723 701 724 size_t returnValueIndex = 0; … … 732 755 allNames[allNameIndex] = "utility"; 733 756 734 / / Add row to CBR.757 /* Add the new optimized set to the CBR database */ 735 758 cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 736 759 760 /* Return the set of new parameter values. */ 737 761 return pList; 738 762 }