ControlBandit.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <unistd.h>
  2. #include "ControlBandit.hpp"
  3. /* --- Public --- */
  4. ControlBandit::ControlBandit() {
  5. //Initialisation de la queue pour les 1er passes
  6. for(int i = 0; i < 2; i++) {
  7. //2 tours pour les 2 passe initiale
  8. for(int f = 2; f < 6; f++) {
  9. for(int h = 0; h < 7; h++) {
  10. fillAndHeuristic fah;
  11. fah.fill = f;
  12. fah.heuristic = h;
  13. queue.push_back(fah);
  14. }
  15. }
  16. }
  17. //Remplissage tableau moyenne
  18. for(int f = 0; f < NB_FILL; f++) {
  19. fema[f] = 0;
  20. }
  21. for(int h = 0; h < NB_HEURISTIC; h++){
  22. hema[h] = 0;
  23. }
  24. //Ini method
  25. alphaStatic = true;
  26. alpha = ALPHA_STATIC;
  27. method = MT_EMA;
  28. epsilon = EPSILON_DEFAULT;
  29. //Ini random
  30. srand(getpid());
  31. }
  32. ControlBandit::~ControlBandit() {
  33. }
  34. void ControlBandit::setMethod(int cstMethod, bool statique) {
  35. if(statique) {
  36. alphaStatic = true;
  37. alpha = ALPHA_STATIC;
  38. } else {
  39. alphaStatic = false;
  40. alpha = ALPHA_MIN;
  41. }
  42. method = cstMethod;
  43. }
  44. void ControlBandit::setEpsilon(double newEpsilon) {
  45. if(newEpsilon >= 0 && newEpsilon <= 1) {
  46. epsilon = (int) (newEpsilon * 100);
  47. }
  48. }
  49. bool ControlBandit::queueIsEmpty() {
  50. bool res;
  51. mutex.lock();
  52. res = queue.size() == 0;
  53. mutex.unlock();
  54. return res;
  55. }
  56. fillAndHeuristic ControlBandit::next() {
  57. fillAndHeuristic fah;
  58. mutex.lock();
  59. if(queue.size() == 0) {
  60. switch(method) {
  61. case MT_EPS:
  62. fah = this->methodEps();
  63. break;
  64. default:
  65. fah = this->methodEma();
  66. }
  67. } else {
  68. fah = queue[0];
  69. queue.erase(queue.begin());
  70. }
  71. mutex.unlock();
  72. return fah;
  73. }
  74. void ControlBandit::addFill(int num, int score) {
  75. num -= 2;
  76. //Recup la valeur d'alpha
  77. double localAlpha;
  78. if(!alphaStatic && alpha < ALPHA_MAX){
  79. //Increment la valeur
  80. mutex.lock();
  81. alpha += ALPHA_STEP;
  82. localAlpha = alpha;
  83. mutex.unlock();
  84. } else {
  85. localAlpha = alpha;
  86. }
  87. //Met a jour la moyenne
  88. mutexFill.lock();
  89. fema[num] = (1 - localAlpha) * fema[num] + localAlpha * score;
  90. mutexFill.unlock();
  91. }
  92. void ControlBandit::addHeuristic(int num, int score) {
  93. //Recup la valeur d'alpha
  94. double localAlpha;
  95. if(!alphaStatic && alpha < ALPHA_MAX){
  96. //Increment la valeur
  97. mutex.lock();
  98. alpha += ALPHA_STEP;
  99. localAlpha = alpha;
  100. mutex.unlock();
  101. } else {
  102. localAlpha = alpha;
  103. }
  104. mutexHeuristic.lock();
  105. hema[num] = (1 - localAlpha) * hema[num] + localAlpha * score;
  106. mutexHeuristic.unlock();
  107. }
  108. /* --- Private --- */
  109. fillAndHeuristic ControlBandit::methodEma() {
  110. fillAndHeuristic fah;
  111. fah.fill = this->getBestFill();
  112. fah.heuristic = this->getBestHeuristic();
  113. return fah;
  114. }
  115. fillAndHeuristic ControlBandit::methodEps() {
  116. fillAndHeuristic fah;
  117. //Tirage au sort pour savoir si on prend la meilleur moyenne ou non
  118. int random = rand() % 100;
  119. if(random < epsilon) {
  120. //Tirage au sort de fill et heuristic
  121. fah.fill = this->getRandomFill();
  122. fah.heuristic = this->getRandomHeuristic();
  123. //printf("=== Random === : %d %d\n", fah.fill, fah.heuristic);
  124. } else {
  125. //Utilisation des meilleurs fill et heuristic
  126. fah.fill = this->getBestFill();
  127. fah.heuristic = this->getBestHeuristic();
  128. }
  129. return fah;
  130. }
  131. int ControlBandit::getBestFill() {
  132. int max = 0;
  133. int fill = 0;
  134. mutexFill.lock();
  135. for(int i = 0; i < NB_FILL; i++) {
  136. if(fema[i] > max) {
  137. max = fema[i];
  138. fill = i;
  139. }
  140. }
  141. mutexFill.unlock();
  142. return fill + 2;
  143. }
  144. int ControlBandit::getBestHeuristic() {
  145. int max = 0;
  146. int h = 0;
  147. mutexHeuristic.lock();
  148. for(int i = 0; i < NB_HEURISTIC; i++) {
  149. if(hema[i] > max) {
  150. max = hema[i];
  151. h = i;
  152. }
  153. }
  154. mutexHeuristic.unlock();
  155. return h;
  156. }