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