Fixed Mutation.
[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 #include "condition/condition.h"
15
16 using namespace std;
17
18 namespace genetic {
19     /**
20      * Genetic Algorithm itself
21      */
22     template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
23     class Algorithm {
24     public:
25         typedef typename _Selection::FitnessValueType FitnessValueType;
26     protected:
27         /**
28          * Generator which draws initial population
29          */
30         generator::Generation<_Chromosome>& generator;
31
32         /**
33          * Fitness function used in selection
34          */
35         Fitness<_Chromosome, FitnessValueType>& fitness;
36
37         /**
38          * Crossover function
39          */
40         _Crossover crossover;
41
42         /**
43          * Mutation function
44          */
45         _Mutation mutation;
46
47         /**
48          * Current population
49          */
50         Generation<_Chromosome> generation;
51
52         void do_search() {
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);
57         }
58     public:
59         Algorithm(
60             generator::Generation<_Chromosome>& _generator,
61             Fitness<_Chromosome, FitnessValueType>& _fitness,
62             double crossoverChance,
63             double mutationChance) :
64                 generator(_generator),
65                 fitness(_fitness),
66                 crossover(crossoverChance),
67                 mutation(mutationChance) {
68
69             this->generation = this->generator.breed();
70         }
71
72         void searchForResult(Condition<_Chromosome>& condition) {
73             unsigned int i = 0;
74             cout << "generation avg best\n";
75             do {
76                 cout << i;
77                 do_search();
78
79 //                 cout << "New Generation:\n";
80 //                 this->showGeneration();
81                 this->showAvgFitness();
82                 this->showBestFitness();
83             } while(condition.check(this->generation) && ++i);
84         }
85
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();
91                 }
92                 cout << "\n";
93             }
94         }
95
96         void showAvgFitness() {
97             double avg = 0;
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();
101             }
102             cout << " " << avg / this->generation.get().size();
103         }
104
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();
110                 if (tmp > best) {
111                     best = tmp;
112                 }
113             }
114             cout << " " << best << endl;
115         }
116     };
117 }
118
119 #endif /* __ALGORITHM_H */