[Avida-SVN] r2165 - in branches/developers/avida-edward/source: bindings/Boost.Python tools

kaben at myxo.css.msu.edu kaben at myxo.css.msu.edu
Tue Oct 30 19:38:38 PDT 2007


Author: kaben
Date: 2007-10-30 22:38:38 -0400 (Tue, 30 Oct 2007)
New Revision: 2165

Modified:
   branches/developers/avida-edward/source/bindings/Boost.Python/cRandom.pyste
   branches/developers/avida-edward/source/tools/cRandom.cc
   branches/developers/avida-edward/source/tools/cRandom.h
Log:
This diagnostic code will be present only in the Avida-ED developer branch. It
provides a good example use of exceptions to analyze bugs at or across the
language boundary between C++ and Python.


Modified: branches/developers/avida-edward/source/bindings/Boost.Python/cRandom.pyste
===================================================================
--- branches/developers/avida-edward/source/bindings/Boost.Python/cRandom.pyste	2007-10-31 02:33:33 UTC (rev 2164)
+++ branches/developers/avida-edward/source/bindings/Boost.Python/cRandom.pyste	2007-10-31 02:38:38 UTC (rev 2165)
@@ -1,7 +1,27 @@
 
 Include("tList.h")
 Include("tArray.h")
+Include("cException.h")
 
+declaration_code("""
+void translate_cRandomDebugException(cRandomDebugException const& e)
+{
+  // Use the Python 'C' API to set up an exception object
+  PyErr_SetString(PyExc_RuntimeError, e.what());
+}
+
+void throws_cRandomDebugException(const char * msg = 0)
+{
+  throw cRandomDebugException(msg);
+}
+
+""")
+
+module_code("""
+register_exception_translator<cRandomDebugException>(&translate_cRandomDebugException);
+def("throws_cRandomDebugException", throws_cRandomDebugException);
+""")
+
 cRandom = Class("cRandom", "cRandom.h")
 
 # vim: set ft=python:

Modified: branches/developers/avida-edward/source/tools/cRandom.cc
===================================================================
--- branches/developers/avida-edward/source/tools/cRandom.cc	2007-10-31 02:33:33 UTC (rev 2164)
+++ branches/developers/avida-edward/source/tools/cRandom.cc	2007-10-31 02:38:38 UTC (rev 2165)
@@ -13,6 +13,10 @@
 #include "tList.h"
 #endif
 
+#ifndef cException_h
+#include "cException.h"
+#endif
+
 /* FIXME this is not defined in Visual Studio.net. -- kgn */
 //#ifdef MSVC_COMPILER
 /* FIXME find out what the VS.n macro might be. for now use WIN32 -- kgn */
@@ -39,9 +43,16 @@
 const double cRandom::_RAND_uP_FAC=(_RAND_MBIG/1000000);
 
 
+// Debugging Exception ////////////////////////////////////////////////////////
+
+void cRandom::throwDebuggingException(const char* msg){
+  throw cRandomDebugException(msg);
+}
+
 // Constructor and setup //////////////////////////////////////////////////////
 
 cRandom::cRandom(const int in_seed) :
+ m_throw_debugging_exceptions(false),
  seed(0),
  original_seed(0),
  inext(0),
