diff options
author | Mike Buland <mike@xagasoft.com> | 2012-07-09 08:39:37 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2012-07-09 08:39:37 -0600 |
commit | 40ee7ad5aeadeb9823e1cd6e1218a1999c608a65 (patch) | |
tree | 6e819d8406d818eaa63cb6f04e2a129b8561c213 /src/phenotypebinary.cpp | |
download | libgenetic-40ee7ad5aeadeb9823e1cd6e1218a1999c608a65.tar.gz libgenetic-40ee7ad5aeadeb9823e1cd6e1218a1999c608a65.tar.bz2 libgenetic-40ee7ad5aeadeb9823e1cd6e1218a1999c608a65.tar.xz libgenetic-40ee7ad5aeadeb9823e1cd6e1218a1999c608a65.zip |
New libgenetic. Genetic algorithms, only good.
Diffstat (limited to 'src/phenotypebinary.cpp')
-rw-r--r-- | src/phenotypebinary.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/phenotypebinary.cpp b/src/phenotypebinary.cpp new file mode 100644 index 0000000..d16588b --- /dev/null +++ b/src/phenotypebinary.cpp | |||
@@ -0,0 +1,101 @@ | |||
1 | #include "genetic/phenotypebinary.h" | ||
2 | |||
3 | #include <bu/random.h> | ||
4 | #include <bu/sio.h> | ||
5 | |||
6 | using namespace Bu; | ||
7 | |||
8 | // Bits per word | ||
9 | #define BPW (sizeof(uint_fast32_t)*8) | ||
10 | #define wordsForBits( x ) (((x)/BPW)+(((x)%BPW)?1:0)) | ||
11 | #define wordWithBit( x ) ((x)/BPW) | ||
12 | |||
13 | Genetic::PhenotypeBinary::PhenotypeBinary( int iSize, bool bRandom ) : | ||
14 | iSize( iSize ), | ||
15 | iWords( wordsForBits(iSize) ), | ||
16 | aGenes( NULL ) | ||
17 | { | ||
18 | aGenes = new uint_fast32_t[iWords]; | ||
19 | if( bRandom ) | ||
20 | randomize(); | ||
21 | } | ||
22 | |||
23 | Genetic::PhenotypeBinary::~PhenotypeBinary() | ||
24 | { | ||
25 | delete[] aGenes; | ||
26 | } | ||
27 | |||
28 | Genetic::Phenotype &Genetic::PhenotypeBinary::randomize() | ||
29 | { | ||
30 | for( int j = 0; j < iWords*sizeof(uint_fast32_t); j++ ) | ||
31 | { | ||
32 | ((uint8_t *)aGenes)[j] = (uint8_t)Bu::Random::rand(); | ||
33 | } | ||
34 | |||
35 | return *this; | ||
36 | } | ||
37 | |||
38 | void Genetic::PhenotypeBinary::mutate( int iLocation, float fMagnitude ) | ||
39 | { | ||
40 | if( fMagnitude > -0.5 && fMagnitude < 0.5 ) | ||
41 | return; | ||
42 | |||
43 | aGenes[wordWithBit(iLocation)] = | ||
44 | (aGenes[wordWithBit(iLocation)]&(~(1<<((iLocation)%BPW)))) | | ||
45 | ((~aGenes[wordWithBit(iLocation)])&((1<<((iLocation)%BPW)))); | ||
46 | } | ||
47 | |||
48 | Genetic::Phenotype *Genetic::PhenotypeBinary::makeEmptyOffspring( | ||
49 | int iNewSize ) | ||
50 | { | ||
51 | if( iNewSize < 0 ) | ||
52 | iNewSize = iSize; | ||
53 | |||
54 | return new PhenotypeBinary( iSize ); | ||
55 | } | ||
56 | |||
57 | Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc, | ||
58 | int iStart, int iCount, int iDest ) | ||
59 | { | ||
60 | const PhenotypeBinary &rbSrc = | ||
61 | dynamic_cast<const Genetic::PhenotypeBinary &>(rSrc); | ||
62 | if( iDest < 0 ) | ||
63 | iDest = iStart; | ||
64 | |||
65 | // Fist draft, very sloppy: bit by bit copy, this is stupid, but easy | ||
66 | for( int j = 0; j < iCount; j++ ) | ||
67 | { | ||
68 | int wd = wordWithBit(j+iDest); | ||
69 | int ws = wordWithBit(j+iStart); | ||
70 | if( (rbSrc.aGenes[ws]&(1<<((j+iStart)%BPW))) == 0) | ||
71 | { | ||
72 | aGenes[wd] &= ~(1<<((j+iDest)%BPW)); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | aGenes[wd] |= (1<<((j+iDest)%BPW)); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | /* | ||
81 | iStart%BPW | ||
82 | int iWords = wordsForBits(iCount); | ||
83 | for( int j = wordWithBit(iStart); j < iWords; j++ ) | ||
84 | { | ||
85 | } | ||
86 | */ | ||
87 | |||
88 | return *this; | ||
89 | } | ||
90 | |||
91 | Bu::String Genetic::PhenotypeBinary::toString() | ||
92 | { | ||
93 | Bu::String sRet; | ||
94 | for( int j = 0; j < iSize; j++ ) | ||
95 | { | ||
96 | sRet += (aGenes[j/BPW]&(1<<(j%BPW)))?'1':'0'; | ||
97 | } | ||
98 | |||
99 | return sRet; | ||
100 | } | ||
101 | |||