#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;
}
};
// }