Added classes: Algorithm, Crossover, Mutation. Fixed Roulette.
[genetic.git] / src / crossover / crossover.h
1 #ifndef __CROSSOVER_CROSSOVER_H
2 #define __CROSSOVER_CROSSOVER_H
3
4 #include "chromosome.h"
5 #include "generation.h"
6
7 using namespace std;
8
9 namespace genetic {
10 //     namespace crossover {
11         template < typename _Chromosome >
12         class Crossover {
13         public:
14             typedef double CrossoverChanceType;
15             typedef typename _Chromosome::GeneType GeneType;
16         protected:
17             CrossoverChanceType chance;
18
19             _Chromosome do_cross(_Chromosome first, _Chromosome second, unsigned int splitPlace) {
20                 const unsigned int chromosomeSize = first.get().size();
21
22 //                 cout << "        ";
23 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
24 //                     cout << first.get()[i].get();
25 //                 }
26 //                 cout << "\n      x ";
27 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
28 //                     cout << second.get()[i].get();
29 //                 }
30 //                 cout << "\n--------";
31 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
32 //                     if (i == splitPlace) {
33 //                         cout << "^";
34 //                     }
35 //                     else {
36 //                         cout << "-";
37 //                     }
38 //                 }
39
40                 vector<GeneType> crossedChromosome;
41                 for (unsigned int i = 0; i < chromosomeSize; i++) {
42                     if (i < splitPlace) {
43                         crossedChromosome.push_back(first.get()[i].get());
44                     }
45                     else {
46                         crossedChromosome.push_back(second.get()[i].get());
47                     }
48                 }
49
50 //                 cout << "\n        ";
51 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
52 //                     cout << crossedChromosome[i].get();
53 //                 }
54 //                 cout << "\n";
55
56                 return _Chromosome(crossedChromosome);
57             }
58
59         public:
60             Crossover(CrossoverChanceType chance) :
61                 chance(chance) {
62             }
63
64             Generation<_Chromosome> cross(Generation<_Chromosome> _generation) {
65                 const unsigned int generationSize = _generation.get().size();
66                 vector<_Chromosome> newGeneration;
67
68                 for (unsigned int i = 0; i < generationSize; i++) {
69                     CrossoverChanceType random = (rand() + 1 % 10000) / 10000.0;
70                     _Chromosome chromosome = _generation.get()[i];
71                     if (random < this->chance) {
72                         const unsigned int chromosomeSize = chromosome.get().size();
73                         const unsigned int splitPlace = rand() % chromosomeSize;
74                         unsigned int pairedChromosome = 0;
75
76                         /**
77                          * Search for different Chromosome, and crossover them together
78                          */
79                         do {
80                              pairedChromosome = rand() % generationSize;
81                         } while(pairedChromosome == i);
82
83                         chromosome = do_cross(chromosome, _generation.get()[pairedChromosome], splitPlace);
84                     }
85                     newGeneration.push_back(chromosome);
86                 }
87
88                 return Generation<_Chromosome>(newGeneration);
89             }
90         };
91 //     }
92 }
93
94 #endif /* __CROSSOVER_CROSSOVER_H */