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