Remove some not used namespace declarations.
[genetic.git] / include / crossover / crossover.h
1 #ifndef __CROSSOVER_CROSSOVER_H
2 #define __CROSSOVER_CROSSOVER_H
3
4 #include <vector>
5
6 #include "chromosome.h"
7 #include "generation.h"
8
9 using namespace std;
10
11 namespace genetic {
12 //     namespace crossover {
13         template < typename _Chromosome >
14         class Crossover {
15         public:
16             /**
17              * Type of probability of crossover chance
18              */
19             typedef float CrossoverChanceType;
20
21             /**
22              * Type representing Chromosome Gene
23              */
24             typedef typename _Chromosome::GeneType GeneType;
25         protected:
26             /**
27              * Probability of Crossover (0 = 0%, 1 = 100%)
28              */
29             CrossoverChanceType chance;
30
31             /**
32              * Crossover two Chromosome's between themself.
33              *
34              * @param first first Chromosome to Crossover
35              * @param second second Chromosome to Crossover
36              * @param splitPlace Gene number on which the Genes should be swapped
37              * @return new Chromosome crossed between given two
38              */
39             _Chromosome do_cross(_Chromosome& first, _Chromosome& second, const unsigned int splitPlace) {
40                 const unsigned int chromosomeSize = first.size();
41
42 //                 cout << "        ";
43 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
44 //                     cout << first[i].get();
45 //                 }
46 //                 cout << "\n      x ";
47 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
48 //                     cout << second[i].get();
49 //                 }
50 //                 cout << "\n--------";
51 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
52 //                     if (i == splitPlace) {
53 //                         cout << "^";
54 //                     }
55 //                     else {
56 //                         cout << "-";
57 //                     }
58 //                 }
59
60                 std::vector<GeneType> crossedChromosome;
61                 for (unsigned int i = 0; i < chromosomeSize; i++) {
62                     if (i < splitPlace) {
63                         crossedChromosome.push_back(first[i].get());
64                     }
65                     else {
66                         crossedChromosome.push_back(second[i].get());
67                     }
68                 }
69
70 //                 cout << "\n        ";
71 //                 for (unsigned int i = 0; i < chromosomeSize; i++) {
72 //                     cout << crossedChromosome[i];
73 //                 }
74 //                 cout << "\n";
75
76                 return _Chromosome(crossedChromosome);
77             }
78
79         public:
80             /**
81              * Class constructor. Initializes values.
82              *
83              * @param chance probability of Crossover (0 = 0%, 1 = 100%)
84              */
85             Crossover(const CrossoverChanceType& chance) :
86                 chance(chance) {
87             }
88
89             /**
90              * Invokes Crossover calculations
91              *
92              * @param _generation Generation for which the crossover should be applied
93              * @return new Generation of Chromosome's after the Crossover
94              */
95             Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) {
96                 const unsigned int generationSize = _generation.size();
97                 std::vector<_Chromosome> newGeneration;
98
99                 for (unsigned int i = 0; i < generationSize; i++) {
100                     CrossoverChanceType random = (rand() + 1 % 10000) / 10000.0;
101                     _Chromosome chromosome = _generation[i];
102                     if (random < this->chance) {
103                         const unsigned int chromosomeSize = chromosome.size();
104                         const unsigned int splitPlace = rand() % chromosomeSize;
105                         unsigned int pairedChromosome = 0;
106
107                         /**
108                          * Search for different Chromosome, and crossover them together
109                          */
110                         do {
111                              pairedChromosome = rand() % generationSize;
112                         } while(pairedChromosome == i);
113
114                         chromosome = do_cross(chromosome, _generation[pairedChromosome], splitPlace);
115                     }
116                     newGeneration.push_back(chromosome);
117                 }
118
119                 return Generation<_Chromosome>(newGeneration);
120             }
121         };
122 //     }
123 }
124
125 #endif /* __CROSSOVER_CROSSOVER_H */