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