ControlBandit.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = EPSILON_DEFAULT;
  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. default:
  58. fah = this->methodAvg();
  59. }
  60. } else {
  61. fah = queue[0];
  62. queue.erase(queue.begin());
  63. }
  64. mutex.unlock();
  65. return fah;
  66. }
  67. void ControlBandit::addFill(int num, int score) {
  68. num -= 2;
  69. mutexFill.lock();
  70. favg[num] = favg[num] + (1 / (fnb[num] + 1.0)) * (score - favg[num]);
  71. fnb[num]++;
  72. mutexFill.unlock();
  73. }
  74. void ControlBandit::addHeuristic(int num, int score) {
  75. mutexHeuristic.lock();
  76. havg[num] = havg[num] + (1 / (hnb[num] + 1.0)) * (score - havg[num]);
  77. hnb[num]++;
  78. mutexHeuristic.unlock();
  79. }
  80. /* --- Private --- */
  81. fillAndHeuristic ControlBandit::methodAvg() {
  82. fillAndHeuristic fah;
  83. fah.fill = this->getBestFill();
  84. fah.heuristic = this->getBestHeuristic();
  85. return fah;
  86. }
  87. fillAndHeuristic ControlBandit::methodEps() {
  88. fillAndHeuristic fah;
  89. //Tirage au sort pour savoir si on prend la meilleur moyenne ou non
  90. int random = rand() % 100;
  91. if(random < epsilon) {
  92. //Tirage au sort de fill et heuristic
  93. fah.fill = this->getRandomFill();
  94. fah.heuristic = this->getRandomHeuristic();
  95. //printf("=== Random === : %d %d\n", fah.fill, fah.heuristic);
  96. } else {
  97. //Utilisation des meilleurs fill et heuristic
  98. fah.fill = this->getBestFill();
  99. fah.heuristic = this->getBestHeuristic();
  100. }
  101. return fah;
  102. }
  103. int ControlBandit::getBestFill() {
  104. int max = 0;
  105. int fill = 0;
  106. mutexFill.lock();
  107. for(int i = 0; i < NB_FILL; i++) {
  108. if(favg[i] > max) {
  109. max = favg[i];
  110. fill = i;
  111. }
  112. }
  113. mutexFill.unlock();
  114. return fill + 2;
  115. }
  116. int ControlBandit::getBestHeuristic() {
  117. int max = 0;
  118. int h = 0;
  119. mutexHeuristic.lock();
  120. for(int i = 0; i < NB_HEURISTIC; i++) {
  121. if(havg[i] > max) {
  122. max = havg[i];
  123. h = i;
  124. }
  125. }
  126. mutexHeuristic.unlock();
  127. return h;
  128. }