Add GeneTriangle
Fix a few Gene type issues
This commit is contained in:
parent
d032b3eabf
commit
901a5f2b3c
3 changed files with 121 additions and 51 deletions
148
src/Genes.cpp
148
src/Genes.cpp
|
|
@ -50,6 +50,54 @@ Gene* Gene::copy(Gene* gene)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Gene::randomizeColor(sf::Color& color)
|
||||||
|
{
|
||||||
|
std::uniform_int_distribution<> colordist(0, 255);
|
||||||
|
color.r = colordist(*Gene::re);
|
||||||
|
color.g = colordist(*Gene::re);
|
||||||
|
color.b = colordist(*Gene::re);
|
||||||
|
color.a = colordist(*Gene::re);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Gene::mutateColor(sf::Color& color, float stddev)
|
||||||
|
{
|
||||||
|
std::normal_distribution<> coldist(0, stddev);
|
||||||
|
|
||||||
|
color.r = std::clamp<unsigned int>(color.r + coldist(*Gene::re), 0, 255);
|
||||||
|
color.g = std::clamp<unsigned int>(color.g + coldist(*Gene::re), 0, 255);
|
||||||
|
color.b = std::clamp<unsigned int>(color.b + coldist(*Gene::re), 0, 255);
|
||||||
|
color.a = std::clamp<unsigned int>(color.a + coldist(*Gene::re), 0, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Gene::randomizePosition(sf::Vector2f& position)
|
||||||
|
{
|
||||||
|
std::uniform_real_distribution<> floatdist(0, 1);
|
||||||
|
position.x = floatdist(*Gene::re)*Gene::size.x;
|
||||||
|
position.y = floatdist(*Gene::re)*Gene::size.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Gene::mutatePosition(sf::Vector2f& position, float stddev)
|
||||||
|
{
|
||||||
|
std::normal_distribution<> posdist(0, stddev);
|
||||||
|
float min_side = std::min(Gene::size.x, Gene::size.y);
|
||||||
|
|
||||||
|
position.x = std::clamp<float>(
|
||||||
|
position.x + posdist(*Gene::re)*min_side,
|
||||||
|
0,
|
||||||
|
Gene::size.x
|
||||||
|
);
|
||||||
|
|
||||||
|
position.y = std::clamp<float>(
|
||||||
|
position.y + posdist(*Gene::re)*min_side,
|
||||||
|
0,
|
||||||
|
Gene::size.y
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Gene::~Gene()
|
Gene::~Gene()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -62,6 +110,13 @@ 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()
|
||||||
|
{
|
||||||
|
this->type = Gene::Circle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
GeneCircle::~GeneCircle()
|
GeneCircle::~GeneCircle()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -69,25 +124,20 @@ GeneCircle::~GeneCircle()
|
||||||
|
|
||||||
void GeneCircle::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void GeneCircle::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
this->circle.setPosition(this->position);
|
GeneCircle::circle.setPosition(this->position);
|
||||||
this->circle.setRadius(this->radius);
|
GeneCircle::circle.setRadius(this->radius);
|
||||||
this->circle.setOrigin(this->radius, this->radius);
|
GeneCircle::circle.setOrigin(this->radius, this->radius);
|
||||||
this->circle.setFillColor(this->color);
|
GeneCircle::circle.setFillColor(this->color);
|
||||||
target.draw(this->circle, states);
|
target.draw(GeneCircle::circle, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneCircle::randomize()
|
void GeneCircle::randomize()
|
||||||
{
|
{
|
||||||
std::uniform_int_distribution<> colordist(0, 255);
|
Gene::randomizeColor(this->color);
|
||||||
this->color.r = colordist(*Gene::re);
|
Gene::randomizePosition(this->position);
|
||||||
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);
|
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();
|
this->radius = floatdist(*Gene::re)*this->maxRadius();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,37 +148,18 @@ void GeneCircle::mutate()
|
||||||
|
|
||||||
switch (choicedist(*Gene::re)) {
|
switch (choicedist(*Gene::re)) {
|
||||||
case 0:
|
case 0:
|
||||||
this->mutatePosition();
|
Gene::mutatePosition(this->position, GeneCircle::stddev_position);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
this->mutateRadius();
|
this->mutateRadius();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
this->mutateColor();
|
Gene::mutateColor(this->color, GeneCircle::stddev_color);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneCircle::mutatePosition()
|
|
||||||
{
|
|
||||||
std::normal_distribution<> posdist(0, GeneCircle::stddev_position);
|
|
||||||
float min_side = std::min(Gene::size.x, Gene::size.y);
|
|
||||||
|
|
||||||
this->position.x = std::clamp<float>(
|
|
||||||
this->position.x + posdist(*Gene::re)*min_side,
|
|
||||||
0,
|
|
||||||
Gene::size.x
|
|
||||||
);
|
|
||||||
|
|
||||||
this->position.y = std::clamp<float>(
|
|
||||||
this->position.y + posdist(*Gene::re)*min_side,
|
|
||||||
0,
|
|
||||||
Gene::size.y
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void GeneCircle::mutateRadius()
|
void GeneCircle::mutateRadius()
|
||||||
{
|
{
|
||||||
std::normal_distribution<> raddist(0, GeneCircle::stddev_radius);
|
std::normal_distribution<> raddist(0, GeneCircle::stddev_radius);
|
||||||
|
|
@ -143,17 +174,6 @@ void GeneCircle::mutateRadius()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneCircle::mutateColor()
|
|
||||||
{
|
|
||||||
std::normal_distribution<> coldist(0, GeneCircle::stddev_color);
|
|
||||||
|
|
||||||
this->color.r = std::clamp<unsigned int>(this->color.r + coldist(*Gene::re), 0, 255);
|
|
||||||
this->color.g = std::clamp<unsigned int>(this->color.g + coldist(*Gene::re), 0, 255);
|
|
||||||
this->color.b = std::clamp<unsigned int>(this->color.b + coldist(*Gene::re), 0, 255);
|
|
||||||
this->color.a = std::clamp<unsigned int>(this->color.a + coldist(*Gene::re), 0, 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float GeneCircle::maxRadius()
|
float GeneCircle::maxRadius()
|
||||||
{
|
{
|
||||||
// return std::min(Gene::size.x, Gene::size.y);
|
// return std::min(Gene::size.x, Gene::size.y);
|
||||||
|
|
@ -163,6 +183,17 @@ float GeneCircle::maxRadius()
|
||||||
|
|
||||||
|
|
||||||
// GeneTriangle
|
// GeneTriangle
|
||||||
|
float GeneTriangle::stddev_position = .1;
|
||||||
|
float GeneTriangle::stddev_color = 20;
|
||||||
|
sf::VertexArray GeneTriangle::vertices(sf::Triangles, 3);
|
||||||
|
|
||||||
|
|
||||||
|
GeneTriangle::GeneTriangle()
|
||||||
|
{
|
||||||
|
this->type = Gene::Triangle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
GeneTriangle::~GeneTriangle()
|
GeneTriangle::~GeneTriangle()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -170,14 +201,43 @@ GeneTriangle::~GeneTriangle()
|
||||||
|
|
||||||
void GeneTriangle::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void GeneTriangle::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
|
GeneTriangle::vertices[0].position = this->pos1;
|
||||||
|
GeneTriangle::vertices[1].position = this->pos2;
|
||||||
|
GeneTriangle::vertices[2].position = this->pos3;
|
||||||
|
|
||||||
|
GeneTriangle::vertices[0].color = this->color;
|
||||||
|
GeneTriangle::vertices[1].color = this->color;
|
||||||
|
GeneTriangle::vertices[2].color = this->color;
|
||||||
|
|
||||||
|
target.draw(GeneTriangle::vertices, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneTriangle::randomize()
|
void GeneTriangle::randomize()
|
||||||
{
|
{
|
||||||
|
Gene::randomizePosition(this->pos1);
|
||||||
|
Gene::randomizePosition(this->pos2);
|
||||||
|
Gene::randomizePosition(this->pos3);
|
||||||
|
Gene::randomizeColor(this->color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneTriangle::mutate()
|
void GeneTriangle::mutate()
|
||||||
{
|
{
|
||||||
|
std::uniform_int_distribution<> choicedist(0, 3);
|
||||||
|
|
||||||
|
switch (choicedist(*Gene::re)) {
|
||||||
|
case 0:
|
||||||
|
Gene::mutatePosition(this->pos1, this->stddev_position);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Gene::mutatePosition(this->pos2, this->stddev_position);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Gene::mutatePosition(this->pos3, this->stddev_position);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
Gene::mutateColor(this->color, this->stddev_color);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,12 @@ public:
|
||||||
static Gene* create(GeneType type);
|
static Gene* create(GeneType type);
|
||||||
static Gene* copy(Gene* gene);
|
static Gene* copy(Gene* gene);
|
||||||
|
|
||||||
|
// common randomizations/mutations
|
||||||
|
static void randomizeColor(sf::Color& color);
|
||||||
|
static void mutateColor(sf::Color& color, float stddev);
|
||||||
|
static void randomizePosition(sf::Vector2f& position);
|
||||||
|
static void mutatePosition(sf::Vector2f& position, float stddev);
|
||||||
|
|
||||||
|
|
||||||
GeneType type;
|
GeneType type;
|
||||||
|
|
||||||
|
|
@ -39,8 +45,7 @@ public:
|
||||||
static float stddev_radius;
|
static float stddev_radius;
|
||||||
static float stddev_color;
|
static float stddev_color;
|
||||||
|
|
||||||
GeneType type = Gene::Circle;
|
GeneCircle();
|
||||||
|
|
||||||
virtual ~GeneCircle();
|
virtual ~GeneCircle();
|
||||||
|
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
|
|
@ -54,10 +59,7 @@ private:
|
||||||
float radius;
|
float radius;
|
||||||
sf::Color color;
|
sf::Color color;
|
||||||
|
|
||||||
void mutatePosition();
|
|
||||||
void mutateRadius();
|
void mutateRadius();
|
||||||
void mutateColor();
|
|
||||||
|
|
||||||
float maxRadius();
|
float maxRadius();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -66,8 +68,10 @@ private:
|
||||||
class GeneTriangle : public Gene
|
class GeneTriangle : public Gene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GeneType type = Gene::Triangle;
|
static float stddev_position;
|
||||||
|
static float stddev_color;
|
||||||
|
|
||||||
|
GeneTriangle();
|
||||||
virtual ~GeneTriangle();
|
virtual ~GeneTriangle();
|
||||||
|
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
|
|
@ -75,5 +79,10 @@ public:
|
||||||
virtual void mutate();
|
virtual void mutate();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// TODO: vertices
|
static sf::VertexArray vertices;
|
||||||
|
|
||||||
|
sf::Vector2f pos1;
|
||||||
|
sf::Vector2f pos2;
|
||||||
|
sf::Vector2f pos3;
|
||||||
|
sf::Color color;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ int main()
|
||||||
randomEngine.seed(time(nullptr));
|
randomEngine.seed(time(nullptr));
|
||||||
|
|
||||||
Chromosome::allowGeneType(Gene::Circle);
|
Chromosome::allowGeneType(Gene::Circle);
|
||||||
|
Chromosome::allowGeneType(Gene::Triangle);
|
||||||
|
|
||||||
Chromosome::re = &randomEngine;
|
Chromosome::re = &randomEngine;
|
||||||
Gene::re = &randomEngine;
|
Gene::re = &randomEngine;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue