Algorithm(
generator::Generator<_Chromosome>& _generator,
_Fitness& _fitness,
- double crossoverChance,
- double mutationChance) :
+ float crossoverChance,
+ float mutationChance) :
generator(_generator),
fitness(_fitness),
crossover(crossoverChance),
* Displays average fitness value of the entire generation.
*/
void showAvgFitness() {
- double avg = 0;
+ float avg = 0;
unsigned int generationSize = this->generation.size();
for (unsigned int i = 0; i < generationSize; i++) {
* Displays best fitness value of the entire generation.
*/
void showBestFitness() {
- double best = -100000;
+ float best = -100000;
unsigned int generationSize = this->generation.size();
for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
fit.parseArguments(fitness.getArguments());
- double tmp = fit.calculate();
+ float tmp = fit.calculate();
if (tmp > best) {
best = tmp;
}
* If condition is satisfied, we can check another generation.
* All the custom Condition checks should be invoked in this method.
*
- * @param generation current generation to check
+ * @param generation reference of current generation to check
*
* @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(Generation<_Chromosome>&) = 0;
public:
/**
* Checks if current generation passes stop condition.
* If condition is satisfied, we can check another generation.
*
- * @param generation current generation to check
+ * @param generation reference of current generation to check
*
* @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(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(Generation<_Chromosome> &) {
/* Initial population is never checked, as method is invoked after
* selection, crossover and mutation. It is safe to increment it now.
*/
/**
* Type of probability of crossover chance
*/
- typedef double CrossoverChanceType;
+ typedef float CrossoverChanceType;
/**
* Type representing Chromosome Gene
* @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, unsigned int splitPlace) {
const unsigned int chromosomeSize = first.size();
// cout << " ";
* @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) {
+ Generation<_Chromosome> cross(Generation<_Chromosome>& _generation) {
const unsigned int generationSize = _generation.size();
vector<_Chromosome> newGeneration;
* Base Fitness template class. It should be a base class for any custom
* fitness functions.
*/
- template < typename _Chromosome, typename _Value = double >
+ template < typename _Chromosome, typename _Value = float >
class Fitness {
template<typename> friend class Selection;
public:
/**
* Just an example Fitness function based on WSTI version.
*/
- template <typename _Chromosome, typename _Value = double>
+ template <typename _Chromosome, typename _Value = float>
class WSTI : public Fitness<_Chromosome, _Value> {
protected:
/**
/**
* Type of probability of mutation chance
*/
- typedef double MutationChanceType;
+ typedef float MutationChanceType;
/**
* Type representing Chromosome Gene
* @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) {
+ Generation<_Chromosome> mutate(Generation<_Chromosome>& _generation) {
const unsigned int generationSize = _generation.size();
const unsigned int chromosomeSize = _generation[0].size();
vector<_Chromosome> newGeneration;
return Generation<_Chromosome>(selected);
}
public:
- LinearRankSelection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
+ LinearRankSelection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
}
};
vector<FitnessValueType> calculateGenerationFitness(
Generation<_Chromosome> generation) {
vector<FitnessValueType> generationFitness;
- unsigned int generationSize = generation.size();
+ const unsigned int generationSize = generation.size();
for (unsigned int i = 0; i < generationSize; i++) {
generationFitness.push_back(this->checkChromosomeFitness(generation[i]));
* Selection
* @param _fitness Fitness method to calculate fitness of Chromosomes
*/
- RouletteSelection(Generation<_Chromosome> _generation, genetic::Fitness<_Chromosome>& _fitness) :
+ RouletteSelection(Generation<_Chromosome>& _generation, genetic::Fitness<_Chromosome>& _fitness) :
Selection<_Chromosome>(_generation, _fitness) {
this->generation = _generation;
this->fitness = _fitness;
* applied
* @param _fitness Fitness function to use in Selection
*/
- Selection(Generation<_Chromosome> _generation, GeneticFitness& _fitness) :
+ Selection(Generation<_Chromosome>& _generation, GeneticFitness& _fitness) :
generation(_generation), fitness(_fitness) {
}