From b635429affebfb60eb4e932d19f326f9a89e8f8a Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 2 May 2018 03:12:08 -0700 Subject: [PATCH 1/2] Increment generation number of kiddo after breeding --- Breeder.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Breeder.cpp b/Breeder.cpp index 1a35347..6c4c897 100644 --- a/Breeder.cpp +++ b/Breeder.cpp @@ -81,6 +81,9 @@ namespace BitEvolver // Reset kiddo's fitness kiddo->ResetFitness(); + // Increment kiddo's generation number + kiddo->IncrementGenerationNumber(); + return kiddo; } From 020c9e458d2d39caa1e40f12e0935c10213cb86e Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 2 May 2018 03:12:40 -0700 Subject: [PATCH 2/2] Remember Chromosome's generation; Allow set bits by string --- Chromosome.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ Chromosome.h | 12 +++++++++++ 2 files changed, 69 insertions(+) diff --git a/Chromosome.cpp b/Chromosome.cpp index c6b7761..c7b0e9b 100644 --- a/Chromosome.cpp +++ b/Chromosome.cpp @@ -26,6 +26,11 @@ namespace BitEvolver { // this->random = _random; + + // + this->generation_number = 1; + + // this->SetBitCount(_bits); // @@ -55,6 +60,27 @@ namespace BitEvolver } } + // + void Chromosome::SetGenerationNumber(int g) + { + // + this->generation_number = g; + } + + // + void Chromosome::IncrementGenerationNumber() + { + // + this->generation_number++; + } + + // + int Chromosome::GetGenerationNumber() + { + // + return this->generation_number; + } + // void Chromosome::SetBitCount(int count) { @@ -122,6 +148,36 @@ namespace BitEvolver this->bits[index] = b; } + // + void Chromosome::SetBits(string s) + { + // + std::unique_lock lock(this->modification_mutex); + size_t i; + char c; + + // + this->SetBitCount( (int) s.size() ); + + // + for ( i=0; ibits[i] = true; + } + else if ( c == '0' ) { + this->bits[i] = false; + } + else{ + stringstream ss; + ss << "Chromosome::SetBits() - Invalid character '" << c << "' (" << (int)c << ") in bit string input"; + throw std::runtime_error( ss.str() ); + } + } + } + // void Chromosome::ResetFitness() { @@ -210,6 +266,7 @@ namespace BitEvolver // this->random = other.random; + this->generation_number = other.generation_number; this->bits = other.bits; this->bits_count_desired = other.bits_count_desired; this->fitness = other.fitness; diff --git a/Chromosome.h b/Chromosome.h index b07eb37..665328c 100644 --- a/Chromosome.h +++ b/Chromosome.h @@ -19,6 +19,9 @@ // namespace BitEvolver { + // + using std::string; + // class Chromosome { @@ -32,6 +35,11 @@ namespace BitEvolver void Reset(); void Randomize(); + // + void SetGenerationNumber(int g); + void IncrementGenerationNumber(); + int GetGenerationNumber(); + // void SetBitCount(int count); int GetBitCount(); @@ -42,6 +50,7 @@ namespace BitEvolver // bool GetBit(int index); void SetBit(int index, bool b); + void SetBits(string s); // void ResetFitness(); @@ -69,6 +78,9 @@ namespace BitEvolver // Random number generator std::shared_ptr random; + // + int generation_number; + // std::vector bits; int bits_count_desired;