From: Rafał Długołęcki Date: Mon, 6 Apr 2015 10:42:21 +0000 (+0200) Subject: Created custom Condition class: GenerationLimit. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=f3f3b7e90e19984eff428e2d7832cec59f643b93;p=genetic.git Created custom Condition class: GenerationLimit. --- diff --git a/src/condition/condition.h b/src/condition/condition.h index d9a1373..3562129 100644 --- a/src/condition/condition.h +++ b/src/condition/condition.h @@ -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 */ diff --git a/src/condition/generationLimit.h b/src/condition/generationLimit.h new file mode 100644 index 0000000..9cd11da --- /dev/null +++ b/src/condition/generationLimit.h @@ -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 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 */ diff --git a/src/main.cpp b/src/main.cpp index f82cc92..3e73d58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include "algorithm.h" #include "condition/condition.h" +#include "condition/generationLimit.h" using namespace std; using namespace genetic; @@ -30,7 +31,7 @@ int main() { 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; @@ -41,7 +42,7 @@ int main() { _Generator generationGenerator(generationSize, chromosomeSize); _Algorithm algorithm(generationGenerator, fitness, crossoverChance, mutationChance); - _Condition condition; + _Condition condition(1000); algorithm.searchForResult(condition);