From a550f267df36f4f134dd4940d3ee7a7cac01f5e7 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 7 May 2017 19:02:33 +0000 Subject: [PATCH] Reorganize Gene code --- Makefile | 2 +- src/Genes.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Genes.hpp | 62 ++++++++++++++++++++++ 3 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 src/Genes.cpp create mode 100644 src/Genes.hpp diff --git a/Makefile b/Makefile index d355957..1cd822d 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ SRCDIR = src TMPDIR = build TARGET = gross #FILES = Chromosome Fitness Generation main -FILES = Chromosome Fitness Generation main +FILES = Genes #SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES)) OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES)) diff --git a/src/Genes.cpp b/src/Genes.cpp new file mode 100644 index 0000000..5deefcd --- /dev/null +++ b/src/Genes.cpp @@ -0,0 +1,142 @@ +#include "Genes.hpp" + +#include + +#if __GNUC__ < 7 // gcc 7 will support clamp +namespace std { + template + T clamp(T v, T lo, T hi) + { + return std::min(hi, std::max(lo, v)); + } +} +#endif + + + +// Common to all genes +std::minstd_rand* Gene::re; +sf::Vector2f Gene::size; + + +Gene::~Gene() +{ +} + + + +// GeneCircle +sf::CircleShape GeneCircle::circle(0, 36); +float GeneCircle::stddev_position = .1; // in percent of min side length +float GeneCircle::stddev_radius = .1; // in percent of max radius +float GeneCircle::stddev_color = 20; // absolute value + + +GeneCircle::~GeneCircle() +{ +} + + +void GeneCircle::draw(sf::RenderTarget& target, sf::RenderStates states) const +{ + this->circle.setPosition(this->position); + this->circle.setRadius(this->radius); + this->circle.setFillColor(this->color); + target.draw(this->circle, states); +} + + +void GeneCircle::randomize() +{ +} + + +void GeneCircle::mutate() +{ + std::uniform_int_distribution<> choicedist(0, 2); + + switch (choicedist(*Gene::re)) { + case 0: + this->mutatePosition(); + break; + case 1: + this->mutateRadius(); + break; + case 2: + this->mutateColor(); + 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( + this->position.x + posdist(*Gene::re)*min_side, + 0, + Gene::size.x + ); + + this->position.y = std::clamp( + this->position.y + posdist(*Gene::re)*min_side, + 0, + Gene::size.y + ); +} + + +void GeneCircle::mutateRadius() +{ + std::normal_distribution<> raddist(0, GeneCircle::stddev_radius); + float max_radius = this->maxRadius(); + + this->radius = std::clamp( +// gene.radius + pow(raddist(*Gene::re)*sqrt(max_radius), 2), + this->radius + raddist(*Gene::re)*max_radius, + 0, + max_radius + ); +} + + +void GeneCircle::mutateColor() +{ + std::normal_distribution<> coldist(0, GeneCircle::stddev_color); + + this->color.r = std::clamp(this->color.r + coldist(*Gene::re), 0, 255); + this->color.g = std::clamp(this->color.g + coldist(*Gene::re), 0, 255); + this->color.b = std::clamp(this->color.b + coldist(*Gene::re), 0, 255); + this->color.a = std::clamp(this->color.a + coldist(*Gene::re), 0, 255); +} + + +float GeneCircle::maxRadius() +{ +// return std::min(Gene::size.x, Gene::size.y); + return std::min(Gene::size.x, Gene::size.y)/2; +} + + + +// GeneTriangle +GeneTriangle::~GeneTriangle() +{ +} + + +void GeneTriangle::draw(sf::RenderTarget& target, sf::RenderStates states) const +{ +} + + +void GeneTriangle::randomize() +{ +} + + +void GeneTriangle::mutate() +{ +} diff --git a/src/Genes.hpp b/src/Genes.hpp new file mode 100644 index 0000000..605041b --- /dev/null +++ b/src/Genes.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +#include + + + +class Gene : public sf::Drawable +{ +public: + static std::minstd_rand* re; + static sf::Vector2f size; + + virtual ~Gene(); + + virtual void randomize() =0; + virtual void mutate() =0; +}; + + + +class GeneCircle : public Gene +{ +public: + virtual ~GeneCircle(); + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + virtual void randomize(); + virtual void mutate(); + +private: + static sf::CircleShape circle; + static float stddev_position; + static float stddev_radius; + static float stddev_color; + + sf::Vector2f position; + float radius; + sf::Color color; + + void mutatePosition(); + void mutateRadius(); + void mutateColor(); + + float maxRadius(); +}; + + + +class GeneTriangle : public Gene +{ +public: + virtual ~GeneTriangle(); + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + virtual void randomize(); + virtual void mutate(); + +private: + // TODO: vertices +};