Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/Genetic/include/Roulette.h

Go to the documentation of this file.
00001 //-----------------------------------------------------------
00002 //  FORGE
00003 //-----------------------------------------------------------
00004 //
00005 //      roulette.h     v1.00
00006 //
00007 //      Defines the Roulette Wheel index class
00008 //
00009 //-----------------------------------------------------------
00010 //  Copyright 1995 by Scott Robert Ladd. All rights reserved.
00011 //-----------------------------------------------------------
00012 
00013 #ifndef FORGE_ROULETTE_H
00014 #define FORGE_ROULETTE_H
00015 
00016 #include "RandomNumbers.h"
00017 
00018 class RW_Except { };
00019 
00020 template < class FType >
00021     class RouletteWheel
00022         {
00023         public:
00024             // creation constructor
00025             RouletteWheel
00026                 (
00027                 size_t sz,
00028                 FType * weights = NULL,
00029                 int seed = 0
00030                 );
00031 
00032             // copy constructor
00033             RouletteWheel
00034                 (
00035                 const RouletteWheel & rw
00036                 );
00037 
00038             // assignment operator
00039             void operator =
00040                 (
00041                 const RouletteWheel & rw
00042                 );
00043 
00044             // destructor
00045             ~RouletteWheel();
00046 
00047             // change the weight of an entry
00048             FType Change
00049                 (
00050                 size_t i,
00051                 FType weight
00052                 );
00053 
00054             // interrogation function
00055             size_t GetSize() { return N; }
00056 
00057             FType GetWeight
00058                 (
00059                 size_t i
00060                 );
00061 
00062             // retrieve a random index
00063             size_t GetIndex();
00064 
00065         protected:
00066             // array of weights
00067             size_t  N;
00068             FType * W;
00069 
00070             // total weight of all indexes
00071             FType T;
00072 
00073             // shared random deviate generator
00074             UniformRandom G;
00075 
00076         private:
00077             // internal absolute value of a float
00078             FType AbsVal
00079                 (
00080                 FType f
00081                 )
00082                 {
00083                 if (f < FType(0.0))
00084                     return -f;
00085                 else
00086                     return f;
00087                 }
00088 
00089             // internal copy function
00090             void Copy
00091                 (
00092                 const RouletteWheel & rw
00093                 );
00094         };
00095 
00096 // creation constructor
00097 template < class FType>
00098     RouletteWheel<FType>::RouletteWheel
00099         (
00100         size_t  sz,
00101         FType * weights,
00102         int seed
00103         ) 
00104         {
00105           
00106         if( seed != 0 )
00107           G = UniformRandom(seed);
00108 
00109         size_t i;
00110 
00111         N = sz;
00112         T = FType(0);
00113         W = new FType [N];
00114 
00115         //if (W == NULL) throw RW_Except();
00116 
00117         if (weights == NULL)
00118             {
00119             for (i = 0; i < N; ++i)
00120                 {
00121                 W[i] = FType(1);
00122                 T   += FType(1);
00123                 }
00124             }
00125         else
00126             {
00127             for (i = 0; i < N; ++i)
00128                 {
00129                 W[i] = AbsVal(weights[i]);
00130                 T   += AbsVal(weights[i]);
00131                 }
00132             }
00133         }
00134 
00135 template < class FType>
00136     void RouletteWheel<FType>::Copy
00137         (
00138         const RouletteWheel & rw
00139         )
00140         {
00141         T = rw.T;
00142         N = rw.N;
00143         W = new FType [N];
00144 
00145         //if (W == NULL) throw RW_Except();
00146 
00147         memcpy(W,rw.W,sizeof(FType) * N);
00148         }
00149 
00150 // copy constructor
00151 template < class FType >
00152     inline RouletteWheel<FType>::RouletteWheel
00153         (
00154         const RouletteWheel & rw
00155         )
00156         {
00157         Copy(rw);
00158         }
00159 
00160 // assignment operator
00161 template < class FType >
00162     inline void RouletteWheel<FType>::operator =
00163         (
00164         const RouletteWheel & rw
00165         )
00166         {
00167         delete [] W;
00168         Copy(rw);
00169         }
00170 
00171 // destructor
00172 template < class FType >
00173     RouletteWheel<FType>::~RouletteWheel()
00174         {
00175         delete [] W;
00176         }
00177 
00178 // change the weight of an entry
00179 template < class FType >
00180     FType RouletteWheel<FType>::Change
00181         (
00182         size_t i,
00183         FType weight
00184         )
00185         {
00186         if (i >= N)
00187             return FType(-1);
00188 
00189         T -= W[i];
00190         T += weight;
00191 
00192         FType res = W[i];
00193         W[i] = weight;
00194         return res;
00195         }
00196 
00197 // interrogator
00198 template < class FType >
00199     inline FType RouletteWheel<FType>::GetWeight
00200         (
00201         size_t i
00202         )
00203         {
00204         return (i < N) ? W[i] : FType(-1);
00205         }
00206 
00207 // retrieve a random index
00208 template < class FType >
00209     size_t RouletteWheel<FType>::GetIndex()
00210         {
00211         FType s  = FType(G.rand() * T);
00212         size_t i = 0;
00213 
00214         while ((i < N) && (s > W[i]))
00215             {
00216             s -= W[i];
00217             ++i;
00218             }
00219 
00220         return i;
00221         }
00222 
00223 #endif
00224 

Generated at Tue Jun 19 09:49:36 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001