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