*/
Algorithm(
generator::Generator<_Chromosome>& _generator,
- _Fitness& _fitness,
+ _Fitness& _fitness,
float crossoverChance,
float mutationChance) :
generator(_generator),
* \attention Method is currently not in use.
*/
void showGeneration() {
- unsigned int generationSize = this->generation.size();
- unsigned int chromosomeSize = this->generation[0].size();
+ const unsigned int generationSize = this->generation.size();
+ const unsigned int chromosomeSize = this->generation[0].size();
for (unsigned int i = 0; i < generationSize; i++) {
cout << "# " << i << ") ";
*/
void showAvgFitness() {
float avg = 0;
- unsigned int generationSize = this->generation.size();
+ const unsigned int generationSize = this->generation.size();
+ std::map<string, string> arguments = fitness.getArguments();
for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
- fit.parseArguments(fitness.getArguments());
+ fit.parseArguments(arguments);
avg += fit.calculate();
}
cout << " " << avg / generationSize;
*/
void showBestFitness() {
float best = -100000;
- unsigned int generationSize = this->generation.size();
+ const unsigned int generationSize = this->generation.size();
+ std::map<string, string> arguments = fitness.getArguments();
for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
- fit.parseArguments(fitness.getArguments());
+ fit.parseArguments(arguments);
float tmp = fit.calculate();
if (tmp > best) {
best = tmp;
}
}
- cout << " " << best << endl;
+ cout << " " << best << "\n";
}
};
}
/**
* Genes of the chromosome
*/
- vector<_Gene> genes;
+ std::vector<_Gene> genes;
template<typename> friend class Chromosome;
public:
*
* @param genes vector containing Genes to use in Chromosome
*/
- Chromosome(vector<_Gene> genes) {
+ Chromosome(const std::vector<_Gene>& genes) {
this->genes = genes;
}
*
* @return i-th Gene in the current Chromosome
*/
- _Gene& operator[](unsigned int i) {
+ _Gene& operator[](const unsigned int i) {
return this->genes[i];
}
};
* @return true if condition is satisfied and another generation can checked;
* false if condition is not satisfied and algorithm should stop.
*/
- virtual bool do_check(Generation<_Chromosome>&) = 0;
+ virtual bool do_check(const Generation<_Chromosome>&) = 0;
public:
/**
* @return true if condition is satisfied and another generation can checked;
* false if condition is not satisfied and algorithm should stop.
*/
- bool check(Generation<_Chromosome>& generation) {
+ bool check(const Generation<_Chromosome>& generation) {
return do_check(generation);
}
};
* @return true if limit is not reached and another iteration of
* calculations should be started, false otherwise
*/
- bool do_check(Generation<_Chromosome> &) {
+ bool do_check(const Generation<_Chromosome> &) {
/* Initial population is never checked, as method is invoked after
* selection, crossover and mutation. It is safe to increment it now.
*/
*
* @param limit number of generations after which algorithm should stop
*/
- GenerationLimitCondition(unsigned int limit)
+ GenerationLimitCondition(const unsigned int limit)
: maxNumberOfGenerations(limit) {
}
};
* @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) {
+ _Chromosome do_cross(_Chromosome& first, _Chromosome& second, const unsigned int splitPlace) {
const unsigned int chromosomeSize = first.size();
// cout << " ";
*
* @param chance probability of Crossover (0 = 0%, 1 = 100%)
*/
- Crossover(CrossoverChanceType chance) :
+ Crossover(const CrossoverChanceType& chance) :
chance(chance) {
}
*/
Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) {
const unsigned int generationSize = _generation.size();
- vector<_Chromosome> newGeneration;
+ std::vector<_Chromosome> newGeneration;
for (unsigned int i = 0; i < generationSize; i++) {
CrossoverChanceType random = (rand() + 1 % 10000) / 10000.0;
/**
* Copy constructor
*/
- Fitness(_Chromosome& _chromosome)
+ Fitness(const _Chromosome& _chromosome)
: chromosome(_chromosome) {
}
* Method used to pass additional arguments needed by the function to
* run correctly.
*/
- virtual void parseArguments(std::map<string, string>) { }
+ virtual void parseArguments(std::map<string, string>&) { }
/**
* Method used to get additional arguments needed by the function to
*
* @param _chromosome Chromosome, for which value will be calculated
*/
- WSTI(_Chromosome& _chromosome)
+ WSTI(const _Chromosome& _chromosome)
: Fitness<_Chromosome>(_chromosome) {
}
* @param start begining of the Fitness function domain
* @param end end of the Fitness function domain
*/
- WSTI(_Chromosome& _chromosome, float start, float end)
+ WSTI(const _Chromosome& _chromosome, float start, float end)
: Fitness<_Chromosome>(_chromosome), span_start(start), span_end(end) {
}
* @param args map containing span_start and span_end as a keys and
* their values (_double_) passed as strings
*/
- virtual void parseArguments(std::map<string, string> args) {
+ virtual void parseArguments(std::map<string, string>& args) {
std::map<string, string>::iterator it;
for (it = args.begin(); it != args.end(); it++) {
if (it->first == "span_start") {
/**
* Class constructor, initializes Gene with default value.
*/
- Gene(Type value) {
+ Gene(const Type& value) {
this->value = value;
}
*
* @param chromosomes vector containing Chromosomes to use in Generation
*/
- Generation(vector<_Chromosome> chromosomes) {
+ Generation(const std::vector<_Chromosome>& chromosomes) {
this->chromosomes = chromosomes;
}
*
* @return i-th Chromosome in the current Generation
*/
- _Chromosome& operator[](unsigned int i) {
+ _Chromosome& operator[](const unsigned int i) {
return this->chromosomes[i];
}
};
*
* @param chance Chance on which Mutation can occur.
*/
- Mutation(MutationChanceType chance) :
+ Mutation(const MutationChanceType& chance) :
chance(chance) {
}
ChromosomeMap rankedGeneration;
unsigned int rank = generationSize;
- double denominator = generationSize * (generationSize - 1);
+ const double denominator = generationSize * (generationSize - 1);
for (ChromosomeMapIterator it = generationFitness.begin(); it != generationFitness.end(); it++) {
rankedGeneration.insert(std::make_pair(rank--/denominator, it->second));
ChromosomeMap probabilities = rankChromosomes();
unsigned int size = probabilities.size();
- unsigned int denominator = size * (size - 1);
+ const unsigned int denominator = size * (size - 1);
std::vector<_Chromosome> selected;
while (size > 0) {
bool found = false;
- random = (rand() % size) / (double)denominator;
+ random = (rand() % size) / (float)denominator;
for (ChromosomeMapIterator it = probabilities.begin(); it != probabilities.end(); it++) {
if (random < it->first) {
return Generation<_Chromosome>(selected);
}
public:
- LinearRankSelection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
+ LinearRankSelection(const Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
}
};
#include <vector>
#include <utility> // std::pair
#include <map>
-#include <cstdlib>
-#include <iostream>
#include "chromosome.h"
#include "selection.h"
* Generation. Values are set in the same order as they are in
* the Generation.
*/
- vector<FitnessValueType> calculateGenerationFitness(
- Generation<_Chromosome> generation) {
- vector<FitnessValueType> generationFitness;
+ std::vector<FitnessValueType> calculateGenerationFitness(
+ const Generation<_Chromosome>& generation) {
+ std::vector<FitnessValueType> generationFitness;
const unsigned int generationSize = generation.size();
for (unsigned int i = 0; i < generationSize; i++) {
* @return multimap containing normalized Fitness as a key and its
* Chromosome as the value
*/
- multimap<FitnessValueType, _Chromosome> normalizeFitness(
- vector<FitnessValueType> generationFitness,
+ std::multimap<FitnessValueType, _Chromosome> normalizeFitness(
+ const std::vector<FitnessValueType>& generationFitness,
unsigned int chromosomeSize) {
FitnessValueType min;
FitnessValueType max;
FitnessValueType offset;
- unsigned int fitnessSize = generationFitness.size();
+ const unsigned int fitnessSize = generationFitness.size();
/* we use multimap because it stores multiple values with the
* same key
*/
- multimap<FitnessValueType, _Chromosome> normalizedFitness;
+ std::multimap<FitnessValueType, _Chromosome> normalizedFitness;
min = max = generationFitness[0];
offset = 0;
* @return new Generation of Chromosome's that passed the Selection
*/
Generation<_Chromosome> spinRoulette(
- multimap<FitnessValueType, _Chromosome> normalizedFitness) {
+ const std::multimap<FitnessValueType, _Chromosome>& normalizedFitness) {
typedef typename std::multimap<FitnessValueType, _Chromosome>::iterator FitnessIterator;
- vector<_Chromosome> selected;
- multimap<FitnessValueType, _Chromosome> probabilities;
+ std::vector<_Chromosome> selected;
+ std::multimap<FitnessValueType, _Chromosome> probabilities;
unsigned int size = this->generation.size();
const unsigned int power2N = 1 << this->generation[0].size();
* @return new Generation of Chromosome's that passed the Selection
*/
Generation<_Chromosome> do_draw() {
- multimap<FitnessValueType, _Chromosome> normalizedFitness;
+ std::multimap<FitnessValueType, _Chromosome> normalizedFitness;
normalizedFitness = this->normalizeFitness(
this->calculateGenerationFitness(this->generation),
* Selection
* @param _fitness Fitness method to calculate fitness of Chromosomes
*/
- RouletteSelection(Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
+ RouletteSelection(const Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
- this->generation = _generation;
- this->fitness = _fitness;
}
};
// }
* checked.
* @return Value of the Fitness function
*/
- FitnessValueType checkChromosomeFitness(_Chromosome chromosome) {
+ FitnessValueType checkChromosomeFitness(const _Chromosome& chromosome) {
this->fitness.chromosome = chromosome;
return fitness.calculate();
}
* applied
* @param _fitness Fitness function to use in Selection
*/
- Selection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
+ Selection(const Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
generation(_generation), fitness(_fitness) {
}