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