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