Show
Ignore:
Timestamp:
11/26/08 19:35:17 (16 years ago)
Author:
ahe
Message:

receive experience from cr, have bugs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c

    r83 r88  
    291291 
    292292 
     293int GetExperienceSize(int sockfd, int *num_rows, int *num_cols) 
     294{ 
     295    char buffer[256]; 
     296     
     297    // number of rows 
     298    bzero(buffer,256); 
     299    ReadMessage(sockfd,buffer); 
     300    *num_rows = atoi(buffer); 
     301    // number of columns 
     302    bzero(buffer,256); 
     303    ReadMessage(sockfd,buffer); 
     304    *num_cols = atoi(buffer); 
     305    /*printf("number of rows: %d, number of columns: %d\n",  
     306            *num_rows, *num_cols); */ 
     307    return 1; 
     308} 
     309 
     310 
     311int GetExperience(int sockfd, int num_rows, int num_cols,  
     312        float **past_exp) 
     313{ 
     314    char buffer[256]; 
     315    int i, j; 
     316 
     317    // read experience 
     318    for (i = 0; i < num_rows; i++){ 
     319        for (j = 0; j < num_cols; j++){ 
     320            bzero(buffer,256); 
     321            ReadMessage(sockfd,buffer); 
     322    //    printf("experience: %s\n", buffer); 
     323        past_exp[i][j] = atof(buffer); 
     324        } 
     325    } 
     326 
     327    return 1; 
     328} 
     329 
     330 
    293331void print_current_config(struct Utility uList[],  
    294332        struct Parameter pList[],  
     
    342380int RunCBREngine(struct Utility uList[], struct Parameter pList[], 
    343381        struct Observable oList[], struct CE_Info *ce_info,  
    344         int num_cols) 
    345 { 
    346      
    347     int i; 
     382        int num_cols, int num_rows, float ** past_exp) 
     383{ 
     384     
     385    int i, j; 
    348386 
    349387    // get column names 
    350388    //char *cols[50]; 
     389    //char cols[num_cols][50]; 
    351390    char **cols; 
    352391    cols = (char **)malloc(sizeof(char)*num_cols); 
    353     int j = 0; 
     392    j = 0; 
    354393    for (i = 0; i < ce_info->numUtilities; i++){ 
    355394        cols[j] = malloc(strlen(uList[i].name)+1); 
    356395        cols[j] = uList[i].name; 
     396    //    strcpy(cols[j], uList[i].name); 
    357397        j++; 
    358398    } 
     
    360400        cols[j] = malloc(strlen(pList[i].name)+1); 
    361401        cols[j] = pList[i].name; 
     402    //    strcpy(cols[j], pList[i].name); 
    362403        j++; 
    363404    } 
     
    365406        cols[j] = malloc(strlen(oList[i].name)+1); 
    366407        cols[j] = oList[i].name; 
     408    //    strcpy(cols[j], oList[i].name); 
    367409        j++; 
    368410    } 
     411    printf("number of columns: %d, %d\n", num_cols, j); 
    369412    cols[j] = malloc(strlen("utility")+1); 
    370413    cols[j] = "utility"; 
    371      
    372     // sample table entry 
    373     float vals[num_cols]; 
    374     vals[0] = 10e3f;    // throughput 
    375     vals[1] = 1;        // spectral_efficiency 
    376     vals[2] = 1e-3f;    // log10_ber 
    377     vals[3] = 1;        // mod_scheme 
    378     vals[4] = -3.0f;    // tx_power 
    379     vals[5] = 10.0f;   // SNR 
    380     vals[6] = 0.762;    // utility 
     414    //strcpy(cols[j], "utility"); 
     415    printf("here\n"); 
    381416     
    382417    printf("number of columns: %d\n", num_cols); 
     
    396431     
    397432    // add row here 
    398     //printf("add row to cbr table\n"); 
    399     rc = cbr_add_row(mycbr, cols, vals, num_cols); 
     433    float vals[num_cols]; 
     434    /*// sample table entry 
     435    vals[0] = 1e3f;    // throughput 
     436    vals[1] = 1;        // spectral_efficiency 
     437    vals[2] = -3.50;    // log10_ber 
     438    vals[3] = 1;        // mod_scheme 
     439    vals[4] = -3.5f;    // tx_power 
     440    vals[5] = 10.0f;   // SNR 
     441    vals[6] = 0.762;    // utility*/ 
     442    printf("here\n"); 
     443    for (i = 0; i < num_rows; i++){ 
     444        for (j = 0; j < num_cols; j++){ 
     445            vals[j] = past_exp[i][j]; 
     446        } 
     447        printf("add row to cbr table\n"); 
     448        rc = cbr_add_row(mycbr, cols, vals, num_cols); 
     449    } 
    400450     
    401451    // print 
     
    423473    float retvals[num_cols]; 
    424474    rc = cbr_search(mycbr, search_names, search_ops,  
    425             search_vals, 2, retvals); 
     475            search_vals, ce_info->numUtilities, retvals); 
    426476 
    427477    j = 0; 
     
    492542    print_current_config(uList, pList, oList, &ce_info);  
    493543 
     544    // get experience size rom server 
     545    int num_rows, num_cols; 
     546    GetExperienceSize(sockfd, &num_rows, &num_cols); 
     547    //printf("number of rows: %d, number of columns: %d\n",  
     548    //        num_rows, num_cols);  
     549    // get experience 
     550    float **past_exp; 
     551    int i, j; 
     552    past_exp = (float **)malloc(sizeof(float)*num_rows); 
     553    for (i = 0; i< 1; i++){ 
     554        past_exp[i] = (float*)malloc(sizeof(float)*num_cols); 
     555    } 
     556    GetExperience(sockfd, num_rows, num_cols, past_exp); 
     557    for (i = 0; i < num_rows; i++){ 
     558        for (j = 0; j < num_cols; j++){ 
     559        printf("experience: %f\n", past_exp[i][j]); 
     560        } 
     561    } 
     562    
     563    // calculate utility 
     564     
     565 
    494566    // cbr operation 
    495     unsigned int num_cols; 
     567    //unsigned int num_cols; 
    496568    // get number of columns 
    497569    num_cols = ce_info.numUtilities + ce_info.numParameters; 
    498570    num_cols = num_cols + ce_info.numObservables; 
    499     num_cols = num_cols + 1; 
    500     RunCBREngine(uList, pList, oList, &ce_info, num_cols);     
     571    num_cols = num_cols + 1;    // overall utility 
     572    printf("number of rows: %d, number of columns: %d\n", 
     573            num_rows, num_cols); 
     574    RunCBREngine(uList, pList, oList, &ce_info, num_cols, num_rows,  
     575            past_exp);     
    501576    // print out results 
    502     int i; 
     577    //int i; 
    503578    printf("search result: "); 
    504579    for (i=0;i<ce_info.numUtilities; i++) 
     
    513588    SendCEResults(sockfd, uList, pList, oList, &ce_info); 
    514589 
    515 /*    char counter[50]; 
    516     char var[50]; 
    517     // utility 
    518     for (i = 0; i < ce_info.numUtilities; i++){ 
    519         sprintf(var, "%f", uList[i].value); 
    520         SendMessage(sockfd, var); 
    521     } 
    522     // parameter 
    523     for (i = 0; i < ce_info.numParameters; i++){ 
    524         sprintf(var, "%f", pList[i].value); 
    525         SendMessage(sockfd, var); 
    526     } 
    527     // observable 
    528     for (i = 0; i < ce_info.numObservables; i++){ 
    529         sprintf(var, "%f", oList[i].value); 
    530         SendMessage(sockfd, var); 
    531     }*/ 
    532  
    533590 
    534591  return 0;