From: <den...@us...> - 2010-09-27 11:37:10
|
Revision: 339 http://rmol.svn.sourceforge.net/rmol/?rev=339&view=rev Author: denis_arnaud Date: 2010-09-27 11:37:00 +0000 (Mon, 27 Sep 2010) Log Message: ----------- [Dev] Replaced calls to the GSL (GNU Scientific Library) by Boost.Math. Modified Paths: -------------- trunk/rmol/test/rmol/Makefile.am trunk/rmol/test/rmol/SimulateTestSuite.cpp Property Changed: ---------------- trunk/rmol/test/rmol/ Property changes on: trunk/rmol/test/rmol ___________________________________________________________________ Modified: svn:ignore - .deps .libs Makefile Makefile.in optimise simulate sim.gnumeric OptimiseTestSuite OptimiseTestSuite.log OptimiseTestSuite_results.xml SimulateTestSuite SimulateTestSuite_results.xml rmol.log ForecasterTestSuite ForecasterTestSuite.log ForecasterTestSuite_results.xml UnconstrainerTestSuite UnconstrainerTestSuite.log UnconstrainerTestSuite_results.xml bomsforforecaster bomsforforecaster.log + .deps .libs Makefile Makefile.in optimise simulate sim.gnumeric OptimiseTestSuite OptimiseTestSuite.log OptimiseTestSuite_results.xml SimulateTestSuite SimulateTestSuite.log SimulateTestSuite_results.xml rmol.log ForecasterTestSuite ForecasterTestSuite.log ForecasterTestSuite_results.xml UnconstrainerTestSuite UnconstrainerTestSuite.log UnconstrainerTestSuite_results.xml bomsforforecaster bomsforforecaster.log Modified: trunk/rmol/test/rmol/Makefile.am =================================================================== --- trunk/rmol/test/rmol/Makefile.am 2010-09-27 11:36:24 UTC (rev 338) +++ trunk/rmol/test/rmol/Makefile.am 2010-09-27 11:37:00 UTC (rev 339) @@ -26,10 +26,10 @@ $(top_builddir)/rmol/core/librmol.la SimulateTestSuite_SOURCES = SimulateTestSuite.hpp SimulateTestSuite.cpp -SimulateTestSuite_CXXFLAGS = $(BOOST_CFLAGS) $(GSL_CFLAGS) $(CPPUNIT_CFLAGS) \ +SimulateTestSuite_CXXFLAGS = $(BOOST_CFLAGS) $(CPPUNIT_CFLAGS) \ $(EXTRACC_CFLAGS) $(STDAIR_CFLAGS) SimulateTestSuite_LDADD = -SimulateTestSuite_LDFLAGS = $(BOOST_LIBS) $(GSL_LIBS) $(CPPUNIT_LIBS) \ +SimulateTestSuite_LDFLAGS = $(BOOST_LIBS) $(CPPUNIT_LIBS) \ $(EXTRACC_LIBS) $(STDAIR_LIBS) ForecasterTestSuite_SOURCES = ForecasterTestSuite.hpp ForecasterTestSuite.cpp Modified: trunk/rmol/test/rmol/SimulateTestSuite.cpp =================================================================== --- trunk/rmol/test/rmol/SimulateTestSuite.cpp 2010-09-27 11:36:24 UTC (rev 338) +++ trunk/rmol/test/rmol/SimulateTestSuite.cpp 2010-09-27 11:37:00 UTC (rev 339) @@ -1,64 +1,58 @@ // STL #include <iostream> #include <sstream> -// Boost (Extended STL) +// Boost Date-Time #include <boost/date_time/posix_time/posix_time.hpp> -// GSL Random Number Generation (GSL Reference Manual, version 1.7, Chapter 17) -#include <gsl/gsl_rng.h> -// GSL Random Number Distributions (GSL Reference Manual, version 1.7, -// Chapter 19) -#include <gsl/gsl_randist.h> -// CPPUNIT +// Boost Random +#include <boost/random/linear_congruential.hpp> +#include <boost/random/poisson_distribution.hpp> +#include <boost/random/exponential_distribution.hpp> +#include <boost/random/variate_generator.hpp> +// Boost Math +#include <boost/math/distributions/normal.hpp> +// Extra-CPPUnit #include <extracppunit/CppUnitCore.hpp> // RMOL Test Suite #include <test/rmol/SimulateTestSuite.hpp> -// Random Generator Type -const gsl_rng_type* _rngTypePtr = NULL; +// ////////// Type definitions ////////// +/** Type definition for a random number generator base (mt19937). */ +typedef boost::minstd_rand base_generator_type; -// Random Generator -gsl_rng* _rngExponentialPtr = NULL; -gsl_rng* _rngPoissonPtr = NULL; +/** Type definition for the random generator seed. + <br>That seed must be unsigned, otherwise the wrong overload may be + selected when using mt19937 as the base_generator_type. */ +typedef unsigned int random_generator_seed_type; -// /////////////////////////////////////////////////// -void init () { - // Random Generator type - _rngTypePtr = gsl_rng_default; +/** Type definiton for the Poisson distribution (characteristics). */ +typedef boost::poisson_distribution<> poisson_dist_type; + +/** Type definiton for the exponential distribution (characteristics). */ +typedef boost::exponential_distribution<> exponential_dist_type; + +/** Type definition for the Poisson distribution random generator. */ +typedef boost::variate_generator<base_generator_type&, + poisson_dist_type> poisson_gen_type; - // Initialise the Random Generator - gsl_rng_env_setup (); +/** Type definition for the exponential distribution random generator. */ +typedef boost::variate_generator<base_generator_type&, + exponential_dist_type> exponential_gen_type; - // Allocate the memory for the random generator - _rngExponentialPtr = gsl_rng_alloc (_rngTypePtr); - _rngPoissonPtr = gsl_rng_alloc (_rngTypePtr); -} - -// /////////////////////////////////////////////////// -double generateExponentialVariate (const double iLambda) { - const double lMu = 1/iLambda; - double result = gsl_ran_exponential (_rngExponentialPtr, lMu); - return result; -} - -// /////////////////////////////////////////////////// -double generatePoissonVariate (const double iMu) { - double result = gsl_ran_poisson (_rngPoissonPtr, iMu); - return result; -} - -// /////////////////////////////////////////////////// -void finalise () { - gsl_rng_free (_rngExponentialPtr); - gsl_rng_free (_rngPoissonPtr); -} - // ////////////////////////////////////////////////////////////////////// void testSimulateHelper() { + try { // Output log File std::string lLogFilename ("SimulateTestSuite.log"); + // Set the log parameters + std::ofstream logOutputFile; + + // Open and clean the log outputfile + logOutputFile.open (lLogFilename.c_str()); + logOutputFile.clear(); + // Time duration representing a full day // (i.e., 24h or 1,440 minutes or 86,400 seconds) const boost::posix_time::time_duration kDay (24, 0, 0, 0); @@ -67,15 +61,31 @@ const int kSimulationLength = 365; // Rate, expressed as a number of events per day - // (lambda => mu = 1/ lambda) + // (lambda => mu = 1 / lambda) const double lambda = 10.0; + const double mu = 1 / lambda; // mu = 0.1 (= 2.4h, i.e., in average, an event occurs every 2.4h) // Initialisation of random generators - init(); + /** Base for the random generator. */ + base_generator_type lGenerator (42u); + /** Poisson distribution. */ + exponential_dist_type lExponentialDistribution (mu); + + /** Random generator for the Poisson distribution. */ + exponential_gen_type lExponentialDistributionGenerator (lGenerator, + lExponentialDistribution); + + /** Poisson distribution. */ + poisson_dist_type lPoissonDistribution (lambda); + + /** Random generator for the Poisson distribution. */ + poisson_gen_type lPoissonDistributionGenerator (lGenerator, + lPoissonDistribution); + // Generate k draws - // std::cout << "Event#; Time; " << std::endl; + logOutputFile << "Event#; Time; " << std::endl; for (int i=1; i != kSimulationLength; i++) { // Current time @@ -87,29 +97,29 @@ // Repeat until the current time exceeds 24h (i.e., 86,400 seconds) while (lCurrentTime <= kDay) { const double lExponentialVariateDay = - generateExponentialVariate (lambda); + lExponentialDistributionGenerator(); const long int lExponentialVariateHours = static_cast<long int> (lExponentialVariateDay * 24); const boost::posix_time::time_duration lExponentialVariateSeconds = boost::posix_time::hours (lExponentialVariateHours); // Add an event - lEventNumber++; + ++lEventNumber; // Add the inter-arrival time to the current time lCurrentTime += lExponentialVariateSeconds; - // const double lPoissonVariate = generatePoissonVariate (lambda); - - // std::cout << lEventNumber << "; " << lCurrentTime << "; " << std::endl; + // const double lPoissonVariate = lPoissonDistributionGenerator(); + logOutputFile << lEventNumber << "; " << lCurrentTime << "; " + << std::endl; } } - // Cleaning of random generators - finalise(); - // TODO: check that the average number of events corresponds to the given // input + + // Close the log file + logOutputFile.close(); } catch (const std::exception& stde) { std::cerr << "Standard exception: " << stde.what() << std::endl; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |