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