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