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