+# Genetic Algorithm Library
+
#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"
/**
* Generator which draws initial population
*/
- generator::Generation<_Chromosome>& generator;
+ generator::Generator<_Chromosome>& generator;
/**
* Fitness function used in selection
* @param mutationChance probability of fitness (0 = 0%, 1 = 100%)
*/
Algorithm(
- generator::Generation<_Chromosome>& _generator,
+ generator::Generator<_Chromosome>& _generator,
_Fitness& _fitness,
double crossoverChance,
double mutationChance) :
--- /dev/null
+#ifndef __GENERATOR_BIT_H
+#define __GENERATOR_BIT_H
+
+#include <vector>
+#include <cstdlib>
+
+#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<GeneType> 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 */
-#ifndef __GENERATOR_GENERATION_H
-#define __GENERATOR_GENERATION_H
+#ifndef __GENERATOR_GENERATOR_H
+#define __GENERATOR_GENERATOR_H
-#include <vector>
#include <cstdlib>
#include "../gene.h"
* Generator for generating entire Generation of Chromosomes (individuals)
*/
template < typename _Chromosome >
- class Generation {
+ class Generator {
public:
/**
* Type representing Chromosome Gene
*/
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
* @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;
* @return ::genetic::Generation Generation of Chromosomes
*/
::genetic::Generation<_Chromosome> breed() {
- vector<_Chromosome> chromosomes;
-
- for (unsigned int i = 0; i < generationSize; i++) {
- vector<GeneType> 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 */
#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"
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;