diff options
| author | Mike Buland <mike@xagasoft.com> | 2012-08-02 15:20:58 -0600 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2012-08-02 15:20:58 -0600 |
| commit | 3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a (patch) | |
| tree | a860e10dbd35766b05e4bec1bd7b648884f24a2d /src | |
| parent | 299640ce5f6499e07c5799f48897ac5a77e72c54 (diff) | |
| download | libgenetic-3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a.tar.gz libgenetic-3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a.tar.bz2 libgenetic-3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a.tar.xz libgenetic-3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a.zip | |
New commit method
Diffstat (limited to 'src')
| -rw-r--r-- | src/phenotypebinary.cpp | 56 | ||||
| -rw-r--r-- | src/tests/binary.cpp | 18 |
2 files changed, 68 insertions, 6 deletions
diff --git a/src/phenotypebinary.cpp b/src/phenotypebinary.cpp index 0d32c9f..9d9e26c 100644 --- a/src/phenotypebinary.cpp +++ b/src/phenotypebinary.cpp | |||
| @@ -18,6 +18,8 @@ Genetic::PhenotypeBinary::PhenotypeBinary( int iSize, bool bRandom ) : | |||
| 18 | aGenes = new uint_fast32_t[iWords]; | 18 | aGenes = new uint_fast32_t[iWords]; |
| 19 | if( bRandom ) | 19 | if( bRandom ) |
| 20 | randomize(); | 20 | randomize(); |
| 21 | else | ||
| 22 | memset( aGenes, 0, sizeof(uint_fast32_t)*iWords ); | ||
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | Genetic::PhenotypeBinary::~PhenotypeBinary() | 25 | Genetic::PhenotypeBinary::~PhenotypeBinary() |
| @@ -57,12 +59,16 @@ Genetic::Phenotype *Genetic::PhenotypeBinary::makeEmptyOffspring( | |||
| 57 | Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc, | 59 | Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc, |
| 58 | int iStart, int iCount, int iDest ) | 60 | int iStart, int iCount, int iDest ) |
| 59 | { | 61 | { |
| 62 | if( iCount <= 0 ) | ||
| 63 | return *this; | ||
| 64 | |||
| 60 | const PhenotypeBinary &rbSrc = | 65 | const PhenotypeBinary &rbSrc = |
| 61 | dynamic_cast<const Genetic::PhenotypeBinary &>(rSrc); | 66 | dynamic_cast<const Genetic::PhenotypeBinary &>(rSrc); |
| 62 | if( iDest < 0 ) | 67 | if( iDest < 0 ) |
| 63 | iDest = iStart; | 68 | iDest = iStart; |
| 64 | 69 | ||
| 65 | // Fist draft, very sloppy: bit by bit copy, this is stupid, but easy | 70 | // Fist draft, very sloppy: bit by bit copy, this is stupid, but easy |
| 71 | /* | ||
| 66 | for( int j = 0; j < iCount; j++ ) | 72 | for( int j = 0; j < iCount; j++ ) |
| 67 | { | 73 | { |
| 68 | int wd = wordWithBit(j+iDest); | 74 | int wd = wordWithBit(j+iDest); |
| @@ -76,6 +82,56 @@ Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc, | |||
| 76 | aGenes[wd] |= (1<<((j+iDest)%BPW)); | 82 | aGenes[wd] |= (1<<((j+iDest)%BPW)); |
| 77 | } | 83 | } |
| 78 | } | 84 | } |
| 85 | */ | ||
| 86 | |||
| 87 | { | ||
| 88 | if( iDest == iStart ) | ||
| 89 | { | ||
| 90 | if( wordWithBit(iStart) == wordWithBit(iStart+iCount-1) ) | ||
| 91 | { | ||
| 92 | uint_fast32_t uMask = -1; | ||
| 93 | uMask = (uMask << (iStart%BPW)) & | ||
| 94 | (uMask >> (BPW-(((iStart+iCount))%BPW))); | ||
| 95 | // sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl; | ||
| 96 | aGenes[wordWithBit( iDest )] = | ||
| 97 | (aGenes[wordWithBit( iDest )]&(~uMask)) | | ||
| 98 | (rbSrc.aGenes[wordWithBit( iStart )]&uMask); | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | int iStartWord = wordWithBit( iStart ); | ||
| 103 | int iEndWord = wordWithBit( iStart+iCount ); | ||
| 104 | |||
| 105 | // sio << wordWithBit( iStart+iCount ) << "/" << iWords << sio.nl; | ||
| 106 | // sio << iStart << " + " << iCount << " = " << iStart+iCount << " / " << iSize << sio.nl; | ||
| 107 | |||
| 108 | |||
| 109 | uint_fast32_t uMask = -1; | ||
| 110 | uMask = uMask << (iStart%BPW); | ||
| 111 | // sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl; | ||
| 112 | aGenes[wordWithBit( iDest )] = | ||
| 113 | (aGenes[wordWithBit( iDest )]&(~uMask)) | | ||
| 114 | (rbSrc.aGenes[wordWithBit( iStart )]&uMask); | ||
| 115 | |||
| 116 | for( int j = iStartWord+1; j < iEndWord; j++ ) | ||
| 117 | aGenes[j] = rbSrc.aGenes[j]; | ||
| 118 | |||
| 119 | uMask = ((uint_fast32_t)-1) >> (BPW-(((iStart+iCount))%BPW)); | ||
| 120 | // sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl; | ||
| 121 | aGenes[wordWithBit( iDest+iCount-1 )] = | ||
| 122 | (aGenes[wordWithBit( iDest+iCount-1 )]&(~uMask)) | | ||
| 123 | (rbSrc.aGenes[wordWithBit( iStart+iCount-1 )]&uMask); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | else if( iDest < iStart ) | ||
| 127 | { | ||
| 128 | sio << "iDest < iStart not finished." << sio.nl; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | sio << "iDest > iStart not finished." << sio.nl; | ||
| 133 | } | ||
| 134 | } | ||
| 79 | 135 | ||
| 80 | /* | 136 | /* |
| 81 | iStart%BPW | 137 | iStart%BPW |
diff --git a/src/tests/binary.cpp b/src/tests/binary.cpp index 717c171..727feec 100644 --- a/src/tests/binary.cpp +++ b/src/tests/binary.cpp | |||
| @@ -13,24 +13,30 @@ int main( int argc, char *argv[] ) | |||
| 13 | Bu::Random::setGenerator<Bu::RandomCmwc>(); | 13 | Bu::Random::setGenerator<Bu::RandomCmwc>(); |
| 14 | Bu::Random::seed( time( NULL ) ); | 14 | Bu::Random::seed( time( NULL ) ); |
| 15 | 15 | ||
| 16 | Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 45 ), 0.1 ); | 16 | Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 422*1024 ), 0.0 ); |
| 17 | Genetic::Phenotype *pPb1 = op.random(); | 17 | Genetic::Phenotype *pPb1 = op.random(); |
| 18 | Genetic::Phenotype *pPb2 = op.random(); | 18 | Genetic::Phenotype *pPb2 = op.random(); |
| 19 | Genetic::Phenotype *pPb3; | 19 | Genetic::Phenotype *pPb3; |
| 20 | 20 | ||
| 21 | sio << pPb1->toString() << sio.nl; | 21 | // sio << pPb1->toString() << sio.nl; |
| 22 | sio << pPb2->toString() << sio.nl; | 22 | // sio << pPb2->toString() << sio.nl; |
| 23 | 23 | ||
| 24 | Genetic::PhenotypeList lParents; | 24 | Genetic::PhenotypeList lParents; |
| 25 | lParents.append( pPb1 ); | 25 | lParents.append( pPb1 ); |
| 26 | lParents.append( pPb2 ); | 26 | lParents.append( pPb2 ); |
| 27 | pPb3 = op.mate( lParents ); | ||
| 28 | 27 | ||
| 29 | sio << pPb3->toString() << sio.nl; | 28 | for( int j = 0; j < 1000; j++ ) |
| 29 | { | ||
| 30 | delete op.mate( lParents ); | ||
| 31 | } | ||
| 32 | |||
| 33 | // pPb3 = pPb1->makeEmptyOffspring(); | ||
| 34 | // pPb3->copyFrom( *pPb1, 4, 88 ); | ||
| 35 | // sio << pPb3->toString() << sio.nl; | ||
| 30 | 36 | ||
| 31 | delete pPb1; | 37 | delete pPb1; |
| 32 | delete pPb2; | 38 | delete pPb2; |
| 33 | delete pPb3; | 39 | // delete pPb3; |
| 34 | 40 | ||
| 35 | return 0; | 41 | return 0; |
| 36 | } | 42 | } |
