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