Fixed copying of Gene.
Added Condition class which is used to check stop conditions.
Cleaned code.
#include "crossover/crossover.h"
#include "mutation/mutation.h"
+#include "condition/condition.h"
+
using namespace std;
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,
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() {
--- /dev/null
+#ifndef __ALGORITHM_CONDITION_H
+#define __ALGORITHM_CONDITION_H
+
+#include "chromosome.h"
+#include "generation.h"
+
+using namespace std;
+
+namespace genetic {
+ /**
+ * 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:
+ unsigned int currentGeneration = 0;
+ private:
+ const unsigned int maxNumberOfGenerations = 1000;
+ public:
+ Condition() { }
+
+ /**
+ * Checks if current generation passes stop condition.
+ * If condition is satisfied, we can check another generation.
+ *
+ * @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.
+ */
+ virtual bool check(Generation<_Chromosome> generation) {
+ if (currentGeneration >= maxNumberOfGenerations) {
+ return false;
+ }
+ currentGeneration++;
+
+ return true;
+ }
+ };
+}
+
+#endif /* __ALGORITHM_CONDITION_H */
/** Copy constructor */
Gene(const Gene& gene) : value(gene.get()) {}
- Gene& operator=(const Gene&){
+ Gene& operator=(const Gene& gene) {
+ this->value = gene.get();
return *this;
}
#include "fitness/wsti.h"
#include "algorithm.h"
+#include "condition/condition.h"
using namespace std;
using namespace genetic;
typedef generator::Generation<_Chromosome> _Generator;
typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation> _Algorithm;
+ typedef Condition<_Chromosome> _Condition;
- const int chromosomeSize = 11;
- const int generationSize = 30;
+ const int chromosomeSize = 10;
+ const int generationSize = 200;
const double crossoverChance = 0.75;
const double mutationChance = 0.01;
-// const int numberOfGenerations = 100;
_Fitness fitness(0.5, 2.5);
_Generator generationGenerator(generationSize, chromosomeSize);
_Algorithm algorithm(generationGenerator, fitness, crossoverChance, mutationChance);
+ _Condition condition;
- algorithm.searchForResult();
+ algorithm.searchForResult(condition);
return 0;
}
class Mutation {
public:
typedef double MutationChanceType;
+ typedef typename _Chromosome::GeneType GeneType;
protected:
MutationChanceType chance;
for (unsigned int i = 0; i < generationSize; i++) {
MutationChanceType random = (rand() % 10000) / 10000.0;
-// cout << " Mutation chance: " << chance << ", random: " << random << "\n";
- newGeneration.push_back(_generation.get()[i]);
+ _Chromosome chromosome = _generation.get()[i];
if (random < this->chance) {
-// cout << " > Mutated!\n";
- unsigned int which = (rand() % chromosomeSize);
- newGeneration[i].get()[which] = !newGeneration[i].get()[which].get();
+ unsigned int mutatedGene = (rand() % chromosomeSize);
+ vector<GeneType> newChromosome = chromosome.get();
+
+ newChromosome[mutatedGene] = GeneType(!newChromosome[mutatedGene].get());
+
+ chromosome = _Chromosome(newChromosome);
}
+ newGeneration.push_back(chromosome);
}
return Generation<_Chromosome>(newGeneration);
}