Changeset 228

Show
Ignore:
Timestamp:
04/02/09 17:35:03 (15 years ago)
Author:
trnewman
Message:

Added sending current parameters in the libvt request optimization function.

Added guts to the CBR so it actually creates an sql db and searches it.

Location:
vtcross/trunk/src
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp

    r224 r228  
    1919#include "vtcross/error.h" 
    2020#include "vtcross/socketcomm.h" 
    21  
     21#include "vtcross/cbr.h" 
     22 
     23#include "cbr.c" 
     24 
     25#include <sqlite3.h> 
     26#include <sqlite3ext.h> 
     27 
     28 
     29 
     30static cbr myCBR; 
    2231 
    2332CognitiveEngine::CognitiveEngine() 
     
    3140CognitiveEngine::~CognitiveEngine() 
    3241{ 
     42        cbr_free(myCBR); 
    3343    delete [] pList; 
    3444    delete [] oList; 
     
    8292    } 
    8393} 
    84  
    8594 
    8695void  
     
    101110        // MUCH faster since instead of donig string compares we can simply 
    102111        // switch on the integer value... 
    103         if(strcmp(buffer, "get_solution") == 0) { 
     112        if(strcmp(buffer, "request_optimization") == 0) { 
    104113             
    105114            /* Receive Set of Observables */ 
     
    122131            }   
    123132 
     133            /* Receive Set of current Parameters */ 
     134                        LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); 
     135 
     136            memset(buffer, 0, 256); 
     137            ReadMessage(commandSocketFD,buffer); 
     138            uint32_t numCurrentParameters = atoi(buffer); 
     139    
     140            Parameter *cp = new Parameter[numCurrentParameters]; 
     141  
     142            for(size_t i = 0; i < numCurrentParameters; i++) { 
     143                memset(buffer, 0, 256); 
     144                ReadMessage(commandSocketFD, buffer); 
     145                cp[i].name = std::string(buffer); 
     146     
     147                memset(buffer, 0, 256); 
     148                ReadMessage(commandSocketFD, buffer); 
     149                cp[i].value = atof(buffer); 
     150            }   
    124151                        LOG("Cognitive Engine:: Processing parameters....\n"); 
     152 
     153            Parameter *solutionSet; 
     154                         
     155                        solutionSet = GetSolution(o,cp); 
    125156 
    126157            // TODO need to actually do something with the observables here 
    127158             
    128159                        LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); 
    129  
    130             SendMessage(commandSocketFD,"1"); 
    131             SendMessage(commandSocketFD,"txPower"); 
    132             SendMessage(commandSocketFD,"10"); 
     160                        char numParametersChar[10]; 
     161                        char solutionValue[50]; 
     162                        sprintf(numParametersChar,"%i",radioInfo->numParameters); 
     163                        SendMessage(commandSocketFD,numParametersChar); 
     164            for(size_t i = 0; i < radioInfo->numParameters; i++) { 
     165                SendMessage(commandSocketFD,solutionSet[i].name.c_str()); 
     166                memset(solutionValue, 0, 50); 
     167                            sprintf(solutionValue,"%f",solutionSet[i].value); 
     168                SendMessage(commandSocketFD,solutionValue); 
     169                        } 
    133170 
    134171            delete [] o; 
     172            delete [] cp; 
    135173        } 
    136174        else if(strcmp(buffer, "query_component_type") == 0) { 
     
    295333        ReadMessage(commandSocketFD,buffer); 
    296334        uList[i].name = std::string(buffer); 
    297      
     335    
    298336        memset(buffer, 0, 256); 
    299337        ReadMessage(commandSocketFD,buffer); 
     
    386424 
    387425    SendMessage(commandSocketFD, "receive_config_ack"); 
     426 
     427        BuildCognitiveEngine(); 
    388428} 
    389429 
     
    405445} 
    406446 
    407 void  
    408 CognitiveEngine::GetSolution(Observable *observables) 
     447Parameter*  
     448CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) 
    409449{ 
    410450    LOG("Cognitive Engine:: Generating solution.\n"); 
    411 } 
    412  
    413 void  
    414 CognitiveEngine::GetSolution(Observable *observables, std::string service) 
     451 
     452    char *searchNames[radioInfo->numUtilities]; 
     453 
     454    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
     455        searchNames[i] = (char*)uList[i].name.c_str(); 
     456        } 
     457 
     458    float searchVals[radioInfo->numUtilities]; 
     459 
     460        for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
     461                searchVals[i] = uList[i].target; 
     462    } 
     463 
     464    uint32_t numberColumns =  
     465                radioInfo->numUtilities + 
     466                radioInfo->numParameters + 
     467                radioInfo->numObservables + 1; 
     468         
     469        float returnValues[numberColumns]; 
     470         
     471        int searchOps[radioInfo->numUtilities]; 
     472    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
     473 
     474        /* If the goal is to maximum, set the search operation to 
     475                 * return values greater than the target. 
     476                 * 
     477                 * If the goal is to minimize, set the search operation to 
     478                 * return values less than the target. 
     479                 */ 
     480 
     481                if(strcmp(uList[i].goal.c_str(),"max") == 0) { 
     482                    searchOps[i] = GT; 
     483                } else if(strcmp(uList[i].goal.c_str(),"min") == 0) { 
     484                        searchOps[i] = LT; 
     485                } 
     486        } 
     487 
     488        /* CBR specific call */ 
     489        uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     490                        radioInfo->numUtilities, returnValues); 
     491 
     492        if(rc == 0){ 
     493        /* Adapt the returned parameters to meet the objective */ 
     494 
     495    } else if(rc == 31337){ 
     496                /* No rows in the CBR, pick default parameters */ 
     497                // Currently this is hard coded and implementation specific! 
     498            //returnValues[2] = currentParameters[0].value + 5; 
     499            //returnValues[3] = currentParameters[1].value + 10; 
     500         
     501        } else { 
     502        WARNING("Cognitive Engine:: Search return an invalid value.\n"); 
     503        } 
     504 
     505        size_t returnValueIndex = 0; 
     506    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
     507                uList[i].value = returnValues[returnValueIndex]; 
     508                returnValueIndex++; 
     509        } 
     510    for(size_t i = 0; i < radioInfo->numParameters; i++) { 
     511                pList[i].value = returnValues[returnValueIndex]; 
     512                returnValueIndex++; 
     513        } 
     514    for(size_t i = 0; i < radioInfo->numObservables; i++) { 
     515                oList[i].value = returnValues[returnValueIndex]; 
     516                returnValueIndex++; 
     517        } 
     518 
     519        // Add row to CBR.  
     520 
     521        return pList; 
     522 
     523} 
     524 
     525 
     526Parameter*  
     527CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters, std::string service) 
    415528{ 
    416529    LOG("Cognitive Engine:: Generating solution for %s service.\n",service.c_str()); 
    417 } 
     530 
     531    return pList; 
     532} 
     533 
    418534 
    419535void  
     
    424540} 
    425541 
     542 
    426543void  
    427544CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ 
     
    430547    LOG("Cognitive Engine:: Receiving feedback.\n"); 
    431548} 
     549 
     550 
     551void 
     552CognitiveEngine::BuildCognitiveEngine() 
     553{ 
     554        char filename[] = {"ex1"}; 
     555        char tablename[] = {"data"}; 
     556 
     557    uint32_t numberColumns =  
     558                radioInfo->numUtilities + 
     559                radioInfo->numParameters + 
     560                radioInfo->numObservables + 1; 
     561 
     562    char *cols[numberColumns]; 
     563 
     564    size_t columnIndex = 0; 
     565    for (size_t i = 0; i < radioInfo->numUtilities; i++){ 
     566                cols[columnIndex] = (char*)uList[i].name.c_str(); 
     567        columnIndex++; 
     568    }    
     569    for (size_t i = 0; i < radioInfo->numParameters; i++){ 
     570                cols[columnIndex] = (char*)pList[i].name.c_str(); 
     571        columnIndex++; 
     572    }    
     573    for (size_t i = 0; i < radioInfo->numObservables; i++){ 
     574                cols[columnIndex] = (char*)oList[i].name.c_str(); 
     575        columnIndex++; 
     576    }    
     577    cols[columnIndex] = "utility"; 
     578 
     579    myCBR = cbr_create(filename, tablename, cols, numberColumns); 
     580} 
     581 
  • vtcross/trunk/src/cognitive_engines/cbr.c

    r161 r228  
    6060    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 
    6161    if( rc!=SQLITE_OK){ 
    62         fprintf(stderr, "SQL error: %s\n", zErrMsg); 
     62        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command); 
    6363        sqlite3_free(zErrMsg); 
    6464    } else{ 
     
    7272int ExecuteSearchCommand(cbr _cbr, float *_retvals){ 
    7373    int rc; 
    74     //char *zErrMsg = 0; 
    7574    unsigned int i; 
    7675 
    77     //printf("command: %s\n", _cbr->command); 
    7876    sqlite3_stmt * pStatement; 
    7977    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL); 
    80     //if (rc == SQLITE_OK && sqlite3_step(pStatement) == SQLITE_ROW){ 
    8178    if (rc == SQLITE_OK){  
    8279        if (sqlite3_step(pStatement) == SQLITE_ROW){ 
    83         printf("there is search return data\n"); 
    84         for (i=0; i<_cbr->num_columns; ++i) 
    85             _retvals[i] = sqlite3_column_double(pStatement, i); 
     80            for (i=0; i<_cbr->num_columns; ++i) 
     81                _retvals[i] = sqlite3_column_double(pStatement, i); 
    8682        } else { 
    87             printf("no matched rearch results. use default values\n"); 
    88              
    89             _retvals[0] = 100;  // throughput 
    90             _retvals[1] = 1;    // spectral_efficiency 
    91             _retvals[2] = -2;   // log10_ber 
    92             _retvals[3] = 1;    // mod_scheme 
    93             _retvals[4] = -10;  // tx_power 
    94             _retvals[5] = 10;   // snr 
    95             _retvals[6] = 0.5;  // utility 
    96         } 
     83                    printf("CBR:: No matched results returning default.\n"); 
     84                        rc=31337; 
     85                } 
     86    } else { 
     87                printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command); 
    9788    } 
    9889 
    9990    sqlite3_finalize(pStatement); 
    100      
    101     /*rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg); 
    102     if( rc!=SQLITE_OK){ 
    103         fprintf(stderr, "SQL error: %s\n", zErrMsg); 
    104         sqlite3_free(zErrMsg); 
    105     } else{ 
    106         printf("command executed.\n"); 
    107     }*/ 
    108  
     91    
    10992    return rc; 
    11093} 
     
    185168    // execute print (select all)  command 
    186169    ExecuteCommand(_cbr); 
    187     //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); 
     170    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); 
    188171} 
    189172 
     
    209192    float *_retvals) 
    210193{    
     194    int rc; 
    211195    // generate command 
    212196    strcpy(_cbr->command, "select "); 
     
    231215        strcat(_cbr->command, str_buffer); 
    232216 
     217 
    233218        if (i<_n-1) 
    234219            strcat(_cbr->command, " AND "); 
     
    237222    } 
    238223 
    239     //printf("search command: %s\n", _cbr->command); 
     224    printf("search command: %s\n", _cbr->command); 
    240225 
    241226    //ExecuteCommand(_cbr); 
    242     ExecuteSearchCommand(_cbr, _retvals); 
     227    rc = ExecuteSearchCommand(_cbr, _retvals); 
    243228     
    244229    /*printf("search result: "); 
     
    247232    printf("\n");*/ 
    248233 
    249     return 0; 
     234    return rc; 
    250235} 
    251236 
  • vtcross/trunk/src/include/vtcross/components.h

    r222 r228  
    260260         * and active. 
    261261         */ 
    262         void GetSolution(Observable *observables); 
    263         void GetSolution(Observable *observables, std::string service); 
     262        Parameter *GetSolution(Observable *observables, Parameter *currentParameters); 
     263        Parameter *GetSolution(Observable *observables, Parameter *currentParameters, std::string service); 
    264264 
    265265        /* Receive a feedback from the radio regarding the performance of a 
     
    275275        void ReceiveFeedback(Observable *observables, \ 
    276276                Parameter *parameters, Utility *utilities, std::string service); 
     277 
     278 
     279                /* BuildCognitiveEngine performs the CE implementation specific work 
     280                 * that defines the internals of a CE.  For example, a CBR CE engine 
     281                 * would build the case-base reasoner or create the database, a neural 
     282                 * network based CE may perform the initial training, a GA based CE 
     283                 * may build the chromosome structure. 
     284                 */ 
     285                void BuildCognitiveEngine(); 
    277286 
    278287        /* The SML_present bool reflects whether or not the remote component 
  • vtcross/trunk/src/include/vtcross/libvtcross.h

    r222 r228  
    7676 * use this?  Shouldn't this be internal to the radio operation?  
    7777 */ 
    78 Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables); 
     78Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,  
     79                Parameter *currentParameters, uint32_t numCurrentParameters); 
    7980 
    8081 
     
    103104bool Shutdown(); 
    104105 
     106/* Return total number of currently recognized transmission parameters. 
     107 */ 
     108uint32_t GetNumParameters(); 
     109 
     110 
    105111#endif 
  • vtcross/trunk/src/libvtcross/libvtcross.cpp

    r227 r228  
    2727} 
    2828 
     29uint32_t 
     30GetNumParameters() 
     31{ 
     32        uint32_t socketfd, numParameters; 
     33    char buffer[256]; 
     34 
     35    socketfd = ConnectToRemoteComponent(); 
     36    SendMessage(socketfd,"get_number_parameters"); 
     37     
     38    memset(buffer, 0, 256); 
     39    ReadMessage(socketfd, buffer); 
     40    numParameters = atoi(buffer); 
     41         
     42        return numParameters;  
     43} 
    2944 
    3045/* Given a certain set of observables, ask the radio to find the optimum radio 
     
    3853 * memory when it is done using it. 
    3954 */ 
    40 Parameter*  
    41 GetOptimalParameters(Observable *radioObservables, uint32_t numObservables) 
    42 { 
     55Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, 
     56                Parameter *currentParameters, uint32_t numCurrentParameters) { 
     57    uint32_t i,socketfd,numParameters; 
    4358    char var[50]; 
    4459    char counter[55]; 
     
    6176    } 
    6277 
     78    // Send Observables 
     79    memset(counter, 0, 55); 
     80    sprintf(counter,"%i",numCurrentParameters); 
     81    SendMessage(socketfd,counter); 
     82    for(i = 0; i < numCurrentParameters; i++) { 
     83        SendMessage(socketfd,currentParameters[i].name.c_str()); 
     84        sprintf(var,"%f",currentParameters[i].value); 
     85        SendMessage(socketfd,var);       
     86    } 
    6387    /* Receive Set of Parameters */ 
    6488    memset(buffer, 0, 256); 
  • vtcross/trunk/src/libvtcross/libvtcross_demo.cpp

    r225 r228  
    2323main(int32_t argc, char *argv[]) 
    2424{ 
    25     Observable * o = new Observable[1]; 
     25    Observable *o = new Observable[1]; 
     26    Parameter *currentParameters = new Parameter[2]; 
    2627    Parameter * p; 
    2728 
    28     o[0].name = "Test123"; 
    29         o[0].value = 1243.00; 
     29        uint32_t numParameters; 
    3030 
    31     p = GetOptimalParameters(o,1); 
     31    o[0].name = "PER"; 
     32        o[0].value = 12.00; 
     33 
     34    currentParameters[0].name = "tx_power"; 
     35    currentParameters[0].value = 10.0; 
     36    currentParameters[1].name = "bandwidth"; 
     37    currentParameters[1].value = 300.0; 
     38 
     39    p = GetOptimalParameters(o,1,currentParameters,2); 
     40    numParameters = GetNumParameters(); 
    3241 
    3342        printf("Application:: Received the following parameters.\n"); 
    34         printf("%s:: %f\n",p[0].name.c_str(),p[0].value); 
     43         
     44        for(size_t i = 0; i < numParameters; i++) { 
     45                printf("%s:: %f\n",p[i].name.c_str(),p[i].value); 
     46        } 
    3547 
    36     delete [] p;  
     48        // Try them out! Do they work? 
     49     
     50    Utility *resultingUtility = new Utility[2]; 
     51         
     52        // UpdateParameterPerformance(p,o,resultingUtility);     
     53         
     54         
     55        delete [] p;  
    3756    delete [] o;  
    3857 
  • vtcross/trunk/src/shell/CognitiveRadioShell.cpp

    r227 r228  
    454454{ 
    455455    char buffer[256]; 
     456    uint32_t numObservables,numParameters,numCurrentParameters; 
    456457    char counter[55]; 
    457458    char var[50]; 
     
    476477    } 
    477478 
     479    /* Receive Set of Current Parameters */ 
     480    memset(buffer, 0, 256); 
     481    ReadMessage(commandSocketFD,buffer); 
     482    numCurrentParameters = atoi(buffer); 
     483   
     484        LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters); 
     485    Parameter * cp = new Parameter[numCurrentParameters]; 
     486  
     487    for (i = 0; i < numCurrentParameters; i++){ 
     488        memset(buffer, 0, 256); 
     489        ReadMessage(commandSocketFD,buffer); 
     490        cp[i].name = std::string(buffer); 
     491     
     492        memset(buffer, 0, 256); 
     493        ReadMessage(commandSocketFD,buffer); 
     494        cp[i].value = atof(buffer); 
     495    } 
    478496    /* Send to Cognitive Engine  
    479497         * TODO: With multiple CEs we need to make a decision about where 
     
    481499         */ 
    482500 
    483         LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n"); 
    484     SendMessage(ceSocketFD, "get_solution"); 
    485     sprintf(counter, "%i", numObservables); 
    486     SendMessage(ceSocketFD, counter); 
    487     for(size_t i = 0; i < numObservables; i++) { 
    488         SendMessage(ceSocketFD, o[i].name.c_str()); 
    489         sprintf(var, "%f", o[i].value); 
    490             SendMessage(ceSocketFD, var); 
    491     } 
    492  
     501        if(!SML_present) {  
     502            LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n"); 
     503        SendMessage(ceSocketFD,"request_optimization"); 
     504        sprintf(counter,"%i",numObservables); 
     505        SendMessage(ceSocketFD,counter); 
     506        for(i = 0; i < numObservables; i++) { 
     507            SendMessage(ceSocketFD,o[i].name.c_str()); 
     508            sprintf(var,"%f",o[i].value); 
     509                SendMessage(ceSocketFD,var); 
     510        } 
     511             
     512                LOG("Cognitive Radio Shell:: Passing on current parameters to Cognitive Engine\n"); 
     513        sprintf(counter,"%i",numCurrentParameters); 
     514        SendMessage(ceSocketFD,counter); 
     515        for(i = 0; i < numCurrentParameters; i++) { 
     516            SendMessage(ceSocketFD,cp[i].name.c_str()); 
     517            sprintf(var,"%f",cp[i].value); 
     518                SendMessage(ceSocketFD,var); 
     519        } 
     520        } 
    493521 
    494522        LOG("Cognitive Radio Shell:: Receiving optimized parameters from Cognitive Engine.\n"); 
     
    546574    } else if(strcmp(buffer, "deregister_sml") == 0) { 
    547575        DeregisterSML(socketFD); 
    548     } else if(strcmp(buffer, "request_optimization") == 0) { 
     576    } else if(strcmp(buffer,"get_number_parameters") == 0) { 
     577                char numParameters[20]; 
     578                sprintf(numParameters,"%i",radio_info->numParameters); 
     579        SendMessage(commandSocketFD,numParameters); 
     580    } else if(strcmp(buffer,"request_optimization") == 0) { 
    549581        /* Receive optimization request and current environment */ 
    550582        GetOptimalParameters(socketFD);   
     583    } else if(strcmp(buffer,"request_optimization_service") == 0) { 
     584        /* Receive optimization request and current environment */ 
     585        //GetOptimalParametersService(socketFD);   
    551586    } 
    552587}