/**
* Fitness function used in selection
+ *
+ * It is a reference, because Fitness is purely virtual class and we
+ * can't have instance of it. It must be one of derived classes.
*/
Fitness<_Chromosome, FitnessValueType>& fitness;
*/
Generation<_Chromosome> generation;
+ /**
+ * Method invoked in each iteration of algorithm.
+ *
+ * Applies selection, crossover and mutation on each of the chromosomes
+ * in generation.
+ */
void do_search() {
_Selection selection(this->generation, fitness);
this->generation = selection.draw();
this->generation = mutation.mutate(this->generation);
}
public:
+ /**
+ * Class constructor. Initializes class with values and breeds initial
+ * population using passed generator::Generation.
+ *
+ * @param _generator Generator used to generate initial population
+ * @param _fitness Fitness class used to check fitness of the chromosome
+ * @param crossoverChance probability of crossover (0 = 0%, 1 = 100%)
+ * @param mutationChance probability of fitness (0 = 0%, 1 = 100%)
+ */
Algorithm(
generator::Generation<_Chromosome>& _generator,
Fitness<_Chromosome, FitnessValueType>& _fitness,
this->generation = this->generator.breed();
}
+ /**
+ * Starts Algorithm in search for result.
+ * For each generation but initial, condition is checked, if algorithm
+ * should stop.
+ *
+ * Displays separated by space: generation number, generation average
+ * fitness and best chromosome fitness.
+ *
+ * @param condition Condition used to check after breeding new generation
+ */
void searchForResult(Condition<_Chromosome>& condition) {
unsigned int i = 0;
cout << "generation avg best\n";
} while(condition.check(this->generation) && ++i);
}
+ /**
+ * Displays entire generation.
+ * \attention Method is currently not in use.
+ */
void showGeneration() {
for (unsigned int i = 0; i < this->generation.get().size(); i++) {
cout << "# " << i << ") ";
}
}
+ /**
+ * Displays average fitness value of the entire generation.
+ * @todo make it more generic
+ */
void showAvgFitness() {
double avg = 0;
for (unsigned int i = 0; i < this->generation.get().size(); i++) {
cout << " " << avg / this->generation.get().size();
}
+ /**
+ * Displays best fitness value of the entire generation.
+ * @todo make it more generic, currently has hardcoded WSTI Fitness
+ * class, due its additional arguments
+ */
void showBestFitness() {
double best = -100000;
for (unsigned int i = 0; i < this->generation.get().size(); i++) {
class Condition {
public:
protected:
+ /**
+ * Variable indicating current generation
+ */
unsigned int currentGeneration = 0;
private:
+ /**
+ * Variable indicating max number of generations, after which program
+ * will be stopped
+ */
const unsigned int maxNumberOfGenerations = 1000;
public:
Condition() { }
#include <vector>
#include <utility> // std::pair
-#include <algorithm> // std::sort
#include <map>
#include <cstdlib>
#include <iostream>
namespace genetic {
// namespace selection {
+ /**
+ * Selection class Roulette.
+ * Applies Selection on given Generation using Roulette method.
+ */
template < typename _Chromosome >
class Roulette : public Selection<_Chromosome> {
public:
typedef Selection<_Chromosome> BaseType;
typedef typename BaseType::FitnessValueType FitnessValueType;
protected:
+ /**
+ * Method used for calculating fitness of each Chromosome in
+ * Generation.
+ *
+ * @param generation Generation for which fitness will be calculated
+ * @return vector containing fitness value of each Chromosome in
+ * Generation. Values are set in the same order as they are in
+ * the Generation.
+ */
vector<FitnessValueType> calculateGenerationFitness(
Generation<_Chromosome> generation) {
vector<FitnessValueType> generationFitness;
return generationFitness;
}
+
+ /**
+ * Normalizes calculated fitness values of the generation.
+ *
+ * @param generationFitness fitness values of the generation
+ * @param chromosomeSize number of Gene's in the Chromosome.
+ *
+ * @return multimap containing normalized Fitness as a key and its
+ * Chromosome as the value
+ */
multimap<FitnessValueType, _Chromosome> normalizeFitness(
vector<FitnessValueType> generationFitness,
unsigned int chromosomeSize) {
FitnessValueType min;
FitnessValueType max;
FitnessValueType offset;
+
+ /* we use multimap because it stores multiple values with the
+ * same key
+ */
multimap<FitnessValueType, _Chromosome> normalizedFitness;
min = max = generationFitness[0];
/**
* Spins the Roulette
+ *
+ * @param normalizedFitness multimap containing normalized Fitness
+ * as a key and its Chromosome as the value
+ * @return new Generation of Chromosome's that passed the selection
*/
Generation<_Chromosome> spinRoulette(
multimap<FitnessValueType, _Chromosome> normalizedFitness) {
}
/**
- * Draws random
+ * Draws new Generation using Roulette method
*/
Generation<_Chromosome> do_draw() {
multimap<FitnessValueType, _Chromosome> normalizedFitness;
}
public:
+ /**
+ * Class constructor. Initializes class with values.
+ *
+ * @param _generation Generation that will be checked against this
+ * Selection
+ * @param _fitness Fitness method to calculate fitness of Chromosomes
+ */
Roulette(Generation<_Chromosome> _generation, genetic::Fitness<_Chromosome>& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
this->generation = _generation;