#include <vector>
-#include <gene.h>
+#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;
}
return *this;
}
- vector<_Chromosome> get() {
+ vector<_Gene> get() const {
return this->genes;
}
};
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() {
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;
}
};
--- /dev/null
+#ifndef __GENERATOR_GENERATION_H
+#define __GENERATOR_GENERATION_H
+
+#include <vector>
+
+#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<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);
+ }
+ };
+ }
+}
+
+#endif /* __GENERATOR_GENERATION_H */
#include "gene.h"
#include "chromosome.h"
+#include "generation.h"
+#include "generator/generation.h"
#include "fitness/wsti.h"
int main() {
typedef Gene<int> _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;
}