ControlBandit.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. favg[f] = 0;
  20. fnb[f] = 0;
  21. }
  22. for(int h = 0; h < NB_HEURISTIC; h++){
  23. havg[h] = 0;
  24. hnb[h] = 0;
  25. }
  26. //Ini method
  27. method = MT_AVG;
  28. epsilon = 10;
  29. //Ini random
  30. srand(getpid());
  31. }
  32. ControlBandit::~ControlBandit() {
  33. }
  34. void ControlBandit::setMethod(int cstMethod) {
  35. method = cstMethod;
  36. }
  37. void ControlBandit::setEpsilon(double newEpsilon) {
  38. if(newEpsilon >= 0 && newEpsilon <= 1) {
  39. epsilon = (int) (newEpsilon * 100);
  40. }
  41. }
  42. bool ControlBandit::queueIsEmpty() {
  43. bool res;
  44. mutex.lock();
  45. res = queue.size() == 0;
  46. mutex.unlock();
  47. return res;
  48. }
  49. fillAndHeuristic ControlBandit::next() {
  50. fillAndHeuristic fah;
  51. mutex.lock();
  52. if(queue.size() == 0) {
  53. switch(method) {
  54. case MT_EPS:
  55. fah = this->methodEps();
  56. break;
  57. case MT_EMA:
  58. fah = this->methodEma();
  59. break;
  60. default:
  61. fah = this->methodAvg();
  62. }
  63. } else {
  64. fah = queue[0];
  65. queue.erase(queue.begin());
  66. }
  67. mutex.unlock();
  68. return fah;
  69. }
  70. void ControlBandit::addFill(int num, int score) {
  71. num -= 2;
  72. mutexFill.lock();
  73. favg[num] = favg[num] + (1 / (fnb[num] + 1.0)) * (score - favg[num]);
  74. fnb[num]++;
  75. mutexFill.unlock();
  76. }
  77. void ControlBandit::addHeuristic(int num, int score) {
  78. mutexHeuristic.lock();
  79. havg[num] = havg[num] + (1 / (hnb[num] + 1.0)) * (score - havg[num]);
  80. hnb[num]++;
  81. mutexHeuristic.unlock();
  82. }
  83. /* --- Private --- */
  84. fillAndHeuristic ControlBandit::methodAvg() {
  85. fillAndHeuristic fah;
  86. fah.fill = this->getBestFill();
  87. fah.heuristic = this->getBestHeuristic();
  88. return fah;
  89. }
  90. fillAndHeuristic ControlBandit::methodEps() {
  91. fillAndHeuristic fah;
  92. //Tirage au sort pour savoir si on prend la meilleur moyenne ou non
  93. int random = rand() % 100;
  94. if(random < epsilon) {
  95. //Tirage au sort de fill et heuristic
  96. fah.fill = this->getRandomFill();
  97. fah.heuristic = this->getRandomHeuristic();
  98. //printf("=== Random === : %d %d\n", fah.fill, fah.heuristic);
  99. } else {
  100. //Utilisation des meilleurs fill et heuristic
  101. fah.fill = this->getBestFill();
  102. fah.heuristic = this->getBestHeuristic();
  103. }
  104. return fah;
  105. }
  106. fillAndHeuristic ControlBandit::methodEma() {
  107. fillAndHeuristic fah;
  108. fah.fill = -1;
  109. fah.heuristic = -1;
  110. return fah;
  111. }
  112. int ControlBandit::getBestFill() {
  113. int max = 0;
  114. int fill = 0;
  115. mutexFill.lock();
  116. for(int i = 0; i < NB_FILL; i++) {
  117. if(favg[i] > max) {
  118. max = favg[i];
  119. fill = i;
  120. }
  121. }
  122. mutexFill.unlock();
  123. return fill + 2;
  124. }
  125. int ControlBandit::getBestHeuristic() {
  126. int max = 0;
  127. int h = 0;
  128. mutexHeuristic.lock();
  129. for(int i = 0; i < NB_HEURISTIC; i++) {
  130. if(havg[i] > max) {
  131. max = havg[i];
  132. h = i;
  133. }
  134. }
  135. mutexHeuristic.unlock();
  136. return h;
  137. }