Fix building and installing package.
[genetic.git] / include / condition / generationLimitCondition.h
diff --git a/include/condition/generationLimitCondition.h b/include/condition/generationLimitCondition.h
new file mode 100644 (file)
index 0000000..ffe977f
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef __CONDITION_GENERATION_LIMIT_H
+#define __CONDITION_GENERATION_LIMIT_H
+
+#include "chromosome.h"
+#include "generation.h"
+
+#include "condition.h"
+
+using namespace std;
+
+namespace genetic {
+    /**
+     * Condition class. It is used for checking if algorithm should stop after
+     * iteration.
+     */
+    template < typename _Chromosome>
+    class GenerationLimitCondition : public Condition<_Chromosome> {
+    public:
+    protected:
+        /**
+         * Variable indicating current generation
+         */
+        unsigned int currentGeneration = 0;
+
+        /**
+         * Variable indicating max number of generations, after which program
+         * will be stopped
+         */
+        const unsigned int maxNumberOfGenerations = 0;
+
+        /**
+         * Checks if the given limit of generations has occured
+         *
+         * @return true if limit is not reached and another iteration of
+         *      calculations should be started, false otherwise
+         */
+        bool do_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;
+        }
+    public:
+        /**
+         * Class constructor. Initializes required variables.
+         *
+         * @param limit number of generations after which algorithm should stop
+         */
+        GenerationLimitCondition(unsigned int limit)
+            : maxNumberOfGenerations(limit) {
+        }
+    };
+}
+
+#endif /* __CONDITION_GENERATION_LIMIT_H */