00001 #ifndef _LATIN_HYPERCUBE_SAMPLER_HPP_
00002 #define _LATIN_HYPERCUBE_SAMPLER_HPP_
00003
00004 #include <string.h>
00005 #include "sampler.h"
00006
00007
00008
00009
00010 class LatinHypercubeSampler : public Sampler {
00011 public:
00012
00013 LatinHypercubeSampler(const int& iNSamples,
00014 const int& iNDimensions) :
00015 Sampler(iNSamples, iNDimensions)
00016 {
00017 _samples = new float[_nSamples * _nDimensions];
00018 _samplePos = _nSamples;
00019 }
00020
00021
00022 virtual
00023 ~LatinHypercubeSampler() {
00024 if(_samples) delete [] _samples;
00025 _samples = 0;
00026 }
00027
00028
00029
00030
00031 bool next(float* iV) {
00032 if(_samplePos == _nSamples) {
00033 init();
00034 _samplePos = 0;
00035 }
00036
00037 memcpy (iV, _samples + (_samplePos * _nDimensions), sizeof(float[_nDimensions]));
00038 ++_samplePos;
00039 return (_samplesPos < _nSamples);
00040 }
00041
00042 private:
00043
00044 void init() {
00045 int i, j;
00046 float delta;
00047
00048
00049 delta = 1.0 / _nSamples;
00050
00051 for(j = 0; j < _nSamples; ++j) {
00052 for(i = 0; i < _nDimensions; ++i) {
00053 _samples[i + (j * _nDimensions)] = (j + (random() / (float)RAND_MAX)) * delta;
00054 }
00055 }
00056
00057
00058 for(i = 0; i < _nDimensions; ++i) {
00059 for(j = 0; j < _nSamples; ++j) {
00060 int other = random() % _nSamples;
00061 float tmp = _samples[i + (j * _nDimensions)];
00062
00063 _samples[i + (j * nDimensions)] = _samples[i + (other * _nDimensions)];
00064 _samples[i + (other * _nDimensions)] = tmp;
00065 }
00066 }
00067 }
00068
00069 private:
00070
00071 float* _samples;
00072 };
00073
00074 #endif