-#ifndef __ALGORITHM_CONDITION_H
-#define __ALGORITHM_CONDITION_H
+#ifndef __CONDITION_CONDITION_H
+#define __CONDITION_CONDITION_H
#include "chromosome.h"
#include "generation.h"
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.
* @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 */