Implement more Chromosome

Properly implement copy constructor and copy assignment operator,
so I can use them in vectors etc.
This commit is contained in:
Joscha 2017-05-18 15:41:26 +00:00
parent 0784268046
commit d032b3eabf
2 changed files with 44 additions and 15 deletions

View file

@ -28,20 +28,10 @@ Chromosome::Chromosome()
} }
Chromosome::Chromosome(Chromosome& father) Chromosome::Chromosome(const Chromosome& father, const Chromosome& mother)
{ {
// reserve to father's size/capacity? auto split_father = father.selectConstGene();
auto split_mother = mother.selectConstGene();
for (auto& ptr : father.genes) {
this->genes.push_back(std::unique_ptr<Gene>(Gene::copy(ptr.get())));
}
}
Chromosome::Chromosome(Chromosome& father, Chromosome& mother)
{
auto split_father = father.selectGene();
auto split_mother = mother.selectGene();
// reserve to father's size/capacity, and shrink_to_fit afterwards? // 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>(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>(Gene::copy(ptr.get())));
}
}
return *this;
}
void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ {
for (auto& geneptr : this->genes) { for (auto& geneptr : this->genes) {
@ -100,6 +114,17 @@ std::vector<std::unique_ptr<Gene>>::iterator Chromosome::selectGene()
} }
std::vector<std::unique_ptr<Gene>>::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() void Chromosome::addGene()
{ {
std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1); std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1);

View file

@ -18,8 +18,11 @@ public:
static bool isGeneTypeAllowed(Gene::GeneType gt); static bool isGeneTypeAllowed(Gene::GeneType gt);
Chromosome(); // create empty chromosome Chromosome(); // create empty chromosome
Chromosome(Chromosome& father); // copy Chromosome(const Chromosome& father, const Chromosome& mother); // crossover
Chromosome(Chromosome& father, 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; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
@ -33,6 +36,7 @@ private:
std::vector<std::unique_ptr<Gene>> genes; std::vector<std::unique_ptr<Gene>> genes;
std::vector<std::unique_ptr<Gene>>::iterator selectGene(); std::vector<std::unique_ptr<Gene>>::iterator selectGene();
std::vector<std::unique_ptr<Gene>>::const_iterator selectConstGene() const;
void addGene(); void addGene();
void removeGene(); void removeGene();