6 #include "chromosome.h"
7 #include "generation.h"
8 #include "fitness/fitness.h"
9 #include "generator/generation.h"
10 #include "selection/selection.h"
11 #include "crossover/crossover.h"
12 #include "mutation/mutation.h"
14 #include "condition/condition.h"
20 * Genetic Algorithm itself
22 template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation, typename _Fitness = Fitness<_Chromosome, typename _Selection::FitnessValueType > >
26 * Type representing Fitness value
28 typedef typename _Selection::FitnessValueType FitnessValueType;
31 * Generator which draws initial population
33 generator::Generation<_Chromosome>& generator;
36 * Fitness function used in selection
38 * It is a reference, because Fitness is purely virtual class and we
39 * can't have instance of it. It must be one of derived classes.
56 Generation<_Chromosome> generation;
59 * Method invoked in each iteration of algorithm.
61 * Applies selection, crossover and mutation on each of the chromosomes
65 _Selection selection(this->generation, fitness);
66 this->generation = selection.draw();
67 this->generation = crossover.cross(this->generation);
68 this->generation = mutation.mutate(this->generation);
72 * Class constructor. Initializes class with values and breeds initial
73 * population using passed generator::Generation.
75 * @param _generator Generator used to generate initial population
76 * @param _fitness Fitness class used to check fitness of the chromosome
77 * @param crossoverChance probability of crossover (0 = 0%, 1 = 100%)
78 * @param mutationChance probability of fitness (0 = 0%, 1 = 100%)
81 generator::Generation<_Chromosome>& _generator,
83 double crossoverChance,
84 double mutationChance) :
85 generator(_generator),
87 crossover(crossoverChance),
88 mutation(mutationChance) {
90 this->generation = this->generator.breed();
94 * Starts Algorithm in search for result.
95 * For each generation but initial, condition is checked, if algorithm
98 * Displays separated by space: generation number, generation average
99 * fitness and best chromosome fitness.
101 * @param condition Condition used to check after breeding new generation
103 void searchForResult(Condition<_Chromosome>& condition) {
105 cout << "generation avg best\n";
110 // cout << "New Generation:\n";
111 // this->showGeneration();
112 this->showAvgFitness();
113 this->showBestFitness();
114 } while(condition.check(this->generation) && ++i);
118 * Displays entire generation.
119 * \attention Method is currently not in use.
121 void showGeneration() {
122 unsigned int generationSize = this->generation.size();
123 unsigned int chromosomeSize = this->generation[0].size();
125 for (unsigned int i = 0; i < generationSize; i++) {
126 cout << "# " << i << ") ";
127 for (unsigned int j = 0; j < chromosomeSize; j++) {
128 cout << this->generation[i][j].get();
135 * Displays average fitness value of the entire generation.
137 void showAvgFitness() {
139 unsigned int generationSize = this->generation.size();
141 for (unsigned int i = 0; i < generationSize; i++) {
142 _Fitness fit(this->generation[i]);
143 fit.parseArguments(fitness.getArguments());
144 avg += fit.calculate();
146 cout << " " << avg / generationSize;
150 * Displays best fitness value of the entire generation.
152 void showBestFitness() {
153 double best = -100000;
154 unsigned int generationSize = this->generation.size();
156 for (unsigned int i = 0; i < generationSize; i++) {
157 _Fitness fit(this->generation[i]);
158 fit.parseArguments(fitness.getArguments());
160 double tmp = fit.calculate();
165 cout << " " << best << endl;
170 #endif /* __ALGORITHM_H */