6 #include "chromosome.h"
7 #include "generation.h"
8 #include "fitness/fitness.h"
9 #include "generator/generation.h"
10 #include "selection/selection.h"
11 #include "crossover/crossover.h"
12 #include "mutation/mutation.h"
14 #include "condition/condition.h"
20 * Genetic Algorithm itself
22 template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
25 typedef typename _Selection::FitnessValueType FitnessValueType;
28 * Generator which draws initial population
30 generator::Generation<_Chromosome>& generator;
33 * Fitness function used in selection
35 Fitness<_Chromosome, FitnessValueType>& fitness;
50 Generation<_Chromosome> generation;
53 _Selection selection(this->generation, fitness);
54 this->generation = selection.draw();
55 this->generation = crossover.cross(this->generation);
56 this->generation = mutation.mutate(this->generation);
60 generator::Generation<_Chromosome>& _generator,
61 Fitness<_Chromosome, FitnessValueType>& _fitness,
62 double crossoverChance,
63 double mutationChance) :
64 generator(_generator),
66 crossover(crossoverChance),
67 mutation(mutationChance) {
69 this->generation = this->generator.breed();
72 void searchForResult(Condition<_Chromosome>& condition) {
74 cout << "generation avg best\n";
79 // cout << "New Generation:\n";
80 // this->showGeneration();
81 this->showAvgFitness();
82 this->showBestFitness();
83 } while(condition.check(this->generation) && ++i);
86 void showGeneration() {
87 for (unsigned int i = 0; i < this->generation.get().size(); i++) {
88 cout << "# " << i << ") ";
89 for (unsigned int j = 0; j < this->generation.get()[i].get().size(); j++) {
90 cout << this->generation.get()[i].get()[j].get();
96 void showAvgFitness() {
98 for (unsigned int i = 0; i < this->generation.get().size(); i++) {
99 WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
100 avg += fit.calculate();
102 cout << " " << avg / this->generation.get().size();
105 void showBestFitness() {
106 double best = -100000;
107 for (unsigned int i = 0; i < this->generation.get().size(); i++) {
108 WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
109 double tmp = fit.calculate();
114 cout << " " << best << endl;
119 #endif /* __ALGORITHM_H */