Improve comments and documentation.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 4 Apr 2015 19:00:37 +0000 (21:00 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 4 Apr 2015 19:00:37 +0000 (21:00 +0200)
12 files changed:
src/algorithm.h
src/chromosome.h
src/crossover/crossover.h
src/fitness/example.h
src/fitness/fitness.h
src/fitness/wsti.h
src/gene.h
src/generation.h
src/generator/generation.h
src/mutation/mutation.h
src/selection/roulette.h
src/selection/selection.h

index fe79938302c95720cc724de4a8e31d8cef9f1f4a..894f4880019034d0e2004bca4e45cfff4d49ecfd 100644 (file)
@@ -22,6 +22,9 @@ namespace genetic {
     template < typename _Chromosome, typename _Selection, typename _Crossover, typename _Mutation >
     class Algorithm {
     public:
+        /**
+         * Type representing Fitness value
+         */
         typedef typename _Selection::FitnessValueType FitnessValueType;
     protected:
         /**
index e197f169be2a649d5427cb13f0264b25269c547d..7399ea45959607575d476c741b316b49913efb41 100644 (file)
@@ -14,6 +14,9 @@ namespace genetic {
     template < typename _Gene >
     class Chromosome {
     public:
+        /**
+         * Type representing Chromosome Gene
+         */
         typedef _Gene GeneType;
     protected:
         /**
@@ -27,6 +30,11 @@ namespace genetic {
          */
         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;
         }
@@ -36,11 +44,20 @@ namespace genetic {
             : 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;
         }
index f494b7bbae01e7c61a690fb14643908082aa1776..0d9b309f7fc961f26594911f326a530727e028c5 100644 (file)
@@ -11,11 +11,29 @@ namespace genetic {
         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();
 
@@ -57,10 +75,21 @@ namespace genetic {
             }
 
         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;
index 46d89075762e44722153c9c0f8afa1d46182fe90..522b9d30c0675d2b6212d2da3922c1db8bb2085b 100644 (file)
@@ -11,17 +11,23 @@ namespace genetic {
     /**
      * 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;
index 14b6487f18b8e83708e04c15286988a33ee974ab..66263f1bbc82363adfea48830cbccb148c607bfe 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef __FITNESS_FITNESS_H
 #define __FITNESS_FITNESS_H
 
-#include "../chromosome.h"
+#include "chromosome.h"
 
 namespace genetic {
     /**
@@ -10,25 +10,48 @@ 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();
         }
index eaa0538b77d58f46cab63a68efd648d448fd4750..d99dd18b8e6d58673a5f750d6e7ca8634fe7d55b 100644 (file)
@@ -16,9 +16,19 @@ namespace genetic {
     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;
@@ -30,19 +40,39 @@ namespace genetic {
             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) {
         }
index e4142811b3e23bd31724d9a3ae7a243af828e801..f3b3c55fa1846ae6efc45c339724afd8d4421e20 100644 (file)
@@ -4,15 +4,26 @@
 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;
         }
@@ -20,11 +31,20 @@ namespace genetic {
         /** 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;
         }
index 39b9577bc4f7c855da8acdb0c2413de7dffe5721..a37602897694cc6d5afabf80d59331ff74fadca5 100644 (file)
@@ -10,16 +10,27 @@ using namespace std;
 
 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;
         }
@@ -29,10 +40,19 @@ namespace genetic {
             : 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;
index a51e4b364525df380d5d70e126b1d7f67e908c46..91e605d7e28035d15d073860c1c054e8c5d9ddcf 100644 (file)
@@ -19,7 +19,7 @@ namespace genetic {
         class Generation {
         public:
             /**
-             * Type of used Genes in Chromosome
+             * Type representing Chromosome Gene
              */
             typedef typename _Chromosome::GeneType GeneType;
         protected:
@@ -38,7 +38,7 @@ namespace genetic {
              * 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;
index 69c23e7fde54c4d47334a806d61299235e71ce15..7b626ba1e63c54e07db72e0b70552191e787c046 100644 (file)
@@ -10,19 +10,44 @@ using namespace std;
 
 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();
index 7f2d2401cd9f58c00e8a2f149099183d429d4e3b..6db71efbd38491dd7259db4f919521a963270c19 100644 (file)
@@ -21,7 +21,14 @@ namespace genetic {
         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:
             /**
@@ -92,7 +99,7 @@ namespace genetic {
              *
              * @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) {
@@ -155,6 +162,8 @@ namespace genetic {
 
             /**
              * Draws new Generation using Roulette method
+             *
+             * @return new Generation of Chromosome's that passed the Selection
              */
             Generation<_Chromosome> do_draw() {
                 multimap<FitnessValueType, _Chromosome> normalizedFitness;
index f519ca1eebdb47e0a05636db9d85db452cdb984d..dd6e846299b0ae34c159843e883b63a5b6c43082 100644 (file)
@@ -9,27 +9,69 @@ using namespace std;
 
 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();
             }