-#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 */
--- /dev/null
+#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 GenerationLimit : 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
+ */
+ 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
+ */
+ GenerationLimit(unsigned int limit)
+ : maxNumberOfGenerations(limit) {
+ }
+ };
+}
+
+#endif /* __CONDITION_GENERATION_LIMIT_H */
#include "algorithm.h"
#include "condition/condition.h"
+#include "condition/generationLimit.h"
using namespace std;
using namespace genetic;
typedef generator::Bit<_Chromosome> _Generator;
typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation, _Fitness> _Algorithm;
- typedef Condition<_Chromosome> _Condition;
+ typedef GenerationLimit<_Chromosome> _Condition;
const int chromosomeSize = 10;
const int generationSize = 200;
_Generator generationGenerator(generationSize, chromosomeSize);
_Algorithm algorithm(generationGenerator, fitness, crossoverChance, mutationChance);
- _Condition condition;
+ _Condition condition(1000);
algorithm.searchForResult(condition);