From: Rafał Długołęcki Date: Sun, 5 Apr 2015 21:02:41 +0000 (+0200) Subject: Make generators be more Generic. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=171bb6f1ba7b2ee5d9be674beb8b828910c8e659;p=genetic.git Make generators be more Generic. --- diff --git a/README b/README index e69de29..39dd7c3 100644 --- a/README +++ b/README @@ -0,0 +1,2 @@ +# Genetic Algorithm Library + diff --git a/src/algorithm.h b/src/algorithm.h index bc92850..9386607 100644 --- a/src/algorithm.h +++ b/src/algorithm.h @@ -6,7 +6,7 @@ #include "chromosome.h" #include "generation.h" #include "fitness/fitness.h" -#include "generator/generation.h" +#include "generator/generator.h" #include "selection/selection.h" #include "crossover/crossover.h" #include "mutation/mutation.h" @@ -30,7 +30,7 @@ namespace genetic { /** * Generator which draws initial population */ - generator::Generation<_Chromosome>& generator; + generator::Generator<_Chromosome>& generator; /** * Fitness function used in selection @@ -78,7 +78,7 @@ namespace genetic { * @param mutationChance probability of fitness (0 = 0%, 1 = 100%) */ Algorithm( - generator::Generation<_Chromosome>& _generator, + generator::Generator<_Chromosome>& _generator, _Fitness& _fitness, double crossoverChance, double mutationChance) : diff --git a/src/generator/bit.h b/src/generator/bit.h new file mode 100644 index 0000000..7bd5007 --- /dev/null +++ b/src/generator/bit.h @@ -0,0 +1,62 @@ +#ifndef __GENERATOR_BIT_H +#define __GENERATOR_BIT_H + +#include +#include + +#include "gene.h" +#include "chromosome.h" +#include "generation.h" + +#include "generator/generator.h" + +using namespace std; + +namespace genetic { + namespace generator { + /** + * Generator for generating entire Generation of Chromosomes (individuals) + */ + template < typename _Chromosome > + class Bit : public Generator<_Chromosome> { + public: + /** + * Type representing Chromosome Gene + */ + typedef typename _Chromosome::GeneType GeneType; + + protected: + /** + * Breeds new generation of chromosomes. + * + * @return ::genetic::Generation Generation of Chromosomes + */ + virtual ::genetic::Generation<_Chromosome> do_breed() { + vector<_Chromosome> chromosomes; + + for (unsigned int i = 0; i < this->generationSize; i++) { + vector genes; + for (unsigned int j = 0; j < this->chromosomeSize; j++) { + GeneType gene(rand() % 2); + genes.push_back(gene); + } + chromosomes.push_back(genes); + } + + return ::genetic::Generation<_Chromosome>(chromosomes); + } + public: + /** + * Constructor. Initializes required variables and constants + * + * @param generationSize Indicates size of the generation + * @param chromosomeSize Indicates size of the chromosome + */ + Bit(unsigned int generationSize, unsigned int chromosomeSize) + : Generator<_Chromosome>(generationSize, chromosomeSize) { + } + }; + } +} + +#endif /* __GENERATOR_GENERATOR_H */ diff --git a/src/generator/generation.h b/src/generator/generator.h similarity index 66% rename from src/generator/generation.h rename to src/generator/generator.h index 91e605d..64f2720 100644 --- a/src/generator/generation.h +++ b/src/generator/generator.h @@ -1,7 +1,6 @@ -#ifndef __GENERATOR_GENERATION_H -#define __GENERATOR_GENERATION_H +#ifndef __GENERATOR_GENERATOR_H +#define __GENERATOR_GENERATOR_H -#include #include #include "../gene.h" @@ -16,7 +15,7 @@ namespace genetic { * Generator for generating entire Generation of Chromosomes (individuals) */ template < typename _Chromosome > - class Generation { + class Generator { public: /** * Type representing Chromosome Gene @@ -33,6 +32,13 @@ namespace genetic { */ unsigned int chromosomeSize; + /** + * Breeding calculations should be done here... + * + * @return newly breeded Generation + */ + virtual ::genetic::Generation<_Chromosome> do_breed() = 0; + public: /** * Constructor. Initializes required variables and constants @@ -40,7 +46,7 @@ namespace genetic { * @param generationSize Indicates size of the generation * @param chromosomeSize Indicates size of the chromosome */ - Generation(unsigned int generationSize, unsigned int chromosomeSize) { + Generator(unsigned int generationSize, unsigned int chromosomeSize) { this->generationSize = generationSize; this->chromosomeSize = chromosomeSize; @@ -54,21 +60,10 @@ namespace genetic { * @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); + return do_breed(); } }; } } -#endif /* __GENERATOR_GENERATION_H */ +#endif /* __GENERATOR_GENERATOR_H */ diff --git a/src/main.cpp b/src/main.cpp index f2aec13..f82cc92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "gene.h" #include "chromosome.h" #include "generation.h" -#include "generator/generation.h" +#include "generator/bit.h" #include "selection/roulette.h" #include "crossover/crossover.h" @@ -28,7 +28,7 @@ int main() { typedef Crossover<_Chromosome> _Crossover; typedef Mutation<_Chromosome> _Mutation; - typedef generator::Generation<_Chromosome> _Generator; + typedef generator::Bit<_Chromosome> _Generator; typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation, _Fitness> _Algorithm; typedef Condition<_Chromosome> _Condition;