[218] | 1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
| 2 | * Virginia Tech, 2009 |
---|
| 3 | * |
---|
| 4 | * LICENSE INFORMATION GOES HERE |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* DESCRIPTION OF FILE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #include <cstdlib> |
---|
| 12 | #include <cstring> |
---|
| 13 | #include <stdint.h> |
---|
[230] | 14 | #include <math.h> |
---|
[218] | 15 | |
---|
| 16 | #include "vtcross/common.h" |
---|
| 17 | #include "vtcross/components.h" |
---|
| 18 | #include "vtcross/containers.h" |
---|
| 19 | #include "vtcross/debug.h" |
---|
| 20 | #include "vtcross/error.h" |
---|
| 21 | #include "vtcross/socketcomm.h" |
---|
[228] | 22 | #include "vtcross/cbr.h" |
---|
[218] | 23 | |
---|
[231] | 24 | // TODO this is really bad; need to move to a proper cbr.h |
---|
[228] | 25 | #include "cbr.c" |
---|
[218] | 26 | |
---|
[268] | 27 | #include "sqlite3.h" |
---|
| 28 | #include "sqlite3ext.h" |
---|
[228] | 29 | |
---|
| 30 | |
---|
| 31 | static cbr myCBR; |
---|
| 32 | |
---|
[231] | 33 | |
---|
[218] | 34 | CognitiveEngine::CognitiveEngine() |
---|
| 35 | { |
---|
| 36 | LOG("Creating Cognitive Engine.\n"); |
---|
| 37 | SML_present = false; |
---|
| 38 | commandSocketFD = -1; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | CognitiveEngine::~CognitiveEngine() |
---|
| 43 | { |
---|
[231] | 44 | cbr_free(myCBR); |
---|
[218] | 45 | delete [] pList; |
---|
| 46 | delete [] oList; |
---|
| 47 | delete [] uList; |
---|
| 48 | delete [] radioInfo; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
[231] | 52 | CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \ |
---|
[218] | 53 | const bool SML) |
---|
| 54 | { |
---|
| 55 | LOG("Creating Cognitive Engine.\n"); |
---|
| 56 | |
---|
| 57 | pList = new Parameter[10]; |
---|
| 58 | oList = new Observable[10]; |
---|
| 59 | uList = new Utility[10]; |
---|
| 60 | radioInfo = new Radio_Info; |
---|
| 61 | |
---|
| 62 | ConnectToRemoteComponent(serverName, serverPort, SML); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | void |
---|
| 67 | CognitiveEngine::SendComponentType() |
---|
| 68 | { |
---|
| 69 | SendMessage(commandSocketFD, "response_engine_cognitive"); |
---|
| 70 | LOG("Cognitive Engine responded to GetRemoteComponentType query.\n"); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | void |
---|
[231] | 75 | CognitiveEngine::ConnectToRemoteComponent(const char *serverName, \ |
---|
| 76 | const char *serverPort, const bool SML) |
---|
[218] | 77 | { |
---|
| 78 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
| 79 | |
---|
| 80 | SML_present = SML; |
---|
| 81 | |
---|
| 82 | if(SML) { |
---|
[285] | 83 | |
---|
[218] | 84 | RegisterComponent(); |
---|
| 85 | LOG("Cognitive Engine connected to SML at %s.\n", serverName); |
---|
| 86 | ReceiveRadioConfiguration(); |
---|
| 87 | ReceiveExperience(); |
---|
[285] | 88 | |
---|
| 89 | RegisterServices(); |
---|
[218] | 90 | } |
---|
| 91 | else { |
---|
| 92 | RegisterComponent(); |
---|
| 93 | LOG("Cognitive Engine connected to shell at %s.\n", serverName); |
---|
| 94 | ReceiveRadioConfiguration(); |
---|
| 95 | ReceiveExperience(); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void |
---|
[230] | 100 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters) |
---|
| 101 | { |
---|
| 102 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
[231] | 103 | |
---|
| 104 | uint32_t numberColumns = |
---|
| 105 | radioInfo->numParameters + |
---|
| 106 | radioInfo->numUtilities; |
---|
[230] | 107 | |
---|
[231] | 108 | uint32_t obsColumns = radioInfo->numObservables + 1; |
---|
[230] | 109 | |
---|
[231] | 110 | float valList[numberColumns]; |
---|
| 111 | float obsVals[numberColumns]; |
---|
[230] | 112 | char *nameList[numberColumns]; |
---|
| 113 | char *obsList[obsColumns]; |
---|
| 114 | |
---|
| 115 | size_t columnObsIndex = 0; |
---|
| 116 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
[231] | 117 | obsList[columnObsIndex] = (char*)observables[i].name.c_str(); |
---|
[230] | 118 | columnObsIndex++; |
---|
| 119 | } |
---|
| 120 | obsList[columnObsIndex] = "utility"; |
---|
| 121 | |
---|
| 122 | size_t columnIndex = 0; |
---|
| 123 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
[231] | 124 | nameList[columnIndex] = (char*)parameters[i].name.c_str(); |
---|
[230] | 125 | columnIndex++; |
---|
| 126 | } |
---|
| 127 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
[231] | 128 | nameList[columnIndex] = (char*)uList[i].name.c_str(); |
---|
[230] | 129 | columnIndex++; |
---|
| 130 | } |
---|
| 131 | |
---|
[231] | 132 | size_t obsValueIndex = 0; |
---|
[230] | 133 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
[231] | 134 | obsVals[obsValueIndex] = observables[i].value; |
---|
| 135 | obsValueIndex++; |
---|
| 136 | } |
---|
[230] | 137 | |
---|
[231] | 138 | /* Calculate Utility */ |
---|
| 139 | float newUtilityValue = 0; |
---|
| 140 | |
---|
[230] | 141 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[231] | 142 | newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); |
---|
| 143 | } |
---|
| 144 | obsVals[obsValueIndex] = newUtilityValue; |
---|
[230] | 145 | |
---|
[231] | 146 | size_t returnValueIndex = 0; |
---|
[230] | 147 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
[231] | 148 | valList[returnValueIndex] = parameters[i].value; |
---|
| 149 | returnValueIndex++; |
---|
| 150 | } |
---|
[230] | 151 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[231] | 152 | valList[returnValueIndex] = uList[i].target; |
---|
| 153 | returnValueIndex++; |
---|
| 154 | } |
---|
[230] | 155 | |
---|
[231] | 156 | cbr_update(myCBR, nameList, obsList, valList, obsVals, |
---|
| 157 | numberColumns, obsColumns); |
---|
[230] | 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | void |
---|
[218] | 162 | CognitiveEngine::WaitForSignal() |
---|
| 163 | { |
---|
| 164 | char buffer[256]; |
---|
| 165 | |
---|
| 166 | while(true) { |
---|
| 167 | memset(buffer, 0, 256); |
---|
| 168 | |
---|
| 169 | ReadMessage(commandSocketFD, buffer); |
---|
| 170 | |
---|
| 171 | // TODO this is ugly... is there a better way? Doesn't strcmp compare the |
---|
| 172 | // whole string? We only need to compare until we find a single different |
---|
| 173 | // byte... |
---|
| 174 | // |
---|
| 175 | // If we send integer op codes rather than strings, this process will be |
---|
| 176 | // MUCH faster since instead of donig string compares we can simply |
---|
| 177 | // switch on the integer value... |
---|
[230] | 178 | if(strcmp(buffer, "update_performance") == 0) { |
---|
[222] | 179 | |
---|
[230] | 180 | /* Receive Set of current Parameters */ |
---|
| 181 | memset(buffer, 0, 256); |
---|
[231] | 182 | ReadMessage(commandSocketFD, buffer); |
---|
[230] | 183 | uint32_t numParameters = atoi(buffer); |
---|
| 184 | |
---|
| 185 | Parameter *p = new Parameter[numParameters]; |
---|
| 186 | |
---|
| 187 | for(size_t i = 0; i < numParameters; i++) { |
---|
| 188 | memset(buffer, 0, 256); |
---|
| 189 | ReadMessage(commandSocketFD, buffer); |
---|
| 190 | p[i].name = std::string(buffer); |
---|
| 191 | |
---|
| 192 | memset(buffer, 0, 256); |
---|
| 193 | ReadMessage(commandSocketFD, buffer); |
---|
| 194 | p[i].value = atof(buffer); |
---|
| 195 | } |
---|
| 196 | |
---|
[222] | 197 | /* Receive Set of Observables */ |
---|
[230] | 198 | memset(buffer, 0, 256); |
---|
[231] | 199 | ReadMessage(commandSocketFD, buffer); |
---|
[230] | 200 | uint32_t numObservables = atoi(buffer); |
---|
| 201 | |
---|
| 202 | Observable *o = new Observable[numObservables]; |
---|
| 203 | |
---|
| 204 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 205 | memset(buffer, 0, 256); |
---|
| 206 | ReadMessage(commandSocketFD, buffer); |
---|
| 207 | o[i].name = std::string(buffer); |
---|
| 208 | |
---|
| 209 | memset(buffer, 0, 256); |
---|
| 210 | ReadMessage(commandSocketFD, buffer); |
---|
| 211 | o[i].value = atof(buffer); |
---|
| 212 | } |
---|
[218] | 213 | |
---|
[231] | 214 | ReceiveFeedback(o,p); |
---|
[230] | 215 | |
---|
| 216 | delete [] o; |
---|
| 217 | delete [] p; |
---|
[231] | 218 | } |
---|
[285] | 219 | else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
| 220 | // THIS IS CURRENTLY IN DEMO MODE |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | /* Receive Set of Observables */ |
---|
| 224 | LOG("\nCognitive Engine:: Receiving service name\n"); |
---|
| 225 | |
---|
| 226 | memset(buffer, 0, 256); |
---|
| 227 | ReadMessage(commandSocketFD,buffer); |
---|
| 228 | LOG("\nCognitive Engine:: Got service name, %s\n", buffer); |
---|
| 229 | |
---|
| 230 | /* Receive Set of Observables */ |
---|
| 231 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
| 232 | |
---|
| 233 | memset(buffer, 0, 256); |
---|
| 234 | ReadMessage(commandSocketFD,buffer); |
---|
| 235 | uint32_t numObservables = atoi(buffer); |
---|
| 236 | |
---|
| 237 | Observable *o = new Observable[numObservables]; |
---|
| 238 | |
---|
| 239 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 240 | memset(buffer, 0, 256); |
---|
| 241 | ReadMessage(commandSocketFD, buffer); |
---|
| 242 | o[i].name = std::string(buffer); |
---|
| 243 | |
---|
| 244 | memset(buffer, 0, 256); |
---|
| 245 | ReadMessage(commandSocketFD, buffer); |
---|
| 246 | o[i].value = atof(buffer); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /* Receive Set of current Parameters */ |
---|
| 250 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
| 251 | |
---|
| 252 | memset(buffer, 0, 256); |
---|
| 253 | ReadMessage(commandSocketFD, buffer); |
---|
| 254 | uint32_t numCurrentParameters = atoi(buffer); |
---|
| 255 | |
---|
| 256 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
| 257 | |
---|
| 258 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
| 259 | memset(buffer, 0, 256); |
---|
| 260 | ReadMessage(commandSocketFD, buffer); |
---|
| 261 | cp[i].name = std::string(buffer); |
---|
| 262 | |
---|
| 263 | memset(buffer, 0, 256); |
---|
| 264 | ReadMessage(commandSocketFD, buffer); |
---|
| 265 | cp[i].value = atof(buffer); |
---|
| 266 | } |
---|
| 267 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
| 268 | |
---|
| 269 | Parameter *solutionSet; |
---|
| 270 | |
---|
| 271 | //solutionSet = GetSolution(o,cp); |
---|
| 272 | |
---|
| 273 | // TODO need to actually do something with the observables here |
---|
| 274 | |
---|
| 275 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
| 276 | char numParametersChar[10]; |
---|
| 277 | char solutionValue[50]; |
---|
| 278 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
| 279 | SendMessage(commandSocketFD, numParametersChar); |
---|
| 280 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 281 | //SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
| 282 | SendMessage(commandSocketFD, "test"); |
---|
| 283 | //memset(solutionValue, 0, 50); |
---|
| 284 | //sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
| 285 | //SendMessage(commandSocketFD, solutionValue); |
---|
| 286 | SendMessage(commandSocketFD, "00"); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | delete [] o; |
---|
| 290 | delete [] cp; |
---|
| 291 | } |
---|
[231] | 292 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
[230] | 293 | |
---|
| 294 | /* Receive Set of Observables */ |
---|
[231] | 295 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
[230] | 296 | |
---|
[222] | 297 | memset(buffer, 0, 256); |
---|
| 298 | ReadMessage(commandSocketFD,buffer); |
---|
| 299 | uint32_t numObservables = atoi(buffer); |
---|
| 300 | |
---|
[224] | 301 | Observable *o = new Observable[numObservables]; |
---|
[222] | 302 | |
---|
[224] | 303 | for(size_t i = 0; i < numObservables; i++) { |
---|
[222] | 304 | memset(buffer, 0, 256); |
---|
[224] | 305 | ReadMessage(commandSocketFD, buffer); |
---|
[222] | 306 | o[i].name = std::string(buffer); |
---|
| 307 | |
---|
| 308 | memset(buffer, 0, 256); |
---|
[224] | 309 | ReadMessage(commandSocketFD, buffer); |
---|
[222] | 310 | o[i].value = atof(buffer); |
---|
| 311 | } |
---|
| 312 | |
---|
[228] | 313 | /* Receive Set of current Parameters */ |
---|
[231] | 314 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
[228] | 315 | |
---|
| 316 | memset(buffer, 0, 256); |
---|
[231] | 317 | ReadMessage(commandSocketFD, buffer); |
---|
[228] | 318 | uint32_t numCurrentParameters = atoi(buffer); |
---|
| 319 | |
---|
| 320 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
| 321 | |
---|
| 322 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
| 323 | memset(buffer, 0, 256); |
---|
| 324 | ReadMessage(commandSocketFD, buffer); |
---|
| 325 | cp[i].name = std::string(buffer); |
---|
| 326 | |
---|
| 327 | memset(buffer, 0, 256); |
---|
| 328 | ReadMessage(commandSocketFD, buffer); |
---|
| 329 | cp[i].value = atof(buffer); |
---|
| 330 | } |
---|
[231] | 331 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
[224] | 332 | |
---|
[228] | 333 | Parameter *solutionSet; |
---|
[231] | 334 | |
---|
| 335 | solutionSet = GetSolution(o,cp); |
---|
[228] | 336 | |
---|
[224] | 337 | // TODO need to actually do something with the observables here |
---|
[230] | 338 | |
---|
[231] | 339 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
| 340 | char numParametersChar[10]; |
---|
| 341 | char solutionValue[50]; |
---|
| 342 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
| 343 | SendMessage(commandSocketFD, numParametersChar); |
---|
[228] | 344 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
[231] | 345 | SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
[228] | 346 | memset(solutionValue, 0, 50); |
---|
[231] | 347 | sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
| 348 | SendMessage(commandSocketFD, solutionValue); |
---|
| 349 | } |
---|
[224] | 350 | |
---|
| 351 | delete [] o; |
---|
[228] | 352 | delete [] cp; |
---|
[218] | 353 | } |
---|
| 354 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
| 355 | SendComponentType(); |
---|
| 356 | } |
---|
| 357 | else if(strcmp(buffer, "connect_sml") == 0) { |
---|
| 358 | /* This command implies that we are disconnecting from the shell and |
---|
| 359 | * connecting to a SML component. */ |
---|
| 360 | char serverName[256]; |
---|
| 361 | char serverPort[256]; |
---|
| 362 | // TODO is this going to end up being too slow? |
---|
| 363 | memset(serverName, 0, 256); |
---|
| 364 | memset(serverPort, 0, 256); |
---|
| 365 | |
---|
| 366 | ReadMessage(commandSocketFD, serverName); |
---|
| 367 | ReadMessage(commandSocketFD, serverPort); |
---|
| 368 | |
---|
| 369 | /* Only continue if we are currently connected to a shell. */ |
---|
| 370 | if(!SML_present) { |
---|
| 371 | DeregisterComponent(); |
---|
| 372 | |
---|
| 373 | shutdown(commandSocketFD, 2); |
---|
| 374 | close(commandSocketFD); |
---|
| 375 | |
---|
| 376 | ConnectToRemoteComponent(serverName, serverPort, true); |
---|
| 377 | } |
---|
| 378 | } |
---|
| 379 | else if(strcmp(buffer, "disconnect_sml") == 0) { |
---|
| 380 | /* This command implies that we are disconnecting from the SML and |
---|
| 381 | * connecting to a shell component. */ |
---|
| 382 | char serverName[256]; |
---|
| 383 | char serverPort[256]; |
---|
| 384 | // TODO is this going to end up being too slow? |
---|
| 385 | memset(serverName, 0, 256); |
---|
| 386 | memset(serverPort, 0, 256); |
---|
| 387 | |
---|
| 388 | ReadMessage(commandSocketFD, serverName); |
---|
| 389 | ReadMessage(commandSocketFD, serverPort); |
---|
| 390 | |
---|
| 391 | /* We only want to do this if we are actually connected to an SML |
---|
| 392 | * currently. */ |
---|
| 393 | if(SML_present) { |
---|
| 394 | DeregisterServices(); |
---|
| 395 | |
---|
| 396 | shutdown(commandSocketFD, 2); |
---|
| 397 | close(commandSocketFD); |
---|
| 398 | |
---|
| 399 | ConnectToRemoteComponent(serverName, serverPort, false); |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
| 403 | Reset(); |
---|
| 404 | } |
---|
| 405 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
| 406 | Shutdown(); |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | void |
---|
| 413 | CognitiveEngine::Shutdown() |
---|
| 414 | { |
---|
| 415 | if(SML_present) { |
---|
[255] | 416 | //DeregisterServices(); |
---|
[218] | 417 | DeregisterComponent(); |
---|
| 418 | } |
---|
| 419 | else { |
---|
| 420 | DeregisterComponent(); |
---|
| 421 | } |
---|
| 422 | // TODO should something else be happening here? |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | |
---|
| 426 | void |
---|
| 427 | CognitiveEngine::Reset() |
---|
| 428 | { |
---|
| 429 | LOG("Resetting Cognitive Engine.\n"); |
---|
| 430 | |
---|
| 431 | if(SML_present) { |
---|
| 432 | DeregisterServices(); |
---|
| 433 | DeregisterComponent(); |
---|
| 434 | } |
---|
| 435 | else { |
---|
| 436 | DeregisterComponent(); |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | void |
---|
| 442 | CognitiveEngine::RegisterComponent() |
---|
| 443 | { |
---|
[285] | 444 | char buffer[256]; |
---|
[218] | 445 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
| 446 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
[285] | 447 | |
---|
| 448 | memset(buffer, 0, 256); |
---|
| 449 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 450 | } |
---|
| 451 | |
---|
| 452 | void |
---|
| 453 | CognitiveEngine::DeregisterComponent() |
---|
| 454 | { |
---|
| 455 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
| 456 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
| 457 | |
---|
| 458 | shutdown(commandSocketFD, 2); |
---|
| 459 | close(commandSocketFD); |
---|
| 460 | commandSocketFD = -1; |
---|
| 461 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | |
---|
| 465 | void |
---|
| 466 | CognitiveEngine::RegisterServices() |
---|
| 467 | { |
---|
| 468 | LOG("Cognitive Engine:: Registering services.\n"); |
---|
| 469 | |
---|
| 470 | SendMessage(commandSocketFD, "register_service"); |
---|
[285] | 471 | SendMessage(commandSocketFD, "test_srv"); |
---|
[218] | 472 | |
---|
| 473 | SendMessage(commandSocketFD, "register_service"); |
---|
[285] | 474 | SendMessage(commandSocketFD, "test_srv1"); |
---|
[218] | 475 | |
---|
| 476 | SendMessage(commandSocketFD, "register_service"); |
---|
[285] | 477 | SendMessage(commandSocketFD, "test_srv2"); |
---|
[218] | 478 | |
---|
| 479 | SendMessage(commandSocketFD, "register_service"); |
---|
[285] | 480 | SendMessage(commandSocketFD, "test_srv3"); |
---|
| 481 | |
---|
[218] | 482 | } |
---|
| 483 | |
---|
[255] | 484 | //Combined with deregister component since those two things must happen togeather |
---|
[278] | 485 | void |
---|
[218] | 486 | CognitiveEngine::DeregisterServices() |
---|
| 487 | { |
---|
| 488 | LOG("Cognitive Engine:: Deregistering services.\n"); |
---|
| 489 | |
---|
| 490 | SendMessage(commandSocketFD, "deregister_service"); |
---|
[285] | 491 | SendMessage(commandSocketFD, "test_srv"); |
---|
[218] | 492 | |
---|
| 493 | SendMessage(commandSocketFD, "deregister_service"); |
---|
[285] | 494 | SendMessage(commandSocketFD, "test_srv1"); |
---|
[218] | 495 | |
---|
| 496 | SendMessage(commandSocketFD, "deregister_service"); |
---|
[285] | 497 | SendMessage(commandSocketFD, "test_srv2"); |
---|
[218] | 498 | |
---|
| 499 | SendMessage(commandSocketFD, "deregister_service"); |
---|
[285] | 500 | SendMessage(commandSocketFD, "test_srv3"); |
---|
[218] | 501 | |
---|
[278] | 502 | } |
---|
[218] | 503 | |
---|
| 504 | void |
---|
| 505 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
| 506 | { |
---|
| 507 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
| 508 | |
---|
| 509 | char buffer[256]; |
---|
| 510 | |
---|
| 511 | /* Receive Set of Utilities */ |
---|
| 512 | memset(buffer, 0, 256); |
---|
[231] | 513 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 514 | radioInfo->numUtilities = atoi(buffer); |
---|
[285] | 515 | LOG("Cognitive Engine:: Receiving Radio Configuration. %s\n",buffer); |
---|
| 516 | |
---|
| 517 | |
---|
[224] | 518 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[218] | 519 | memset(buffer, 0, 256); |
---|
[231] | 520 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 521 | uList[i].name = std::string(buffer); |
---|
[228] | 522 | |
---|
[218] | 523 | memset(buffer, 0, 256); |
---|
[231] | 524 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 525 | uList[i].units = std::string(buffer); |
---|
| 526 | |
---|
| 527 | memset(buffer, 0, 256); |
---|
[231] | 528 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 529 | uList[i].goal = std::string(buffer); |
---|
| 530 | |
---|
| 531 | memset(buffer, 0, 256); |
---|
[231] | 532 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 533 | uList[i].target = atof(buffer); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | /* Receive Set of Parameters */ |
---|
| 537 | memset(buffer, 0, 256); |
---|
| 538 | ReadMessage(commandSocketFD, buffer); |
---|
| 539 | radioInfo->numParameters = atoi(buffer); |
---|
| 540 | |
---|
| 541 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 542 | memset(buffer, 0, 256); |
---|
| 543 | ReadMessage(commandSocketFD, buffer); |
---|
| 544 | pList[i].name = std::string(buffer); |
---|
| 545 | |
---|
| 546 | memset(buffer, 0, 256); |
---|
| 547 | ReadMessage(commandSocketFD, buffer); |
---|
| 548 | pList[i].units = std::string(buffer); |
---|
| 549 | |
---|
| 550 | memset(buffer, 0, 256); |
---|
| 551 | ReadMessage(commandSocketFD, buffer); |
---|
| 552 | pList[i].min = atof(buffer); |
---|
| 553 | |
---|
| 554 | memset(buffer, 0, 256); |
---|
| 555 | ReadMessage(commandSocketFD, buffer); |
---|
| 556 | pList[i].max = atof(buffer); |
---|
| 557 | |
---|
| 558 | memset(buffer, 0, 256); |
---|
| 559 | ReadMessage(commandSocketFD, buffer); |
---|
| 560 | pList[i].step = atof(buffer); |
---|
| 561 | |
---|
| 562 | memset(buffer, 0, 256); |
---|
| 563 | ReadMessage(commandSocketFD,buffer); |
---|
| 564 | pList[i].numAffects = atoi(buffer); |
---|
| 565 | |
---|
[224] | 566 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
[218] | 567 | memset(buffer, 0, 256); |
---|
| 568 | ReadMessage(commandSocketFD,buffer); |
---|
[231] | 569 | // TODO for + if{break} = while? |
---|
[224] | 570 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
[231] | 571 | if(uList[k].name == std::string(buffer)) { |
---|
[218] | 572 | pList[i].affection_list[j].u = &uList[k]; |
---|
| 573 | break; |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | memset(buffer, 0, 256); |
---|
[231] | 578 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 579 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | /* Receive Set of Observables */ |
---|
| 584 | memset(buffer, 0, 256); |
---|
[231] | 585 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 586 | radioInfo->numObservables = atoi(buffer); |
---|
| 587 | |
---|
[224] | 588 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
[218] | 589 | memset(buffer, 0, 256); |
---|
[231] | 590 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 591 | oList[i].name = std::string(buffer); |
---|
| 592 | |
---|
| 593 | memset(buffer, 0, 256); |
---|
[231] | 594 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 595 | oList[i].numAffects = atoi(buffer); |
---|
| 596 | |
---|
[224] | 597 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
[218] | 598 | memset(buffer, 0, 256); |
---|
[231] | 599 | ReadMessage(commandSocketFD, buffer); |
---|
| 600 | // TODO for + if{break} = while? |
---|
[224] | 601 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
[231] | 602 | if(uList[k].name == std::string(buffer)){ |
---|
[218] | 603 | oList[i].affection_list[j].u = &uList[k]; |
---|
| 604 | break; |
---|
| 605 | } |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | memset(buffer, 0, 256); |
---|
[231] | 609 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 610 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
[228] | 615 | |
---|
[231] | 616 | BuildCognitiveEngine(); |
---|
[218] | 617 | } |
---|
| 618 | |
---|
| 619 | void |
---|
| 620 | CognitiveEngine::ReceiveExperience() |
---|
| 621 | { |
---|
| 622 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
| 623 | char buffer[256]; |
---|
| 624 | uint32_t numberExp; |
---|
| 625 | |
---|
| 626 | /* Receive number of experience entries */ |
---|
| 627 | memset(buffer, 0, 256); |
---|
[231] | 628 | ReadMessage(commandSocketFD, buffer); |
---|
[218] | 629 | numberExp = atoi(buffer); |
---|
| 630 | |
---|
[231] | 631 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
[218] | 632 | |
---|
| 633 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
| 634 | } |
---|
| 635 | |
---|
[228] | 636 | Parameter* |
---|
| 637 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
[218] | 638 | { |
---|
| 639 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
[228] | 640 | |
---|
| 641 | char *searchNames[radioInfo->numUtilities]; |
---|
| 642 | |
---|
| 643 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[230] | 644 | searchNames[i] = (char*)observables[i].name.c_str(); |
---|
[231] | 645 | } |
---|
[228] | 646 | |
---|
| 647 | float searchVals[radioInfo->numUtilities]; |
---|
| 648 | |
---|
[231] | 649 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 650 | searchVals[i] = uList[i].target; |
---|
[228] | 651 | } |
---|
| 652 | |
---|
| 653 | uint32_t numberColumns = |
---|
[231] | 654 | radioInfo->numUtilities + |
---|
| 655 | radioInfo->numParameters + |
---|
| 656 | radioInfo->numObservables + 1; |
---|
| 657 | |
---|
| 658 | float returnValues[numberColumns]; |
---|
| 659 | |
---|
| 660 | int searchOps[radioInfo->numUtilities]; |
---|
[228] | 661 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 662 | |
---|
| 663 | /* If the goal is to maximum, set the search operation to |
---|
[231] | 664 | * return values greater than the target. |
---|
| 665 | * |
---|
| 666 | * If the goal is to minimize, set the search operation to |
---|
| 667 | * return values less than the target. |
---|
| 668 | */ |
---|
| 669 | if(strcmp(uList[i].goal.c_str(), "max") == 0) { |
---|
| 670 | searchOps[i] = GT; |
---|
| 671 | } else if(strcmp(uList[i].goal.c_str(), "min") == 0) { |
---|
| 672 | searchOps[i] = LT; |
---|
| 673 | } |
---|
| 674 | } |
---|
[228] | 675 | |
---|
[231] | 676 | /* CBR specific call */ |
---|
| 677 | uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, |
---|
| 678 | radioInfo->numUtilities, returnValues); |
---|
[228] | 679 | |
---|
[231] | 680 | if(rc == 0){ |
---|
[228] | 681 | /* Adapt the returned parameters to meet the objective */ |
---|
[230] | 682 | WARNING("Cognitive Engine:: Found\n"); |
---|
[228] | 683 | |
---|
[231] | 684 | /* Should do a random adaptation.. */ |
---|
| 685 | if(returnValues[numberColumns-1] < 0) { |
---|
| 686 | returnValues[2] = returnValues[2] - 15; |
---|
| 687 | returnValues[3] = returnValues[3] - 2; |
---|
[230] | 688 | } else { |
---|
[231] | 689 | returnValues[2] = returnValues[2] + 15; |
---|
| 690 | returnValues[3] = returnValues[3] + 2; |
---|
| 691 | } |
---|
| 692 | } else if(rc == 31337) { |
---|
[230] | 693 | WARNING("Cognitive Engine:: Not Found.\n"); |
---|
[231] | 694 | /* No rows in the CBR, pick default parameters */ |
---|
| 695 | /* Currently this is hard coded and implementation specific! */ |
---|
| 696 | returnValues[2] = currentParameters[0].value + 5; |
---|
| 697 | returnValues[3] = currentParameters[1].value + 10; |
---|
[228] | 698 | |
---|
[231] | 699 | } else { |
---|
[228] | 700 | WARNING("Cognitive Engine:: Search return an invalid value.\n"); |
---|
[231] | 701 | } |
---|
[228] | 702 | |
---|
[231] | 703 | size_t returnValueIndex = 0; |
---|
[228] | 704 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[231] | 705 | uList[i].value = returnValues[returnValueIndex]; |
---|
| 706 | returnValueIndex++; |
---|
| 707 | } |
---|
[228] | 708 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
[231] | 709 | pList[i].value = returnValues[returnValueIndex]; |
---|
| 710 | returnValueIndex++; |
---|
| 711 | } |
---|
[228] | 712 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
[231] | 713 | oList[i].value = returnValues[returnValueIndex]; |
---|
| 714 | returnValueIndex++; |
---|
| 715 | } |
---|
[230] | 716 | returnValues[returnValueIndex] = 0; |
---|
[228] | 717 | |
---|
[230] | 718 | char *allNames[numberColumns]; |
---|
[231] | 719 | size_t allNameIndex = 0; |
---|
[230] | 720 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 721 | allNames[allNameIndex] = (char*)uList[i].name.c_str(); |
---|
[231] | 722 | returnValues[allNameIndex] = uList[i].target; |
---|
| 723 | allNameIndex++; |
---|
| 724 | } |
---|
[230] | 725 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 726 | allNames[allNameIndex] = (char*)pList[i].name.c_str(); |
---|
[231] | 727 | allNameIndex++; |
---|
| 728 | } |
---|
[230] | 729 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
| 730 | allNames[allNameIndex] = (char*)oList[i].name.c_str(); |
---|
[231] | 731 | returnValues[allNameIndex] = 0; |
---|
| 732 | allNameIndex++; |
---|
| 733 | } |
---|
[230] | 734 | allNames[allNameIndex] = "utility"; |
---|
| 735 | |
---|
[231] | 736 | // Add row to CBR. |
---|
| 737 | cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); |
---|
[228] | 738 | |
---|
[231] | 739 | return pList; |
---|
[218] | 740 | } |
---|
| 741 | |
---|
[228] | 742 | |
---|
| 743 | Parameter* |
---|
[231] | 744 | CognitiveEngine::GetSolution(Observable *observables, \ |
---|
| 745 | Parameter *currentParameters, std::string service) |
---|
[218] | 746 | { |
---|
[231] | 747 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
[228] | 748 | |
---|
| 749 | return pList; |
---|
[218] | 750 | } |
---|
| 751 | |
---|
[228] | 752 | |
---|
[218] | 753 | void |
---|
| 754 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
[230] | 755 | std::string service) |
---|
[218] | 756 | { |
---|
| 757 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
| 758 | } |
---|
| 759 | |
---|
[228] | 760 | |
---|
| 761 | void |
---|
| 762 | CognitiveEngine::BuildCognitiveEngine() |
---|
| 763 | { |
---|
[231] | 764 | char filename[] = {"ex1"}; |
---|
| 765 | char tablename[] = {"data"}; |
---|
[228] | 766 | |
---|
| 767 | uint32_t numberColumns = |
---|
[231] | 768 | radioInfo->numUtilities + |
---|
| 769 | radioInfo->numParameters + |
---|
| 770 | radioInfo->numObservables + 1; |
---|
[228] | 771 | |
---|
| 772 | char *cols[numberColumns]; |
---|
| 773 | |
---|
| 774 | size_t columnIndex = 0; |
---|
| 775 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
[231] | 776 | cols[columnIndex] = (char*)uList[i].name.c_str(); |
---|
[228] | 777 | columnIndex++; |
---|
| 778 | } |
---|
| 779 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
[231] | 780 | cols[columnIndex] = (char*)pList[i].name.c_str(); |
---|
[228] | 781 | columnIndex++; |
---|
| 782 | } |
---|
| 783 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
[231] | 784 | cols[columnIndex] = (char*)oList[i].name.c_str(); |
---|
[228] | 785 | columnIndex++; |
---|
| 786 | } |
---|
| 787 | cols[columnIndex] = "utility"; |
---|
| 788 | |
---|
| 789 | myCBR = cbr_create(filename, tablename, cols, numberColumns); |
---|
| 790 | } |
---|
| 791 | |
---|