Adjust circle radius and position limits

This commit is contained in:
Joscha 2017-04-25 21:24:16 +00:00
parent 8f47088a56
commit 0183d4737c
3 changed files with 29 additions and 11 deletions

View file

@ -109,17 +109,23 @@ void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const
} }
size_t Chromosome::length()
{
return this->genes.size();
}
Chromosome::Gene Chromosome::randomGene() Chromosome::Gene Chromosome::randomGene()
{ {
float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/2; float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/4;
std::uniform_real_distribution<> xdist(-max_radius, Chromosome::size.x + max_radius); std::uniform_real_distribution<> xdist(0, Chromosome::size.x);
std::uniform_real_distribution<> ydist(-max_radius, Chromosome::size.y + max_radius); std::uniform_real_distribution<> ydist(0, Chromosome::size.y);
std::uniform_real_distribution<> rdist(0, sqrt(max_radius)); std::uniform_real_distribution<> rdist(0, sqrt(max_radius));
std::uniform_int_distribution<> colordist(0, 255); std::uniform_int_distribution<> colordist(0, 255);
sf::Vector2f position(xdist(*Chromosome::re), ydist(*Chromosome::re)); sf::Vector2f position(xdist(*Chromosome::re), ydist(*Chromosome::re));
float radius = (pow(rdist(*Chromosome::re), 2)); float radius = (pow(rdist(*Chromosome::re), 2));
sf::Color color(colordist(*Chromosome::re), colordist(*Chromosome::re), colordist(*Chromosome::re)); sf::Color color(colordist(*Chromosome::re), colordist(*Chromosome::re), colordist(*Chromosome::re), 50);
Chromosome::Gene gene; Chromosome::Gene gene;
gene.position = position; gene.position = position;
@ -133,19 +139,19 @@ Chromosome::Gene Chromosome::randomGene()
void Chromosome::mutateGene(Gene& gene) void Chromosome::mutateGene(Gene& gene)
{ {
std::uniform_int_distribution<> booldist(0, 1); std::uniform_int_distribution<> booldist(0, 1);
float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/2; float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/4;
if (booldist(*Chromosome::re)) { // position if (booldist(*Chromosome::re)) { // position
std::normal_distribution<> posdist(0, Chromosome::stddev_position); std::normal_distribution<> posdist(0, Chromosome::stddev_position);
gene.position.x = std::clamp<float>( gene.position.x = std::clamp<float>(
gene.position.x + posdist(*Chromosome::re)*max_radius, gene.position.x + posdist(*Chromosome::re)*max_radius,
-max_radius, 0,
Chromosome::size.x + max_radius Chromosome::size.x
); );
gene.position.y = std::clamp<float>( gene.position.y = std::clamp<float>(
gene.position.y + posdist(*Chromosome::re)*max_radius, gene.position.y + posdist(*Chromosome::re)*max_radius,
-max_radius, 0,
Chromosome::size.y + max_radius Chromosome::size.y
); );
} }

View file

@ -24,6 +24,8 @@ public:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
size_t length();
protected: protected:
struct Gene struct Gene
{ {

View file

@ -177,14 +177,24 @@ int main() {
// Exit the app when a key is pressed // Exit the app when a key is pressed
if (event.type == sf::Event::KeyPressed) { if (event.type == sf::Event::KeyPressed) {
father = Chromosome(); father = Chromosome();
mother = Chromosome(); // mother = Chromosome();
for (int i=0; i<100; ++i) { for (int i=0; i<1000; ++i) {
father.mutate(); father.mutate();
// mother.mutate();
}
mother = father;
for (int i=0; i<20; ++i) {
mother.mutate(); mother.mutate();
} }
child = Chromosome(father, mother); child = Chromosome(father, mother);
monster = child; monster = child;
monster.mutate(); monster.mutate();
std::cout << "----------SIZES----------" << std::endl;
std::cout << "father size: " << father.length() << std::endl;
std::cout << "mother size: " << mother.length() << std::endl;
std::cout << "child size: " << child.length() << std::endl;
std::cout << "monster size: " << monster.length() << std::endl;
} }
} }