sampler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Sampler *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/sampler.cpp $*
  25. * *
  26. * Original Author:: Hector Yee *
  27. * *
  28. * $Author:: Vss_sync $*
  29. * *
  30. * $Modtime:: 8/29/01 10:25p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * RandomSamplingClass::Sample -- Samples randomly over a hypercube using Mersenne Twister *
  37. * RegularSamplingClass::Sample -- Samples over a regular hypergrid *
  38. * StratifiedSamplingClass::Sample -- samples over a regular hypergrid with random offset *
  39. * QMCSamplingClass::Sample -- Samples using the Halton sequence *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. // Use Random Sampling if you're not sure
  42. // Use Regular Sampling if you're not concerned about bias
  43. // Use Stratified Sampling if you want good results in low dimensions
  44. // Use QMC if you want low discrepancy and you want reproduceablility
  45. #include "always.h"
  46. #include "sampler.h"
  47. #include "random.h"
  48. #include <math.h>
  49. #include <assert.h>
  50. #include <memory.h>
  51. Random4Class Random;
  52. RandomSamplingClass::RandomSamplingClass(unsigned int dimensions,unsigned char divisions):
  53. SamplingClass(dimensions,divisions)
  54. {
  55. }
  56. /***********************************************************************************************
  57. * RandomSamplingClass::Sample -- Samples randomly over a hypercube using Mersenne Twister *
  58. * *
  59. * *
  60. * *
  61. * *
  62. * INPUT: *
  63. * *
  64. * OUTPUT: *
  65. * *
  66. * WARNINGS: *
  67. * *
  68. * HISTORY: *
  69. * 6/11/2001 hy : Created. *
  70. *=============================================================================================*/
  71. void RandomSamplingClass::Sample(float *target)
  72. {
  73. unsigned int i;
  74. for (i=0; i<Dimensions; i++)
  75. {
  76. target[i]=Random.Get_Float();
  77. }
  78. }
  79. RegularSamplingClass::RegularSamplingClass(unsigned int dimensions,unsigned char divisions):
  80. SamplingClass(dimensions,divisions)
  81. {
  82. index=W3DNEWARRAY unsigned char[Dimensions];
  83. Reset();
  84. }
  85. void RegularSamplingClass::Reset()
  86. {
  87. memset(index,0,sizeof(unsigned char)*Dimensions);
  88. }
  89. RegularSamplingClass::~RegularSamplingClass()
  90. {
  91. delete [] index;
  92. }
  93. /***********************************************************************************************
  94. * RegularSamplingClass::Sample -- Samples over a regular hypergrid *
  95. * *
  96. * *
  97. * *
  98. * *
  99. * INPUT: *
  100. * *
  101. * OUTPUT: *
  102. * *
  103. * WARNINGS: *
  104. * *
  105. * HISTORY: *
  106. * 6/11/2001 hy : Created. *
  107. *=============================================================================================*/
  108. void RegularSamplingClass::Sample(float *target)
  109. {
  110. unsigned int i;
  111. for (i=0; i<Dimensions; i++)
  112. {
  113. // minus one because we want to get 1.0f also
  114. target[i]=(float) index[i]/(Divisions-1.0f);
  115. }
  116. // index[i] will always be 0..Divisons-1
  117. // add 1 and carry mod Divisions
  118. // e.g. increase x until x reaches Divisions
  119. // then and only then increase y. Now z increases
  120. // only when x=Divisions and y=Divisions etc..
  121. for (i=0; i<Dimensions; i++)
  122. {
  123. index[i]++;
  124. if (index[i]<Divisions) break;
  125. index[i]=0;
  126. }
  127. }
  128. StratifiedSamplingClass::StratifiedSamplingClass(unsigned int dimensions,unsigned char divisions):
  129. SamplingClass(dimensions,divisions)
  130. {
  131. index=W3DNEWARRAY unsigned char[Dimensions];
  132. Reset();
  133. }
  134. void StratifiedSamplingClass::Reset()
  135. {
  136. memset(index,0,sizeof(unsigned char)*Dimensions);
  137. }
  138. StratifiedSamplingClass::~StratifiedSamplingClass()
  139. {
  140. delete [] index;
  141. }
  142. /***********************************************************************************************
  143. * StratifiedSamplingClass::Sample -- samples over a regular hypergrid with random offset *
  144. * *
  145. * *
  146. * *
  147. * *
  148. * INPUT: *
  149. * *
  150. * OUTPUT: *
  151. * *
  152. * WARNINGS: *
  153. * *
  154. * HISTORY: *
  155. * 6/11/2001 hy : Created. *
  156. *=============================================================================================*/
  157. void StratifiedSamplingClass::Sample(float *target)
  158. {
  159. unsigned int i;
  160. for (i=0; i<Dimensions; i++)
  161. {
  162. target[i]=(index[i]+Random.Get_Float())/(float) Divisions;
  163. }
  164. // index[i] will always be 0..Divisons-1
  165. // add 1 and carry mod Divisions
  166. // e.g. increase x until x reaches Divisions
  167. // then and only then increase y. Now z increases
  168. // only when x=Divisions and y=Divisions etc..
  169. for (i=0; i<Dimensions; i++)
  170. {
  171. index[i]++;
  172. if (index[i]<Divisions) break;
  173. index[i]=0;
  174. }
  175. }
  176. // first 100 primes
  177. const static int primes[]=
  178. {
  179. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
  180. , 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
  181. , 73, 79, 83, 89, 97,101,103,107,109,113
  182. ,127,131,137,139,149,151,157,163,167,173
  183. ,179,181,191,193,197,199,211,223,227,229
  184. ,233,239,241,251,257,263,269,271,277,281
  185. ,283,293,307,311,313,317,331,337,347,349
  186. ,353,359,367,373,379,383,389,397,401,409
  187. ,419,421,431,433,439,443,449,457,461,463
  188. ,467,479,487,491,499,503,509,521,523,541
  189. };
  190. inline float RadInv(int i,int base)
  191. // returns the radical inverse of i in base b
  192. // basically write a number in base b and reverse it over the decimal point
  193. // e.g. RadInv(1) base 2 = 0.1 base 2 = 0.5
  194. {
  195. float sum=0;
  196. int residue;
  197. float power=1.0f/base;
  198. while (i!=0)
  199. {
  200. residue=i%base;
  201. i/=base;
  202. sum+=residue*power;
  203. power/=base;
  204. }
  205. return sum;
  206. }
  207. QMCSamplingClass::QMCSamplingClass(unsigned int dimensions,unsigned char divisions):
  208. SamplingClass(dimensions,divisions),
  209. index(0)
  210. {
  211. assert(Dimensions<100);
  212. }
  213. /***********************************************************************************************
  214. * QMCSamplingClass::Sample -- Samples using the Halton sequence *
  215. * *
  216. * *
  217. * *
  218. * *
  219. * INPUT: *
  220. * *
  221. * OUTPUT: *
  222. * *
  223. * WARNINGS: *
  224. * *
  225. * HISTORY: *
  226. * 6/11/2001 hy : Created. *
  227. *=============================================================================================*/
  228. void QMCSamplingClass::Sample(float *target)
  229. {
  230. unsigned int i;
  231. for (i=0; i<Dimensions; i++)
  232. {
  233. target[i]=RadInv(index,primes[i]);
  234. }
  235. index++;
  236. }