Fixed Mutation.
[genetic.git] / src / algorithm.h
index 162fdd02afa761dc7a27e52c43944defd5c0af32..f38fff57acb3d11b055e160fc1663feae634a89e 100644 (file)
@@ -11,6 +11,8 @@
 #include "crossover/crossover.h"
 #include "mutation/mutation.h"
 
+#include "condition/condition.h"
+
 using namespace std;
 
 namespace genetic {
@@ -47,7 +49,12 @@ namespace genetic {
          */
         Generation<_Chromosome> generation;
 
-        const int numberOfGenerations = 100;
+        void do_search() {
+            _Selection selection(this->generation, fitness);
+            this->generation = selection.draw();
+            this->generation = crossover.cross(this->generation);
+            this->generation = mutation.mutate(this->generation);
+        }
     public:
         Algorithm(
             generator::Generation<_Chromosome>& _generator,
@@ -62,20 +69,18 @@ namespace genetic {
             this->generation = this->generator.breed();
         }
 
-        void searchForResult() {
-            cout << "generation avg best" << endl;
-            for(int i = 0; i < this->numberOfGenerations; i++) {
+        void searchForResult(Condition<_Chromosome>& condition) {
+            unsigned int i = 0;
+            cout << "generation avg best\n";
+            do {
                 cout << i;
-                _Selection selection(this->generation, fitness);
-                this->generation = selection.draw();
-                this->generation = crossover.cross(this->generation);
-                this->generation = mutation.mutate(this->generation);
+                do_search();
 
 //                 cout << "New Generation:\n";
 //                 this->showGeneration();
                 this->showAvgFitness();
                 this->showBestFitness();
-            }
+            } while(condition.check(this->generation) && ++i);
         }
 
         void showGeneration() {