Changeset 562

Show
Ignore:
Timestamp:
05/20/10 17:34:20 (14 years ago)
Author:
nikhil
Message:

new changes in cbr file

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  
    2222#include <iostream> 
    2323 
     24 
    2425using namespace std; 
     26 
     27// ZERO is consider +1 sign 
     28inline int signof(int nos) { return ( (nos < 0) ? -1:1 ); } 
     29//inline int signof(int nos) { return (nos==0)?0:(nos<0?-1:1); } 
     30 
    2531 
    2632 
     
    3743{ 
    3844    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; 
    3949 
    4050    string searchNames[radioInfo->numUtilities]; 
     
    8191 
    8292    /* CBR specific call */ 
    83     uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, 
     93    uint32_t rc = myCBR1->Search(searchNames, searchOps, searchVals, 
    8494            radioInfo->numUtilities, returnValues); 
    8595 
     
    8797        /* Adapt the returned parameters to meet the objective */ 
    8898        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*/ 
    90112    } else if(rc == 31337) { 
    91113        LOG("Cognitive Engine:: Not Found.\n"); 
     
    93115        /* Currently this is hard coded and implementation specific! */ 
    94116 
     117     int Pt = optList->Ptune;  
     118 // helps to cycle from on parameter to another..  
     119 
    95120        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) { 
    98128                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; 
    100131            } 
    101             if (returnValues[i] < pList[i].min) { 
     132            if (returnValues[Pt] < pList[Pt].min) { 
    102133                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;              
    104136            } 
    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++) { 
    108145            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++) { 
    111149            returnValues[radioInfo->numParameters + radioInfo->numUtilities + i] = oList[i].value; 
     150        } 
    112151 
    113152    size_t returnValueIndex = 0; 
    114     for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
     153    for(size_t i = 0; i < radioInfo->numParameters; i++) { 
    115154        pList[i].value = returnValues[returnValueIndex]; 
    116155        returnValueIndex++; 
    117156    } 
    118     for(size_t i = 0; i < radioInfo->numParameters; i++) { 
     157    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
    119158        uList[i].value = returnValues[returnValueIndex]; 
    120159        returnValueIndex++; 
     
    143182 
    144183    // 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);        
    147186        } else { 
    148187        LOG("Cognitive Engine:: Search return an invalid value.\n"); 
    149188    } 
    150  
    151189 
    152190    return pList; 
     
    162200 
    163201    uint32_t whereLen = radioInfo->numParameters; 
    164     uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables; 
     202    uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables + 1; 
    165203 
    166204 
     
    170208    string setList[setLen]; 
    171209 
     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; 
    172254 
    173255    for(size_t i = 0; i < radioInfo->numParameters; i++) { 
    174256        whereList[i] = parameters[i].name; 
    175257        whereValue[i] = parameters[i].value; 
     258        TotalWeight += parameters[i].value*(optList->Pweights[i]); 
    176259    } 
    177260 
     
    179262    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
    180263        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;    
    182269        returnValueIndex++; 
    183270    } 
     
    187274        returnValueIndex++; 
    188275    } 
    189  
    190  
    191 myCBR->Update(whereList,setList,whereValue,setValue,whereLen,setLen); 
    192  
    193 } 
    194  
     276        setList[returnValueIndex] = "TotalWeight"; 
     277        setValue[returnValueIndex] = TotalWeight;  
     278         
     279myCBR1->Update(whereList,setList,whereValue,setValue,whereLen,setLen); 
     280 
     281// MAIN CODE HERE // 
     282 
     283int searchOps[radioInfo->numParameters]; 
     284string searchNames[radioInfo->numParameters]; 
     285float searchVals[radioInfo->numParameters]; 
     286float returnValues[numberColumns]; 
     287 
     288 
     289for (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 
     296for (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} 
    195379 
    196380void 
    197381CBR_CE::BuildCognitiveEngine() 
    198382{ 
    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 
    202427 
    203428    uint32_t numberColumns = radioInfo->numParameters + radioInfo->numUtilities \ 
     
    208433 
    209434    string cols[numberColumns]; 
    210     string Plist[numberParameters]; 
    211     string Ulist[numberUtilities]; 
    212     float Utarget[numberUtilities]; 
    213     string Ugoal[numberUtilities]; 
    214  
    215435    size_t columnIndex = 0; 
    216     size_t Pindex = 0;              // new 
    217     size_t Uindex = 0;              // new 
    218436 
    219437 
     
    221439        cols[columnIndex] = pList[i].name; 
    222440        columnIndex++; 
    223         Plist[Pindex] = pList[i].name;   // new 
    224         Pindex++;                        // new 
    225441    } 
    226442    for (size_t i = 0; i < radioInfo->numUtilities; i++){ 
    227443        cols[columnIndex] = uList[i].name; 
    228444        columnIndex++; 
    229         Ulist[Uindex] = uList[i].name;     // new 
    230         Utarget[Uindex] = uList[i].target; // new 
    231         Ugoal[Uindex] = uList[i].goal;     // new 
    232         Uindex++;                          // new 
    233  
    234445    }       
    235446    for (size_t i = 0; i < radioInfo->numObservables; i++){    
     
    239450    cols[columnIndex] = "TotalWeight";   
    240451 
    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 
    242456 
    243457} 
     
    399613} 
    400614 
     615void 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 
    401642 
    402643void 
     
    412653    Shutdown(); 
    413654} 
    414   
     655 
     656void 
     657CBR_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  
    3737 
    3838 
     39//typedef float (*array)[radioInfo->numParameters][radioInfo->numUtilities]; 
     40 
    3941class CBR_CE : public CognitiveEngine 
    4042{ 
    4143    public: 
     44 
    4245        /*! Default constructor. */ 
    4346        CBR_CE() : CognitiveEngine(){}; 
     
    9295         * cognitive engine's functionality. 
    9396         */ 
     97        void InitialSetting(); 
     98 
    9499        void PerformUpdatePerformance(); 
    95100        void PerformRequestOptimization(); 
     
    97102        void PerformResetEngineCognitive(); 
    98103        void PerformShutdownEngineCognitive(); 
     104        void FineTune(); 
    99105 
    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*/  
    101127 
    102128};