From: Rafał Długołęcki Date: Sun, 22 Mar 2015 14:24:23 +0000 (+0100) Subject: Created Generator for breeding Generations. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=ec0bd5c9a9350200f58ec7b84707bca822d145ed;p=genetic.git Created Generator for breeding Generations. --- diff --git a/src/chromosome.h b/src/chromosome.h index a20c793..d37569f 100644 --- a/src/chromosome.h +++ b/src/chromosome.h @@ -3,20 +3,22 @@ #include -#include +#include "gene.h" using namespace std; namespace genetic { - template < typename _Chromosome > + template < typename _Gene > class Chromosome { + public: + typedef _Gene GeneType; protected: - vector<_Chromosome> genes; + vector<_Gene> genes; public: Chromosome() {} - Chromosome(vector<_Chromosome> genes) { + Chromosome(vector<_Gene> genes) { this->genes = genes; } @@ -29,7 +31,7 @@ namespace genetic { return *this; } - vector<_Chromosome> get() { + vector<_Gene> get() const { return this->genes; } }; diff --git a/src/fitness/wsti.h b/src/fitness/wsti.h index 028c322..b93439f 100644 --- a/src/fitness/wsti.h +++ b/src/fitness/wsti.h @@ -20,13 +20,13 @@ namespace genetic { float span_end; double fenotype() { - int fen = 0; - int rat = 1; + int _fenotype = 0; + int ratio = 1; for (unsigned int i = 0; i < chromosome.get().size(); i++) { - fen = fen + chromosome.get()[i].get() * rat; - rat = rat * 2; + _fenotype = _fenotype + chromosome.get()[i].get() * ratio; + ratio = ratio * 2; } - return fen; + return _fenotype; } double calculateFenotype() { diff --git a/src/generation.h b/src/generation.h index c1adc73..8ca0eb8 100644 --- a/src/generation.h +++ b/src/generation.h @@ -15,11 +15,18 @@ namespace genetic { vector<_Chromosome> chromosomes; public: + Generation() {} + Generation(vector<_Chromosome> chromosomes) { this->chromosomes = chromosomes; } - vector<_Chromosome> get() { + /** Copy constructor */ + Generation(const Generation& generation) + : chromosomes(generation.get()) { + } + + vector<_Chromosome> get() const { return this->chromosomes; } }; diff --git a/src/generator/generation.h b/src/generator/generation.h new file mode 100644 index 0000000..c90a376 --- /dev/null +++ b/src/generator/generation.h @@ -0,0 +1,70 @@ +#ifndef __GENERATOR_GENERATION_H +#define __GENERATOR_GENERATION_H + +#include + +#include "../gene.h" +#include "../chromosome.h" +#include "../generation.h" + +using namespace std; + +namespace genetic { + namespace generator { + /** + * Generator for generating entire Generation of Chromosomes (individuals) + */ + template < typename _Chromosome > + class Generation { + public: + /** + * Type of used Genes in Chromosome + */ + typedef typename _Chromosome::GeneType GeneType; + protected: + /** + * Size of the generation to generate + */ + unsigned int generationSize; + + /** + * Size of the chromosome to generate + */ + unsigned int chromosomeSize; + + public: + /** + * Constructor. + * + * @param generationSize Indicates size of the generation + * @param generationSize Indicates size of the chromosome + */ + Generation(unsigned int generationSize, unsigned int chromosomeSize) { + this->generationSize = generationSize; + this->chromosomeSize = chromosomeSize; + } + + /** + * Breeds new generation of chromosomes. + * + * @return ::genetic::Generation Generation of Chromosomes + */ + ::genetic::Generation<_Chromosome> breed() { + vector<_Chromosome> chromosomes; + + for (unsigned int i = 0; i < generationSize; i++) { + vector genes; + for (unsigned int j = 0; j < chromosomeSize; j++) { + GeneType gene(rand() % 2); + genes.push_back(gene); + } + chromosomes.push_back(genes); + } + + return ::genetic::Generation<_Chromosome>(chromosomes); + } + }; + } +} + +#endif /* __GENERATOR_GENERATION_H */ diff --git a/src/main.cpp b/src/main.cpp index 60ea330..38bbf42 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include "gene.h" #include "chromosome.h" +#include "generation.h" +#include "generator/generation.h" #include "fitness/wsti.h" @@ -14,28 +16,21 @@ using namespace genetic; int main() { typedef Gene _Gene; typedef Chromosome<_Gene> _Chromosome; + + const int chromosomeSize = 11; + const int generationSize = 20; time_t t; srand((unsigned)time(&t)); - vector<_Gene> genes; + generator::Generation<_Chromosome> generationGenerator(generationSize, chromosomeSize); + Generation<_Chromosome> generation = generationGenerator.breed(); - for (int i = 0; i < 11; i++) { - _Gene gene(rand() % 2); - cout << "Generated gene: " << (int)gene.get() << "\n"; - genes.push_back(gene); - } - - _Chromosome chromosome(genes); + for (unsigned int i = 0; i < generationSize; i++) { + WSTI<_Chromosome> fitness(generation.get()[i], 0.5, 2.5); - cout << "Entire chromosome: "; - for (unsigned int i = 0; i < chromosome.get().size(); i++) { - cout << chromosome.get()[i].get(); + cout << "Fitness is equal to: " << fitness.calculate() << "\n"; } - cout << endl; - WSTI<_Chromosome> fitness(chromosome, 0.5, 2.5); - - cout << "Fitness is equal to: " << fitness.calculate() << "\n"; return 0; }