• Main Page
  • Related Pages
  • Classes
  • Files
  • Examples
  • File List
  • File Members

ChromosomeT_bool.cpp

Go to the documentation of this file.
00001 
00042 #include <EALib/ChromosomeT.h>
00043 
00044 //===========================================================================
00045 
00046 void ChromosomeT< bool >::initialize()
00047 {
00048     for (unsigned i = size(); i--;)
00049         (*this)[ i ] = bool(Rng::coinToss(0.5));
00050 }
00051 
00052 void ChromosomeT< bool >::initialize(const unsigned pos)
00053 {
00054     for (unsigned i = pos; i < size(); ++i)
00055         (*this)[ i ] = bool(Rng::coinToss(0.5));
00056 }
00057 
00058 
00059 //===========================================================================
00060 
00061 inline unsigned long pow2(unsigned n)
00062 {
00063     return 1UL << n;
00064 }
00065 
00066 void ChromosomeT< bool >::encode(double v,
00067                                  const Interval& range,
00068                                  unsigned nbits,
00069                                  bool useGray)
00070 {
00071     unsigned long l;
00072 
00073     l = (unsigned long)((pow2(nbits) - 1)
00074                         * (v - range.lowerBound())
00075                         / range.width());
00076 
00077     if (useGray) l ^= l >> 1;
00078 
00079     resize(nbits);
00080     for (unsigned i = 0; i < nbits; i++, l >>= 1)
00081         (*this)[ i ] = l & 1;
00082 }
00083 
00084 //===========================================================================
00085 
00086 double ChromosomeT< bool >::decode(const Interval& range, bool useGray) const
00087 {
00088     unsigned long l, m;
00089     unsigned      i;
00090 
00091     if (useGray)
00092         for (l = m = 0, i = size(); i--;)
00093             l = (l << 1) | (m ^= ((*this)[ i ] ? 1 : 0));
00094     else
00095         for (l = 0, i = size(); i--;)
00096             l = (l << 1) | ((*this)[ i ] ? 1 : 0);
00097 
00098     return l * range.width() / (pow2(size()) - 1)
00099            + range.lowerBound();
00100 }
00101 
00102 //===========================================================================
00103 
00104 void ChromosomeT< bool >::encodeBinary(const std::vector< double >& src,
00105                                        const Interval&         range,
00106                                        unsigned                nbits,
00107                                        bool                    useGray)
00108 {
00109     double stepSize = (pow2(nbits) - 1) / range.width();
00110     unsigned i, j, k;
00111     unsigned long l;
00112 
00113     resize(src.size() * nbits);
00114 
00115     for (j = k = 0; j < src.size(); j++) {
00116         l = (unsigned long)((src[ j ] - range.lowerBound()) * stepSize);
00117 
00118         if (useGray) l ^= l >> 1;
00119 
00120         for (i = nbits; i--; l >>= 1)
00121             (*this)[ k++ ] = l & 1;
00122     }
00123 }
00124 
00125 //===========================================================================
00126 
00127 void ChromosomeT< bool >::encodeBinary
00128 (
00129     const Chromosome& chrom,
00130     const Interval&   range,
00131     unsigned          nbits,
00132     bool              useGray
00133 )
00134 {
00135     encodeBinary(dynamic_cast< const std::vector< double >& >(chrom),
00136                  range, nbits, useGray);
00137 }
00138 
00139 //===========================================================================
00140 //
00141 //
00142 //
00143 void ChromosomeT< bool >::flip(double p)
00144 {
00145     for (unsigned i = size(); i--;)
00146         if (Rng::coinToss(p))
00147             //( *this )[ i ] ^= true;
00148             //
00149             // vector< bool > is now represented by a packed bit array
00150             //
00151             (*this)[ i ] = (*this)[ i ] ^ true;
00152 }
00153 
00154 
00155 //===========================================================================
00156 //
00157 //
00158 //
00159 void ChromosomeT< bool >::flip(const std::vector< double >& p, bool cycle)
00160 {
00161     RANGE_CHECK(p.size() <= size())
00162 
00163     for (unsigned i = cycle ? size() : p.size(); i--;)
00164         if (Rng::coinToss(p[ i % p.size()]))
00165             //( *this )[ i ] ^= true;
00166             //
00167             // vector< bool > is now represented by a packed bit array
00168             //
00169             (*this)[ i ] = (*this)[ i ] ^ true;
00170 }
00171 
00172 
00173 //===========================================================================
00174 //
00175 //
00176 //
00177 void ChromosomeT< bool >::flip(const Chromosome& p, bool cycle)
00178 {
00179     flip(dynamic_cast< const std::vector< double >& >(p), cycle);
00180 }
00181 
00182 //===========================================================================
00183 
00184 bool ChromosomeT< bool >::operator == (const Chromosome& c) const
00185 {
00186     //
00187     // this annoying static cast is necessary to satisfy
00188     // the pedantic VC++ 5.0
00189     //
00190     return static_cast< const std::vector< bool >& >(*this) == dynamic_cast< const std::vector< bool >& >(c);
00191 }
00192 
00193 bool ChromosomeT< bool >::operator < (const Chromosome& c) const
00194 {
00195 #ifdef __BOOL_COMPARE__
00196     return static_cast< const std::vector< bool >& >(*this) <  dynamic_cast< const std::vector< bool >& >(c);
00197 #else
00198     UNDEFINED
00199     return false;
00200 #endif
00201 }
00202 
00203 //===========================================================================
00204 
00205 /*
00206 #ifndef __NO_GENERIC_IOSTREAM
00207 
00208 void ChromosomeT< bool >::writeTo( ostream& os ) const
00209 {
00210     os << "ChromosomeT<" << typeid( bool ).name( ) << ">(" << size() << ")" << endl;
00211     for( unsigned i = 0; i < size( ); i++ )
00212         os.put( ( *this )[ i ] ? '1' : '0' );
00213     os << endl;
00214 }
00215 
00216 void ChromosomeT< bool >::readFrom( istream& is )
00217 {
00218     string s;
00219   //is.getline( s );
00220     is >> s;
00221     is.get( );   // skip end of line
00222 
00223     if( is.good( ) &&
00224     s.substr( 0, 12 ) == "ChromosomeT<" &&
00225     s.find( '>' ) != string::npos &&
00226     s.substr( 12, s.find( '>' ) - 12 ) == typeid( bool ).name( ) ) {
00227 
00228         resize( atoi( s.substr( s.find( '>' ) + 2 ).c_str( ) ) );
00229     char c;
00230     for( unsigned i = 0; i < size( ); i++ ) {
00231         is.get( c );
00232         ( *this )[ i ] = c != '0';
00233     }
00234     } else
00235       is.setf( ios::failbit );
00236 }
00237 
00238 #endif // !__NO_GENERIC_IOSTREAM
00239 */
00240 
00241 //===========================================================================
00242 
  • Shark Main Page
  • Array
  • Rng
  • LinAlg
  • FileUtil
  • EALib
  • MOO-EALib
  • ReClaM
  • Fuzzy
  • Mixture
  • Tutorials
  • FAQ