From 0183d4737c883383c91a43c218513d9a941db4af Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 25 Apr 2017 21:24:16 +0000 Subject: [PATCH] Adjust circle radius and position limits --- src/Chromosome.cpp | 24 +++++++++++++++--------- src/Chromosome.hpp | 2 ++ src/main.cpp | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Chromosome.cpp b/src/Chromosome.cpp index a57aed2..2d1d14d 100644 --- a/src/Chromosome.cpp +++ b/src/Chromosome.cpp @@ -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() { - float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/2; - std::uniform_real_distribution<> xdist(-max_radius, Chromosome::size.x + max_radius); - std::uniform_real_distribution<> ydist(-max_radius, Chromosome::size.y + max_radius); + float max_radius = std::min(Chromosome::size.x, Chromosome::size.y)/4; + std::uniform_real_distribution<> xdist(0, Chromosome::size.x); + std::uniform_real_distribution<> ydist(0, Chromosome::size.y); std::uniform_real_distribution<> rdist(0, sqrt(max_radius)); std::uniform_int_distribution<> colordist(0, 255); sf::Vector2f position(xdist(*Chromosome::re), ydist(*Chromosome::re)); 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; gene.position = position; @@ -133,19 +139,19 @@ Chromosome::Gene Chromosome::randomGene() void Chromosome::mutateGene(Gene& gene) { 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 std::normal_distribution<> posdist(0, Chromosome::stddev_position); gene.position.x = std::clamp( gene.position.x + posdist(*Chromosome::re)*max_radius, - -max_radius, - Chromosome::size.x + max_radius + 0, + Chromosome::size.x ); gene.position.y = std::clamp( gene.position.y + posdist(*Chromosome::re)*max_radius, - -max_radius, - Chromosome::size.y + max_radius + 0, + Chromosome::size.y ); } diff --git a/src/Chromosome.hpp b/src/Chromosome.hpp index 8d40275..79946dc 100644 --- a/src/Chromosome.hpp +++ b/src/Chromosome.hpp @@ -24,6 +24,8 @@ public: virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + size_t length(); + protected: struct Gene { diff --git a/src/main.cpp b/src/main.cpp index 2b3cada..ccd94d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -177,14 +177,24 @@ int main() { // Exit the app when a key is pressed if (event.type == sf::Event::KeyPressed) { father = Chromosome(); - mother = Chromosome(); - for (int i=0; i<100; ++i) { +// mother = Chromosome(); + for (int i=0; i<1000; ++i) { father.mutate(); +// mother.mutate(); + } + mother = father; + for (int i=0; i<20; ++i) { mother.mutate(); } child = Chromosome(father, mother); monster = child; 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; } }