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