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