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