diff --git a/Makefile b/Makefile index 4d0dcb6..d2723ef 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ SOURCEFILES = cpp SRCDIR = src TMPDIR = build TARGET = gross -FILES = main +FILES = Chromosome main #SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES)) OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES)) diff --git a/src/Chromosome.cpp b/src/Chromosome.cpp new file mode 100644 index 0000000..34d36a4 --- /dev/null +++ b/src/Chromosome.cpp @@ -0,0 +1,57 @@ +#include "Chromosome.hpp" + + + +/* +class Chromosome : public sf::Drawable +{ +public: + Chromosome(); // create empty chromosome + Chromosome(Chromosome& father, Chromosome& mother); // cross over two chromosomes + + void mutate(); // randomly mutate chromosome's genes + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + +private: + struct Gene + { + sf::Vector2f position; + float size; + sf::Color color; + }; + + sf::CircleShape circle; // drawing the chromosome, one draw call at a time + std::vector genes; +}; +*/ + +Chromosome::Chromosome() +{ + // this->genes is already empty +}; + +Chromosome::Chromosome(Chromosome& father, Chromosome& mother) { + // randomly swap father and mother + if (/*TODO: random bool*/ false) { + Chromosome& tmp = father; + father = mother; + mother = tmp; + } + + // replace random segment of mother with random segment of father + + // TODO: in seperate function: + // finding random segment: + // randomly find starting position, then length + // find starting iterator, then end iterator + + // using function from above: + // find father segment: f_start, f_stop (iterators) + // find mother segment: m_start, m_stop (iterators) + + // RIGHT: + // append mother until m_start + // append father from f_start to f_end + // append mother from m_end +} diff --git a/src/Chromosome.hpp b/src/Chromosome.hpp new file mode 100644 index 0000000..822ed9e --- /dev/null +++ b/src/Chromosome.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + + + +class Chromosome : public sf::Drawable +{ +public: + Chromosome(); // create empty chromosome + Chromosome(Chromosome& father, Chromosome& mother); // cross over two chromosomes + + void mutate(); // randomly mutate chromosome's genes + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + +private: + // TODO: random numbers + // pass as reference to Chromosome and Mutate? + // pass as reference on object creation? + + struct Gene + { + sf::Vector2f position; + float size; + sf::Color color; + }; + + sf::CircleShape circle; // drawing the chromosome, one draw call at a time + + size_t genesize = 0; + std::forward_list genes; +}; diff --git a/src/main.cpp b/src/main.cpp index 6c07acd..845585e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,6 +45,64 @@ * - invisible? */ +/* + * Project structure plan: + * - Chromosome + * - static (class) size variables + * - prevents situation where two differently-sized chromosomes mate + * - can mate + * - can mutate + * - add circle + * - remove circle + * - change circle (stays at same layer) + * - maybe change circle color, size, position separately, or all at once + * - move circle (between layers) + * - can be rendered to texture + * - Comparator (fitness tester) (maybe just function in Generations) + * - can render progress/info to window (simple renderable) + * - c.compare() sets variables in instance, draw directly after compare()-ing + * - screen update logic in Generations + * - initialized with size (or static size variables?) + * - compares two textures + * - Generations + * - keeps track of Chromosomes + * - applies fitness function to Chromosomes + * - applies selection to Chromosomes + * - makes Chromosomes mate and mutate + * - mutate only freshly mated chromosomes + * - displays various states of genetic algorithm + * - Graph + * - displays nice graph for use with Generations + * - Button? + */ + +/* + * Generations states: + * - display generation stats (resting) (all other visual states optional) + * - run modes: step-by-step, visual, fast, off? + * - toggles for visibility of steps? + * - display evaluating fitness process + * - step-through: both images, image diff, chromosome stats (fitness etc.), generation stats + * autorun mode + * - fast: only generation stats, updated every half second or so + * - off: nothing rendered at all + * - display selection process + * - show all chromosomes, in tiny RenderTextures? + * - show big version on hover-over, including original image? + * - sort and move to respective position + * - blend out deleted ones (set opacity to 0) + * - display mating process + * - show parents + * - show result + * - show chromosomes? (string of circles maybe?) + * - show parent fitness? + * + * We've come full circle! + * + * Further notes: + * - controls for stepping <-> runnning with delay + */ + int main() { const float winW = 480; const float winH = 480;