Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. /*
  2. * Experiment.cpp
  3. *
  4. */
  5.  
  6. #include<stdlib.h>
  7. #include<iostream>
  8.  
  9. #include "Experiment.h"
  10. #include "Distribution.h"
  11.  
  12. #define DEBUG_ON_
  13.  
  14. using namespace std;
  15.  
  16. Experiment::Experiment(int balls, int drawsNumber) {
  17. this->balls = balls;
  18. this->drawsNumber = drawsNumber;
  19.  
  20. // najgorszy scenariusz - losowanie ze zwracaniem i
  21. // wylosowano tylko kule o najwyzszym numerze.
  22. hmax = this->drawsNumber * (balls-1);
  23.  
  24. histogram = new long[hmax];
  25. used = new bool[balls];
  26. forbidden = new bool[ balls ];
  27. for ( int i = 0; i < balls; i++ )
  28. forbidden[ i ] = false;
  29. }
  30.  
  31. void Experiment::setSamplingWithReplacement() {
  32. withReplacement = true;
  33. }
  34.  
  35. void Experiment::setSamplingWithoutReplacement() {
  36. withReplacement = false;
  37. }
  38.  
  39. void Experiment::setForbidden( int ball ) {
  40. #ifdef DEBUG_ON
  41. cout << "Zablokowano mozliwosc uzywania kuli " << ball << endl;
  42. #endif
  43. forbidden[ ball ] = true;
  44. }
  45.  
  46. void Experiment::setAllowed( int ball ) {
  47. forbidden[ ball ] = false;
  48. }
  49.  
  50. void Experiment::clearHistogram() {
  51. for (long i = 0; i < hmax; i++) {
  52. histogram[i] = 0;
  53. }
  54. }
  55.  
  56. void Experiment::allBallsToUrn() {
  57. if ( withReplacement ) return;
  58. for (int i = 0; i < balls; i++)
  59. used[i] = false;
  60. }
  61.  
  62. void Experiment::ballIsDrawn( int ball ) {
  63. if ( withReplacement ) return;
  64.  
  65. #ifdef DEBUG_ON
  66. cout << "Kula o numerze " << ball << " nie moze juz byc ponownie wybrana" << endl;
  67. #endif
  68.  
  69. used[ball] = true;
  70. }
  71.  
  72. bool Experiment::isAllowed( int ball ) {
  73. if ( forbidden[ ball ] ) return false; // tej kuli nie mozna uzywac
  74.  
  75. if ( withReplacement ) return true; // kule sa zwracane, wiec mozna ich ponownie uzywac
  76.  
  77. return ! used[ ball ];
  78. }
  79.  
  80. void Experiment::setMyMPIModule(MyMPI *mpi) {
  81. this->myMPI = mpi;
  82. }
  83.  
  84. long Experiment::singleExperimentResult() {
  85. long sum = 0;
  86. int ball;
  87. double p;
  88.  
  89. allBallsToUrn();
  90. for (int i = 0; i < drawsNumber;) {
  91. ball = (int) (((double) balls * rand()) / ( RAND_MAX + 1.0)); // rand losuje od 0 do RAND_MAX wlacznie
  92.  
  93. #ifdef DEBUG_ON
  94. cout << "Propozycja " << ball << endl;
  95. #endif
  96.  
  97. if ( ! isAllowed( ball ) ) {
  98. #ifdef DEBUG_ON
  99. cout << "Propozycja - ta kula nie moze byc uzyta " << ball << endl;
  100. #endif
  101. continue; // jeszcze raz losujemy
  102. } else {
  103. #ifdef DEBUG_ON
  104. cout << "Propozycja - OK " << ball << endl;
  105. #endif
  106. }
  107.  
  108. p = Distribution::getProbability(i + 1, ball); // pobieramy prawdopodobienstwo
  109. // wybrania tej kuli
  110.  
  111. if ((rand() / ( RAND_MAX + 1.0)) < p) // akceptacja wyboru kuli z zadanym prawdopodobienstwem
  112. {
  113. ballIsDrawn(ball);
  114. sum += ball;
  115. i++;
  116. }
  117. }
  118.  
  119. return sum;
  120. }
  121.  
  122. void Experiment::setNumberOfExperiments( long experiments ) {
  123. this->experiments = experiments;
  124. }
  125.  
  126. void Experiment::calc() {
  127. for (long l = 0; l < experiments; l++) {
  128. histogram[singleExperimentResult()]++;
  129. }
  130. }
  131.  
  132. long Experiment::getHistogramSize() {
  133. return hmax;
  134. }
  135.  
  136. long *Experiment::getHistogram() {
  137. return histogram;
  138. }
  139.  
  140. Experiment::~Experiment() {
  141. delete[] histogram;
  142. delete[] used;
  143. delete[] forbidden;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement