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