Rename GenerationLimit to GenerationLimitCondition. Add test for this class.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Wed, 8 Apr 2015 18:42:15 +0000 (20:42 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Wed, 8 Apr 2015 18:42:15 +0000 (20:42 +0200)
src/condition/generationLimitCondition.h [moved from src/condition/generationLimit.h with 92% similarity]
src/main.cpp
tests/Makefile.am
tests/condition/generationLimitCondition/check.cpp [new file with mode: 0644]

similarity index 92%
rename from src/condition/generationLimit.h
rename to src/condition/generationLimitCondition.h
index e387669b4dc2f960e7a56b8a9d0e056a89ca0ad0..ffe977f3430a494411944cbecce1e764662b1b2e 100644 (file)
@@ -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) {
         }
     };
index 336285a52c5175239d4b074d1500f429710e7cab..daf7fbcbbf047b95a5885b51a296463077e7030c 100644 (file)
 
 #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<int> _Gene;
+    typedef Gene<bool> _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;
index 6dcf89a1a729fc9075cc72b8048320477c974515..c6bac99aabd31988230e35e79567a985de980d44 100644 (file)
@@ -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 (file)
index 0000000..7173437
--- /dev/null
@@ -0,0 +1,40 @@
+#include <iostream>
+#include <vector>
+
+#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<int> _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;
+}
+