[531] | 1 | /* |
---|
| 2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
| 3 | |
---|
| 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 |
---|
| 9 | |
---|
| 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 | */ |
---|
| 16 | |
---|
| 17 | /*! This file provides a default implementation for a cognitive engine. Note |
---|
| 18 | * that this implementation is very basic, and is meant to act as a non-abstract |
---|
| 19 | * parent class. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <cstdlib> |
---|
| 23 | #include <cstring> |
---|
| 24 | #include <stdint.h> |
---|
| 25 | #include <cmath> |
---|
| 26 | #include <string> |
---|
| 27 | |
---|
| 28 | #include "vtcross/cbr.h" |
---|
| 29 | #include "vtcross/cognitive_engine.h" |
---|
| 30 | #include "vtcross/common.h" |
---|
| 31 | #include "vtcross/containers.h" |
---|
| 32 | #include "vtcross/debug.h" |
---|
| 33 | #include "vtcross/error.h" |
---|
| 34 | #include "vtcross/socketcomm.h" |
---|
| 35 | |
---|
| 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | CognitiveEngine::CognitiveEngine() |
---|
| 40 | { |
---|
| 41 | LOG("Creating Cognitive Engine.\n"); |
---|
| 42 | SML_present = false; |
---|
| 43 | commandSocketFD = -1; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | CognitiveEngine::~CognitiveEngine() |
---|
| 48 | { |
---|
| 49 | delete [] pList; |
---|
| 50 | delete [] oList; |
---|
| 51 | delete [] uList; |
---|
| 52 | |
---|
| 53 | delete [] radioInfo; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \ |
---|
| 58 | const int32_t numFields, const bool SML) |
---|
| 59 | { |
---|
| 60 | LOG("Creating Cognitive Engine.\n"); |
---|
| 61 | |
---|
| 62 | pList = new Parameter[numFields]; |
---|
| 63 | oList = new Observable[numFields]; |
---|
| 64 | uList = new Utility[numFields]; |
---|
| 65 | |
---|
| 66 | radioInfo = new Radio_Info; |
---|
| 67 | |
---|
| 68 | ConnectToRemoteComponent(serverName, serverPort, SML); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | void |
---|
| 73 | CognitiveEngine::SendComponentType() |
---|
| 74 | { |
---|
| 75 | SendMessage(commandSocketFD, "response_engine_cognitive"); |
---|
| 76 | LOG("Cognitive Engine responded to GetRemoteComponentType query.\n"); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | void |
---|
| 81 | CognitiveEngine::ConnectToRemoteComponent(const char *serverName, \ |
---|
| 82 | const char *serverPort, const bool SML) |
---|
| 83 | { |
---|
| 84 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
| 85 | |
---|
| 86 | SML_present = SML; |
---|
| 87 | |
---|
| 88 | if(SML) { |
---|
| 89 | LOG("Cognitive Engine connected to SML at %s.\n", serverName); |
---|
| 90 | |
---|
| 91 | RegisterComponent(); |
---|
| 92 | ReceiveRadioConfiguration(); |
---|
| 93 | ReceiveExperience(); |
---|
| 94 | RegisterServices(); |
---|
| 95 | } |
---|
| 96 | else { |
---|
| 97 | LOG("Cognitive Engine connected to shell at %s.\n", serverName); |
---|
| 98 | |
---|
| 99 | RegisterComponent(); |
---|
| 100 | ReceiveRadioConfiguration(); |
---|
| 101 | ReceiveExperience(); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void |
---|
| 106 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters) |
---|
| 107 | { |
---|
| 108 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
| 109 | |
---|
| 110 | uint32_t numberColumns = radioInfo->numParameters + radioInfo->numUtilities; |
---|
| 111 | |
---|
| 112 | uint32_t obsColumns = radioInfo->numObservables + 1; |
---|
| 113 | |
---|
| 114 | float valList[numberColumns]; |
---|
| 115 | float obsVals[numberColumns]; |
---|
| 116 | string nameList[numberColumns]; |
---|
| 117 | string obsList[obsColumns]; |
---|
| 118 | |
---|
| 119 | size_t columnObsIndex = 0; |
---|
| 120 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
| 121 | obsList[columnObsIndex] = observables[i].name; |
---|
| 122 | columnObsIndex++; |
---|
| 123 | } |
---|
| 124 | obsList[columnObsIndex] = "utility"; |
---|
| 125 | |
---|
| 126 | size_t columnIndex = 0; |
---|
| 127 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
| 128 | nameList[columnIndex] = parameters[i].name; |
---|
| 129 | columnIndex++; |
---|
| 130 | } |
---|
| 131 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
| 132 | nameList[columnIndex] = uList[i].name; |
---|
| 133 | columnIndex++; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | size_t obsValueIndex = 0; |
---|
| 137 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
| 138 | obsVals[obsValueIndex] = observables[i].value; |
---|
| 139 | obsValueIndex++; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* Calculate Utility */ |
---|
| 143 | float newUtilityValue = 0; |
---|
| 144 | |
---|
| 145 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 146 | newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); |
---|
| 147 | } |
---|
| 148 | obsVals[obsValueIndex] = newUtilityValue; |
---|
| 149 | |
---|
| 150 | size_t returnValueIndex = 0; |
---|
| 151 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 152 | valList[returnValueIndex] = parameters[i].value; |
---|
| 153 | returnValueIndex++; |
---|
| 154 | } |
---|
| 155 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 156 | valList[returnValueIndex] = uList[i].target; |
---|
| 157 | returnValueIndex++; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | void |
---|
| 163 | CognitiveEngine::WaitForSignal() |
---|
| 164 | { |
---|
| 165 | char buffer[256]; |
---|
| 166 | |
---|
| 167 | while(true) { |
---|
| 168 | memset(buffer, 0, 256); |
---|
| 169 | |
---|
| 170 | ReadMessage(commandSocketFD, buffer); |
---|
| 171 | |
---|
| 172 | if(strcmp(buffer, "update_performance") == 0) { |
---|
| 173 | PerformUpdatePerformance(); |
---|
| 174 | } |
---|
| 175 | else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
| 176 | PerformRequestOptimizationService(); |
---|
| 177 | } |
---|
| 178 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
| 179 | PerformRequestOptimization(); |
---|
| 180 | } |
---|
| 181 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
| 182 | PerformQueryComponentType(); |
---|
| 183 | } |
---|
| 184 | else if(strcmp(buffer, "connect_sml") == 0) { |
---|
| 185 | PerformConnectSML(); |
---|
| 186 | } |
---|
| 187 | else if(strcmp(buffer, "disconnect_sml") == 0) { |
---|
| 188 | PerformDisconnectSML(); |
---|
| 189 | } |
---|
| 190 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
| 191 | PerformResetEngineCognitive(); |
---|
| 192 | } |
---|
| 193 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
| 194 | PerformShutdownEngineCognitive(); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | void |
---|
| 201 | CognitiveEngine::Shutdown() |
---|
| 202 | { |
---|
| 203 | if(SML_present) { |
---|
| 204 | DeregisterServices(); |
---|
| 205 | DeregisterComponent(); |
---|
| 206 | } |
---|
| 207 | else { |
---|
| 208 | DeregisterComponent(); |
---|
| 209 | } |
---|
| 210 | // TODO Need to actually kill this process... |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | void |
---|
| 215 | CognitiveEngine::Reset() |
---|
| 216 | { |
---|
| 217 | LOG("Resetting Cognitive Engine.\n"); |
---|
| 218 | |
---|
| 219 | if(SML_present) { |
---|
| 220 | DeregisterServices(); |
---|
| 221 | DeregisterComponent(); |
---|
| 222 | } |
---|
| 223 | else { |
---|
| 224 | DeregisterComponent(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // TODO This function then needs to re-call "ConnectToRemoteComponent" to |
---|
| 228 | // finish the reset - otherwise, it is just a shutdown. |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | void |
---|
| 233 | CognitiveEngine::RegisterComponent() |
---|
| 234 | { |
---|
| 235 | char buffer[256]; |
---|
| 236 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
| 237 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
| 238 | |
---|
| 239 | memset(buffer, 0, 256); |
---|
| 240 | ReadMessage(commandSocketFD, buffer); |
---|
| 241 | |
---|
| 242 | // TODO Are we reading an ACK here? If so, why aren't we doing anything with |
---|
| 243 | // it? |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void |
---|
| 247 | CognitiveEngine::DeregisterComponent() |
---|
| 248 | { |
---|
| 249 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
| 250 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
| 251 | |
---|
| 252 | shutdown(commandSocketFD, 2); |
---|
| 253 | close(commandSocketFD); |
---|
| 254 | commandSocketFD = -1; |
---|
| 255 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | void |
---|
| 260 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
| 261 | { |
---|
| 262 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
| 263 | |
---|
| 264 | char buffer[256]; |
---|
| 265 | |
---|
| 266 | /* Receive Set of Utilities */ |
---|
| 267 | memset(buffer, 0, 256); |
---|
| 268 | ReadMessage(commandSocketFD, buffer); |
---|
| 269 | radioInfo->numUtilities = atoi(buffer); |
---|
| 270 | |
---|
| 271 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
| 272 | memset(buffer, 0, 256); |
---|
| 273 | ReadMessage(commandSocketFD, buffer); |
---|
| 274 | uList[i].name = std::string(buffer); |
---|
| 275 | |
---|
| 276 | memset(buffer, 0, 256); |
---|
| 277 | ReadMessage(commandSocketFD, buffer); |
---|
| 278 | uList[i].units = std::string(buffer); |
---|
| 279 | |
---|
| 280 | memset(buffer, 0, 256); |
---|
| 281 | ReadMessage(commandSocketFD, buffer); |
---|
| 282 | uList[i].goal = std::string(buffer); |
---|
| 283 | |
---|
| 284 | memset(buffer, 0, 256); |
---|
| 285 | ReadMessage(commandSocketFD, buffer); |
---|
| 286 | uList[i].target = atof(buffer); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /* Receive Set of Parameters */ |
---|
| 290 | memset(buffer, 0, 256); |
---|
| 291 | ReadMessage(commandSocketFD, buffer); |
---|
| 292 | radioInfo->numParameters = atoi(buffer); |
---|
| 293 | |
---|
| 294 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
| 295 | memset(buffer, 0, 256); |
---|
| 296 | ReadMessage(commandSocketFD, buffer); |
---|
| 297 | pList[i].name = std::string(buffer); |
---|
| 298 | |
---|
| 299 | memset(buffer, 0, 256); |
---|
| 300 | ReadMessage(commandSocketFD, buffer); |
---|
| 301 | pList[i].units = std::string(buffer); |
---|
| 302 | |
---|
| 303 | memset(buffer, 0, 256); |
---|
| 304 | ReadMessage(commandSocketFD, buffer); |
---|
| 305 | pList[i].min = atof(buffer); |
---|
| 306 | |
---|
| 307 | memset(buffer, 0, 256); |
---|
| 308 | ReadMessage(commandSocketFD, buffer); |
---|
| 309 | pList[i].max = atof(buffer); |
---|
| 310 | |
---|
| 311 | memset(buffer, 0, 256); |
---|
| 312 | ReadMessage(commandSocketFD, buffer); |
---|
| 313 | pList[i].step = atof(buffer); |
---|
| 314 | |
---|
| 315 | memset(buffer, 0, 256); |
---|
| 316 | ReadMessage(commandSocketFD,buffer); |
---|
| 317 | pList[i].numAffects = atoi(buffer); |
---|
| 318 | |
---|
| 319 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
| 320 | memset(buffer, 0, 256); |
---|
| 321 | ReadMessage(commandSocketFD,buffer); |
---|
| 322 | // TODO for + if{break} = while? |
---|
| 323 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
| 324 | if(uList[k].name == std::string(buffer)) { |
---|
| 325 | pList[i].affection_list[j].u = &uList[k]; |
---|
| 326 | break; |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | memset(buffer, 0, 256); |
---|
| 331 | ReadMessage(commandSocketFD, buffer); |
---|
| 332 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /* Receive Set of Observables */ |
---|
| 337 | memset(buffer, 0, 256); |
---|
| 338 | ReadMessage(commandSocketFD, buffer); |
---|
| 339 | radioInfo->numObservables = atoi(buffer); |
---|
| 340 | |
---|
| 341 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
| 342 | memset(buffer, 0, 256); |
---|
| 343 | ReadMessage(commandSocketFD, buffer); |
---|
| 344 | oList[i].name = std::string(buffer); |
---|
| 345 | |
---|
| 346 | memset(buffer, 0, 256); |
---|
| 347 | ReadMessage(commandSocketFD, buffer); |
---|
| 348 | oList[i].numAffects = atoi(buffer); |
---|
| 349 | |
---|
| 350 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
| 351 | memset(buffer, 0, 256); |
---|
| 352 | ReadMessage(commandSocketFD, buffer); |
---|
| 353 | // TODO for + if{break} = while? |
---|
| 354 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
| 355 | if(uList[k].name == std::string(buffer)){ |
---|
| 356 | oList[i].affection_list[j].u = &uList[k]; |
---|
| 357 | break; |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | memset(buffer, 0, 256); |
---|
| 362 | ReadMessage(commandSocketFD, buffer); |
---|
| 363 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
| 368 | |
---|
| 369 | BuildCognitiveEngine(); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | void |
---|
| 374 | CognitiveEngine::ReceiveExperience() |
---|
| 375 | { |
---|
| 376 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
| 377 | char buffer[256]; |
---|
| 378 | uint32_t numberExp; |
---|
| 379 | |
---|
| 380 | /* Receive number of experience entries */ |
---|
| 381 | memset(buffer, 0, 256); |
---|
| 382 | ReadMessage(commandSocketFD, buffer); |
---|
| 383 | numberExp = atoi(buffer); |
---|
| 384 | |
---|
| 385 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
| 386 | |
---|
| 387 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | /*************************************************************************** |
---|
| 392 | * |
---|
| 393 | * Every function below this banner must be overloaded by child classes. |
---|
| 394 | * |
---|
| 395 | ***************************************************************************/ |
---|
| 396 | |
---|
| 397 | |
---|
| 398 | Parameter* |
---|
| 399 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
| 400 | { |
---|
| 401 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
| 402 | |
---|
| 403 | LOG("CognitiveEngine:: GetSolution not implemented.\n"); |
---|
| 404 | |
---|
| 405 | return NULL; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | Parameter* |
---|
| 410 | CognitiveEngine::GetSolution(Observable *observables, \ |
---|
| 411 | Parameter *currentParameters, std::string service) |
---|
| 412 | { |
---|
| 413 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
| 414 | |
---|
| 415 | LOG("CognitiveEngine:: GetSolution with service not implemented.\n"); |
---|
| 416 | |
---|
| 417 | return NULL; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | void |
---|
| 422 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
| 423 | std::string service) |
---|
| 424 | { |
---|
| 425 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
| 426 | |
---|
| 427 | LOG("CognitiveEngine:: ReceiveFeedback not implemented.\n"); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | void |
---|
| 432 | CognitiveEngine::BuildCognitiveEngine() |
---|
| 433 | { |
---|
| 434 | LOG("Cognitive Engine:: BuildCognitiveEngine not implemented.\n"); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | void |
---|
| 439 | CognitiveEngine::RegisterServices() |
---|
| 440 | { |
---|
| 441 | LOG("Cognitive Engine:: RegisterServices not implemented.\n"); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | void |
---|
| 446 | CognitiveEngine::DeregisterServices() |
---|
| 447 | { |
---|
| 448 | LOG("Cognitive Engine:: DeregisterServices not implemented.\n"); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | |
---|
| 452 | void |
---|
| 453 | PerformUpdatePerformance() |
---|
| 454 | { |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | |
---|
| 458 | void |
---|
| 459 | PerformRequestOptimizationService() |
---|
| 460 | { |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | void |
---|
| 465 | PerformRequestOptimization() |
---|
| 466 | { |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | void |
---|
| 471 | PerformQueryComponentType() |
---|
| 472 | { |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | |
---|
| 476 | void |
---|
| 477 | PerformConnectSML() |
---|
| 478 | { |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | |
---|
| 482 | void |
---|
| 483 | PerformDisconnectSML() |
---|
| 484 | { |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | void |
---|
| 489 | PerformResetEngineCognitive() |
---|
| 490 | { |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | void |
---|
| 495 | PerformShutdownEngineCognitive() |
---|
| 496 | { |
---|
| 497 | } |
---|
| 498 | |
---|