Show
Ignore:
Timestamp:
07/28/09 23:23:14 (15 years ago)
Author:
trnewman
Message:

Added DSA CBR reference implementation.
Added simple python example application for DSA CBR.
Added GNUradio python application that uses CROSS and the DSA CBR.

Fixed several bugs in the socket interface.

Files:
1 modified

Legend:

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

    r375 r389  
    2727#include "sqlite3.h" 
    2828#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 
    2936 
    3037 
     
    632639} 
    633640 
     641 
     642/* The core of the CE is this function */ 
     643 
    634644Parameter*  
    635645CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) 
     
    637647    LOG("Cognitive Engine:: Generating solution.\n"); 
    638648 
    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; 
    640657 
    641658    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         
    667661        if(strcmp(uList[i].goal.c_str(), "max") == 0) { 
    668662            searchOps[i] = GT; 
     
    670664            searchOps[i] = LT; 
    671665        } 
    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 */ 
    675681    uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
    676682            radioInfo->numUtilities, returnValues); 
    677683 
    678684    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 
    683690        if(returnValues[numberColumns-1] < 0) { 
     691         
    684692            returnValues[2] = returnValues[2] - 15; 
    685693            returnValues[3] = returnValues[3] - 2; 
     694         
    686695        } else { 
     696        
    687697            returnValues[2] = returnValues[2] + 15; 
    688698            returnValues[3] = returnValues[3] + 2; 
     699         
    689700        } 
    690701    } 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  
    697716    } else { 
    698717        LOG("Cognitive Engine:: Search return an invalid value.\n"); 
    699718    } 
     719 
     720 
     721    /* Package up the new set of parameters in order to add 
     722       the new entry into the CBR database.  */ 
    700723 
    701724    size_t returnValueIndex = 0; 
     
    732755    allNames[allNameIndex] = "utility"; 
    733756 
    734     // Add row to CBR.  
     757    /* Add the new optimized set to the CBR database */ 
    735758    cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 
    736759 
     760    /* Return the set of new parameter values.  */ 
    737761    return pList; 
    738762}