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