Cleanup code.
[genetic.git] / src / algorithm.h
1 #ifndef __ALGORITHM_H
2 #define __ALGORITHM_H
3
4 #include <iostream>
5
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"
13
14 using namespace std;
15
16 namespace genetic {
17     /**
18      * Genetic Algorithm itself
19      */
20     template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
21     class Algorithm {
22     public:
23         typedef typename _Selection::FitnessValueType FitnessValueType;
24     protected:
25         /**
26          * Generator which draws initial population
27          */
28         generator::Generation<_Chromosome>& generator;
29
30         /**
31          * Fitness function used in selection
32          */
33         Fitness<_Chromosome, FitnessValueType>& fitness;
34
35         /**
36          * Crossover function
37          */
38         _Crossover crossover;
39
40         /**
41          * Mutation function
42          */
43         _Mutation mutation;
44
45         /**
46          * Current population
47          */
48         Generation<_Chromosome> generation;
49
50         const int numberOfGenerations = 100;
51     public:
52         Algorithm(
53             generator::Generation<_Chromosome>& _generator,
54             Fitness<_Chromosome, FitnessValueType>& _fitness,
55             double crossoverChance,
56             double mutationChance) :
57                 generator(_generator),
58                 fitness(_fitness),
59                 crossover(crossoverChance),
60                 mutation(mutationChance) {
61
62             this->generation = this->generator.breed();
63         }
64
65         void searchForResult() {
66             cout << "generation avg best" << endl;
67             for(int i = 0; i < this->numberOfGenerations; i++) {
68                 cout << i;
69                 _Selection selection(this->generation, fitness);
70                 this->generation = selection.draw();
71                 this->generation = crossover.cross(this->generation);
72                 this->generation = mutation.mutate(this->generation);
73
74 //                 cout << "New Generation:\n";
75 //                 this->showGeneration();
76                 this->showAvgFitness();
77                 this->showBestFitness();
78             }
79         }
80
81         void showGeneration() {
82             for (unsigned int i = 0; i < this->generation.get().size(); i++) {
83                 cout << "# " << i << ") ";
84                 for (unsigned int j = 0; j < this->generation.get()[i].get().size(); j++) {
85                     cout << this->generation.get()[i].get()[j].get();
86                 }
87                 cout << "\n";
88             }
89         }
90
91         void showAvgFitness() {
92             double avg = 0;
93             for (unsigned int i = 0; i < this->generation.get().size(); i++) {
94                 WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
95                 avg += fit.calculate();
96             }
97             cout << " " << avg / this->generation.get().size();
98         }
99
100         void showBestFitness() {
101             double best = -100000;
102             for (unsigned int i = 0; i < this->generation.get().size(); i++) {
103                 WSTI<_Chromosome, FitnessValueType> fit(this->generation.get()[i], 0.5, 2.5);
104                 double tmp = fit.calculate();
105                 if (tmp > best) {
106                     best = tmp;
107                 }
108             }
109             cout << " " << best << endl;
110         }
111     };
112 }
113
114 #endif /* __ALGORITHM_H */