From: Rafał Długołęcki Date: Wed, 8 Apr 2015 18:42:15 +0000 (+0200) Subject: Rename GenerationLimit to GenerationLimitCondition. Add test for this class. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=2bdd4fe440ea0c4a824f7566cf574b17dfbd12b6;p=genetic.git Rename GenerationLimit to GenerationLimitCondition. Add test for this class. --- diff --git a/src/condition/generationLimit.h b/src/condition/generationLimitCondition.h similarity index 92% rename from src/condition/generationLimit.h rename to src/condition/generationLimitCondition.h index e387669..ffe977f 100644 --- a/src/condition/generationLimit.h +++ b/src/condition/generationLimitCondition.h @@ -14,7 +14,7 @@ namespace genetic { * iteration. */ template < typename _Chromosome> - class GenerationLimit : public Condition<_Chromosome> { + class GenerationLimitCondition : public Condition<_Chromosome> { public: protected: /** @@ -52,7 +52,7 @@ namespace genetic { * * @param limit number of generations after which algorithm should stop */ - GenerationLimit(unsigned int limit) + GenerationLimitCondition(unsigned int limit) : maxNumberOfGenerations(limit) { } }; diff --git a/src/main.cpp b/src/main.cpp index 336285a..daf7fbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,13 +16,13 @@ #include "algorithm.h" #include "condition/condition.h" -#include "condition/generationLimit.h" +#include "condition/generationLimitCondition.h" using namespace std; using namespace genetic; int main() { - typedef Gene _Gene; + typedef Gene _Gene; typedef Chromosome<_Gene> _Chromosome; typedef WSTI<_Chromosome> _Fitness; @@ -32,7 +32,7 @@ int main() { typedef generator::BitGenerator<_Chromosome> _Generator; typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation, _Fitness> _Algorithm; - typedef GenerationLimit<_Chromosome> _Condition; + typedef GenerationLimitCondition<_Chromosome> _Condition; const int chromosomeSize = 10; const int generationSize = 200; diff --git a/tests/Makefile.am b/tests/Makefile.am index 6dcf89a..c6bac99 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,11 +7,12 @@ TESTS = \ chromosome_copy.test \ generation_create.test\ generation_copy.test \ - generator_bitGeneratorBreed.test + generator_bitGeneratorBreed.test \ + condition_generationLimitConditionCheck.test noinst_PROGRAMS = $(TESTS) -AM_CPPFLAGS = -O2 -I$(top_srcdir)/src +AM_CXXFLAGS = -O3 -I$(top_srcdir)/src -std=c++11 gene_create_test_SOURCES = gene/create.cpp gene_copy_test_SOURCES = gene/copy.cpp @@ -24,6 +25,8 @@ generation_copy_test_SOURCES = generation/copy.cpp generator_bitGeneratorBreed_test_SOURCES = generator/bitGenerator/breed.cpp +condition_generationLimitConditionCheck_test_SOURCES = condition/generationLimitCondition/check.cpp + clean-local: rm -f *.log diff --git a/tests/condition/generationLimitCondition/check.cpp b/tests/condition/generationLimitCondition/check.cpp new file mode 100644 index 0000000..7173437 --- /dev/null +++ b/tests/condition/generationLimitCondition/check.cpp @@ -0,0 +1,40 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" +#include "generation.h" +#include "generator/bitGenerator.h" +#include "condition/generationLimitCondition.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + typedef Generation<_Chromosome> _Generation; + typedef generator::BitGenerator<_Chromosome> _Generator; + typedef GenerationLimitCondition<_Chromosome> _Condition; + + const unsigned int chromosomeSize = 20; + const unsigned int generationSize = 100; + const unsigned int numberOfChecks = 20; + + _Generator generator(generationSize, chromosomeSize); + _Generation generation = generator.breed(); + _Condition condition(numberOfChecks); + + unsigned int i = 0; + while(condition.check(generation)) { + if (i > numberOfChecks) { + cout << "GenerationLimitCondition got outside limit\n"; + return 1; + } + i++; + } + + cout << "BitGenerator breeded correctly\n"; + return 0; +} +