Continue finishing to implement Chromosome and Gene

This commit is contained in:
Joscha 2017-05-12 11:12:44 +00:00
parent 693f9db274
commit bd45691996
4 changed files with 68 additions and 43 deletions

View file

@ -28,6 +28,16 @@ Chromosome::Chromosome()
} }
Chromosome::Chromosome(Chromosome& father)
{
// reserve to father's size/capacity?
for (auto& ptr : father.genes) {
this->genes.push_back(std::unique_ptr<Gene>(Gene::copy(ptr.get())));
}
}
Chromosome::Chromosome(Chromosome& father, Chromosome& mother) Chromosome::Chromosome(Chromosome& father, Chromosome& mother)
{ {
auto split_father = father.selectGene(); auto split_father = father.selectGene();
@ -36,11 +46,11 @@ Chromosome::Chromosome(Chromosome& father, Chromosome& mother)
// reserve to father's size/capacity, and shrink_to_fit afterwards? // reserve to father's size/capacity, and shrink_to_fit afterwards?
for (auto it=father.genes.begin(); it!=split_father; ++it) { for (auto it=father.genes.begin(); it!=split_father; ++it) {
this->genes.push_back(std::unique_ptr<Gene>(this->copyGene(it->get()))); this->genes.push_back(std::unique_ptr<Gene>(Gene::copy(it->get())));
} }
for (auto it=split_mother; it!=mother.genes.end(); ++it) { for (auto it=split_mother; it!=mother.genes.end(); ++it) {
this->genes.push_back(std::unique_ptr<Gene>(this->copyGene(it->get()))); this->genes.push_back(std::unique_ptr<Gene>(Gene::copy(it->get())));
} }
} }
@ -90,40 +100,6 @@ std::vector<std::unique_ptr<Gene>>::iterator Chromosome::selectGene()
} }
Gene* Chromosome::copyGene(Gene* gene)
{
switch (gene->type) {
case Gene::Circle:
{
GeneCircle* newgene = new GeneCircle();
*newgene = *static_cast<GeneCircle*>(gene);
return newgene;
}
case Gene::Triangle:
{
GeneTriangle* newgene = new GeneTriangle();
*newgene = *static_cast<GeneTriangle*>(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() void Chromosome::addGene()
{ {
std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1); std::uniform_int_distribution<> typedist(0, this->allowedGeneTypes.size()-1);
@ -132,7 +108,11 @@ void Chromosome::addGene()
std::advance(it, typedist(*Gene::re)); std::advance(it, typedist(*Gene::re));
const Gene::GeneType type = *it; const Gene::GeneType type = *it;
this->genes.push_back(std::unique_ptr<Gene>(this->createGene(type)));
Gene* gene = Gene::create(type);
gene->randomize();
this->genes.push_back(std::unique_ptr<Gene>(gene));
// this->genes.push_back(std::unique_ptr<Gene>(Gene::create(type)));
} }

View file

@ -18,6 +18,7 @@ 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(Chromosome& father, Chromosome& mother); // crossover Chromosome(Chromosome& father, Chromosome& mother); // crossover
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
@ -32,8 +33,6 @@ 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();
Gene* copyGene(Gene* gene);
Gene* createGene(Gene::GeneType type);
void addGene(); void addGene();
void removeGene(); void removeGene();

View file

@ -19,6 +19,37 @@ std::minstd_rand* Gene::re;
sf::Vector2f Gene::size; sf::Vector2f Gene::size;
Gene* Gene::create(GeneType type)
{
Gene* gene = nullptr;
switch (type) {
case Gene::Circle:
gene = new GeneCircle();
break;
case Gene::Triangle:
gene = new GeneTriangle();
break;
}
gene->randomize();
return gene;
}
Gene* Gene::copy(Gene* gene)
{
switch (gene->type) {
case Gene::Circle:
return new GeneCircle(*static_cast<GeneCircle*>(gene));
case Gene::Triangle:
return new GeneTriangle(*static_cast<GeneTriangle*>(gene));
}
return nullptr;
}
Gene::~Gene() Gene::~Gene()
{ {
} }
@ -31,7 +62,6 @@ float GeneCircle::stddev_position = .1; // in percent of min side length
float GeneCircle::stddev_radius = .1; // in percent of max radius float GeneCircle::stddev_radius = .1; // in percent of max radius
float GeneCircle::stddev_color = 20; // absolute value float GeneCircle::stddev_color = 20; // absolute value
GeneCircle::~GeneCircle() GeneCircle::~GeneCircle()
{ {
} }
@ -41,6 +71,7 @@ void GeneCircle::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ {
this->circle.setPosition(this->position); this->circle.setPosition(this->position);
this->circle.setRadius(this->radius); this->circle.setRadius(this->radius);
this->circle.setOrigin(this->radius, this->radius);
this->circle.setFillColor(this->color); this->circle.setFillColor(this->color);
target.draw(this->circle, states); target.draw(this->circle, states);
} }
@ -48,6 +79,16 @@ void GeneCircle::draw(sf::RenderTarget& target, sf::RenderStates states) const
void GeneCircle::randomize() void GeneCircle::randomize()
{ {
std::uniform_int_distribution<> colordist(0, 255);
this->color.r = colordist(*Gene::re);
this->color.g = colordist(*Gene::re);
this->color.b = colordist(*Gene::re);
this->color.a = colordist(*Gene::re);
std::uniform_real_distribution<> floatdist(0, 1);
this->position.x = floatdist(*Gene::re)*Gene::size.x;
this->position.y = floatdist(*Gene::re)*Gene::size.y;
this->radius = floatdist(*Gene::re)*this->maxRadius();
} }

View file

@ -18,6 +18,10 @@ public:
Triangle Triangle
}; };
static Gene* create(GeneType type);
static Gene* copy(Gene* gene);
GeneType type; GeneType type;
virtual ~Gene(); virtual ~Gene();
@ -31,6 +35,10 @@ public:
class GeneCircle : public Gene class GeneCircle : public Gene
{ {
public: public:
static float stddev_position;
static float stddev_radius;
static float stddev_color;
GeneType type = Gene::Circle; GeneType type = Gene::Circle;
virtual ~GeneCircle(); virtual ~GeneCircle();
@ -41,9 +49,6 @@ public:
private: private:
static sf::CircleShape circle; static sf::CircleShape circle;
static float stddev_position;
static float stddev_radius;
static float stddev_color;
sf::Vector2f position; sf::Vector2f position;
float radius; float radius;