Changeset 391

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

Fleshed out DSA reference implementation

Location:
vtcross/trunk/src/cognitive_engines/DSA_CE
Files:
3 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 
  • vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c

    r390 r391  
    5757    char *zErrMsg = 0; 
    5858 
    59     printf("command: %s\n", _cbr->command); 
     59    //printf("command: %s\n", _cbr->command); 
    6060    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 
    6161    if( rc!=SQLITE_OK){ 
     
    9393} 
    9494 
     95// create database 
     96cbr cbr_create_with_primary(char * _filename, char * _tablename,  
     97        char * _cols[], char * _primcols[], unsigned int _len, 
     98        unsigned int _primlen) 
     99{ 
     100    // cbr is a pointer to struct cbr_s 
     101    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); 
     102 
     103    // create database 
     104 
     105    // copy filename 
     106    unsigned int i=0; 
     107    strcpy(_cbr->filename, _filename); 
     108 
     109    // execute create database command 
     110    // database handle 
     111    //_cbr->db = NULL; 
     112    OpenDatabase(_cbr); 
     113 
     114    // create table 
     115 
     116    // copy tablename 
     117    strcpy(_cbr->tablename, _tablename); 
     118 
     119    // number of columns in the table 
     120    _cbr->num_columns = _len; 
     121 
     122    // generate command 
     123    strcpy(_cbr->command, "CREATE TABLE "); 
     124    strcat(_cbr->command, _cbr->tablename); 
     125    strcat(_cbr->command, "("); 
     126    for (i=0; i<_cbr->num_columns; i++) { 
     127        strcat(_cbr->command, _cols[i]); 
     128        strcat(_cbr->command, " FLOAT"); 
     129        strcat(_cbr->command, ", "); 
     130    } 
     131   strcat(_cbr->command, "PRIMARY KEY ("); 
     132    for (i=0; i<_primlen; i++) { 
     133        strcat(_cbr->command, _primcols[i]); 
     134        if (i != _primlen-1) // not last entry 
     135            strcat(_cbr->command, ", "); 
     136    } 
     137    strcat(_cbr->command, "));"); 
     138 
     139    // execute create table command 
     140    ExecuteCommand(_cbr); 
     141 
     142    return _cbr; 
     143} 
    95144 
    96145// create database 
     
    181230const char * ops_str[] = { 
    182231    "==", "!=", ">", ">=", "<", "<="}; 
     232 
     233 
     234// cbr search for a sum 
     235int cbr_search_sum( 
     236    cbr _cbr, 
     237    char *_name, 
     238    float *_retvals) 
     239{    
     240    int rc; 
     241    // generate command 
     242    strcpy(_cbr->command, "select SUM( "); 
     243    strcat(_cbr->command, _cbr->tablename); 
     244    strcat(_cbr->command, "."); 
     245    strcat(_cbr->command, _name); 
     246    strcat(_cbr->command, ") from "); 
     247    strcat(_cbr->command, _cbr->tablename); 
     248    strcat(_cbr->command, ";"); 
     249 
     250    //printf("search command: %s\n", _cbr->command); 
     251 
     252    rc = ExecuteSearchCommand(_cbr, _retvals); 
     253    /*printf("search result: "); 
     254    for (int i=0; i<_cbr->num_columns; i++) 
     255        printf("%f, ",_retvals[i]); 
     256    printf("\n"); 
     257    */ 
     258    return rc; 
     259} 
     260 
     261 
     262// cbr search 
     263int cbr_search_rand( 
     264    cbr _cbr, 
     265    char *_names[], 
     266    int * _ops, 
     267    float *_vals, 
     268    unsigned int _n, 
     269    float *_retvals) 
     270{    
     271    int rc; 
     272    // generate command 
     273    strcpy(_cbr->command, "select "); 
     274    strcat(_cbr->command, _cbr->tablename); 
     275    strcat(_cbr->command, ".* from "); 
     276    strcat(_cbr->command, _cbr->tablename); 
     277    strcat(_cbr->command, " where "); 
     278 
     279    unsigned int i; 
     280    char str_buffer[64]; 
     281    for (i=0; i<_n; i++) { 
     282        // ensure valid ops value 
     283        if (_ops[i] < 0 || _ops[i] > 5) { 
     284            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); 
     285            exit(1); 
     286        } 
     287 
     288        strcat(_cbr->command, _names[i]); 
     289        strcat(_cbr->command, ops_str[_ops[i]]); 
     290        sprintf(str_buffer, "%E", _vals[i]); 
     291        strcat(_cbr->command, str_buffer); 
     292 
     293        if (i<_n-1) 
     294            strcat(_cbr->command, " AND "); 
     295        else 
     296            strcat(_cbr->command, " order by RAND();"); 
     297    } 
     298 
     299    //printf("search command: %s\n", _cbr->command); 
     300 
     301    rc = ExecuteSearchCommand(_cbr, _retvals); 
     302     
     303    /*printf("search result: "); 
     304    for (i=0; i<_cbr->num_columns; i++) 
     305        printf("%f, ",_retvals[i]); 
     306    printf("\n"); 
     307*/ 
     308    return rc; 
     309} 
    183310 
    184311 
     
    220347    } 
    221348 
    222     printf("search command: %s\n", _cbr->command); 
     349    //printf("search command: %s\n", _cbr->command); 
    223350 
    224351    rc = ExecuteSearchCommand(_cbr, _retvals); 
     
    279406    // generate command 
    280407    //printf("%s\n", _cbr->command); 
    281     strcpy(_cbr->command, "insert into "); 
     408    strcpy(_cbr->command, "replace into "); 
    282409    strcat(_cbr->command, _cbr->tablename); 
    283410     
  • vtcross/trunk/src/cognitive_engines/DSA_CE/examples/gnuradio-examples/dsa.py

    r390 r391  
    3939def main(): 
    4040 
    41         global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
     41        global stats_array, count_array, time_array, n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
    4242        n_rcvd = 0 
    4343        n_right = 0 
    4444        n_attempts = 5 
    4545        return_flag = 0 
     46 
     47        count_array = [ 0, 0, 0, 0, 0] 
     48        time_array = [ 0, 0, 0, 0, 0] 
     49        stats_array = [ 0, 0, 0, 0, 0] 
     50 
    4651 
    4752        def send_pkt(self, payload='', eof=False): 
     
    6368                return real_channel 
    6469 
     70        def get_average_time(channel,absent_time): 
     71 
     72                global count_array, time_array 
     73 
     74                count_array[channel] = count_array[channel] + 1 
     75                average_time = (time_array[channel] + absent_time) / count_array[channel]        
     76         
     77                return average_time 
     78 
    6579        def get_freq(hop_freq,probe_level,absent_time): 
    6680 
     
    7690                        channel = 4 
    7791 
    78                 p = Parameter(1) 
    7992                currentParameters = Parameter(1) 
    80                 o = Observable(1) 
    81  
     93                currentParameters[0].name = "channel" 
     94                currentParameters[0].value = channel 
     95 
     96                o = Observable(2) 
    8297                o[0].value = probe_level 
    8398                o[0].name = "energy" 
    84  
    85                 currentParameters[0].name = "channel" 
    86                 currentParameters[0].value = channel; 
    87  
    88                 p = GetOptimalParameters(o,1,currentParameters,1); 
    89                 print p[0].value 
     99                 
     100                o[1].value = absent_time  
     101                o[1].name = "communication_time" 
     102                 
     103                # If time == 0 then we are scanning and we dont want to  
     104                #  use this time in the averaging process. 
     105 
     106                if absent_time != 0: 
     107                    UpdateParameterPerformance(currentParameters,1,o,1) 
     108 
     109                else: 
     110                    # Get the average communication time 
     111                    average_time = get_average_time(channel, absent_time) 
     112                    o[1].value = average_time  
     113                    o[1].name = "communication_time" 
     114                    UpdateParameterPerformance(currentParameters,1,o,2) 
     115 
     116                p = Parameter(1) 
     117                p = GetOptimalParameters(o,2,currentParameters,1); 
    90118                 
    91119                channel = get_real_channel(int(p[0].value)) 
     
    96124                        hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets       
    97125 
     126                stats_array[int(p[0].value)] = stats_array[int(p[0].value)] + 1 
     127                print "Stats:: 1..",stats_array[1]," 2..",stats_array[2]," 3..",stats_array[3]," 4..",stats_array[4]  
    98128                return channel,hop_freq #returning the channel number and hop frequency 
    99129         
     
    154184 
    155185        parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 
    156         expert_grp_tx = parser_tx.add_option_group("Expert_tx") 
    157  
     186        parser_tx.add_option("", "--vtcross", action="store_true", default=False, 
     187                                help="Use the CROSS engine for DSA decisions: default is random.") 
     188   
    158189        parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 
    159190                        default='gmsk', 
     
    166197                        help="set megabytes to transmit [default=%default]") 
    167198        parser_tx.add_option("","--discontinuous", action="store_true", default=False, 
    168                         help="enable discontinous transmission (bursts of 5 packets)") 
     199                        help="enable discontinous transmission (bursts of 5 packets)")  
    169200        parser_tx.add_option("","--from-file", default=None, 
    170201                        help="use file for packet contents") 
    171          
     202  
     203        expert_grp_tx = parser_tx.add_option_group("Expert_tx") 
     204 
    172205        transmit_path.add_options(parser_tx, expert_grp_tx) 
    173206 
    174207        for mod in mods.values(): 
    175208                mod.add_options(expert_grp_tx) 
     209 
    176210 
    177211        (options_tx, args_tx) = parser_tx.parse_args () 
     
    188222        options_tx.fusb_nblocks = 16 
    189223        options_tx.bitrate = 0.0125e6 
    190         ############# 
     224        ############# 
    191225 
    192226        if options_tx.tx_freq is None: 
     
    195229                sys.exit(1) 
    196230 
    197         if options_tx.from_file is not None: 
    198                 source_file = open(options_tx.from_file, 'r') 
     231        #if options_tx.from_file is not None: 
     232        #       source_file = open(options_tx.from_file, 'r') 
    199233             
    200234        parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
    201235        expert_grp_rx = parser_rx.add_option_group("Expert_rx") 
    202         parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
    203                         default='gmsk', 
    204                         help="Select modulation from: %s [default=%%default]" 
    205                                 % (', '.join(demods.keys()),)) 
     236        #parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
     237        #               default='gmsk', 
     238        #               help="Select modulation from: %s [default=%%default]" 
     239        #                       % (', '.join(demods.keys()),)) 
    206240         
    207241        receive_path.add_options(parser_rx, expert_grp_rx) 
    208242 
    209         for mod in demods.values(): 
    210                 mod.add_options(expert_grp_rx) 
     243        #for mod in demods.values(): 
     244        #       mod.add_options(expert_grp_rx) 
    211245 
    212246        (options_rx, args_rx) = parser_rx.parse_args () 
    213247 
    214         if len(args_rx) != 0: 
    215                 parser_rx.print_help(sys.stderr) 
    216                 sys.exit(1) 
     248        #if len(args_rx) != 0: 
     249        #       parser_rx.print_help(sys.stderr) 
     250        #       sys.exit(1) 
    217251        ############# Setting some default values for rx side of the block 
    218252        options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
     
    224258        ############# 
    225259 
    226         if options_rx.rx_freq is None: 
    227                 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
    228                 parser_rx.print_help(sys.stderr) 
    229                 sys.exit(1) 
     260        #if options_rx.rx_freq is None: 
     261        #       sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
     262        #       parser_rx.print_help(sys.stderr) 
     263        #       sys.exit(1) 
    230264         
    231265        # build the graph 
     
    248282 
    249283        # Scan all channels first for inital data 
     284        time.sleep(0.1) 
    250285 
    251286        while running: