* \attention Method is currently not in use.
*/
void showGeneration() {
- for (unsigned int i = 0; i < this->generation.size(); i++) {
+ unsigned int generationSize = this->generation.size();
+ unsigned int chromosomeSize = this->generation[0].size();
+
+ for (unsigned int i = 0; i < generationSize; i++) {
cout << "# " << i << ") ";
- for (unsigned int j = 0; j < this->generation[i].size(); j++) {
+ for (unsigned int j = 0; j < chromosomeSize; j++) {
cout << this->generation[i][j].get();
}
cout << "\n";
*/
void showAvgFitness() {
double avg = 0;
- for (unsigned int i = 0; i < this->generation.size(); i++) {
+ unsigned int generationSize = this->generation.size();
+
+ for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
fit.parseArguments(fitness.getArguments());
avg += fit.calculate();
}
- cout << " " << avg / this->generation.size();
+ cout << " " << avg / generationSize;
}
/**
*/
void showBestFitness() {
double best = -100000;
- for (unsigned int i = 0; i < this->generation.size(); i++) {
+ unsigned int generationSize = this->generation.size();
+
+ for (unsigned int i = 0; i < generationSize; i++) {
_Fitness fit(this->generation[i]);
fit.parseArguments(fitness.getArguments());
_Value fenotype() {
int _fenotype = 0;
int ratio = 1;
- for (unsigned int i = 0; i < this->chromosome.size(); i++) {
+ unsigned int chromosomeSize = this->chromosome.size();
+
+ for (unsigned int i = 0; i < chromosomeSize; i++) {
_fenotype = _fenotype + this->chromosome[i].get() * ratio;
ratio = ratio * 2;
}
* with additional changes complex struct.
*/
Type value;
+
+ template<typename> friend class Gene;
public:
/**
* Default constructor
}
/** Copy constructor */
- Gene(const Gene& gene) : value(gene.get()) {}
+ Gene(const Gene& gene) : value(gene.value) {}
/**
* Copy operator.
* @return Gene instance containing copied value
*/
Gene& operator=(const Gene& gene) {
- this->value = gene.get();
+ this->value = gene.value;
return *this;
}
const int chromosomeSize = 10;
const int generationSize = 200;
const double crossoverChance = 0.75;
- const double mutationChance = 0.1;
+ const double mutationChance = 0.01;
_Fitness fitness(0.5, 2.5);
_Generator generationGenerator(generationSize, chromosomeSize);
vector<FitnessValueType> calculateGenerationFitness(
Generation<_Chromosome> generation) {
vector<FitnessValueType> generationFitness;
+ unsigned int generationSize = generation.size();
- for (unsigned int i = 0; i < generation.size(); i++) {
+ for (unsigned int i = 0; i < generationSize; i++) {
generationFitness.push_back(this->checkChromosomeFitness(generation[i]));
}
FitnessValueType max;
FitnessValueType offset;
+ unsigned int fitnessSize = generationFitness.size();
+
/* we use multimap because it stores multiple values with the
* same key
*/
min = max = generationFitness[0];
offset = 0;
- for (unsigned int i = 0; i < generationFitness.size(); i++) {
+ for (unsigned int i = 0; i < fitnessSize; i++) {
if(generationFitness[i] < min) {
min = generationFitness[i];
}
offset = (max - min) / (chromosomeSize - 1) - min;
- for (unsigned int i = 0; i < generationFitness.size(); i++) {
+ for (unsigned int i = 0; i < fitnessSize; i++) {
normalizedFitness.insert(std::make_pair(generationFitness[i] + offset, this->generation[i]));
}