Small fixes in Condition checking.
[genetic.git] / src / condition / condition.h
1 #ifndef __ALGORITHM_CONDITION_H
2 #define __ALGORITHM_CONDITION_H
3
4 #include "chromosome.h"
5 #include "generation.h"
6
7 using namespace std;
8
9 namespace genetic {
10     /**
11      * Condition class.
12      * It is used for checking if algorithm should stop.
13      *
14      * By default stops algorithm after 1000 of generations.
15      */
16     template < typename _Chromosome>
17     class Condition {
18     public:
19     protected:
20         /**
21          * Variable indicating current generation
22          */
23         unsigned int currentGeneration = 0;
24     private:
25         /**
26          * Variable indicating max number of generations, after which program
27          * will be stopped
28          */
29         const unsigned int maxNumberOfGenerations = 1000;
30     public:
31         Condition() { }
32
33         /**
34          * Checks if current generation passes stop condition.
35          * If condition is satisfied, we can check another generation.
36          *
37          * @param generation current generation to check
38          *
39          * @return true if condition is satisfied and another generation can checked;
40          *      false if condition is not satisfied and algorithm should stop.
41          */
42         virtual bool check(Generation<_Chromosome>) {
43             /* Initial population is never checked, as method is invoked after
44              * selection, crossover and mutation. It is safe to increment it now.
45              */
46             currentGeneration++;
47
48             if (currentGeneration < maxNumberOfGenerations) {
49                 return true;
50             }
51
52             return false;
53         }
54     };
55 }
56
57 #endif /* __ALGORITHM_CONDITION_H */