From 693f9db274acb4d6794e4985d7df3e7db6ae36ec Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 8 May 2017 07:33:51 +0000 Subject: [PATCH] Finish implementing Chromosome and Gene --- src/Chromosome.cpp | 57 +++++++++++++++++++++++++++++++++++++--------- src/Chromosome.hpp | 13 ++++------- src/Genes.hpp | 12 ++++++++++ 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/Chromosome.cpp b/src/Chromosome.cpp index 5eacaf7..15ea253 100644 --- a/src/Chromosome.cpp +++ b/src/Chromosome.cpp @@ -3,10 +3,10 @@ std::minstd_rand* Chromosome::re; -std::unordered_set Chromosome::allowedGeneTypes; +std::unordered_set Chromosome::allowedGeneTypes; -void Chromosome::allowGeneType(GeneType gt, bool allowed) +void Chromosome::allowGeneType(Gene::GeneType gt, bool allowed) { if (allowed) { Chromosome::allowedGeneTypes.insert(gt); @@ -16,7 +16,7 @@ void Chromosome::allowGeneType(GeneType gt, bool allowed) } -bool Chromosome::isGeneTypeAllowed(GeneType gt) +bool Chromosome::isGeneTypeAllowed(Gene::GeneType gt) { return Chromosome::allowedGeneTypes.find(gt) != Chromosome::allowedGeneTypes.end(); } @@ -35,15 +35,12 @@ Chromosome::Chromosome(Chromosome& father, Chromosome& mother) // reserve to father's size/capacity, and shrink_to_fit afterwards? - Gene* geneptr; for (auto it=father.genes.begin(); it!=split_father; ++it) { - *geneptr = **it; - this->genes.push_back(std::unique_ptr(geneptr)); + this->genes.push_back(std::unique_ptr(this->copyGene(it->get()))); } for (auto it=split_mother; it!=mother.genes.end(); ++it) { - *geneptr = **it; - this->genes.push_back(std::unique_ptr(geneptr)); + this->genes.push_back(std::unique_ptr(this->copyGene(it->get()))); } } @@ -93,8 +90,49 @@ std::vector>::iterator Chromosome::selectGene() } +Gene* Chromosome::copyGene(Gene* gene) +{ + switch (gene->type) { + case Gene::Circle: + { + GeneCircle* newgene = new GeneCircle(); + *newgene = *static_cast(gene); + return newgene; + } + case Gene::Triangle: + { + GeneTriangle* newgene = new GeneTriangle(); + *newgene = *static_cast(gene); + return newgene; + } + } + + return nullptr; +} + + +Gene* Chromosome::createGene(Gene::GeneType type) +{ + switch (type) { + case Gene::Circle: + return new GeneCircle(); + case Gene::Triangle: + return new GeneTriangle(); + } + + return nullptr; +} + + void Chromosome::addGene() { + std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1); + + auto it = this->allowedGeneTypes.begin(); + std::advance(it, typedist(*Gene::re)); + + const Gene::GeneType type = *it; + this->genes.push_back(std::unique_ptr(this->createGene(type))); } @@ -113,9 +151,6 @@ void Chromosome::swapGenes() auto it_two = this->selectGene(); if (it_one != this->genes.end() && it_two != this->genes.end() && it_one != it_two) { it_one->swap(*it_two); -// auto tmp = *it_one; -// *it_one = *it_two; -// *it_two = tmp; } } diff --git a/src/Chromosome.hpp b/src/Chromosome.hpp index 42fe79e..62adb0f 100644 --- a/src/Chromosome.hpp +++ b/src/Chromosome.hpp @@ -12,15 +12,10 @@ class Chromosome : public sf::Drawable { public: - enum GeneType - { - Circle - }; - static std::minstd_rand* re; - static void allowGeneType(GeneType gt, bool allowed=true); - static bool isGeneTypeAllowed(GeneType gt); + static void allowGeneType(Gene::GeneType gt, bool allowed=true); + static bool isGeneTypeAllowed(Gene::GeneType gt); Chromosome(); // create empty chromosome Chromosome(Chromosome& father, Chromosome& mother); // crossover @@ -32,11 +27,13 @@ public: size_t length() const; private: - static std::unordered_set allowedGeneTypes; + static std::unordered_set allowedGeneTypes; std::vector> genes; std::vector>::iterator selectGene(); + Gene* copyGene(Gene* gene); + Gene* createGene(Gene::GeneType type); void addGene(); void removeGene(); diff --git a/src/Genes.hpp b/src/Genes.hpp index 605041b..7e6b078 100644 --- a/src/Genes.hpp +++ b/src/Genes.hpp @@ -12,6 +12,14 @@ public: static std::minstd_rand* re; static sf::Vector2f size; + enum GeneType + { + Circle, + Triangle + }; + + GeneType type; + virtual ~Gene(); virtual void randomize() =0; @@ -23,6 +31,8 @@ public: class GeneCircle : public Gene { public: + GeneType type = Gene::Circle; + virtual ~GeneCircle(); virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; @@ -51,6 +61,8 @@ private: class GeneTriangle : public Gene { public: + GeneType type = Gene::Triangle; + virtual ~GeneTriangle(); virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;