Show
Ignore:
Timestamp:
07/29/09 16:10:39 (15 years ago)
Author:
trnewman
Message:

Fleshed out DSA reference implementation

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp

    r390 r391  
    2929 
    3030#define INTERFERENCE 0 
     31 
     32#define CHANNEL 1 
     33 
     34#define ENERGY 0 
    3135#define COMMUNICATION_TIME 1 
    32 #define CHANNEL 2 
    33 #define ENERGY 3 
    34 #define FITNESS 4 
     36 
     37#define UTILITY 4 
    3538 
    3639 
     
    111114     
    112115    uint32_t numberColumns =  
    113         radioInfo->numParameters + 
    114         radioInfo->numUtilities; 
     116        radioInfo->numParameters; 
    115117 
    116118    uint32_t obsColumns = radioInfo->numObservables + 1; 
     119    uint32_t numberTotalColumns = radioInfo->numUtilities + 
     120                                radioInfo->numParameters + 
     121                                radioInfo->numObservables + 1; 
    117122 
    118123    float valList[numberColumns]; 
     
    120125    char *nameList[numberColumns]; 
    121126    char *obsList[obsColumns]; 
     127    char *searchNames[1]; 
     128    float searchVals[1]; 
     129    int searchOps[1]; 
     130    float returnValues[numberTotalColumns]; 
    122131 
    123132    size_t columnObsIndex = 0; 
     
    126135        columnObsIndex++; 
    127136    }   
    128     obsList[columnObsIndex] = "utility"; 
     137    
     138    std::string utility_name = "utility";  
     139    obsList[columnObsIndex] = (char*) utility_name.c_str(); 
    129140 
    130141    size_t columnIndex = 0; 
     
    133144        columnIndex++; 
    134145    }    
    135     for (size_t i = 0; i < radioInfo->numUtilities; i++){ 
    136         nameList[columnIndex] = (char*)uList[i].name.c_str(); 
    137         columnIndex++; 
    138     }    
    139146 
    140147    size_t obsValueIndex = 0; 
     
    144151    } 
    145152 
     153    /* Make sure we do not return any entries for the current channel */ 
     154    std::string channel_name = "channel"; 
     155    searchNames[0] = (char *) channel_name.c_str(); 
     156    searchOps[0] = EQ; 
     157    searchVals[0] = parameters[0].value;  
     158 
     159    /* Execute CBR search and put output into returnValues */ 
     160    cbr_search(myCBR, searchNames, searchOps, searchVals, 
     161            1, returnValues); 
     162 
    146163    /* Calculate Utility */ 
    147     float newUtilityValue = 0; 
    148  
    149     for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
    150         newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); 
    151     } 
     164    float oldUtilityValue = returnValues[UTILITY]; 
     165     
     166    // Set DSA utility to take into account both the previously sensed 
     167    //  energy and the average communication time. 
     168 
     169     
     170 
     171    float newUtilityValue = oldUtilityValue + ( -observables[ENERGY].value + observables[COMMUNICATION_TIME].value); 
     172 
     173    // If communication time value is set, we know we need to change channels because of PU. 
     174    // So we should lower the utility for this channel. 
     175 
     176    if(observables[COMMUNICATION_TIME].value != 0) {  
     177        newUtilityValue = newUtilityValue - 100; 
     178    } else { 
     179        newUtilityValue = newUtilityValue + 20; 
     180    } 
     181 
     182 
     183    if(newUtilityValue <= 0) 
     184        newUtilityValue = 0; 
     185 
    152186    obsVals[obsValueIndex] = newUtilityValue; 
    153187 
     
    155189    for(size_t i = 0; i < radioInfo->numParameters; i++) { 
    156190        valList[returnValueIndex] = parameters[i].value; 
    157         returnValueIndex++; 
    158     } 
    159     for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
    160         valList[returnValueIndex] = uList[i].target; 
    161191        returnValueIndex++; 
    162192    } 
     
    641671    /* Put together the CBR search array */ 
    642672 
     673    uint32_t channel = 0; 
    643674    char *searchNames[1]; 
     675    char *sumSearchName; 
    644676    float searchVals[1]; 
     677    float utilArray[(int)pList[0].max]; 
    645678    int searchOps[1]; 
    646679    uint32_t numberColumns = radioInfo->numUtilities + 
     
    648681                                radioInfo->numObservables + 1; 
    649682 
    650     /* Make sure we do not return any entries for the current channel */ 
    651     searchNames[0] = "channel"; 
    652     searchOps[0] = NE; 
    653     searchVals[0] = currentParameters[0].value;  
    654   
    655683    float returnValues[numberColumns]; 
    656      
    657     /* Execute CBR search and put output into returnValues */ 
    658     uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     684    float sumRetVals[numberColumns]; 
     685    
     686    // Total sum of utilities in sumRetVals[0] 
     687 
     688    std::string channel_name = "channel"; 
     689    std::string utility_name = "utility"; 
     690 
     691    for( int i = 0 ; i < pList[0].max ; i++ ) { 
     692 
     693        searchNames[0] = (char*) pList[0].name.c_str(); 
     694        searchOps[0] = EQ; 
     695        searchVals[0] = i+1;  
     696     
     697        uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
    659698            1, returnValues); 
    660699 
    661     if(rc == 0){ 
    662         /* Similiar environment was found. Adapt the returned parameters to  
    663            meet the objective */ 
    664  
    665         LOG("Cognitive Engine:: Previous similar environment found\n"); 
    666  
    667         /* generate random channel */ 
    668         returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min;  
     700        if(rc == 31337) { 
     701                // No entry - must add 
     702                 
     703                char *rowNames[numberColumns]; 
     704                size_t rowIndex = 0; 
     705                for(size_t j = 0; j < radioInfo->numUtilities; j++) { 
     706                        rowNames[rowIndex] = (char*)uList[j].name.c_str(); 
     707                        rowIndex++; 
     708                } 
     709                for(size_t j = 0; j < radioInfo->numParameters; j++) { 
     710                        rowNames[rowIndex] = (char*)pList[j].name.c_str(); 
     711                        if(pList[j].name == "channel") 
     712                                returnValues[rowIndex] = i+1; 
     713                        rowIndex++; 
     714                } 
     715                for(size_t j = 0; j < radioInfo->numObservables; j++) { 
     716                        rowNames[rowIndex] = (char*)oList[j].name.c_str(); 
     717                        rowIndex++; 
     718                } 
     719    
     720                rowNames[rowIndex] = (char *) utility_name.c_str(); 
     721                returnValues[rowIndex] = 500; 
     722 
     723                /* Add the new optimized set to the CBR database */ 
     724 
     725                cbr_add_row(myCBR, rowNames, returnValues, numberColumns); 
     726        } 
     727 
     728        utilArray[i] = returnValues[UTILITY];  
     729    } 
     730 
     731        printf("1: %f\t2: %f\t3: %f\t4: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]);     
     732    // Get sum of all the channel utilities. 
     733    sumSearchName = (char *) utility_name.c_str(); 
     734    uint32_t rc = cbr_search_sum(myCBR, sumSearchName, sumRetVals); 
     735 
     736    // Psuedo random channel selection based upon utility. 
     737    int k = rand() % (int)sumRetVals[0]; 
     738    int cdf_total(0); 
     739 
     740    for ( int i = 0; i < pList[0].max; i++ ) { 
     741        cdf_total += utilArray[i]; 
     742        if(k < cdf_total) { 
     743                channel = i + 1; 
     744                break; 
     745        } 
     746    } 
    669747         
    670         if(returnValues[numberColumns-1] < 0) { 
    671          
    672         } else { 
    673         
    674         } 
    675     } else if(rc == 31337) { 
    676         /* No previous similar solution was found, use default. */ 
    677         LOG("Cognitive Engine:: No previous similar environment found.\n"); 
    678       
    679         /* generate random channel */ 
    680         returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min;  
    681     } else { 
    682         LOG("Cognitive Engine:: Search return an invalid value.\n"); 
    683     } 
    684  
    685  
     748    searchNames[0] = (char*) pList[0].name.c_str(); 
     749    searchOps[0] = EQ; 
     750    searchVals[0] = channel;  
     751   
     752    rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     753        1, returnValues); 
     754 
     755 
     756    //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min;  
     757    returnValues[CHANNEL] = channel; 
     758  
    686759    /* Package up the new set of parameters in order to add 
    687760       the new entry into the CBR database.  */ 
     
    700773        returnValueIndex++; 
    701774    } 
    702     returnValues[returnValueIndex] = 0; 
     775    //returnValues[returnValueIndex] = 0; 
    703776 
    704777    char *allNames[numberColumns]; 
     
    715788    for(size_t i = 0; i < radioInfo->numObservables; i++) { 
    716789        allNames[allNameIndex] = (char*)oList[i].name.c_str(); 
    717         returnValues[allNameIndex] = 0; 
     790    //    returnValues[allNameIndex] = 0; 
    718791        allNameIndex++; 
    719792    } 
    720     allNames[allNameIndex] = "utility"; 
     793    
     794    allNames[allNameIndex] = (char *) utility_name.c_str(); 
    721795 
    722796    /* Add the new optimized set to the CBR database */ 
    723797    cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 
     798 
    724799 
    725800    /* Return the set of new parameter values.  */ 
     
    764839        columnIndex++; 
    765840    }    
     841 
     842    char *paramCols[radioInfo->numParameters]; 
     843    size_t paramColumnIndex = 0; 
     844    // Also need to make parameters the unique key  
    766845    for (size_t i = 0; i < radioInfo->numParameters; i++){ 
    767846        cols[columnIndex] = (char*)pList[i].name.c_str(); 
     847        paramCols[paramColumnIndex] = (char*)pList[i].name.c_str(); 
    768848        columnIndex++; 
    769     }    
     849        paramColumnIndex++; 
     850    }   
     851 
    770852    for (size_t i = 0; i < radioInfo->numObservables; i++){ 
    771853        cols[columnIndex] = (char*)oList[i].name.c_str(); 
    772854        columnIndex++; 
    773855    }    
    774     cols[columnIndex] = "utility"; 
    775  
    776     myCBR = cbr_create(filename, tablename, cols, numberColumns); 
    777 } 
    778  
     856    
     857    std::string utility_name = "utility";  
     858    cols[columnIndex] = (char *)utility_name.c_str(); 
     859 
     860    myCBR = cbr_create_with_primary(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); 
     861} 
     862