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/cognitive_engines
Files:
2 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