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