Index: vtcross/branches/bhilburn/src/cognitive_engines/cbr.c
===================================================================
--- vtcross/branches/bhilburn/src/cognitive_engines/cbr.c	(revision 123)
+++ vtcross/branches/bhilburn/src/cognitive_engines/cbr.c	(revision 123)
@@ -0,0 +1,285 @@
+//
+// Case-based reasoner
+//
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "cbr.h"
+
+
+struct cbr_s {
+    char filename[64];
+    char tablename[64];
+    char command[2048];
+    sqlite3 *db; 
+    unsigned int num_columns;
+};
+
+
+// open a database or create a database if it does not exist
+int OpenDatabase(cbr _cbr){
+    int rc;
+    //sqlite3 **db;
+
+    //printf("database name: %s\n", _cbr->filename);
+    //rc = sqlite3_open(_cbr->filename, db);
+    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
+    if (rc) {
+        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
+        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
+        sqlite3_close(_cbr->db);
+        exit(1);
+    } else{
+        //printf("database opened.\n");
+    }
+    //_cbr->db = *db;
+    return rc;
+}
+
+
+// simple callback function, display result
+int callback(void *notUsed, int argc, char **argv, char **azColName){
+    int i;
+    for(i=0; i<argc; i++){
+        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    printf("\n");
+    return 0;
+}
+
+
+// execute command
+int ExecuteCommand(cbr _cbr){
+    int rc;
+    char *zErrMsg = 0;
+
+    //printf("command: %s\n", _cbr->command);
+    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
+    if( rc!=SQLITE_OK){
+        fprintf(stderr, "SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+    } else{
+        //printf("command executed.\n");
+    }
+    return rc;
+}
+
+
+// execute search command
+int ExecuteSearchCommand(cbr _cbr, float *_retvals){
+    int rc;
+    //char *zErrMsg = 0;
+    unsigned int i;
+
+    //printf("command: %s\n", _cbr->command);
+    sqlite3_stmt * pStatement;
+    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
+    //if (rc == SQLITE_OK && sqlite3_step(pStatement) == SQLITE_ROW){
+    if (rc == SQLITE_OK){ 
+        if (sqlite3_step(pStatement) == SQLITE_ROW){
+        printf("there is search return data\n");
+        for (i=0; i<_cbr->num_columns; ++i)
+            _retvals[i] = sqlite3_column_double(pStatement, i);
+        } else {
+            printf("no matched rearch results. use default values\n");
+            
+            _retvals[0] = 100;  // throughput
+            _retvals[1] = 1;    // spectral_efficiency
+            _retvals[2] = -2;   // log10_ber
+            _retvals[3] = 1;    // mod_scheme
+            _retvals[4] = -10;  // tx_power
+            _retvals[5] = 10;   // snr
+            _retvals[6] = 0.5;  // utility
+        }
+    }
+
+    sqlite3_finalize(pStatement);
+    
+    /*rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg);
+    if( rc!=SQLITE_OK){
+        fprintf(stderr, "SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+    } else{
+        printf("command executed.\n");
+    }*/
+
+    return rc;
+}
+
+
+// create database
+cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
+{
+    // cbr is a pointer to struct cbr_s
+    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
+
+    // create database
+
+    // copy filename
+    unsigned int i=0;
+    strcpy(_cbr->filename, _filename);
+
+    // execute create database command
+    // database handle
+    //_cbr->db = NULL;
+    OpenDatabase(_cbr);
+
+    // create table
+
+    // copy tablename
+    strcpy(_cbr->tablename, _tablename);
+
+    // number of columns in the table
+    _cbr->num_columns = _len;
+
+    // generate command
+    strcpy(_cbr->command, "CREATE TABLE ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, "(");
+    for (i=0; i<_cbr->num_columns; i++) {
+        strcat(_cbr->command, _cols[i]);
+        strcat(_cbr->command, " FLOAT");
+        if (i != _cbr->num_columns-1) // not last entry
+            strcat(_cbr->command, ", ");
+    }
+    strcat(_cbr->command, ");");
+
+    // execute create table command
+    ExecuteCommand(_cbr);
+
+    return _cbr;
+}
+
+
+// free space
+void cbr_free(cbr _cbr)
+{  
+    // generate command, remove a table with its content
+    strcpy(_cbr->command, "drop table ");
+    strcat(_cbr->command, _cbr->tablename);
+
+    // execute delete command
+    ExecuteCommand(_cbr);
+
+    // clean the database
+    strcpy(_cbr->command, "vacuum");
+    ExecuteCommand(_cbr);
+
+    free(_cbr);
+}
+
+
+// print 
+void cbr_print(cbr _cbr)
+{
+    // generate commandi
+    strcpy(_cbr->command, "select ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, ".* from ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, ";");
+
+    // execute print (select all)  command
+    ExecuteCommand(_cbr);
+    //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
+}
+
+
+/*//static
+int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
+{
+    return 0;
+}*/
+
+
+const char * ops_str[] = {
+    "==", "!=", ">", ">=", "<", "<="};
+
+
+// cbr search
+int cbr_search(
+    cbr _cbr,
+    char *_names[],
+    int * _ops,
+    float *_vals,
+    unsigned int _n,
+    float *_retvals)
+{   
+    // generate command
+    strcpy(_cbr->command, "select ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, ".* from ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, " where ");
+
+    unsigned int i;
+    char str_buffer[64];
+    //printf("number of ops %d:\n", _n);
+    for (i=0; i<_n; i++) {
+        // ensure valid ops value
+        if (_ops[i] < 0 || _ops[i] > 5) {
+            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
+            exit(1);
+        }
+
+        strcat(_cbr->command, _names[i]);
+        strcat(_cbr->command, ops_str[_ops[i]]);
+        sprintf(str_buffer, "%E", _vals[i]);
+        strcat(_cbr->command, str_buffer);
+
+        if (i<_n-1)
+            strcat(_cbr->command, " AND ");
+        else
+            strcat(_cbr->command, " order by utility desc;");
+    }
+
+    //printf("search command: %s\n", _cbr->command);
+
+    //ExecuteCommand(_cbr);
+    ExecuteSearchCommand(_cbr, _retvals);
+    
+    /*printf("search result: ");
+    for (i=0; i<_cbr->num_columns; i++)
+        printf("%f, ",_retvals[i]);
+    printf("\n");*/
+
+    return 0;
+}
+
+
+// cbr add a row
+int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
+{
+    unsigned int i;
+    
+    // generate command
+    //printf("%s\n", _cbr->command);
+    strcpy(_cbr->command, "insert into ");
+    strcat(_cbr->command, _cbr->tablename);
+    
+    strcat(_cbr->command, " (");
+    for (i=0; i<_len; i++) {
+        strcat(_cbr->command, _cols[i]);
+        if (i != _cbr->num_columns-1) // not last entry
+            strcat(_cbr->command, ", ");
+    }
+    strcat(_cbr->command, ") ");
+
+    strcat(_cbr->command, " values(");
+    for (i=0; i<_len; i++) {
+        // ???? how to fill the values if _cbr->num_columns != _len
+        // assume = in the following
+        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
+        if (i != _cbr->num_columns-1) // not last entry
+            strcat(_cbr->command, ", ");
+    }
+    strcat(_cbr->command, ");");
+    
+    // execute add command
+    ExecuteCommand(_cbr);
+
+    return 0;
+}
+
Index: vtcross/branches/bhilburn/src/cognitive_engines/Makefile.am
===================================================================
--- vtcross/branches/bhilburn/src/cognitive_engines/Makefile.am	(revision 120)
+++ vtcross/branches/bhilburn/src/cognitive_engines/Makefile.am	(revision 123)
@@ -0,0 +1,4 @@
+bin_PROGRAMS = cbr_demo
+
+cbr_demo_SOURCES = cbr.c cbr_demo.c
+
Index: vtcross/branches/bhilburn/src/cognitive_engines/cbr_demo.c
===================================================================
--- vtcross/branches/bhilburn/src/cognitive_engines/cbr_demo.c	(revision 123)
+++ vtcross/branches/bhilburn/src/cognitive_engines/cbr_demo.c	(revision 123)
@@ -0,0 +1,594 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+//#include <iostream>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cbr.h"
+
+//using namespace std;
+
+struct CE_Info {
+    int numUtilities;
+    int numParameters;
+    int numObservables;
+};
+
+struct Utility {
+    char name[50];
+    char units[50];
+    char goal[50];
+   /* string name;
+    string units;
+    string goal;*/
+    float target;
+    float value;
+};
+
+struct Affect {
+    struct Utility u; 
+    //string relation;
+    char relation[50];
+};      
+
+struct Parameter {
+    /*string name;
+    string units;*/
+    char name[50];
+    char units[50];
+    float min;
+    int numAffects; 
+    struct Affect affection_list[10];
+    float max;
+    float step;
+    float value;
+};      
+        
+struct Observable {
+    //string name;
+    char name[50];
+    struct Affect affection_list[10];
+    int numAffects;
+    float value;
+};
+
+// error handling
+void error(char *msg)
+{
+    perror(msg);
+    exit(0);
+}
+
+void ReadMessage(int socket,char * buffer) {
+    int i,n;
+
+    n = recv(socket,buffer,256,MSG_PEEK);
+    for(i=0;i<256;i++){
+	    if(strcmp(&buffer[i],"\0") == 0) break;
+    }
+    n = recv(socket,buffer,i+1,0);
+    if (n < 0) 
+         error("ERROR reading from socket");
+    //printf("ReadMessage:%s %d\n",buffer,n);
+}
+
+
+int GetUtility(int sockfd, struct Utility uList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i;
+
+    // read unitilities
+    // numUtilities
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numUtilities = atoi(buffer);
+    //printf("number of utilities: %d\n", ce_info->numUtilities);
+        
+    for (i = 0; i < ce_info->numUtilities; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(uList[i].name, buffer);
+                 
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Units: %s\n", buffer);
+        strcpy(uList[i].units, buffer);
+
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Goal: %s\n", buffer);
+        strcpy(uList[i].goal, buffer);
+        
+	    bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Target: %s\n", buffer);
+        uList[i].target = atof(buffer);
+    }
+    return 1;
+}
+
+
+int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i, j;
+
+    // numParameters
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numParameters = atoi(buffer);
+    //printf("number of parameters: %d\n", ce_info->numParameters);
+        
+    for (i = 0; i < ce_info->numParameters; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(pList[i].name, buffer);
+                 
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Units: %s\n", buffer);
+        strcpy(pList[i].units, buffer);
+
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Min: %s\n", buffer);
+        pList[i].min = atof(buffer);
+        
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Max: %s\n", buffer);
+        pList[i].max = atof(buffer);
+        
+	    bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Step: %s\n", buffer);
+        pList[i].step = atof(buffer);
+    
+        // numAffects
+        bzero(buffer,256);
+        ReadMessage(sockfd,buffer);
+        pList[i].numAffects = atoi(buffer);
+        //printf("number of affects: %d\n", pList[i].numAffects);
+        
+        for (j = 0; j < pList[i].numAffects; j++){
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Utility name: %s\n", buffer);
+            strcpy(pList[i].affection_list[j].u.name, buffer);
+                 
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Relation: %s\n", buffer);
+            strcpy(pList[i].affection_list[j].relation, buffer);
+        }
+    }
+    return 1;
+}
+
+
+int GetObservable(int sockfd, struct Observable oList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i,j;
+
+    // numParameters
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numObservables = atoi(buffer);
+    //printf("number of observables: %d\n", ce_info->numObservables);
+        
+    for (i = 0; i < ce_info->numObservables; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(oList[i].name, buffer);
+                 
+        // numAffects
+        bzero(buffer,256);
+        ReadMessage(sockfd,buffer);
+        oList[i].numAffects = atoi(buffer);
+        //printf("number of affects: %d\n", oList[i].numAffects);
+        
+        for (j = 0; j < oList[i].numAffects; j++){
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Utility name: %s\n", buffer);
+            strcpy(oList[i].affection_list[j].u.name, buffer);
+                 
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Relation: %s\n", buffer);
+            strcpy(oList[i].affection_list[j].relation, buffer);
+        }
+    }
+    return 1;
+}
+
+
+// setup client socket connection
+int ClientSocket(int argc, char** argv)
+{
+    int sockfd;
+    int portno;
+    struct sockaddr_in serv_addr;
+    struct hostent *server;
+    
+    // setup client socket connection
+    if (argc < 3) {
+       fprintf(stderr,"usage: %s hostname port\n", argv[0]);
+       exit(0);
+    }
+    // server name
+    server = gethostbyname(argv[1]);
+    if (server == NULL) {
+        fprintf(stderr,"ERROR, no such host\n");
+        exit(0);
+    }
+    // port no.
+    portno = atoi(argv[2]);
+    // socket file descriptor
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0) 
+        error("ERROR opening socket");
+    // initiate struct socketaddr_in
+    bzero((char *) &serv_addr, sizeof(serv_addr));
+    serv_addr.sin_family = AF_INET;
+    serv_addr.sin_port = htons(portno);
+    bcopy((char *)server->h_addr, 
+         (char *)&serv_addr.sin_addr.s_addr,
+         server->h_length);
+    // cast sockaddr_in to sockaddr
+    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
+        error("ERROR connecting");
+
+    return sockfd;
+}
+
+    
+int SendMessage(int socketfd, char *message) 
+{
+    int n;
+     
+    strcat(message, "\0000");
+    // Write message back to client
+    //n = write(socketfd,message,(strlen(message)+1));
+    n = send(socketfd,message,(strlen(message)+1),0);
+    if (n<0)
+        error("Error sending to server\n");
+    else if(n == 0)
+        printf("Server closed the socket.\n");
+    
+    return n;
+}
+
+
+int GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], 
+        struct Observable oList[], struct CE_Info *ce_info)
+{
+    // get utilities
+    GetUtility(sockfd, uList, ce_info);
+    
+    // get parameters
+    GetParameter(sockfd, pList, ce_info);
+
+    // read obervables
+    GetObservable(sockfd, oList, ce_info);
+
+    // send ack back to server
+    char buffer[256];
+    strcpy(buffer, "xml received");
+    SendMessage(sockfd, buffer);
+    //printf("xml reception done\n");
+    
+    return 1;
+}
+
+
+int GetExperienceSize(int sockfd, int *num_rows, int *num_cols)
+{
+    char buffer[256];
+    
+    // number of rows
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    *num_rows = atoi(buffer);
+    // number of columns
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    *num_cols = atoi(buffer);
+    return 1;
+}
+
+int GetRequest(int sockfd)
+{
+    char buffer[256];
+
+	bzero(buffer,256);
+	ReadMessage(sockfd,buffer);
+
+    return 1;
+}
+
+int GetExperience(int sockfd, int num_rows, int num_cols, 
+        //float **past_exp)
+        float past_exp[num_rows][num_cols])
+{
+    char buffer[256];
+    int i, j;
+
+    // read experience
+    for (i = 0; i < num_rows; i++){
+        for (j = 0; j < num_cols; j++){
+	    bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+    //    printf("experience: %s\n", buffer);
+        past_exp[i][j] = atof(buffer);
+        }
+    }
+
+    return 1;
+}
+
+
+void print_current_config(struct Utility uList[], 
+        struct Parameter pList[], 
+        struct Observable oList[], 
+        struct CE_Info * ce_info) 
+{    
+    int i = 0;
+    int j = 0;
+
+    // utilities
+    for(i = 0; i < ce_info->numUtilities ; i++) {
+        printf("Utility: %s\n", uList[i].name);
+        printf("    Units: %s\n", uList[i].units);
+        printf("    Goal: %s\n", uList[i].goal);
+        printf("    Target: %f\n", uList[i].target);
+        /*cout << "Utility:  " << uList[i].name << endl;
+        cout << "     Units:  " << uList[i].units << endl;
+        cout << "     Goal:   " << uList[i].goal << endl;
+        cout << "     Target: " << uList[i].target << endl;*/
+    }
+                       
+    // parameters                        
+    for(i = 0; i < ce_info->numParameters; i++) {
+        printf("Paramter: %s\n", pList[i].name);
+        printf("    Units: %s\n", pList[i].units);
+        printf("    Min: %f\n", pList[i].min);
+        printf("    Max: %f\n", pList[i].max);
+        printf("    Step: %f\n", pList[i].step);
+        /*cout << "Parameter:  " << pList[i].name << endl;
+        cout << "       Units:   " << pList[i].units << endl;
+        cout << "       Min:     " << pList[i].min << endl;
+        cout << "       Max:     " << pList[i].max << endl;
+        cout << "       Step:    " << pList[i].step << endl;*/
+        for(j = 0; j < pList[i].numAffects; j++) {
+            printf("        Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation);
+            //cout << "       Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl;
+        }
+    }
+
+    // observables
+    for(i = 0; i < ce_info->numObservables; i++) {
+        printf("Observable: %s\n", oList[i].name);
+        //cout << "Observable:  " << oList[i].name << endl;
+        for(j = 0; j < oList[i].numAffects; j++) {
+            printf("        Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation);
+            //cout << "       Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl;
+        }
+    }
+}
+
+int RunCBREngine(struct Utility uList[], struct Parameter pList[],
+        struct Observable oList[], struct CE_Info *ce_info, 
+        //int num_cols, int num_rows, float ** past_exp)
+        int num_cols, int num_rows, float past_exp[num_rows][num_cols])
+{
+    
+    int i, j;
+
+    // get column names
+    /*char *cols[50] = {"throughput", "spectral_efficiency", 
+    "log10_ber", "mod_scheme", "tx_power", "SNR", "utility"};*/
+    //char cols[num_cols][50];
+   
+
+    char **cols;
+    cols = (char **)malloc(sizeof(char)*num_cols);
+    j = 0;
+    for (i = 0; i < ce_info->numUtilities; i++){
+        cols[j] = malloc(strlen(uList[i].name)+1);
+        cols[j] = uList[i].name;
+    //    strcpy(cols[j], uList[i].name);
+        j++;
+    }
+    for (i = 0; i < ce_info->numParameters; i++){
+        cols[j] = malloc(strlen(pList[i].name)+1);
+        cols[j] = pList[i].name;
+    //    strcpy(cols[j], pList[i].name);
+        j++;
+    }
+    for (i = 0; i < ce_info->numObservables; i++){
+        cols[j] = malloc(strlen(oList[i].name)+1);
+        cols[j] = oList[i].name;
+    //    strcpy(cols[j], oList[i].name);
+        j++;
+    }
+    cols[j] = malloc(strlen("utility")+1);
+    cols[j] = "utility";
+    
+    //printf("column names:");
+    //for (i = 0; i<num_cols; i++){
+    //    printf(" %s",cols[i]);
+    //}
+    //printf("\n");
+
+    int rc;
+
+    // create cbr database/table
+    //printf("create cbr database\n");
+    char filename[] = {"ex1"}, tablename[] = {"data"};
+    cbr mycbr = cbr_create(filename, tablename, cols, num_cols);
+    //cbr mycbr = cbr_create("ex1", "data", cols, num_cols);
+
+    // add row here
+    float vals[num_cols];
+    /*// sample table entry
+    vals[0] = 1e3f;    // throughput
+    vals[1] = 1;        // spectral_efficiency
+    vals[2] = -3.50;    // log10_ber
+    vals[3] = 1;        // mod_scheme
+    vals[4] = -3.5f;    // tx_power
+    vals[5] = 10.0f;   // SNR
+    vals[6] = 0.762;    // utility*/
+    for (i = 0; i < num_rows; i++){
+        for (j = 0; j < num_cols; j++){
+            vals[j] = past_exp[i][j];
+        }
+        rc = cbr_add_row(mycbr, cols, vals, num_cols);
+    }
+    
+    // print
+    printf("cbr table\n");
+    cbr_print(mycbr);
+    
+    // simple search: find entry where...
+    //   log10_ber < 1e-2 and SNR > 5
+    //char * search_names[] = {"log10_ber", "SNR"};
+    char ** search_names;
+    search_names
+        = (char **)malloc(sizeof(char)*(ce_info->numUtilities));
+    for (i = 0; i < ce_info->numUtilities; i++){
+        search_names[i] 
+            = malloc(sizeof(char)*(strlen(uList[i].name)+1));
+        search_names[i] = uList[i].name;
+    }
+    int search_ops[] = {GT, GT, LT};
+    //float search_vals[] = {1e-2f, 5.f};
+    float *search_vals;
+    search_vals = malloc(sizeof(float)*(ce_info->numUtilities));
+    for (i = 0; i < ce_info->numUtilities; i++){
+        search_vals[i] = uList[i].target;
+    }
+    float retvals[num_cols];
+    rc = cbr_search(mycbr, search_names, search_ops, 
+            search_vals, ce_info->numUtilities, retvals);
+
+    j = 0;
+    for (i = 0; i < ce_info->numUtilities; i++){
+        uList[i].value = retvals[j];
+        j++;
+    }
+    for (i = 0; i < ce_info->numParameters; i++){
+        pList[i].value = retvals[j];
+        j++;
+    }
+    for (i = 0; i < ce_info->numObservables; i++){
+        oList[i].value = retvals[j];
+        j++;
+    }
+
+    // clean up database
+    //printf("clean cbr database\n");
+    cbr_free(mycbr);
+    
+    return 1;
+}
+
+
+int SendCEResults(int sockfd, struct Utility uList[], struct Parameter pList[], 
+        struct Observable oList[], struct CE_Info *ce_info)
+{
+    // send results back to server
+    char var[50];
+    int i;
+ 
+    /*// utility
+    for (i = 0; i < ce_info->numUtilities; i++){
+        sprintf(var, "%f", uList[i].value);
+        SendMessage(sockfd, var);
+    }*/
+    
+    // parameter
+    for (i = 0; i < ce_info->numParameters; i++){
+        sprintf(var, "%f", pList[i].value);
+        SendMessage(sockfd, var);
+    }
+    
+    /*// observable
+    for (i = 0; i < ce_info->numObservables; i++){
+        sprintf(var, "%f", oList[i].value);
+        SendMessage(sockfd, var);
+    }*/
+
+    return 1;
+}
+
+int RegisterCE(int sockfd, struct Utility uList[], struct Parameter pList[], 
+        struct Observable oList[], struct CE_Info *ce_info)
+{
+    // Send register message to cognitive radio
+    SendMessage(sockfd, "c_register");
+    return 1;
+    
+    
+    
+    return 1;
+}
+
+// main client socket
+int main(int argc, char *argv[])
+{
+    struct Utility uList[10];
+    struct Parameter pList[10];
+    struct Observable oList[10];
+    struct CE_Info ce_info;
+    char buffer[256];
+
+    // setup client socket connection
+    int sockfd;
+    sockfd = ClientSocket(argc, argv);
+
+    RegisterCE(sockfd,uList,pList,oList,&ce_info);
+    // get xml info from server
+    GetXMLFromServer(sockfd, uList, pList, oList, &ce_info);
+    printf("Received Radio Operation Profile from Server Successfully.\n\n");
+    //print_current_config(uList, pList, oList, &ce_info); 
+    
+    // get experience size from server
+    int num_rows, num_cols;
+    GetExperienceSize(sockfd, &num_rows, &num_cols);
+
+    // get experience
+    int i, j;
+    float past_exp[num_rows][num_cols];
+    GetExperience(sockfd, num_rows, num_cols, past_exp);
+    printf("Received Previous Radio Experience from Server Successfully.\n\n");
+    // cbr operation
+    //unsigned int num_cols;
+    // get number of columns
+    num_cols = ce_info.numUtilities + ce_info.numParameters;
+    num_cols = num_cols + ce_info.numObservables;
+    num_cols = num_cols + 1;    // overall utility
+    //while(1) {
+        
+        // Wait until request is received from server to perform optimization 
+        //printf("Waiting for Request for optimization from Server\n\n");
+        //GetRequest(sockfd);
+        //printf("Received optimization request from server\n\n");
+        RunCBREngine(uList, pList, oList, &ce_info, num_cols, num_rows, past_exp);    
+   
+        printf("Sending optimization results to server.\n\n");
+        // send results back to server
+        SendCEResults(sockfd, uList, pList, oList, &ce_info);
+        
+    //}
+    
+    return 0;
+}
