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