@@ -59,6 +70,7 @@
 
 void cRandom::ResetSeed(const int in_seed){
   //if( in_seed<0 ){  // @TCC - make 0 also be seeded with time * pid
+  checkDebuggingException("cRandom::ResetSeed");
   original_seed = in_seed;
   
   if( in_seed<=0 ){
@@ -83,6 +95,7 @@
 
 
 void cRandom::init(){
+  checkDebuggingException("cRandom::init");
   int mj, mk, ii, i;
 
   // Clear variables
@@ -118,6 +131,7 @@
 }
 
 void cRandom::initStatFunctions(){
+  checkDebuggingException("cRandom::initStatFunctions");
   // Setup variables used by Statistical Distribution functions
   expRV=-log(GetDouble());
 }
@@ -126,6 +140,7 @@
 // Statistical functions //////////////////////////////////////////////////////
 
 double cRandom::GetRandNormal(){
+  checkDebuggingException("cRandom::GetRandNormal");
   // Draw from a Unit Normal Dist
   // Using Rejection Method and saving of initial exponential random variable
   double expRV2;
@@ -142,6 +157,7 @@
 }
 
 unsigned int cRandom::GetRandPoisson(const double mean){
+  checkDebuggingException("cRandom::GetRandPoisson");
   // Draw from a Poisson Dist with mean
   // if cannot calculate, returns UINT_MAX
   // Uses Rejection Method
@@ -157,6 +173,7 @@
 }
 
 unsigned int cRandom::GetFullRandBinomial(const double n, const double p){
+  checkDebuggingException("cRandom::GetFullRandBinomial");
   // Actually try n Bernoulli events with probability p
   unsigned int k=0;
   for( unsigned int i=0; i<n; ++i )
@@ -165,6 +182,7 @@
 }
 
 unsigned int cRandom::GetRandBinomial(const double n, const double p){
+  checkDebuggingException("cRandom::GetRandBinomial");
   // Approximate Binomial if appropriate
   // if np(1-p) is large, use a Normal approx
   if( n*p*(1-p) >= _BINOMIAL_TO_NORMAL ){
@@ -182,6 +200,7 @@
 
 
 bool cRandom::Choose(int num_in, tArray<int> & out_array){
+  checkDebuggingException("cRandom::Choose");
   // If you ask for more than you pass in...
   assert ( num_in >= out_array.GetSize() );
 

Modified: branches/developers/avida-edward/source/tools/cRandom.h
===================================================================
--- branches/developers/avida-edward/source/tools/cRandom.h	2007-10-31 02:33:33 UTC (rev 2164)
+++ branches/developers/avida-edward/source/tools/cRandom.h	2007-10-31 02:38:38 UTC (rev 2165)
@@ -37,6 +37,8 @@
 template <class T> class tArray;
 
 class cRandom{
+  bool m_throw_debugging_exceptions;
+  void throwDebuggingException(const char* msg = 0);
 public:
   /**
    * Set up the random generator object.
@@ -46,6 +48,14 @@
    **/
   cRandom(const int in_seed=-1);
 
+  void enableDebuggingException(void){ m_throw_debugging_exceptions = true; }
+  void disableDebuggingException(void){ m_throw_debugging_exceptions = false; }
+  void checkDebuggingException(const char* msg = 0){
+    if(m_throw_debugging_exceptions){
+      throwDebuggingException(msg);
+    }
+  }
+
   /**
    * Makes this random number generator's state match the original.
    *
@@ -83,7 +93,9 @@
    *
    * @return The pseudo random number.
    **/
-  inline double GetDouble(){ return Get()*_RAND_FAC; }
+  inline double GetDouble(){
+    checkDebuggingException("cRandom::GetDouble");
+    return Get()*_RAND_FAC; }
   
   /**
    * Generate a double between 0 and a given number.
@@ -110,6 +122,7 @@
    * @param max The upper bound for the random numbers (will never be returned).
    **/
   inline unsigned int GetUInt(const unsigned int max){
+    checkDebuggingException("cRandom::GetUInt");
     return (int) (GetDouble()*max);}
   
   /**
@@ -130,6 +143,7 @@
    * @param max The upper bound for the random numbers (will never be returned).
    **/
   inline int GetInt(const int max){
+    checkDebuggingException("cRandom::GetInt");
     return (int)GetUInt(max); }
   inline int GetInt(const int min, const int max){
     return ((int)GetUInt(max - min)) + min; }
@@ -139,12 +153,16 @@
   
   // P(p) => if p < [0,1) random variable
   inline bool P(const double _p){
+    checkDebuggingException("cRandom::P");
     return (Get()<(_p*_RAND_MBIG));}
   inline bool mP(const double _p){	// p = _p*10^-3
+    checkDebuggingException("cRandom::mP");
     return (Get()<_RAND_mP_FAC && Get()<(_p*_RAND_MBIG));}
   inline bool uP(const double _p){	// p = _p*10^-6
+    checkDebuggingException("cRandom::uP");
     return (Get()<_RAND_uP_FAC && Get()<(_p*_RAND_MBIG));}
   inline bool pP(const double _p){	// p = _p*10^-6
+    checkDebuggingException("cRandom::pP");
     return (Get()<_RAND_uP_FAC && Get()<_RAND_uP_FAC &&
 	    Get()<(_p*_RAND_MBIG));}
 
@@ -171,6 +189,7 @@
    * mean and variance.
    **/
   inline double GetRandNormal(const double mean, const double variance){
+    checkDebuggingException("cRandom::GetRandNormal");
     return mean+GetRandNormal()*sqrt(variance);
   }
   
@@ -178,6 +197,7 @@
    * Generate a random variable drawn from a Poisson distribution.
    **/
   inline unsigned int GetRandPoisson(const double n, double p) {
+    checkDebuggingException("cRandom::GetRandPoisson");
     // Optimizes for speed and calculability using symetry of the distribution
     if( p>.5 ) return (unsigned int)n-GetRandPoisson(n*(1-p));
     else return GetRandPoisson(n*p);
@@ -243,6 +263,7 @@
   // Basic Random number
   // Returns a random number [0,_RAND_MBIG)
   inline unsigned int Get(){
+    checkDebuggingException("cRandom::Get");
     // use_count++;  // Turn this on if random uses need to be tracked.
 
     if (++inext == 56) inext = 0;
@@ -256,6 +277,7 @@
 };
 
 inline UINT cRandom::MutateByte(UINT value) {
+  checkDebuggingException("cRandom::MutateByte");
   int byte_pos = 8 * GetUInt(4);
   int new_byte = GetUInt(256);
   value &= ~(255 << byte_pos);
@@ -264,18 +286,21 @@
 }
 
 inline UINT cRandom::ClearByte(UINT value) {
+  checkDebuggingException("cRandom::ClearByte");
   int byte_pos = 8 * GetUInt(4);
   value &= ~(255 << byte_pos);
   return value;
 }
 
 inline UINT cRandom::MutateBit(UINT value) {
+  checkDebuggingException("cRandom::MutateBit");
   int bit_pos = GetUInt(32);
   value ^= (1 << bit_pos);
   return value;
 }
 
 inline UINT cRandom::MutateBit(UINT value, int in_byte) {
+  checkDebuggingException("cRandom::MutateBit");
   int bit_pos = (in_byte) * 8 + GetUInt(8);
   value ^= (1 << bit_pos);
   return value;




More information about the Avida-cvs mailing list