7448052e54f9d67ca2801b43f146a2c34733e81e
[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         unsigned int currentGeneration = 0;
21     private:
22         const unsigned int maxNumberOfGenerations = 1000;
23     public:
24         Condition() { }
25
26         /**
27          * Checks if current generation passes stop condition.
28          * If condition is satisfied, we can check another generation.
29          *
30          * @param generation current generation to check
31          *
32          * @return true if condition is satisfied and another generation can checked;
33          *      false if condition is not satisfied and algorithm should stop.
34          */
35         virtual bool check(Generation<_Chromosome> generation) {
36             if (currentGeneration >= maxNumberOfGenerations) {
37                 return false;
38             }
39             currentGeneration++;
40
41             return true;
42         }
43     };
44 }
45
46 #endif /* __ALGORITHM_CONDITION_H */