6 #include "chromosome.h"
7 #include "generation.h"
8 #include "fitness/fitness.h"
9 #include "generator/generator.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::Generator<_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::Generator<_Chromosome>& _generator,
83 float crossoverChance,
84 float 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 const unsigned int generationSize = this->generation.size();
123 const 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 const unsigned int generationSize = this->generation.size();
140 std::map<string, string> arguments = fitness.getArguments();
142 for (unsigned int i = 0; i < generationSize; i++) {
143 _Fitness fit(this->generation[i]);
144 fit.parseArguments(arguments);
145 avg += fit.calculate();
147 cout << " " << avg / generationSize;
151 * Displays best fitness value of the entire generation.
153 void showBestFitness() {
154 float best = -100000;
155 const unsigned int generationSize = this->generation.size();
156 std::map<string, string> arguments = fitness.getArguments();
158 for (unsigned int i = 0; i < generationSize; i++) {
159 _Fitness fit(this->generation[i]);
160 fit.parseArguments(arguments);
162 float tmp = fit.calculate();
167 cout << " " << best << "\n";
172 #endif /* __ALGORITHM_H */