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 | // Put limit on utility score to prevent a positive feedback loop |
---|
199 | if(newUtilityValue >= 800) |
---|
200 | newUtilityValue = 800; |
---|
201 | |
---|
202 | obsVals[obsValueIndex] = newUtilityValue; |
---|
203 | |
---|
204 | ////////////////////////////////////////////// |
---|
205 | // |
---|
206 | // END CORE DSA ALGORITHM UTILITY FUNCTION |
---|
207 | // |
---|
208 | ////////////////////////////////////////////// |
---|
209 | |
---|
210 | |
---|
211 | size_t returnValueIndex = 0; |
---|
212 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
213 | valList[returnValueIndex] = parameters[i].value; |
---|
214 | returnValueIndex++; |
---|
215 | } |
---|
216 | |
---|
217 | myCBR->Update(nameList, obsList, valList, obsVals, |
---|
218 | numberColumns, obsColumns); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | void |
---|
223 | CognitiveEngine::WaitForSignal() |
---|
224 | { |
---|
225 | char buffer[256]; |
---|
226 | |
---|
227 | while(true) { |
---|
228 | memset(buffer, 0, 256); |
---|
229 | |
---|
230 | ReadMessage(commandSocketFD, buffer); |
---|
231 | |
---|
232 | // TODO this is ugly... is there a better way? Doesn't strcmp compare the |
---|
233 | // whole string? We only need to compare until we find a single different |
---|
234 | // byte... |
---|
235 | // |
---|
236 | // If we send integer op codes rather than strings, this process will be |
---|
237 | // MUCH faster since instead of donig string compares we can simply |
---|
238 | // switch on the integer value... |
---|
239 | if(strcmp(buffer, "update_performance") == 0) { |
---|
240 | |
---|
241 | /* Receive Set of current Parameters */ |
---|
242 | memset(buffer, 0, 256); |
---|
243 | ReadMessage(commandSocketFD, buffer); |
---|
244 | uint32_t numParameters = atoi(buffer); |
---|
245 | |
---|
246 | Parameter *p = new Parameter[numParameters]; |
---|
247 | |
---|
248 | for(size_t i = 0; i < numParameters; i++) { |
---|
249 | memset(buffer, 0, 256); |
---|
250 | ReadMessage(commandSocketFD, buffer); |
---|
251 | p[i].name = std::string(buffer); |
---|
252 | |
---|
253 | memset(buffer, 0, 256); |
---|
254 | ReadMessage(commandSocketFD, buffer); |
---|
255 | p[i].value = atof(buffer); |
---|
256 | } |
---|
257 | |
---|
258 | /* Receive Set of Observables */ |
---|
259 | memset(buffer, 0, 256); |
---|
260 | ReadMessage(commandSocketFD, buffer); |
---|
261 | uint32_t numObservables = atoi(buffer); |
---|
262 | |
---|
263 | Observable *o = new Observable[numObservables]; |
---|
264 | |
---|
265 | for(size_t i = 0; i < numObservables; i++) { |
---|
266 | memset(buffer, 0, 256); |
---|
267 | ReadMessage(commandSocketFD, buffer); |
---|
268 | o[i].name = std::string(buffer); |
---|
269 | |
---|
270 | memset(buffer, 0, 256); |
---|
271 | ReadMessage(commandSocketFD, buffer); |
---|
272 | o[i].value = atof(buffer); |
---|
273 | } |
---|
274 | |
---|
275 | ReceiveFeedback(o,p); |
---|
276 | |
---|
277 | delete [] o; |
---|
278 | delete [] p; |
---|
279 | } |
---|
280 | else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
281 | // THIS IS CURRENTLY IN DEMO MODE |
---|
282 | |
---|
283 | |
---|
284 | /* Receive Set of Observables */ |
---|
285 | LOG("\nCognitive Engine:: Receiving service name\n"); |
---|
286 | |
---|
287 | memset(buffer, 0, 256); |
---|
288 | ReadMessage(commandSocketFD,buffer); |
---|
289 | LOG("\nCognitive Engine:: Got service name, %s\n", buffer); |
---|
290 | |
---|
291 | /* Receive Set of Observables */ |
---|
292 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
293 | |
---|
294 | memset(buffer, 0, 256); |
---|
295 | ReadMessage(commandSocketFD,buffer); |
---|
296 | uint32_t numObservables = atoi(buffer); |
---|
297 | |
---|
298 | Observable *o = new Observable[numObservables]; |
---|
299 | |
---|
300 | for(size_t i = 0; i < numObservables; i++) { |
---|
301 | memset(buffer, 0, 256); |
---|
302 | ReadMessage(commandSocketFD, buffer); |
---|
303 | o[i].name = std::string(buffer); |
---|
304 | |
---|
305 | memset(buffer, 0, 256); |
---|
306 | ReadMessage(commandSocketFD, buffer); |
---|
307 | o[i].value = atof(buffer); |
---|
308 | } |
---|
309 | |
---|
310 | /* Receive Set of current Parameters */ |
---|
311 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
312 | |
---|
313 | memset(buffer, 0, 256); |
---|
314 | ReadMessage(commandSocketFD, buffer); |
---|
315 | uint32_t numCurrentParameters = atoi(buffer); |
---|
316 | |
---|
317 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
318 | |
---|
319 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
320 | memset(buffer, 0, 256); |
---|
321 | ReadMessage(commandSocketFD, buffer); |
---|
322 | cp[i].name = std::string(buffer); |
---|
323 | |
---|
324 | memset(buffer, 0, 256); |
---|
325 | ReadMessage(commandSocketFD, buffer); |
---|
326 | cp[i].value = atof(buffer); |
---|
327 | } |
---|
328 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
329 | |
---|
330 | // TODO need to actually do something with the observables here |
---|
331 | |
---|
332 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
333 | char numParametersChar[10]; |
---|
334 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
335 | SendMessage(commandSocketFD, numParametersChar); |
---|
336 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
337 | SendMessage(commandSocketFD, "test"); |
---|
338 | SendMessage(commandSocketFD, "00"); |
---|
339 | } |
---|
340 | |
---|
341 | delete [] o; |
---|
342 | delete [] cp; |
---|
343 | } |
---|
344 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
345 | |
---|
346 | /* Receive Set of Observables */ |
---|
347 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
348 | |
---|
349 | memset(buffer, 0, 256); |
---|
350 | ReadMessage(commandSocketFD,buffer); |
---|
351 | uint32_t numObservables = atoi(buffer); |
---|
352 | |
---|
353 | Observable *o = new Observable[numObservables]; |
---|
354 | |
---|
355 | for(size_t i = 0; i < numObservables; i++) { |
---|
356 | memset(buffer, 0, 256); |
---|
357 | ReadMessage(commandSocketFD, buffer); |
---|
358 | o[i].name = std::string(buffer); |
---|
359 | |
---|
360 | memset(buffer, 0, 256); |
---|
361 | ReadMessage(commandSocketFD, buffer); |
---|
362 | o[i].value = atof(buffer); |
---|
363 | } |
---|
364 | |
---|
365 | /* Receive Set of current Parameters */ |
---|
366 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
367 | |
---|
368 | memset(buffer, 0, 256); |
---|
369 | ReadMessage(commandSocketFD, buffer); |
---|
370 | uint32_t numCurrentParameters = atoi(buffer); |
---|
371 | |
---|
372 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
373 | |
---|
374 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
375 | memset(buffer, 0, 256); |
---|
376 | ReadMessage(commandSocketFD, buffer); |
---|
377 | cp[i].name = std::string(buffer); |
---|
378 | |
---|
379 | memset(buffer, 0, 256); |
---|
380 | ReadMessage(commandSocketFD, buffer); |
---|
381 | cp[i].value = atof(buffer); |
---|
382 | } |
---|
383 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
384 | |
---|
385 | Parameter *solutionSet; |
---|
386 | |
---|
387 | solutionSet = GetSolution(o,cp); |
---|
388 | |
---|
389 | // TODO need to actually do something with the observables here |
---|
390 | |
---|
391 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
392 | char numParametersChar[10]; |
---|
393 | char solutionValue[50]; |
---|
394 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
395 | SendMessage(commandSocketFD, numParametersChar); |
---|
396 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
397 | SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
398 | memset(solutionValue, 0, 50); |
---|
399 | sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
400 | SendMessage(commandSocketFD, solutionValue); |
---|
401 | } |
---|
402 | |
---|
403 | delete [] o; |
---|
404 | delete [] cp; |
---|
405 | } |
---|
406 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
407 | SendComponentType(); |
---|
408 | } |
---|
409 | else if(strcmp(buffer, "connect_sml") == 0) { |
---|
410 | /* This command implies that we are disconnecting from the shell and |
---|
411 | * connecting to a SML component. */ |
---|
412 | char serverName[256]; |
---|
413 | char serverPort[256]; |
---|
414 | // TODO is this going to end up being too slow? |
---|
415 | memset(serverName, 0, 256); |
---|
416 | memset(serverPort, 0, 256); |
---|
417 | |
---|
418 | ReadMessage(commandSocketFD, serverName); |
---|
419 | ReadMessage(commandSocketFD, serverPort); |
---|
420 | |
---|
421 | /* Only continue if we are currently connected to a shell. */ |
---|
422 | if(!SML_present) { |
---|
423 | DeregisterComponent(); |
---|
424 | |
---|
425 | shutdown(commandSocketFD, 2); |
---|
426 | close(commandSocketFD); |
---|
427 | |
---|
428 | ConnectToRemoteComponent(serverName, serverPort, true); |
---|
429 | } |
---|
430 | } |
---|
431 | else if(strcmp(buffer, "disconnect_sml") == 0) { |
---|
432 | /* This command implies that we are disconnecting from the SML and |
---|
433 | * connecting to a shell component. */ |
---|
434 | char serverName[256]; |
---|
435 | char serverPort[256]; |
---|
436 | // TODO is this going to end up being too slow? |
---|
437 | memset(serverName, 0, 256); |
---|
438 | memset(serverPort, 0, 256); |
---|
439 | |
---|
440 | ReadMessage(commandSocketFD, serverName); |
---|
441 | ReadMessage(commandSocketFD, serverPort); |
---|
442 | |
---|
443 | /* We only want to do this if we are actually connected to an SML |
---|
444 | * currently. */ |
---|
445 | if(SML_present) { |
---|
446 | DeregisterServices(); |
---|
447 | |
---|
448 | shutdown(commandSocketFD, 2); |
---|
449 | close(commandSocketFD); |
---|
450 | |
---|
451 | ConnectToRemoteComponent(serverName, serverPort, false); |
---|
452 | } |
---|
453 | } |
---|
454 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
455 | Reset(); |
---|
456 | } |
---|
457 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
458 | Shutdown(); |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | void |
---|
465 | CognitiveEngine::Shutdown() |
---|
466 | { |
---|
467 | if(SML_present) { |
---|
468 | //DeregisterServices(); |
---|
469 | DeregisterComponent(); |
---|
470 | } |
---|
471 | else { |
---|
472 | DeregisterComponent(); |
---|
473 | } |
---|
474 | // TODO should something else be happening here? |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | void |
---|
479 | CognitiveEngine::Reset() |
---|
480 | { |
---|
481 | LOG("Resetting Cognitive Engine.\n"); |
---|
482 | |
---|
483 | if(SML_present) { |
---|
484 | DeregisterServices(); |
---|
485 | DeregisterComponent(); |
---|
486 | } |
---|
487 | else { |
---|
488 | DeregisterComponent(); |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | |
---|
493 | void |
---|
494 | CognitiveEngine::RegisterComponent() |
---|
495 | { |
---|
496 | char buffer[256]; |
---|
497 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
498 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
499 | |
---|
500 | memset(buffer, 0, 256); |
---|
501 | ReadMessage(commandSocketFD, buffer); |
---|
502 | } |
---|
503 | |
---|
504 | void |
---|
505 | CognitiveEngine::DeregisterComponent() |
---|
506 | { |
---|
507 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
508 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
509 | |
---|
510 | shutdown(commandSocketFD, 2); |
---|
511 | close(commandSocketFD); |
---|
512 | commandSocketFD = -1; |
---|
513 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | void |
---|
518 | CognitiveEngine::RegisterServices() |
---|
519 | { |
---|
520 | LOG("Cognitive Engine:: Registering services.\n"); |
---|
521 | |
---|
522 | SendMessage(commandSocketFD, "register_service"); |
---|
523 | SendMessage(commandSocketFD, "test_srv"); |
---|
524 | |
---|
525 | SendMessage(commandSocketFD, "register_service"); |
---|
526 | SendMessage(commandSocketFD, "test_srv1"); |
---|
527 | |
---|
528 | SendMessage(commandSocketFD, "register_service"); |
---|
529 | SendMessage(commandSocketFD, "test_srv2"); |
---|
530 | |
---|
531 | SendMessage(commandSocketFD, "register_service"); |
---|
532 | SendMessage(commandSocketFD, "test_srv3"); |
---|
533 | |
---|
534 | } |
---|
535 | |
---|
536 | //Combined with deregister component since those two things must happen togeather |
---|
537 | void |
---|
538 | CognitiveEngine::DeregisterServices() |
---|
539 | { |
---|
540 | LOG("Cognitive Engine:: Deregistering services.\n"); |
---|
541 | |
---|
542 | SendMessage(commandSocketFD, "deregister_service"); |
---|
543 | SendMessage(commandSocketFD, "test_srv"); |
---|
544 | |
---|
545 | SendMessage(commandSocketFD, "deregister_service"); |
---|
546 | SendMessage(commandSocketFD, "test_srv1"); |
---|
547 | |
---|
548 | SendMessage(commandSocketFD, "deregister_service"); |
---|
549 | SendMessage(commandSocketFD, "test_srv2"); |
---|
550 | |
---|
551 | SendMessage(commandSocketFD, "deregister_service"); |
---|
552 | SendMessage(commandSocketFD, "test_srv3"); |
---|
553 | |
---|
554 | } |
---|
555 | |
---|
556 | void |
---|
557 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
558 | { |
---|
559 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
560 | |
---|
561 | char buffer[256]; |
---|
562 | |
---|
563 | /* Receive Set of Utilities */ |
---|
564 | memset(buffer, 0, 256); |
---|
565 | ReadMessage(commandSocketFD, buffer); |
---|
566 | radioInfo->numUtilities = atoi(buffer); |
---|
567 | |
---|
568 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
569 | memset(buffer, 0, 256); |
---|
570 | ReadMessage(commandSocketFD, buffer); |
---|
571 | uList[i].name = std::string(buffer); |
---|
572 | |
---|
573 | memset(buffer, 0, 256); |
---|
574 | ReadMessage(commandSocketFD, buffer); |
---|
575 | uList[i].units = std::string(buffer); |
---|
576 | |
---|
577 | memset(buffer, 0, 256); |
---|
578 | ReadMessage(commandSocketFD, buffer); |
---|
579 | uList[i].goal = std::string(buffer); |
---|
580 | |
---|
581 | memset(buffer, 0, 256); |
---|
582 | ReadMessage(commandSocketFD, buffer); |
---|
583 | uList[i].target = atof(buffer); |
---|
584 | } |
---|
585 | |
---|
586 | /* Receive Set of Parameters */ |
---|
587 | memset(buffer, 0, 256); |
---|
588 | ReadMessage(commandSocketFD, buffer); |
---|
589 | radioInfo->numParameters = atoi(buffer); |
---|
590 | |
---|
591 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
592 | memset(buffer, 0, 256); |
---|
593 | ReadMessage(commandSocketFD, buffer); |
---|
594 | pList[i].name = std::string(buffer); |
---|
595 | |
---|
596 | memset(buffer, 0, 256); |
---|
597 | ReadMessage(commandSocketFD, buffer); |
---|
598 | pList[i].units = std::string(buffer); |
---|
599 | |
---|
600 | memset(buffer, 0, 256); |
---|
601 | ReadMessage(commandSocketFD, buffer); |
---|
602 | pList[i].min = atof(buffer); |
---|
603 | |
---|
604 | memset(buffer, 0, 256); |
---|
605 | ReadMessage(commandSocketFD, buffer); |
---|
606 | pList[i].max = atof(buffer); |
---|
607 | |
---|
608 | memset(buffer, 0, 256); |
---|
609 | ReadMessage(commandSocketFD, buffer); |
---|
610 | pList[i].step = atof(buffer); |
---|
611 | |
---|
612 | memset(buffer, 0, 256); |
---|
613 | ReadMessage(commandSocketFD,buffer); |
---|
614 | pList[i].numAffects = atoi(buffer); |
---|
615 | |
---|
616 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
617 | memset(buffer, 0, 256); |
---|
618 | ReadMessage(commandSocketFD,buffer); |
---|
619 | // TODO for + if{break} = while? |
---|
620 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
621 | if(uList[k].name == std::string(buffer)) { |
---|
622 | pList[i].affection_list[j].u = &uList[k]; |
---|
623 | break; |
---|
624 | } |
---|
625 | } |
---|
626 | |
---|
627 | memset(buffer, 0, 256); |
---|
628 | ReadMessage(commandSocketFD, buffer); |
---|
629 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | /* Receive Set of Observables */ |
---|
634 | memset(buffer, 0, 256); |
---|
635 | ReadMessage(commandSocketFD, buffer); |
---|
636 | radioInfo->numObservables = atoi(buffer); |
---|
637 | |
---|
638 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
639 | memset(buffer, 0, 256); |
---|
640 | ReadMessage(commandSocketFD, buffer); |
---|
641 | oList[i].name = std::string(buffer); |
---|
642 | |
---|
643 | memset(buffer, 0, 256); |
---|
644 | ReadMessage(commandSocketFD, buffer); |
---|
645 | oList[i].numAffects = atoi(buffer); |
---|
646 | |
---|
647 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
648 | memset(buffer, 0, 256); |
---|
649 | ReadMessage(commandSocketFD, buffer); |
---|
650 | // TODO for + if{break} = while? |
---|
651 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
652 | if(uList[k].name == std::string(buffer)){ |
---|
653 | oList[i].affection_list[j].u = &uList[k]; |
---|
654 | break; |
---|
655 | } |
---|
656 | } |
---|
657 | |
---|
658 | memset(buffer, 0, 256); |
---|
659 | ReadMessage(commandSocketFD, buffer); |
---|
660 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
665 | |
---|
666 | BuildCognitiveEngine(); |
---|
667 | } |
---|
668 | |
---|
669 | void |
---|
670 | CognitiveEngine::ReceiveExperience() |
---|
671 | { |
---|
672 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
673 | char buffer[256]; |
---|
674 | uint32_t numberExp; |
---|
675 | |
---|
676 | /* Receive number of experience entries */ |
---|
677 | memset(buffer, 0, 256); |
---|
678 | ReadMessage(commandSocketFD, buffer); |
---|
679 | numberExp = atoi(buffer); |
---|
680 | |
---|
681 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
682 | |
---|
683 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
684 | } |
---|
685 | |
---|
686 | |
---|
687 | /* The core of the CE is this function */ |
---|
688 | |
---|
689 | Parameter* |
---|
690 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
691 | { |
---|
692 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
693 | |
---|
694 | /* Put together the CBR search array */ |
---|
695 | |
---|
696 | uint32_t channel = 0; |
---|
697 | string searchNames[1]; |
---|
698 | string sumSearchName; |
---|
699 | float searchVals[1]; |
---|
700 | float utilArray[(int)pList[0].max]; |
---|
701 | int searchOps[1]; |
---|
702 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
703 | radioInfo->numObservables + 1; |
---|
704 | |
---|
705 | float returnValues[numberColumns]; |
---|
706 | float sumRetVals[numberColumns]; |
---|
707 | |
---|
708 | // Total sum of utilities in sumRetVals[0] |
---|
709 | |
---|
710 | string channel_name = "channel"; |
---|
711 | string utility_name = "utility"; |
---|
712 | |
---|
713 | for(int32_t i = 0 ; i < pList[0].max ; i++ ) { |
---|
714 | searchNames[0] = pList[0].name; |
---|
715 | searchOps[0] = 0; |
---|
716 | searchVals[0] = i+1; |
---|
717 | |
---|
718 | uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, |
---|
719 | 1, returnValues); |
---|
720 | |
---|
721 | if(rc == 31337) { |
---|
722 | // No entry - must add |
---|
723 | |
---|
724 | string rowNames[numberColumns]; |
---|
725 | size_t rowIndex = 0; |
---|
726 | for(size_t j = 0; j < radioInfo->numUtilities; j++) { |
---|
727 | rowNames[rowIndex] = uList[j].name; |
---|
728 | rowIndex++; |
---|
729 | } |
---|
730 | for(size_t j = 0; j < radioInfo->numParameters; j++) { |
---|
731 | rowNames[rowIndex] = pList[j].name; |
---|
732 | if(pList[j].name == "channel") |
---|
733 | returnValues[rowIndex] = i+1; |
---|
734 | rowIndex++; |
---|
735 | } |
---|
736 | for(size_t j = 0; j < radioInfo->numObservables; j++) { |
---|
737 | rowNames[rowIndex] = oList[j].name; |
---|
738 | rowIndex++; |
---|
739 | } |
---|
740 | |
---|
741 | rowNames[rowIndex] = utility_name; |
---|
742 | returnValues[rowIndex] = 500; |
---|
743 | |
---|
744 | /* Add the new optimized set to the CBR database */ |
---|
745 | |
---|
746 | myCBR->AddRow(rowNames, returnValues, numberColumns); |
---|
747 | } |
---|
748 | |
---|
749 | utilArray[i] = returnValues[UTILITY]; |
---|
750 | } |
---|
751 | |
---|
752 | printf("Current Channel Utility Scores\n"); |
---|
753 | printf("1: %f\t7: %f\t8: %f\t14: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]); |
---|
754 | // Get sum of all the channel utilities. |
---|
755 | sumSearchName = utility_name; |
---|
756 | uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals); |
---|
757 | |
---|
758 | // Psuedo random channel selection based upon utility. |
---|
759 | int k = rand() % (int)sumRetVals[0]; |
---|
760 | int cdf_total(0); |
---|
761 | |
---|
762 | for ( int i = 0; i < pList[0].max; i++ ) { |
---|
763 | cdf_total += utilArray[i]; |
---|
764 | if(k < cdf_total) { |
---|
765 | channel = i + 1; |
---|
766 | break; |
---|
767 | } |
---|
768 | } |
---|
769 | |
---|
770 | searchNames[0] = pList[0].name; |
---|
771 | searchOps[0] = 0; |
---|
772 | searchVals[0] = channel; |
---|
773 | |
---|
774 | rc = myCBR->Search(searchNames, searchOps, searchVals, |
---|
775 | 1, returnValues); |
---|
776 | |
---|
777 | |
---|
778 | //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; |
---|
779 | returnValues[CHANNEL] = channel; |
---|
780 | |
---|
781 | printf("Cognitive Engine:: ..---===***### Channel %i has been selected ###***===---..\n",channel); |
---|
782 | /* Package up the new set of parameters in order to add |
---|
783 | the new entry into the CBR database. */ |
---|
784 | |
---|
785 | size_t returnValueIndex = 0; |
---|
786 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
787 | uList[i].value = returnValues[returnValueIndex]; |
---|
788 | returnValueIndex++; |
---|
789 | } |
---|
790 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
791 | pList[i].value = returnValues[returnValueIndex]; |
---|
792 | returnValueIndex++; |
---|
793 | } |
---|
794 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
795 | oList[i].value = returnValues[returnValueIndex]; |
---|
796 | returnValueIndex++; |
---|
797 | } |
---|
798 | //returnValues[returnValueIndex] = 0; |
---|
799 | |
---|
800 | string allNames[numberColumns]; |
---|
801 | size_t allNameIndex = 0; |
---|
802 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
803 | allNames[allNameIndex] = uList[i].name; |
---|
804 | returnValues[allNameIndex] = uList[i].target; |
---|
805 | allNameIndex++; |
---|
806 | } |
---|
807 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
808 | allNames[allNameIndex] = pList[i].name; |
---|
809 | allNameIndex++; |
---|
810 | } |
---|
811 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
812 | allNames[allNameIndex] = oList[i].name; |
---|
813 | // returnValues[allNameIndex] = 0; |
---|
814 | allNameIndex++; |
---|
815 | } |
---|
816 | |
---|
817 | allNames[allNameIndex] = utility_name; |
---|
818 | |
---|
819 | /* Add the new optimized set to the CBR database */ |
---|
820 | //myCBR->AddRow(allNames, returnValues, returnValueIndex+1); |
---|
821 | |
---|
822 | |
---|
823 | /* Return the set of new parameter values. */ |
---|
824 | return pList; |
---|
825 | } |
---|
826 | |
---|
827 | |
---|
828 | Parameter* |
---|
829 | CognitiveEngine::GetSolution(Observable *observables, \ |
---|
830 | Parameter *currentParameters, std::string service) |
---|
831 | { |
---|
832 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
833 | |
---|
834 | return pList; |
---|
835 | } |
---|
836 | |
---|
837 | |
---|
838 | void |
---|
839 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
840 | std::string service) |
---|
841 | { |
---|
842 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
843 | } |
---|
844 | |
---|
845 | |
---|
846 | void |
---|
847 | CognitiveEngine::BuildCognitiveEngine() |
---|
848 | { |
---|
849 | string filename = "ex1"; |
---|
850 | string tablename = "data"; |
---|
851 | |
---|
852 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
853 | radioInfo->numObservables + 1; |
---|
854 | |
---|
855 | string cols[numberColumns]; |
---|
856 | |
---|
857 | size_t columnIndex = 0; |
---|
858 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
859 | cols[columnIndex] = uList[i].name; |
---|
860 | columnIndex++; |
---|
861 | } |
---|
862 | |
---|
863 | string paramCols[radioInfo->numParameters]; |
---|
864 | size_t paramColumnIndex = 0; |
---|
865 | // Also need to make parameters the unique key |
---|
866 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
867 | cols[columnIndex] = pList[i].name; |
---|
868 | paramCols[paramColumnIndex] = pList[i].name; |
---|
869 | columnIndex++; |
---|
870 | paramColumnIndex++; |
---|
871 | } |
---|
872 | |
---|
873 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
874 | cols[columnIndex] = oList[i].name; |
---|
875 | columnIndex++; |
---|
876 | } |
---|
877 | |
---|
878 | std::string utility_name = "utility"; |
---|
879 | cols[columnIndex] = utility_name; |
---|
880 | |
---|
881 | myCBR = new CBR(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); |
---|
882 | } |
---|
883 | |
---|