diff options
author | Mike Buland <mike@xagasoft.com> | 2012-08-04 16:59:49 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2012-08-04 16:59:49 -0600 |
commit | aaa733d789612289396d69bb6cf3a0e5de1798a8 (patch) | |
tree | 9004c335d19bad5357737d34615572e002d583ee | |
parent | 3cdf68e08d438a0ced0f5f8d957a6aafd68cb43a (diff) | |
download | libgenetic-aaa733d789612289396d69bb6cf3a0e5de1798a8.tar.gz libgenetic-aaa733d789612289396d69bb6cf3a0e5de1798a8.tar.bz2 libgenetic-aaa733d789612289396d69bb6cf3a0e5de1798a8.tar.xz libgenetic-aaa733d789612289396d69bb6cf3a0e5de1798a8.zip |
Optomized the mating / mutation setup.
It should work the same, but it selects the number of bits to mutate
first, then selects the individual bits instead of testing a random
number on each bit. For very large phenotypes this will be
significantly faster while producing about the same result.
-rw-r--r-- | default.bld | 4 | ||||
-rw-r--r-- | src/operatorbasic.cpp | 19 | ||||
-rw-r--r-- | src/tests/binary.cpp | 3 |
3 files changed, 13 insertions, 13 deletions
diff --git a/default.bld b/default.bld index 65c8adb..741f16a 100644 --- a/default.bld +++ b/default.bld | |||
@@ -1,5 +1,5 @@ | |||
1 | CXXFLAGS += "-pg"; | 1 | # CXXFLAGS += "-pg"; |
2 | LDFLAGS += "-pg"; | 2 | # LDFLAGS += "-pg"; |
3 | 3 | ||
4 | target "libgenetic.a" | 4 | target "libgenetic.a" |
5 | { | 5 | { |
diff --git a/src/operatorbasic.cpp b/src/operatorbasic.cpp index ce37f78..2ac2b0f 100644 --- a/src/operatorbasic.cpp +++ b/src/operatorbasic.cpp | |||
@@ -28,21 +28,20 @@ Genetic::Phenotype *Genetic::OperatorBasic::mate( | |||
28 | const Genetic::PhenotypeList &lParents ) | 28 | const Genetic::PhenotypeList &lParents ) |
29 | { | 29 | { |
30 | Genetic::Phenotype *pChild = lParents.first()->makeEmptyOffspring(); | 30 | Genetic::Phenotype *pChild = lParents.first()->makeEmptyOffspring(); |
31 | 31 | ||
32 | int iBits = lParents.first()->getSize(); | ||
32 | // Right now, we assume both parents have the same amount of "DNA" | 33 | // Right now, we assume both parents have the same amount of "DNA" |
33 | int iSplitPoint = ((uint32_t)Bu::Random::rand())%lParents.first()->getSize(); | 34 | int iSplitPoint = ((uint32_t)Bu::Random::rand())%iBits; |
34 | pChild->copyFrom( *lParents.first(), 0, iSplitPoint ); | 35 | pChild->copyFrom( *lParents.first(), 0, iSplitPoint ); |
35 | pChild->copyFrom( *lParents.last(), iSplitPoint, | 36 | pChild->copyFrom( *lParents.last(), iSplitPoint, iBits-iSplitPoint ); |
36 | lParents.last()->getSize()-iSplitPoint ); | 37 | |
38 | int iChange = iBits * (Bu::Random::randNorm() * fMutationRate * 1.25); | ||
37 | 39 | ||
38 | // Mutate the child some | 40 | for( int j = 0; j < iChange; j++ ) |
39 | for( int j = 0; j < pChild->getSize(); j++ ) | ||
40 | { | 41 | { |
41 | //sio << Bu::Random::randNorm() << sio.nl; | 42 | pChild->mutate( Bu::Random::rand( 0, iBits ), 1.0 ); |
42 | if( Bu::Random::randNorm() <= fMutationRate ) | ||
43 | pChild->mutate( j, 1.0 ); | ||
44 | } | 43 | } |
45 | 44 | ||
46 | return pChild; | 45 | return pChild; |
47 | } | 46 | } |
48 | 47 | ||
diff --git a/src/tests/binary.cpp b/src/tests/binary.cpp index 727feec..3e2cbc7 100644 --- a/src/tests/binary.cpp +++ b/src/tests/binary.cpp | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <bu/sio.h> | 4 | #include <bu/sio.h> |
5 | #include <bu/random.h> | 5 | #include <bu/random.h> |
6 | #include <bu/randomcmwc.h> | 6 | #include <bu/randomcmwc.h> |
7 | #include <bu/randomsystem.h> | ||
7 | #include <time.h> | 8 | #include <time.h> |
8 | 9 | ||
9 | using namespace Bu; | 10 | using namespace Bu; |
@@ -13,7 +14,7 @@ int main( int argc, char *argv[] ) | |||
13 | Bu::Random::setGenerator<Bu::RandomCmwc>(); | 14 | Bu::Random::setGenerator<Bu::RandomCmwc>(); |
14 | Bu::Random::seed( time( NULL ) ); | 15 | Bu::Random::seed( time( NULL ) ); |
15 | 16 | ||
16 | Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 422*1024 ), 0.0 ); | 17 | Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 3453120 ), 0.000125 ); |
17 | Genetic::Phenotype *pPb1 = op.random(); | 18 | Genetic::Phenotype *pPb1 = op.random(); |
18 | Genetic::Phenotype *pPb2 = op.random(); | 19 | Genetic::Phenotype *pPb2 = op.random(); |
19 | Genetic::Phenotype *pPb3; | 20 | Genetic::Phenotype *pPb3; |