[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]; |
---|
[511] | 167 | |
---|
| 168 | |
---|
| 169 | ////////////////////////////////////////////// |
---|
| 170 | // |
---|
| 171 | // BEGIN CORE DSA ALGORITHM UTILITY FUNCTION |
---|
| 172 | // |
---|
| 173 | ////////////////////////////////////////////// |
---|
| 174 | |
---|
[391] | 175 | // Set DSA utility to take into account both the previously sensed |
---|
| 176 | // energy and the average communication time. |
---|
[375] | 177 | |
---|
[392] | 178 | float newUtilityValue = oldUtilityValue + observables[COMMUNICATION_TIME].value; |
---|
[391] | 179 | |
---|
| 180 | // If communication time value is set, we know we need to change channels because of PU. |
---|
| 181 | // So we should lower the utility for this channel. |
---|
| 182 | |
---|
[511] | 183 | int reward = 20; |
---|
| 184 | int punishment = -100; |
---|
| 185 | |
---|
[392] | 186 | if((observables[COMMUNICATION_TIME].value != 0) || (observables[ENERGY].value > 1000)) { |
---|
[512] | 187 | printf("Cognitive Engine:: Primary User caused channel change - Decrementing Utility. %f %f",observables[COMMUNICATION_TIME].value,observables[ENERGY].value); |
---|
[511] | 188 | newUtilityValue = newUtilityValue + punishment; |
---|
[391] | 189 | } else { |
---|
[511] | 190 | printf("Cognitive Engine:: Scan Mode:: No PU detected - Incrementing Utility."); |
---|
| 191 | newUtilityValue = newUtilityValue + reward; |
---|
[375] | 192 | } |
---|
[391] | 193 | |
---|
[392] | 194 | if(newUtilityValue <= 100) |
---|
| 195 | newUtilityValue = 100; |
---|
[391] | 196 | |
---|
[375] | 197 | obsVals[obsValueIndex] = newUtilityValue; |
---|
| 198 | |
---|
[511] | 199 | ////////////////////////////////////////////// |
---|
| 200 | // |
---|
| 201 | // END CORE DSA ALGORITHM UTILITY FUNCTION |
---|
| 202 | // |
---|
| 203 | ////////////////////////////////////////////// |
---|
| 204 | |
---|
| 205 | |
---|
[375] | 206 | size_t returnValueIndex = 0; |
---|
| 207 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 208 | valList[returnValueIndex] = parameters[i].value; |
---|
| 209 | returnValueIndex++; |
---|
| 210 | } |
---|
| 211 | |
---|
[469] | 212 | myCBR->Update(nameList, obsList, valList, obsVals, |
---|
[375] | 213 | numberColumns, obsColumns); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | void |
---|
| 218 | CognitiveEngine::WaitForSignal() |
---|
| 219 | { |
---|
| 220 | char buffer[256]; |
---|
| 221 | |
---|
| 222 | while(true) { |
---|
| 223 | memset(buffer, 0, 256); |
---|
| 224 | |
---|
| 225 | ReadMessage(commandSocketFD, buffer); |
---|
| 226 | |
---|
| 227 | // TODO this is ugly... is there a better way? Doesn't strcmp compare the |
---|
| 228 | // whole string? We only need to compare until we find a single different |
---|
| 229 | // byte... |
---|
| 230 | // |
---|
| 231 | // If we send integer op codes rather than strings, this process will be |
---|
| 232 | // MUCH faster since instead of donig string compares we can simply |
---|
| 233 | // switch on the integer value... |
---|
| 234 | if(strcmp(buffer, "update_performance") == 0) { |
---|
| 235 | |
---|
| 236 | /* Receive Set of current Parameters */ |
---|
| 237 | memset(buffer, 0, 256); |
---|
| 238 | ReadMessage(commandSocketFD, buffer); |
---|
| 239 | uint32_t numParameters = atoi(buffer); |
---|
| 240 | |
---|
| 241 | Parameter *p = new Parameter[numParameters]; |
---|
| 242 | |
---|
| 243 | for(size_t i = 0; i < numParameters; i++) { |
---|
| 244 | memset(buffer, 0, 256); |
---|
| 245 | ReadMessage(commandSocketFD, buffer); |
---|
| 246 | p[i].name = std::string(buffer); |
---|
| 247 | |
---|
| 248 | memset(buffer, 0, 256); |
---|
| 249 | ReadMessage(commandSocketFD, buffer); |
---|
| 250 | p[i].value = atof(buffer); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /* Receive Set of Observables */ |
---|
| 254 | memset(buffer, 0, 256); |
---|
| 255 | ReadMessage(commandSocketFD, buffer); |
---|
| 256 | uint32_t numObservables = atoi(buffer); |
---|
| 257 | |
---|
| 258 | Observable *o = new Observable[numObservables]; |
---|
| 259 | |
---|
| 260 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 261 | memset(buffer, 0, 256); |
---|
| 262 | ReadMessage(commandSocketFD, buffer); |
---|
| 263 | o[i].name = std::string(buffer); |
---|
| 264 | |
---|
| 265 | memset(buffer, 0, 256); |
---|
| 266 | ReadMessage(commandSocketFD, buffer); |
---|
| 267 | o[i].value = atof(buffer); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | ReceiveFeedback(o,p); |
---|
| 271 | |
---|
| 272 | delete [] o; |
---|
| 273 | delete [] p; |
---|
| 274 | } |
---|
| 275 | else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
| 276 | // THIS IS CURRENTLY IN DEMO MODE |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | /* Receive Set of Observables */ |
---|
| 280 | LOG("\nCognitive Engine:: Receiving service name\n"); |
---|
| 281 | |
---|
| 282 | memset(buffer, 0, 256); |
---|
| 283 | ReadMessage(commandSocketFD,buffer); |
---|
| 284 | LOG("\nCognitive Engine:: Got service name, %s\n", buffer); |
---|
| 285 | |
---|
| 286 | /* Receive Set of Observables */ |
---|
| 287 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
| 288 | |
---|
| 289 | memset(buffer, 0, 256); |
---|
| 290 | ReadMessage(commandSocketFD,buffer); |
---|
| 291 | uint32_t numObservables = atoi(buffer); |
---|
| 292 | |
---|
| 293 | Observable *o = new Observable[numObservables]; |
---|
| 294 | |
---|
| 295 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 296 | memset(buffer, 0, 256); |
---|
| 297 | ReadMessage(commandSocketFD, buffer); |
---|
| 298 | o[i].name = std::string(buffer); |
---|
| 299 | |
---|
| 300 | memset(buffer, 0, 256); |
---|
| 301 | ReadMessage(commandSocketFD, buffer); |
---|
| 302 | o[i].value = atof(buffer); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /* Receive Set of current Parameters */ |
---|
| 306 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
| 307 | |
---|
| 308 | memset(buffer, 0, 256); |
---|
| 309 | ReadMessage(commandSocketFD, buffer); |
---|
| 310 | uint32_t numCurrentParameters = atoi(buffer); |
---|
| 311 | |
---|
| 312 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
| 313 | |
---|
| 314 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
| 315 | memset(buffer, 0, 256); |
---|
| 316 | ReadMessage(commandSocketFD, buffer); |
---|
| 317 | cp[i].name = std::string(buffer); |
---|
| 318 | |
---|
| 319 | memset(buffer, 0, 256); |
---|
| 320 | ReadMessage(commandSocketFD, buffer); |
---|
| 321 | cp[i].value = atof(buffer); |
---|
| 322 | } |
---|
| 323 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
| 324 | |
---|
| 325 | // TODO need to actually do something with the observables here |
---|
| 326 | |
---|
| 327 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
| 328 | char numParametersChar[10]; |
---|
| 329 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
| 330 | SendMessage(commandSocketFD, numParametersChar); |
---|
| 331 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 332 | SendMessage(commandSocketFD, "test"); |
---|
| 333 | SendMessage(commandSocketFD, "00"); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | delete [] o; |
---|
| 337 | delete [] cp; |
---|
| 338 | } |
---|
| 339 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
| 340 | |
---|
| 341 | /* Receive Set of Observables */ |
---|
| 342 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
| 343 | |
---|
| 344 | memset(buffer, 0, 256); |
---|
| 345 | ReadMessage(commandSocketFD,buffer); |
---|
| 346 | uint32_t numObservables = atoi(buffer); |
---|
| 347 | |
---|
| 348 | Observable *o = new Observable[numObservables]; |
---|
| 349 | |
---|
| 350 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 351 | memset(buffer, 0, 256); |
---|
| 352 | ReadMessage(commandSocketFD, buffer); |
---|
| 353 | o[i].name = std::string(buffer); |
---|
| 354 | |
---|
| 355 | memset(buffer, 0, 256); |
---|
| 356 | ReadMessage(commandSocketFD, buffer); |
---|
| 357 | o[i].value = atof(buffer); |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | /* Receive Set of current Parameters */ |
---|
| 361 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
| 362 | |
---|
| 363 | memset(buffer, 0, 256); |
---|
| 364 | ReadMessage(commandSocketFD, buffer); |
---|
| 365 | uint32_t numCurrentParameters = atoi(buffer); |
---|
| 366 | |
---|
| 367 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
| 368 | |
---|
| 369 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
| 370 | memset(buffer, 0, 256); |
---|
| 371 | ReadMessage(commandSocketFD, buffer); |
---|
| 372 | cp[i].name = std::string(buffer); |
---|
| 373 | |
---|
| 374 | memset(buffer, 0, 256); |
---|
| 375 | ReadMessage(commandSocketFD, buffer); |
---|
| 376 | cp[i].value = atof(buffer); |
---|
| 377 | } |
---|
| 378 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
| 379 | |
---|
| 380 | Parameter *solutionSet; |
---|
| 381 | |
---|
| 382 | solutionSet = GetSolution(o,cp); |
---|
| 383 | |
---|
| 384 | // TODO need to actually do something with the observables here |
---|
| 385 | |
---|
| 386 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
| 387 | char numParametersChar[10]; |
---|
| 388 | char solutionValue[50]; |
---|
| 389 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
| 390 | SendMessage(commandSocketFD, numParametersChar); |
---|
| 391 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 392 | SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
| 393 | memset(solutionValue, 0, 50); |
---|
| 394 | sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
| 395 | SendMessage(commandSocketFD, solutionValue); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | delete [] o; |
---|
| 399 | delete [] cp; |
---|
| 400 | } |
---|
| 401 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
| 402 | SendComponentType(); |
---|
| 403 | } |
---|
| 404 | else if(strcmp(buffer, "connect_sml") == 0) { |
---|
| 405 | /* This command implies that we are disconnecting from the shell and |
---|
| 406 | * connecting to a SML component. */ |
---|
| 407 | char serverName[256]; |
---|
| 408 | char serverPort[256]; |
---|
| 409 | // TODO is this going to end up being too slow? |
---|
| 410 | memset(serverName, 0, 256); |
---|
| 411 | memset(serverPort, 0, 256); |
---|
| 412 | |
---|
| 413 | ReadMessage(commandSocketFD, serverName); |
---|
| 414 | ReadMessage(commandSocketFD, serverPort); |
---|
| 415 | |
---|
| 416 | /* Only continue if we are currently connected to a shell. */ |
---|
| 417 | if(!SML_present) { |
---|
| 418 | DeregisterComponent(); |
---|
| 419 | |
---|
| 420 | shutdown(commandSocketFD, 2); |
---|
| 421 | close(commandSocketFD); |
---|
| 422 | |
---|
| 423 | ConnectToRemoteComponent(serverName, serverPort, true); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | else if(strcmp(buffer, "disconnect_sml") == 0) { |
---|
| 427 | /* This command implies that we are disconnecting from the SML and |
---|
| 428 | * connecting to a shell component. */ |
---|
| 429 | char serverName[256]; |
---|
| 430 | char serverPort[256]; |
---|
| 431 | // TODO is this going to end up being too slow? |
---|
| 432 | memset(serverName, 0, 256); |
---|
| 433 | memset(serverPort, 0, 256); |
---|
| 434 | |
---|
| 435 | ReadMessage(commandSocketFD, serverName); |
---|
| 436 | ReadMessage(commandSocketFD, serverPort); |
---|
| 437 | |
---|
| 438 | /* We only want to do this if we are actually connected to an SML |
---|
| 439 | * currently. */ |
---|
| 440 | if(SML_present) { |
---|
| 441 | DeregisterServices(); |
---|
| 442 | |
---|
| 443 | shutdown(commandSocketFD, 2); |
---|
| 444 | close(commandSocketFD); |
---|
| 445 | |
---|
| 446 | ConnectToRemoteComponent(serverName, serverPort, false); |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
| 450 | Reset(); |
---|
| 451 | } |
---|
| 452 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
| 453 | Shutdown(); |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | |
---|
| 459 | void |
---|
| 460 | CognitiveEngine::Shutdown() |
---|
| 461 | { |
---|
| 462 | if(SML_present) { |
---|
| 463 | //DeregisterServices(); |
---|
| 464 | DeregisterComponent(); |
---|
| 465 | } |
---|
| 466 | else { |
---|
| 467 | DeregisterComponent(); |
---|
| 468 | } |
---|
| 469 | // TODO should something else be happening here? |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | |
---|
| 473 | void |
---|
| 474 | CognitiveEngine::Reset() |
---|
| 475 | { |
---|
| 476 | LOG("Resetting Cognitive Engine.\n"); |
---|
| 477 | |
---|
| 478 | if(SML_present) { |
---|
| 479 | DeregisterServices(); |
---|
| 480 | DeregisterComponent(); |
---|
| 481 | } |
---|
| 482 | else { |
---|
| 483 | DeregisterComponent(); |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | void |
---|
| 489 | CognitiveEngine::RegisterComponent() |
---|
| 490 | { |
---|
| 491 | char buffer[256]; |
---|
| 492 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
| 493 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
| 494 | |
---|
| 495 | memset(buffer, 0, 256); |
---|
| 496 | ReadMessage(commandSocketFD, buffer); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | void |
---|
| 500 | CognitiveEngine::DeregisterComponent() |
---|
| 501 | { |
---|
| 502 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
| 503 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
| 504 | |
---|
| 505 | shutdown(commandSocketFD, 2); |
---|
| 506 | close(commandSocketFD); |
---|
| 507 | commandSocketFD = -1; |
---|
| 508 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | |
---|
| 512 | void |
---|
| 513 | CognitiveEngine::RegisterServices() |
---|
| 514 | { |
---|
| 515 | LOG("Cognitive Engine:: Registering services.\n"); |
---|
| 516 | |
---|
| 517 | SendMessage(commandSocketFD, "register_service"); |
---|
| 518 | SendMessage(commandSocketFD, "test_srv"); |
---|
| 519 | |
---|
| 520 | SendMessage(commandSocketFD, "register_service"); |
---|
| 521 | SendMessage(commandSocketFD, "test_srv1"); |
---|
| 522 | |
---|
| 523 | SendMessage(commandSocketFD, "register_service"); |
---|
| 524 | SendMessage(commandSocketFD, "test_srv2"); |
---|
| 525 | |
---|
| 526 | SendMessage(commandSocketFD, "register_service"); |
---|
| 527 | SendMessage(commandSocketFD, "test_srv3"); |
---|
| 528 | |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | //Combined with deregister component since those two things must happen togeather |
---|
| 532 | void |
---|
| 533 | CognitiveEngine::DeregisterServices() |
---|
| 534 | { |
---|
| 535 | LOG("Cognitive Engine:: Deregistering services.\n"); |
---|
| 536 | |
---|
| 537 | SendMessage(commandSocketFD, "deregister_service"); |
---|
| 538 | SendMessage(commandSocketFD, "test_srv"); |
---|
| 539 | |
---|
| 540 | SendMessage(commandSocketFD, "deregister_service"); |
---|
| 541 | SendMessage(commandSocketFD, "test_srv1"); |
---|
| 542 | |
---|
| 543 | SendMessage(commandSocketFD, "deregister_service"); |
---|
| 544 | SendMessage(commandSocketFD, "test_srv2"); |
---|
| 545 | |
---|
| 546 | SendMessage(commandSocketFD, "deregister_service"); |
---|
| 547 | SendMessage(commandSocketFD, "test_srv3"); |
---|
| 548 | |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | void |
---|
| 552 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
| 553 | { |
---|
| 554 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
| 555 | |
---|
| 556 | char buffer[256]; |
---|
| 557 | |
---|
| 558 | /* Receive Set of Utilities */ |
---|
| 559 | memset(buffer, 0, 256); |
---|
| 560 | ReadMessage(commandSocketFD, buffer); |
---|
| 561 | radioInfo->numUtilities = atoi(buffer); |
---|
| 562 | |
---|
| 563 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 564 | memset(buffer, 0, 256); |
---|
| 565 | ReadMessage(commandSocketFD, buffer); |
---|
| 566 | uList[i].name = std::string(buffer); |
---|
| 567 | |
---|
| 568 | memset(buffer, 0, 256); |
---|
| 569 | ReadMessage(commandSocketFD, buffer); |
---|
| 570 | uList[i].units = std::string(buffer); |
---|
| 571 | |
---|
| 572 | memset(buffer, 0, 256); |
---|
| 573 | ReadMessage(commandSocketFD, buffer); |
---|
| 574 | uList[i].goal = std::string(buffer); |
---|
| 575 | |
---|
| 576 | memset(buffer, 0, 256); |
---|
| 577 | ReadMessage(commandSocketFD, buffer); |
---|
| 578 | uList[i].target = atof(buffer); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /* Receive Set of Parameters */ |
---|
| 582 | memset(buffer, 0, 256); |
---|
| 583 | ReadMessage(commandSocketFD, buffer); |
---|
| 584 | radioInfo->numParameters = atoi(buffer); |
---|
| 585 | |
---|
| 586 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 587 | memset(buffer, 0, 256); |
---|
| 588 | ReadMessage(commandSocketFD, buffer); |
---|
| 589 | pList[i].name = std::string(buffer); |
---|
| 590 | |
---|
| 591 | memset(buffer, 0, 256); |
---|
| 592 | ReadMessage(commandSocketFD, buffer); |
---|
| 593 | pList[i].units = std::string(buffer); |
---|
| 594 | |
---|
| 595 | memset(buffer, 0, 256); |
---|
| 596 | ReadMessage(commandSocketFD, buffer); |
---|
| 597 | pList[i].min = atof(buffer); |
---|
| 598 | |
---|
| 599 | memset(buffer, 0, 256); |
---|
| 600 | ReadMessage(commandSocketFD, buffer); |
---|
| 601 | pList[i].max = atof(buffer); |
---|
| 602 | |
---|
| 603 | memset(buffer, 0, 256); |
---|
| 604 | ReadMessage(commandSocketFD, buffer); |
---|
| 605 | pList[i].step = atof(buffer); |
---|
| 606 | |
---|
| 607 | memset(buffer, 0, 256); |
---|
| 608 | ReadMessage(commandSocketFD,buffer); |
---|
| 609 | pList[i].numAffects = atoi(buffer); |
---|
| 610 | |
---|
| 611 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
| 612 | memset(buffer, 0, 256); |
---|
| 613 | ReadMessage(commandSocketFD,buffer); |
---|
| 614 | // TODO for + if{break} = while? |
---|
| 615 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
| 616 | if(uList[k].name == std::string(buffer)) { |
---|
| 617 | pList[i].affection_list[j].u = &uList[k]; |
---|
| 618 | break; |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | memset(buffer, 0, 256); |
---|
| 623 | ReadMessage(commandSocketFD, buffer); |
---|
| 624 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | /* Receive Set of Observables */ |
---|
| 629 | memset(buffer, 0, 256); |
---|
| 630 | ReadMessage(commandSocketFD, buffer); |
---|
| 631 | radioInfo->numObservables = atoi(buffer); |
---|
| 632 | |
---|
| 633 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
| 634 | memset(buffer, 0, 256); |
---|
| 635 | ReadMessage(commandSocketFD, buffer); |
---|
| 636 | oList[i].name = std::string(buffer); |
---|
| 637 | |
---|
| 638 | memset(buffer, 0, 256); |
---|
| 639 | ReadMessage(commandSocketFD, buffer); |
---|
| 640 | oList[i].numAffects = atoi(buffer); |
---|
| 641 | |
---|
| 642 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
| 643 | memset(buffer, 0, 256); |
---|
| 644 | ReadMessage(commandSocketFD, buffer); |
---|
| 645 | // TODO for + if{break} = while? |
---|
| 646 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
| 647 | if(uList[k].name == std::string(buffer)){ |
---|
| 648 | oList[i].affection_list[j].u = &uList[k]; |
---|
| 649 | break; |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | memset(buffer, 0, 256); |
---|
| 654 | ReadMessage(commandSocketFD, buffer); |
---|
| 655 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
| 660 | |
---|
| 661 | BuildCognitiveEngine(); |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | void |
---|
| 665 | CognitiveEngine::ReceiveExperience() |
---|
| 666 | { |
---|
| 667 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
| 668 | char buffer[256]; |
---|
| 669 | uint32_t numberExp; |
---|
| 670 | |
---|
| 671 | /* Receive number of experience entries */ |
---|
| 672 | memset(buffer, 0, 256); |
---|
| 673 | ReadMessage(commandSocketFD, buffer); |
---|
| 674 | numberExp = atoi(buffer); |
---|
| 675 | |
---|
| 676 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
| 677 | |
---|
| 678 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
| 679 | } |
---|
| 680 | |
---|
[389] | 681 | |
---|
| 682 | /* The core of the CE is this function */ |
---|
| 683 | |
---|
[375] | 684 | Parameter* |
---|
| 685 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
| 686 | { |
---|
| 687 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
| 688 | |
---|
[389] | 689 | /* Put together the CBR search array */ |
---|
[375] | 690 | |
---|
[391] | 691 | uint32_t channel = 0; |
---|
[475] | 692 | string searchNames[1]; |
---|
| 693 | string sumSearchName; |
---|
[390] | 694 | float searchVals[1]; |
---|
[391] | 695 | float utilArray[(int)pList[0].max]; |
---|
[390] | 696 | int searchOps[1]; |
---|
[475] | 697 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
| 698 | radioInfo->numObservables + 1; |
---|
[375] | 699 | |
---|
[389] | 700 | float returnValues[numberColumns]; |
---|
[391] | 701 | float sumRetVals[numberColumns]; |
---|
| 702 | |
---|
| 703 | // Total sum of utilities in sumRetVals[0] |
---|
| 704 | |
---|
[475] | 705 | string channel_name = "channel"; |
---|
| 706 | string utility_name = "utility"; |
---|
[391] | 707 | |
---|
[475] | 708 | for(int32_t i = 0 ; i < pList[0].max ; i++ ) { |
---|
| 709 | searchNames[0] = pList[0].name; |
---|
[469] | 710 | searchOps[0] = 0; |
---|
[391] | 711 | searchVals[0] = i+1; |
---|
[389] | 712 | |
---|
[469] | 713 | uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, |
---|
[390] | 714 | 1, returnValues); |
---|
[375] | 715 | |
---|
[391] | 716 | if(rc == 31337) { |
---|
| 717 | // No entry - must add |
---|
| 718 | |
---|
[475] | 719 | string rowNames[numberColumns]; |
---|
[391] | 720 | size_t rowIndex = 0; |
---|
| 721 | for(size_t j = 0; j < radioInfo->numUtilities; j++) { |
---|
[475] | 722 | rowNames[rowIndex] = uList[j].name; |
---|
[391] | 723 | rowIndex++; |
---|
| 724 | } |
---|
| 725 | for(size_t j = 0; j < radioInfo->numParameters; j++) { |
---|
[475] | 726 | rowNames[rowIndex] = pList[j].name; |
---|
[391] | 727 | if(pList[j].name == "channel") |
---|
| 728 | returnValues[rowIndex] = i+1; |
---|
| 729 | rowIndex++; |
---|
| 730 | } |
---|
| 731 | for(size_t j = 0; j < radioInfo->numObservables; j++) { |
---|
[475] | 732 | rowNames[rowIndex] = oList[j].name; |
---|
[391] | 733 | rowIndex++; |
---|
| 734 | } |
---|
| 735 | |
---|
[475] | 736 | rowNames[rowIndex] = utility_name; |
---|
[391] | 737 | returnValues[rowIndex] = 500; |
---|
[375] | 738 | |
---|
[391] | 739 | /* Add the new optimized set to the CBR database */ |
---|
[389] | 740 | |
---|
[469] | 741 | myCBR->AddRow(rowNames, returnValues, numberColumns); |
---|
[391] | 742 | } |
---|
| 743 | |
---|
| 744 | utilArray[i] = returnValues[UTILITY]; |
---|
[375] | 745 | } |
---|
| 746 | |
---|
[509] | 747 | printf("Current Channel Utility Scores\n"); |
---|
| 748 | printf("1: %f\t2: %f\t3: %f\t4: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]); |
---|
[391] | 749 | // Get sum of all the channel utilities. |
---|
[475] | 750 | sumSearchName = utility_name; |
---|
[469] | 751 | uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals); |
---|
[389] | 752 | |
---|
[391] | 753 | // Psuedo random channel selection based upon utility. |
---|
| 754 | int k = rand() % (int)sumRetVals[0]; |
---|
| 755 | int cdf_total(0); |
---|
| 756 | |
---|
| 757 | for ( int i = 0; i < pList[0].max; i++ ) { |
---|
| 758 | cdf_total += utilArray[i]; |
---|
| 759 | if(k < cdf_total) { |
---|
| 760 | channel = i + 1; |
---|
| 761 | break; |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | |
---|
[475] | 765 | searchNames[0] = pList[0].name; |
---|
[469] | 766 | searchOps[0] = 0; |
---|
[391] | 767 | searchVals[0] = channel; |
---|
| 768 | |
---|
[469] | 769 | rc = myCBR->Search(searchNames, searchOps, searchVals, |
---|
[391] | 770 | 1, returnValues); |
---|
| 771 | |
---|
| 772 | |
---|
| 773 | //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; |
---|
| 774 | returnValues[CHANNEL] = channel; |
---|
[509] | 775 | |
---|
| 776 | printf("Cognitive Engine:: ..---===***### Channel %i has been selected ###***===---..\n",channel); |
---|
[389] | 777 | /* Package up the new set of parameters in order to add |
---|
| 778 | the new entry into the CBR database. */ |
---|
| 779 | |
---|
[375] | 780 | size_t returnValueIndex = 0; |
---|
| 781 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 782 | uList[i].value = returnValues[returnValueIndex]; |
---|
| 783 | returnValueIndex++; |
---|
| 784 | } |
---|
| 785 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 786 | pList[i].value = returnValues[returnValueIndex]; |
---|
| 787 | returnValueIndex++; |
---|
| 788 | } |
---|
| 789 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
| 790 | oList[i].value = returnValues[returnValueIndex]; |
---|
| 791 | returnValueIndex++; |
---|
| 792 | } |
---|
[391] | 793 | //returnValues[returnValueIndex] = 0; |
---|
[375] | 794 | |
---|
[475] | 795 | string allNames[numberColumns]; |
---|
[375] | 796 | size_t allNameIndex = 0; |
---|
| 797 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
[475] | 798 | allNames[allNameIndex] = uList[i].name; |
---|
[375] | 799 | returnValues[allNameIndex] = uList[i].target; |
---|
| 800 | allNameIndex++; |
---|
| 801 | } |
---|
| 802 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
[475] | 803 | allNames[allNameIndex] = pList[i].name; |
---|
[375] | 804 | allNameIndex++; |
---|
| 805 | } |
---|
| 806 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
[475] | 807 | allNames[allNameIndex] = oList[i].name; |
---|
[391] | 808 | // returnValues[allNameIndex] = 0; |
---|
[375] | 809 | allNameIndex++; |
---|
| 810 | } |
---|
[391] | 811 | |
---|
[475] | 812 | allNames[allNameIndex] = utility_name; |
---|
[375] | 813 | |
---|
[389] | 814 | /* Add the new optimized set to the CBR database */ |
---|
[508] | 815 | //myCBR->AddRow(allNames, returnValues, returnValueIndex+1); |
---|
[375] | 816 | |
---|
[391] | 817 | |
---|
[389] | 818 | /* Return the set of new parameter values. */ |
---|
[375] | 819 | return pList; |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | |
---|
| 823 | Parameter* |
---|
| 824 | CognitiveEngine::GetSolution(Observable *observables, \ |
---|
| 825 | Parameter *currentParameters, std::string service) |
---|
| 826 | { |
---|
| 827 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
| 828 | |
---|
| 829 | return pList; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | |
---|
| 833 | void |
---|
| 834 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
| 835 | std::string service) |
---|
| 836 | { |
---|
| 837 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | |
---|
| 841 | void |
---|
| 842 | CognitiveEngine::BuildCognitiveEngine() |
---|
| 843 | { |
---|
[475] | 844 | string filename = "ex1"; |
---|
| 845 | string tablename = "data"; |
---|
[375] | 846 | |
---|
[475] | 847 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
| 848 | radioInfo->numObservables + 1; |
---|
[375] | 849 | |
---|
[475] | 850 | string cols[numberColumns]; |
---|
[375] | 851 | |
---|
| 852 | size_t columnIndex = 0; |
---|
| 853 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
[475] | 854 | cols[columnIndex] = uList[i].name; |
---|
[375] | 855 | columnIndex++; |
---|
| 856 | } |
---|
[391] | 857 | |
---|
[475] | 858 | string paramCols[radioInfo->numParameters]; |
---|
[391] | 859 | size_t paramColumnIndex = 0; |
---|
| 860 | // Also need to make parameters the unique key |
---|
[375] | 861 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
[475] | 862 | cols[columnIndex] = pList[i].name; |
---|
| 863 | paramCols[paramColumnIndex] = pList[i].name; |
---|
[375] | 864 | columnIndex++; |
---|
[391] | 865 | paramColumnIndex++; |
---|
| 866 | } |
---|
| 867 | |
---|
[375] | 868 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
[475] | 869 | cols[columnIndex] = oList[i].name; |
---|
[375] | 870 | columnIndex++; |
---|
| 871 | } |
---|
[391] | 872 | |
---|
| 873 | std::string utility_name = "utility"; |
---|
[475] | 874 | cols[columnIndex] = utility_name; |
---|
[375] | 875 | |
---|
[469] | 876 | myCBR = new CBR(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); |
---|
[375] | 877 | } |
---|
| 878 | |
---|