Skip to content

Commit

Permalink
Add a NDPseudoRandomNumberGenerator. Refs #5322
Browse files Browse the repository at this point in the history
Uses a single generator to allow easy construction of an ND point from
pseudo-random numbers thereby presenting a cleaner interface inline with
the other ND generators.
  • Loading branch information
martyngigg committed May 15, 2012
1 parent c927b81 commit c5998a8
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set ( SRC_FILES
src/MersenneTwister.cpp
src/MultiFileNameParser.cpp
src/MultiFileValidator.cpp
src/NDPseudoRandomNumberGenerator.cpp
src/NeutronAtom.cpp
src/NexusTestHelper.cpp
src/ProgressBase.cpp
Expand Down Expand Up @@ -155,6 +156,7 @@ set ( INC_FILES
inc/MantidKernel/MultiThreaded.h
inc/MantidKernel/MultiFileValidator.h
inc/MantidKernel/NDRandomNumberGenerator.h
inc/MantidKernel/NDPseudoRandomNumberGenerator.h
inc/MantidKernel/NeutronAtom.h
inc/MantidKernel/NexusTestHelper.h
inc/MantidKernel/NullValidator.h
Expand Down Expand Up @@ -251,6 +253,7 @@ set ( TEST_FILES
test/MutexTest.h
test/NeutronAtomTest.h
test/NDRandomNumberGeneratorTest.h
test/NDPseudoRandomNumberGeneratorTest.h
test/NullValidatorTest.h
test/ProgressBaseTest.h
test/ProgressTextTest.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Mantid
/// Generate the next random number in the sequence within the given range, (default=[0.0,1.0]).
virtual double nextValue();
/// Resets the generator
virtual void reset();
virtual void restart();

private:
DISABLE_DEFAULT_CONSTRUCT(MersenneTwister);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_
#define MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_
/**
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/PseudoRandomNumberGenerator.h"
#include "MantidKernel/ClassMacros.h"
#include <boost/shared_ptr.hpp>

namespace Mantid
{
namespace Kernel
{
/**
*
* Defines an ND pseudo-random number generator. This uses a single
* 1D pseudo-random number generator to produce ND random values
*/
class MANTID_KERNEL_DLL NDPseudoRandomNumberGenerator : public NDRandomNumberGenerator
{
/// The type for generating a single random number
typedef boost::shared_ptr<PseudoRandomNumberGenerator> SingleValueGenerator;
public:
/// Constructor
NDPseudoRandomNumberGenerator(const unsigned int ndims, SingleValueGenerator singleValueGen);
/// Set the random number seed
void setSeed(const size_t seedValue);
/// Returns the ND point
std::vector<double> nextPoint();
/// Resets the generator
void restart();

private:
DISABLE_DEFAULT_CONSTRUCT(NDPseudoRandomNumberGenerator);
DISABLE_COPY_AND_ASSIGN(NDPseudoRandomNumberGenerator);

/// The number of dimensions
const unsigned int m_ndims;
/// The single value generator
SingleValueGenerator m_singleValueGen;
};
}
}

#endif /* MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace Mantid
virtual ~NDRandomNumberGenerator() {};
/// Generate the next set of values that form a point in ND space
virtual std::vector<double> nextPoint() = 0;
/// Reset the generator
virtual void reset() = 0;
/// Resets the generator
virtual void restart() = 0;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Mantid
* that takes an initial seed and produces a set of numbers. It specialises
* the interface for a general random number generator.
*/
class MANTID_KERNEL_DLL PseudoRandomNumberGenerator : NDRandomNumberGenerator
class MANTID_KERNEL_DLL PseudoRandomNumberGenerator : public NDRandomNumberGenerator
{
public:
/// Set the random number seed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Mantid
/// Returns the next value in the sequence
std::vector<double> nextPoint();
/// Reset the sequence
void reset();
void restart();

private:
DISABLE_DEFAULT_CONSTRUCT(SobolSequence);
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/src/MersenneTwister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace Mantid
/**
* Resets the generator using the value given at the last call to setSeed
*/
void MersenneTwister::reset()
void MersenneTwister::restart()
{
setSeed(m_currentSeed);
}
Expand Down
52 changes: 52 additions & 0 deletions Code/Mantid/Framework/Kernel/src/NDPseudoRandomNumberGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//-------------------------------------------------------------------
// Includes
//-------------------------------------------------------------------
#include "MantidKernel/NDPseudoRandomNumberGenerator.h"

namespace Mantid
{
namespace Kernel
{
/**
* Constructor taking the number of dimensions & an existing generator
* @param ndims :: The number of dimensions the point should return
* @param singleValueGen :: An existing generator that is capable of
* producing single random numbers. It is called ndims times for each
* call to nextPoint
*/
NDPseudoRandomNumberGenerator::
NDPseudoRandomNumberGenerator(const unsigned int ndims, SingleValueGenerator singleValueGen)
: NDRandomNumberGenerator(), m_ndims(ndims), m_singleValueGen(singleValueGen)
{
}

/**
* Set the random number seed
* @param seedValue :: (Re-)seed the generator
*/
void NDPseudoRandomNumberGenerator::setSeed(const size_t seedValue)
{
m_singleValueGen->setSeed(seedValue);
}

/// Returns the ND point
std::vector<double> NDPseudoRandomNumberGenerator::nextPoint()
{
std::vector<double> point(m_ndims);
for(unsigned int i = 0; i < m_ndims; ++i)
{
point[i] = m_singleValueGen->nextValue();
}
return point;
}

/**
* Resets the underlying generator
*/
void NDPseudoRandomNumberGenerator::restart()
{
m_singleValueGen->restart();
}
}
}

2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/src/SobolSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Mantid
/**
* Reset state back to the start of the sequence
*/
void SobolSequence::reset()
void SobolSequence::restart()
{
if(m_gslGenerator)
{
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Kernel/test/MersenneTwisterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MersenneTwisterTest : public CxxTest::TestSuite
MersenneTwister randGen(1);
randGen.setSeed(39857239);
assertSequenceCorrectForSeed_39857239(randGen);
randGen.reset();
randGen.restart();
assertSequenceCorrectForSeed_39857239(randGen);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef NDPSEUDORANDOMNUMBERGENERATORTEST_H_
#define NDPSEUDORANDOMNUMBERGENERATORTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidKernel/NDPseudoRandomNumberGenerator.h"
#include "MantidKernel/MersenneTwister.h"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using Mantid::Kernel::NDPseudoRandomNumberGenerator;
using Mantid::Kernel::PseudoRandomNumberGenerator;

class NDPseudoRandomNumberGeneratorTest : public CxxTest::TestSuite
{
public:

void test_That_Next_Always_Returns_ND_Size_Array()
{
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand =
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345));
for(int i = 0; i < 20; ++i)
{
std::vector<double> point = ndRand->nextPoint();
TS_ASSERT_EQUALS(static_cast<unsigned int>(point.size()), 3);
}
}

void test_That_Restart_Is_Passed_On_Correctly()
{
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand =
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345));
std::vector<double> firstPoint = ndRand->nextPoint();
TS_ASSERT_THROWS_NOTHING(ndRand->restart());
std::vector<double> firstPointAfterReset = ndRand->nextPoint();
for(size_t i = 0; i < firstPoint.size(); ++i)
{
TS_ASSERT_EQUALS(firstPoint[i], firstPointAfterReset[i]);
}
}

void test_That_Range_Of_SingleValue_Generator_Is_Respected()
{
const double start(2.1), end(3.4);
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand =
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345,start,end));
std::vector<double> firstPoint = ndRand->nextPoint();
for(size_t i = 0; i < firstPoint.size(); ++i)
{
TS_ASSERT(firstPoint[i] >= start && firstPoint[i] <= end);
}
}

private:
boost::shared_ptr<NDPseudoRandomNumberGenerator> createTestGenerator(boost::shared_ptr<PseudoRandomNumberGenerator> singleGen)
{
const unsigned int ndims(3);
return boost::make_shared<NDPseudoRandomNumberGenerator>(ndims, singleGen);
}

};



#endif //NDPSEUDORANDOMNUMBERGENERATORTEST_H_
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NDRandomNumberGeneratorTest : public CxxTest::TestSuite
{
return std::vector<double>();
}
void reset()
void restart()
{
}
};
Expand All @@ -34,7 +34,7 @@ class NDRandomNumberGeneratorTest : public CxxTest::TestSuite
void test_That_Reset_Does_Nothing()
{
FakeNDRandomNumberGenerator rand_gen;
TS_ASSERT_THROWS_NOTHING(rand_gen.reset());
TS_ASSERT_THROWS_NOTHING(rand_gen.restart());
}

};
Expand Down

0 comments on commit c5998a8

Please sign in to comment.