<commit_before>#include "selections/TournamentSelection.hpp"
#include <random>
#include <sstream>
#include <algorithm>

//TEMP
#include <iostream>

using namespace std;

TournamentSelection::TournamentSelection(
	double crossoverRate
) : SelectionStrategy(
	crossoverRate,
	"Tournament"
) {
	init(DEFAULT_TOURNAMENT_SIZE);
}

TournamentSelection::TournamentSelection(
	double crossoverRate,
	unsigned int tournamentSize
) : SelectionStrategy(
	crossoverRate,
	"Tournament"
) {
	init(tournamentSize);
}

TournamentSelection::TournamentSelection(
	double crossoverRate,
	unsigned int tournamentSize,
	unsigned seed
) : SelectionStrategy(seed, crossoverRate, "Tournament") {
	init(tournamentSize);
}

void TournamentSelection::init(unsigned int tournamentSize) {
	this->tournamentSize = tournamentSize;
}

void TournamentSelection::sortByFitness(
	vector<int> & indexes,
	vector<int> & fitnesses
) {
	int tempIndex, tempFitness;
	for (int i = 0; i < indexes.size(); i++) {
		for (int k = 0; k < indexes.size(); k++) {
			if (fitnesses[i] > fitnesses[k]) {
				tempFitness = fitnesses[k];
				tempIndex = indexes[k];
				fitnesses[k] = fitnesses[i];
				indexes[k] = indexes[i];
				fitnesses[i] = tempFitness;
				indexes[i] = tempIndex;
			}
		}
	}
}

int TournamentSelection::getParent(
	vector<Genome*> population,
	vector<int> fitnesses
) {
	unsigned int populationSize = fitnesses.size();
	unsigned int tournamentSize = min(this->tournamentSize, populationSize);
	vector<int> tempFitnesses, indexes;
	int index;

        uniform_real_distribution<double> selectionDist(0,1);
	uniform_int_distribution<int> indexDist(0, fitnesses.size() - 1);

	for (int i = 0; i < tournamentSize; i++) {
		index = indexDist(this->generator);
		tempFitnesses.push_back(fitnesses[index]);
		indexes.push_back(index);
	}

	this->sortByFitness(indexes, tempFitnesses);

	for (int i = 0; i < indexes.size(); i++) {
		if (
			selectionDist(generator) < this->crossoverRate
			|| i == indexes.size() - 1
		) {
			return indexes[i];
		}
	}
}

string TournamentSelection::toString() {
	stringstream ss;

	ss << "Name: " << name
	<< "\nTournament size: " << tournamentSize
	<< "\nRandom seed: " << seed
	<< "\nCrossover Rate: " << crossoverRate<commit_msg>[TournamentSelections]: Fixed warnings<commit_after>#include "selections/TournamentSelection.hpp"
#include <random>
#include <sstream>
#include <algorithm>

//TEMP
#include <iostream>

using namespace std;

TournamentSelection::TournamentSelection(
	double crossoverRate
) : SelectionStrategy(
	crossoverRate,
	"Tournament"
) {
	init(DEFAULT_TOURNAMENT_SIZE);
}

TournamentSelection::TournamentSelection(
	double crossoverRate,
	unsigned int tournamentSize
) : SelectionStrategy(
	crossoverRate,
	"Tournament"
) {
	init(tournamentSize);
}

TournamentSelection::TournamentSelection(
	double crossoverRate,
	unsigned int tournamentSize,
	unsigned seed
) : SelectionStrategy(seed, crossoverRate, "Tournament") {
	init(tournamentSize);
}

void TournamentSelection::init(unsigned int tournamentSize) {
	this->tournamentSize = tournamentSize;
}

void TournamentSelection::sortByFitness(
	vector<int> & indexes,
	vector<int> & fitnesses
) {
	int tempIndex, tempFitness;
	for (unsigned int i = 0; i < indexes.size(); i++) {
		for (unsigned int k = 0; k < indexes.size(); k++) {
			if (fitnesses[i] > fitnesses[k]) {
				tempFitness = fitnesses[k];
				tempIndex = indexes[k];
				fitnesses[k] = fitnesses[i];
				indexes[k] = indexes[i];
				fitnesses[i] = tempFitness;
				indexes[i] = tempIndex;
			}
		}
	}
}

int TournamentSelection::getParent(
	vector<Genome*> population,
	vector<int> fitnesses
) {
	unsigned int populationSize = fitnesses.size();
	unsigned int tournamentSize = min(this->tournamentSize, populationSize);
	vector<int> tempFitnesses, indexes;
	unsigned int index;

        uniform_real_distribution<double> selectionDist(0,1);
	uniform_int_distribution<unsigned int> indexDist(
		0,
		fitnesses.size() - 1
	);

	for (unsigned int i = 0; i < tournamentSize; i++) {
		index = indexDist(this->generator);
		tempFitnesses.push_back(fitnesses[index]);
		indexes.push_back(index);
	}

	this->sortByFitness(indexes, tempFitnesses);

	for (unsigned int i = 0; i < indexes.size(); i++)
		if (selectionDist(generator) < this->crossoverRate)
			return indexes[i];

	return indexes[indexes.size() - 1];
}

string TournamentSelection::toString() {
	stringstream ss;

	ss << "Name: " << name
	<< "\nTournament size: " << tournamentSize
	<< "\nRandom seed: " << seed
	<< "\nCrossover Rate: " << crossoverRate
{'fraction_non_alphanumeric': 0.13486117232260908, 'fraction_numerical': 0.003966505068312032, 'mean_word_length': 5.209302325581396, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674295', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1491, 'n_words': 322}