Created custom Condition class: GenerationLimit.
[genetic.git] / src / condition / condition.h
index d9a1373c9cb4dc17667934105607b62fb17cac0d..3562129a1666999eaad86ea0249029a80012c569 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef __ALGORITHM_CONDITION_H
-#define __ALGORITHM_CONDITION_H
+#ifndef __CONDITION_CONDITION_H
+#define __CONDITION_CONDITION_H
 
 #include "chromosome.h"
 #include "generation.h"
@@ -8,28 +8,25 @@ using namespace std;
 
 namespace genetic {
     /**
-     * Condition class.
+     * Base Condition class.
      * It is used for checking if algorithm should stop.
-     *
-     * By default stops algorithm after 1000 of generations.
      */
     template < typename _Chromosome>
     class Condition {
-    public:
     protected:
         /**
-         * Variable indicating current generation
-         */
-        unsigned int currentGeneration = 0;
-    private:
-        /**
-         * Variable indicating max number of generations, after which program
-         * will be stopped
+         * Check calculations for current generation if it passes stop condition
+         * If condition is satisfied, we can check another generation.
+         * All the custom Condition checks should be invoked in this method.
+         *
+         * @param generation current generation to check
+         *
+         * @return true if condition is satisfied and another generation can checked;
+         *      false if condition is not satisfied and algorithm should stop.
          */
-        const unsigned int maxNumberOfGenerations = 1000;
-    public:
-        Condition() { }
+        virtual bool do_check(Generation<_Chromosome>) = 0;
 
+    public:
         /**
          * Checks if current generation passes stop condition.
          * If condition is satisfied, we can check another generation.
@@ -39,19 +36,10 @@ namespace genetic {
          * @return true if condition is satisfied and another generation can checked;
          *      false if condition is not satisfied and algorithm should stop.
          */
-        virtual bool check(Generation<_Chromosome>) {
-            /* Initial population is never checked, as method is invoked after
-             * selection, crossover and mutation. It is safe to increment it now.
-             */
-            currentGeneration++;
-
-            if (currentGeneration < maxNumberOfGenerations) {
-                return true;
-            }
-
-            return false;
+        bool check(Generation<_Chromosome> generation) {
+            return do_check(generation);
         }
     };
 }
 
-#endif /* __ALGORITHM_CONDITION_H */
+#endif /* __CONDITION_CONDITION_H */