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.

Files:
1 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