Add Chromosome class and plan more stuff
This commit is contained in:
parent
1171c37f88
commit
836887d209
4 changed files with 149 additions and 1 deletions
2
Makefile
2
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))
|
||||
|
|
|
|||
57
src/Chromosome.cpp
Normal file
57
src/Chromosome.cpp
Normal file
|
|
@ -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<Gene> 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
|
||||
}
|
||||
33
src/Chromosome.hpp
Normal file
33
src/Chromosome.hpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <SFML/Graphics.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:
|
||||
// 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<Gene> genes;
|
||||
};
|
||||
58
src/main.cpp
58
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue