diff --git a/src/Chromosome.cpp b/src/Chromosome.cpp index 1e40967..eae6a21 100644 --- a/src/Chromosome.cpp +++ b/src/Chromosome.cpp @@ -28,20 +28,10 @@ Chromosome::Chromosome() } -Chromosome::Chromosome(Chromosome& father) +Chromosome::Chromosome(const Chromosome& father, const Chromosome& mother) { - // reserve to father's size/capacity? - - for (auto& ptr : father.genes) { - this->genes.push_back(std::unique_ptr(Gene::copy(ptr.get()))); - } -} - - -Chromosome::Chromosome(Chromosome& father, Chromosome& mother) -{ - auto split_father = father.selectGene(); - auto split_mother = mother.selectGene(); + auto split_father = father.selectConstGene(); + auto split_mother = mother.selectConstGene(); // reserve to father's size/capacity, and shrink_to_fit afterwards? @@ -55,6 +45,30 @@ Chromosome::Chromosome(Chromosome& father, Chromosome& mother) } +Chromosome::Chromosome(const Chromosome& other) +{ + // reserve to other's size/capacity? + + for (auto& ptr : other.genes) { + this->genes.push_back(std::unique_ptr(Gene::copy(ptr.get()))); + } +} + + +Chromosome& Chromosome::operator=(const Chromosome& other) +{ + // reserve to other's size/capacity? + + if (this != &other) { + for (auto& ptr : other.genes) { + this->genes.push_back(std::unique_ptr(Gene::copy(ptr.get()))); + } + } + + return *this; +} + + void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const { for (auto& geneptr : this->genes) { @@ -100,6 +114,17 @@ std::vector>::iterator Chromosome::selectGene() } +std::vector>::const_iterator Chromosome::selectConstGene() const +{ + if (genes.empty()) { + return this->genes.end(); + } else { + std::uniform_int_distribution<> posdist(0, this->genes.size()-1); + return genes.begin() + posdist(*Chromosome::re); + } +} + + void Chromosome::addGene() { std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1); diff --git a/src/Chromosome.hpp b/src/Chromosome.hpp index b35a4cb..c1e69e2 100644 --- a/src/Chromosome.hpp +++ b/src/Chromosome.hpp @@ -18,8 +18,11 @@ public: static bool isGeneTypeAllowed(Gene::GeneType gt); Chromosome(); // create empty chromosome - Chromosome(Chromosome& father); // copy - Chromosome(Chromosome& father, Chromosome& mother); // crossover + Chromosome(const Chromosome& father, const Chromosome& mother); // crossover + + Chromosome(const Chromosome& father); // copy constructor +// Chromosome& operator=(const Chromosome& chr) = delete; // copy assignment operator + Chromosome& operator=(const Chromosome& chr); // copy assignment operator virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; @@ -33,6 +36,7 @@ private: std::vector> genes; std::vector>::iterator selectGene(); + std::vector>::const_iterator selectConstGene() const; void addGene(); void removeGene();