12#ifndef BELFEM_CL_DNA_HPP
13#define BELFEM_CL_DNA_HPP
28 template <
size_t B,
size_t N >
53 for(
size_t i=0; i<N; ++i )
55 real tValue = aValues( i );
56 real tMin = mMinVals( i );
57 real tMax = mMaxVals( i );
65 if ( mTypes.test( i ) )
67 tValue = std::log( tValue );
68 tMin = std::log( tMin );
69 tMax = std::log( tMax );
73 tValue = std::max( std::min( tValue, tMax ), tMin );
76 tValue = ( tValue - tMin ) / ( tMax - tMin );
79 encode( i,
static_cast< uint >( std::round( tValue * ( ( 1u << B ) - 1 ) ) ) );
86 for(
size_t i=0; i<N; ++i )
89 real tMin = mMinVals( i );
90 real tMax = mMaxVals( i );
99 uint tBits = decode( i );
107 if ( mTypes.test( i ) )
109 tMin = std::log( tMin );
110 tMax = std::log( tMax );
112 tValue = std::exp( tMin + tValue * ( tMax - tMin ) );
117 tValue = tMin + tValue * ( tMax - tMin );
125 aValues( i ) = std::max( std::min( tValue, mMaxVals( i ) ),
134 for(
size_t i=0; i<N; ++i )
136 real tMin = mMinVals( i );
137 real tMax = mMaxVals( i );
145 if ( mTypes.test( i ) )
147 tMin = std::log( tMin );
148 tMax = std::log( tMax );
152 real tMean = 0.5 * ( tMin + tMax );
153 real tStdDev = ( tMax - tMin ) / 6.0;
162 real tZ = std::sqrt( -2.0 * std::log( u1 ) ) * std::cos( 2.0 *
constant::pi * u2 );
164 real tValue = tMean + tStdDev * tZ ;
167 tValue = std::max( std::min( tValue, tMax ), tMin );
170 tValue = ( tValue - tMin ) / ( tMax - tMin );
173 encode( i,
static_cast< uint >( std::round( tValue * ( ( 1u << B ) - 1 ) ) ) );
181 size_t tNumBits = N * B;
193 for(
size_t k=0; k<tSplit; ++k )
195 if( aMom->mDNA.
test( k ) )
202 for(
size_t k=tSplit; k<tNumBits; ++k )
204 if( aDad->mDNA.
test( k ) )
211 mDNA.flip( tMutate );
242 decode(
const size_t aIndex )
const
248 size_t tCount = aIndex * B ;
249 for(
size_t i=0; i<B; ++i )
251 if ( mDNA.
test( tCount++ ) )
261 encode(
const size_t aIndex,
uint aValue )
263 size_t tCount = aIndex *
B;
264 for(
size_t i=0;
i<
B; ++
i)
271 mDNA.reset(tCount++);
278 template <
size_t B,
size_t N >
Compile-time fixed-size bitset.
Definition cl_Bitset.hpp:32
bool test(const index_t aIndex) const
test if a bit is set
Definition cl_Bitset.hpp:138
Definition cl_Genome.hpp:30
void kill()
Definition cl_Genome.hpp:227
void set_fitness(const real aFitness)
Definition cl_Genome.hpp:221
void randomize()
Definition cl_Genome.hpp:131
Genome(const Vector< real > &aMinVals, const Vector< real > &aMaxVals, const Bitset< N > &aTypes)
Definition cl_Genome.hpp:39
void set_values(const Vector< real > &aValues)
Definition cl_Genome.hpp:50
real fitness() const
Definition cl_Genome.hpp:215
void inherit(const Genome *aMom, const Genome *aDad)
Definition cl_Genome.hpp:178
void get_values(Vector< real > &aValues) const
Definition cl_Genome.hpp:84
bool is_alive() const
Definition cl_Genome.hpp:234
const real pi
circle number
Definition constants.hpp:41
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
constexpr real BELFEM_EPSILON
Definition typedefs.hpp:89
double real
Definition typedefs.hpp:36
size_t size_t
Definition typedefs.hpp:25
real rand()
a random number between 0 and 1 must call seed first
Definition random.hpp:86
B
Definition test_twist_crosscheck.py:39
Definition cl_Genome.hpp:280
bool operator()(const Genome< B, N > *aA, const Genome< B, N > *aB)
Definition cl_Genome.hpp:282
#define BELFEM_REAL_MAX
Definition typedefs.hpp:80