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