187db12802fc22ee152e211a96e27ad2d33e2362
[genetic.git] / include / selection / selection.h
1 #ifndef __SELECTION_SELECTION_H
2 #define __SELECTION_SELECTION_H
3
4 #include "chromosome.h"
5 #include "generation.h"
6 #include "../fitness/fitness.h"
7
8 using namespace std;
9
10 namespace genetic {
11 //     namespace selection {
12         /**
13          * Base Selection template class. It should be a base class for any
14          * custom selection functions.
15          */
16         template < typename _Chromosome >
17         class Selection {
18         public:
19             /**
20              * Type representing Fitness function
21              */
22             typedef Fitness<_Chromosome> GeneticFitness;
23
24             /**
25              * Value type returned by the Fitness function
26              */
27             typedef typename GeneticFitness::ValueType FitnessValueType;
28         protected:
29             /**
30              * Generation over which the Selection will be applied
31              */
32             Generation<_Chromosome> generation;
33
34             /**
35              * Fitness which will be used in selection
36              */
37             GeneticFitness& fitness;
38
39             /**
40              * Checks Fitness for the given Chromosome
41              *
42              * @param chromosome Chromosome for which the selection fitness is
43              *      checked.
44              * @return Value of the Fitness function
45              */
46             FitnessValueType checkChromosomeFitness(const _Chromosome& chromosome) {
47                 this->fitness.chromosome = chromosome;
48                 return fitness.calculate();
49             }
50
51             /**
52              * Selection calculations should be done here.
53              *
54              * @return new Generation of Chromosome's that passed the Selection
55              */
56             virtual Generation<_Chromosome> do_draw() = 0;
57
58         public:
59             /**
60              * Class constructor. Initializes required variables and constants
61              *
62              * @param _generation Generation over which the Selection will be
63              *      applied
64              * @param _fitness Fitness function to use in Selection
65              */
66             Selection(const Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
67                 generation(_generation), fitness(_fitness) {
68             }
69
70             /**
71              * Invokes Selection calculations
72              *
73              * @return new Generation of Chromosome's that passed the Selection
74              */
75             Generation<_Chromosome> draw() {
76                 return this->do_draw();
77             }
78         };
79 //     }
80 }
81
82 #endif /* __SELECTION_SELECTION_H */