From 2406848173c445a94a1710106116ad796a8bacb9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 4 Aug 2012 18:34:57 -0600 Subject: Made some changes to make serializing workable. There's no official format yet, but that's coming, most likely. --- src/explicitsimulation.h | 3 +++ src/phenotype.h | 4 ++++ src/phenotypebinary.cpp | 42 ++++++++++++++++++++++++++++++++---------- src/phenotypebinary.h | 8 +++----- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/explicitsimulation.h b/src/explicitsimulation.h index 7713c71..e71b489 100644 --- a/src/explicitsimulation.h +++ b/src/explicitsimulation.h @@ -22,11 +22,14 @@ namespace Genetic virtual ~ExplicitSimulation(); void timestep(); + const Population &getPopulation() const { return xPop; } Genetic::PhenotypeId selectWeighted(); double getMinFitness() const { return dMinFitness; } double getMaxFitness() const { return dMaxFitness; } + double getFitness( Genetic::PhenotypeId id ) const + { return hFitness.get( id ); } protected: void updateFitness(); diff --git a/src/phenotype.h b/src/phenotype.h index 0eafb78..27173e2 100644 --- a/src/phenotype.h +++ b/src/phenotype.h @@ -1,6 +1,7 @@ #ifndef GENETIC_PHENOTYPE_H #define GENETIC_PHENOTYPE_H +#include #include #include #include @@ -33,6 +34,9 @@ namespace Genetic virtual Bu::String toString()=0; + virtual void write( Bu::Stream &rStream ) {}; + virtual void read( Bu::Stream &rStream ) {}; + bool hasProperty( const Bu::String &sKey ) const; Bu::Variant getProperty( const Bu::String &sKey ) const; void setProperty( const Bu::String &sKey, Bu::Variant vValue ); diff --git a/src/phenotypebinary.cpp b/src/phenotypebinary.cpp index 9d9e26c..8ecd394 100644 --- a/src/phenotypebinary.cpp +++ b/src/phenotypebinary.cpp @@ -155,6 +155,38 @@ Bu::String Genetic::PhenotypeBinary::toString() return sRet; } +void Genetic::PhenotypeBinary::write( Bu::Stream &rStream ) +{ + uint32_t uSize = htobe32( iSize ); + rStream.write( &uSize, 4 ); + uSize = (iSize/8) + ((iSize%8)?1:0); + for( int j = 0; j < uSize; j++ ) + { + rStream.write( + &((char *)&aGenes[j/sizeof(uint_fast32_t)])[j%sizeof(uint_fast32_t)], + 1 + ); + } +} + +void Genetic::PhenotypeBinary::read( Bu::Stream &rStream ) +{ + uint32_t uSize; + rStream.read( &uSize, 4 ); + iSize = be32toh( uSize ); + iWords = wordsForBits(iSize); + delete[] aGenes; + aGenes = new uint_fast32_t[iWords]; + uSize = (iSize/8) + ((iSize%8)?1:0); + for( int j = 0; j < uSize; j++ ) + { + rStream.read( + &((char *)&aGenes[j/sizeof(uint_fast32_t)])[j%sizeof(uint_fast32_t)], + 1 + ); + } +} + void Genetic::PhenotypeBinary::extractBits( uint32_t &rTarget, int iStart, int iBits ) { @@ -189,13 +221,3 @@ void Genetic::PhenotypeBinary::extractBits( uint64_t &rTarget, int iStart, } } -void Genetic::PhenotypeBinary::write( Bu::Stream &rStream ) -{ - rStream.write( aGenes, sizeof(uint_fast32_t)*iWords ); -} - -void Genetic::PhenotypeBinary::read( Bu::Stream &rStream ) -{ - rStream.read( aGenes, sizeof(uint_fast32_t)*iWords ); -} - diff --git a/src/phenotypebinary.h b/src/phenotypebinary.h index f312187..92146d2 100644 --- a/src/phenotypebinary.h +++ b/src/phenotypebinary.h @@ -3,8 +3,6 @@ #include "genetic/phenotype.h" -#include - namespace Genetic { class PhenotypeBinary : public Phenotype @@ -21,12 +19,12 @@ namespace Genetic int iCount, int iDest=-1 ); virtual Bu::String toString(); + virtual void write( Bu::Stream &rStream ); + virtual void read( Bu::Stream &rStream ); + void extractBits( uint32_t &rTarget, int iStart, int iBits ); void extractBits( uint64_t &rTarget, int iStart, int iBits ); - void write( Bu::Stream &rStream ); - void read( Bu::Stream &rStream ); - private: int iSize; int iWords; -- cgit v1.2.3