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