template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
class Algorithm {
public:
+ /**
+ * Type representing Fitness value
+ */
typedef typename _Selection::FitnessValueType FitnessValueType;
protected:
/**
template < typename _Gene >
class Chromosome {
public:
+ /**
+ * Type representing Chromosome Gene
+ */
typedef _Gene GeneType;
protected:
/**
*/
Chromosome() {}
+ /**
+ * Class constructor. Initializes Chromosome with the given Gene's
+ *
+ * @param genes vector containing Genes to use in Chromosome
+ */
Chromosome(vector<_Gene> genes) {
this->genes = genes;
}
: genes(chromosome.get()) {
}
- Chromosome& operator=(const Chromosome& chromosome){
+ /**
+ * Copy operator.
+ *
+ * @param chromosome Chromosome from which the Genes should be copied.
+ * @return Chromosome instance containing copied Genes
+ */
+ Chromosome& operator=(const Chromosome& chromosome) {
this->genes = chromosome.get();
return *this;
}
+ /**
+ * Allows read-only access to Chromosome Genes
+ */
vector<_Gene> get() const {
return this->genes;
}
template < typename _Chromosome >
class Crossover {
public:
+ /**
+ * Type of probability of crossover chance
+ */
typedef double CrossoverChanceType;
+
+ /**
+ * Type representing Chromosome Gene
+ */
typedef typename _Chromosome::GeneType GeneType;
protected:
+ /**
+ * Probability of Crossover (0 = 0%, 1 = 100%)
+ */
CrossoverChanceType chance;
+ /**
+ * Crossover two Chromosome's between themself.
+ *
+ * @param first first Chromosome to Crossover
+ * @param second second Chromosome to Crossover
+ * @param splitPlace Gene number on which the Genes should be swapped
+ * @return new Chromosome crossed between given two
+ */
_Chromosome do_cross(_Chromosome first, _Chromosome second, unsigned int splitPlace) {
const unsigned int chromosomeSize = first.get().size();
}
public:
+ /**
+ * Class constructor. Initializes values.
+ *
+ * @param chance probability of Crossover (0 = 0%, 1 = 100%)
+ */
Crossover(CrossoverChanceType chance) :
chance(chance) {
}
+ /**
+ * Invokes Crossover calculations
+ *
+ * @param _generation Generation for which the crossover should be applied
+ * @return new Generation of Chromosome's after the Crossover
+ */
Generation<_Chromosome> cross(Generation<_Chromosome> _generation) {
const unsigned int generationSize = _generation.get().size();
vector<_Chromosome> newGeneration;
/**
* Just an example Fitness function.
*/
- template < typename _Chromosome >
- class FitnessExample {
+ template <typename _Chromosome, typename _Value = double>
+ class FitnessExample : public Fitness<_Chromosome, _Value> {
protected:
- _Chromosome chromosome;
+ /*
+ * Chromosome is inherited so you don't have to declare usage of one
+ */
+ /* _Chromosome chromosome; */
public:
+ /**
+ * Example class constructor
+ */
FitnessExample(_Chromosome& _chromosome)
- : chromosome(_chromosome.get()) {
+ : Fitness<_Chromosome>(_chromosome) {
}
- /*
- * Some calculations here...
+ /**
+ * Some calculations should be passed here...
*/
double calculate() {
return 0;
#ifndef __FITNESS_FITNESS_H
#define __FITNESS_FITNESS_H
-#include "../chromosome.h"
+#include "chromosome.h"
namespace genetic {
/**
*/
template < typename _Chromosome, typename _Value = double >
class Fitness {
- template<typename> friend class Selection ;
+ template<typename> friend class Selection;
public:
+ /**
+ * Type representing Chromosome Gene
+ */
typedef typename _Chromosome::GeneType GeneType;
+
+ /**
+ * Value type returned by the Fitness function
+ */
typedef _Value ValueType;
protected:
+ /**
+ * Chromosome on which calculations are made
+ */
_Chromosome chromosome;
- /*
- * Some calculations here...
+ /**
+ * Calculations should be done here...
+ *
+ * @return Fitness value of the current Chromosome
*/
virtual _Value do_calculate() = 0;
public:
+ /**
+ * Class constructor
+ */
Fitness() {}
+ /**
+ * Copy constructor
+ */
Fitness(_Chromosome& _chromosome)
: chromosome(_chromosome.get()) {
}
+ /**
+ * Invokes calculations
+ *
+ * @return Fitness value of the current Chromosome
+ */
_Value calculate() {
return this->do_calculate();
}
template <typename _Chromosome, typename _Value = double>
class WSTI : public Fitness<_Chromosome, _Value> {
protected:
+ /**
+ * Value of the begining of the Fitness function domain
+ */
float span_start;
+
+ /**
+ * Value of the end of the Fitness function domain
+ */
float span_end;
+ /**
+ * Calculates fenotype of the current Chromosome
+ */
_Value fenotype() {
int _fenotype = 0;
int ratio = 1;
return _fenotype;
}
+ /**
+ * Calculates fenotype of the current Chromosome
+ */
_Value calculateFenotype() {
const unsigned int power2N = 1 << this->chromosome.get().size();
return span_start + (span_end - span_start) * this->fenotype() / power2N;
}
+ /**
+ * Calculates fitness value of the current Chromosome
+ */
_Value do_calculate() {
_Value fenotype = this->calculateFenotype();
return (exp(fenotype) * sin(3.1415 * fenotype) + 1) / fenotype;
}
public:
+ /**
+ * Class constructor. Initializes class with requied values.
+ *
+ * @param start begining of the Fitness function domain
+ * @param end end of the Fitness function domain
+ */
WSTI(float start, float end)
: span_start(start), span_end(end) {
}
+
+ /**
+ * Class constructor. Initializes class with requied values.
+ *
+ * @param _chromosome Chromosome, for which value will be calculated
+ * @param start begining of the Fitness function domain
+ * @param end end of the Fitness function domain
+ */
WSTI(_Chromosome& _chromosome, float start, float end)
: Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) {
}
namespace genetic {
/**
- * Gene.
+ * Class representing Gene
*/
template < typename Type >
class Gene {
protected:
+ /**
+ * Value of the Gene
+ * This for example can be a primitive value such as: int or double, or
+ * with additional changes complex struct.
+ */
Type value;
public:
+ /**
+ * Default constructor
+ */
Gene() {}
+ /**
+ * Class constructor, initializes Gene with default value.
+ */
Gene(Type value) {
this->value = value;
}
/** Copy constructor */
Gene(const Gene& gene) : value(gene.get()) {}
+ /**
+ * Copy operator.
+ *
+ * @param gene Gene from which the value should be copied.
+ * @return Gene instance containing copied value
+ */
Gene& operator=(const Gene& gene) {
this->value = gene.get();
return *this;
}
+ /**
+ * Allows read-only access to Gene value
+ */
Type get() const {
return value;
}
namespace genetic {
/**
- * Generation of given Chromosomes.
+ * Class representing Generation of given Chromosomes.
*/
template < typename _Chromosome >
class Generation {
protected:
+ /**
+ * Chromosomes in the given Generation
+ */
vector<_Chromosome> chromosomes;
public:
+ /**
+ * Default constructor
+ */
Generation() {}
+ /**
+ * Class constructor. Initializes generation with the given chromosomes
+ *
+ * @param chromosomes vector containing Chromosomes to use in Generation
+ */
Generation(vector<_Chromosome> chromosomes) {
this->chromosomes = chromosomes;
}
: chromosomes(generation.get()) {
}
+ /**
+ * Allows read-only access to Generation Chromosomes
+ */
vector<_Chromosome> get() const {
return this->chromosomes;
}
+ /**
+ * Copy operator.
+ *
+ * @param generation Generation from which the Chromosomes should be copied.
+ * @return Generation instance containing copied Chromosomes
+ */
Generation& operator=(const Generation& generation){
this->chromosomes = generation.get();
return *this;
class Generation {
public:
/**
- * Type of used Genes in Chromosome
+ * Type representing Chromosome Gene
*/
typedef typename _Chromosome::GeneType GeneType;
protected:
* Constructor. Initializes required variables and constants
*
* @param generationSize Indicates size of the generation
- * @param generationSize Indicates size of the chromosome
+ * @param chromosomeSize Indicates size of the chromosome
*/
Generation(unsigned int generationSize, unsigned int chromosomeSize) {
this->generationSize = generationSize;
namespace genetic {
// namespace mutation {
+ /**
+ * Mutations class. Applies mutation on Chromosome's.
+ */
template < typename _Chromosome>
class Mutation {
public:
+ /**
+ * Type of probability of mutation chance
+ */
typedef double MutationChanceType;
+
+ /**
+ * Type representing Chromosome Gene
+ */
typedef typename _Chromosome::GeneType GeneType;
protected:
+ /**
+ * Chance with which mutation can occur.
+ */
MutationChanceType chance;
public:
+ /**
+ * Class constructor. Initializes class variables.
+ *
+ * @param chance Chance on which Mutation can occur.
+ */
Mutation(MutationChanceType chance) :
chance(chance) {
}
+ /**
+ * Applies with defined probability mutation on the given generation
+ * of Chromosome's.
+ *
+ * @param _generation Generation for which the mutation should be applied
+ * @return new Generation of Chromosome's that passed the mutation
+ */
Generation<_Chromosome> mutate(Generation<_Chromosome> _generation) {
const unsigned int generationSize = _generation.get().size();
const unsigned int chromosomeSize = _generation.get()[0].get().size();
template < typename _Chromosome >
class Roulette : public Selection<_Chromosome> {
public:
+ /**
+ * Parent Selection class
+ */
typedef Selection<_Chromosome> BaseType;
+
+ /**
+ * Value type returned by the Fitness function
+ */
typedef typename BaseType::FitnessValueType FitnessValueType;
protected:
/**
*
* @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
+ * @return new Generation of Chromosome's that passed the Selection
*/
Generation<_Chromosome> spinRoulette(
multimap<FitnessValueType, _Chromosome> normalizedFitness) {
/**
* Draws new Generation using Roulette method
+ *
+ * @return new Generation of Chromosome's that passed the Selection
*/
Generation<_Chromosome> do_draw() {
multimap<FitnessValueType, _Chromosome> normalizedFitness;
namespace genetic {
// namespace selection {
+ /**
+ * Base Selection template class. It should be a base class for any
+ * custom selection functions.
+ */
template < typename _Chromosome >
class Selection {
public:
+ /**
+ * Type representing Fitness function
+ */
typedef Fitness<_Chromosome> GeneticFitness;
+
+ /**
+ * Value type returned by the Fitness function
+ */
typedef typename GeneticFitness::ValueType FitnessValueType;
protected:
+ /**
+ * Generation over which the Selection will be applied
+ */
Generation<_Chromosome> generation;
+
+ /**
+ * Fitness which will be used in selection
+ */
GeneticFitness& fitness;
-
+
+ /**
+ * Checks Fitness for the given Chromosome
+ *
+ * @param chromosome Chromosome for which the selection fitness is
+ * checked.
+ * @return Value of the Fitness function
+ */
FitnessValueType checkChromosomeFitness(_Chromosome chromosome) {
this->fitness.chromosome = chromosome;
return fitness.calculate();
}
+ /**
+ * Selection calculations should be done here.
+ *
+ * @return new Generation of Chromosome's that passed the Selection
+ */
virtual Generation<_Chromosome> do_draw() = 0;
public:
+ /**
+ * Class constructor. Initializes required variables and constants
+ *
+ * @param _generation Generation over which the Selection will be
+ * applied
+ * @param _fitness Fitness function to use in Selection
+ */
Selection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
generation(_generation), fitness(_fitness) {
}
+ /**
+ * Invokes Selection calculations
+ *
+ * @return new Generation of Chromosome's that passed the Selection
+ */
Generation<_Chromosome> draw() {
return this->do_draw();
}