<commit_before>#include <random>
#include <chrono>
#include <string>
#include <sstream>
#include "nodes/EANode/systems/GA.hpp"
#include "core/utils/HierRNG.hpp"
#include <limits>

GA::GA(
	unsigned int numElites,
	bool randomElitePlacement,
	SelectionStrategy* strategy,
	CrossoverOperation* cross,
	MutationOperation* mutation
) : EvolutionarySystem(strategy, cross, mutation) {
	this->init(numElites, randomElitePlacement);
}

GA::GA(
	unsigned int numElites,
	SelectionStrategy* strategy,
	CrossoverOperation* cross,
	MutationOperation* mutation
) : EvolutionarySystem(strategy, cross, mutation) {
	this->init(numElites, false);
}

void GA::init(unsigned int numElites, bool randomElitePlacement) {
	this->numElites = numElites;
	this->randomElitePlacement = randomElitePlacement;
}

void GA::placeElites(
	std::vector<Genome*> initialPopulation,
	std::vector<Fitness> initialPopulationFitnesses,
	std::vector<Genome*>& newPopulation,
	std::vector<Fitness>& newPopulationFitnesses
) {
	std::vector<unsigned int> eliteLocations = this->findElites(
		initialPopulationFitnesses
	);

	for (unsigned int i = 0; i < eliteLocations.size(); i++) {
		if (!this->randomElitePlacement) {
			newPopulation[i] = new Genome(
				initialPopulation[eliteLocations[i]]
			);
			newPopulationFitnesses[i] =
				initialPopulationFitnesses[eliteLocations[i]];
		} else {
			unsigned int index;

			do {
				index = HierRNG::index(
					initialPopulation.size() - 1
				);
			} while (newPopulation[index] != NULL);
			newPopulation[index] = new Genome(
				initialPopulation[eliteLocations[i]]
			);
			newPopulationFitnesses[index] =
				initialPopulationFitnesses[eliteLocations[i]];
		}
	}
}

std::vector<unsigned int> GA::findElites(std::vector<Fitness> fitnesses) {
	unsigned int populationSize = fitnesses.size(), bestFitnessIndex = 0;
	Fitness bestFitness;
	std::vector<bool> eliteLocations(populationSize, false);
	std::vector<unsigned int> eliteIndexes;
	unsigned int trueNumElites = this->numElites >= fitnesses.size() ?
		fitnesses.size()/2 : this->numElites;

	for (unsigned int i = 0; i < trueNumElites; i++) {
		// Still a dirty ugly hack, but it works
		bestFitness = Fitness(std::numeric_limits<double>::lowest());
		for (unsigned int k = 0; k < populationSize; k++) {
			if (
				fitnesses[k] > bestFitness
				&& eliteLocations[k] == false
			) {
				bestFitnessIndex = k;
				bestFitness  = fitnesses[k];
			}<commit_msg>[GA]: Updated to use utility function<commit_after>#include <random>
#include <chrono>
#include <string>
#include <sstream>
#include "nodes/EANode/systems/GA.hpp"
#include "core/utils/HierRNG.hpp"
#include <limits>

GA::GA(
	unsigned int numElites,
	bool randomElitePlacement,
	SelectionStrategy* strategy,
	CrossoverOperation* cross,
	MutationOperation* mutation
) : EvolutionarySystem(strategy, cross, mutation) {
	this->init(numElites, randomElitePlacement);
}

GA::GA(
	unsigned int numElites,
	SelectionStrategy* strategy,
	CrossoverOperation* cross,
	MutationOperation* mutation
) : EvolutionarySystem(strategy, cross, mutation) {
	this->init(numElites, false);
}

void GA::init(unsigned int numElites, bool randomElitePlacement) {
	this->numElites = numElites;
	this->randomElitePlacement = randomElitePlacement;
}

void GA::placeElites(
	std::vector<Genome*> initialPopulation,
	std::vector<Fitness> initialPopulationFitnesses,
	std::vector<Genome*>& newPopulation,
	std::vector<Fitness>& newPopulationFitnesses
) {
	std::vector<unsigned int> eliteLocations = this->findElites(
		initialPopulationFitnesses
	);

	for (unsigned int i = 0; i < eliteLocations.size(); i++) {
		if (!this->randomElitePlacement) {
			newPopulation[i] = new Genome(
				initialPopulation[eliteLocations[i]]
			);
			newPopulationFitnesses[i] =
				initialPopulationFitnesses[eliteLocations[i]];
		} else {
			unsigned int index;

			do {
				index = HierRNG::index(
					initialPopulation.size() - 1
				);
			} while (newPopulation[index] != NULL);
			newPopulation[index] = new Genome(
				initialPopulation[eliteLocations[i]]
			);
			newPopulationFitnesses[index] =
				initialPopulationFitnesses[eliteLocations[i]];
		}
	}
}

std::vector<unsigned int> GA::findElites(std::vector<Fitness> fitnesses) {
	unsigned int populationSize = fitnesses.size(), bestFitnessIndex = 0;
	Fitness bestFitness;
	std::vector<bool> eliteLocations(populationSize, false);
	std::vector<unsigned int> eliteIndexes;
	unsigned int trueNumElites = this->numElites >= fitnesses.size() ?
		fitnesses.size()/2 : this->numElites;

	for (unsigned int i = 0; i < trueNumElites; i++) {
		bestFitness = Fitness::lowestPossible();
		for (unsigned int k = 0; k < populationSize; k++) {
			if (
				fitnesses[k] > bestFitness
				&& eliteLocations[k] == false
			) {
				bestFitnessIndex = k;
				bestFitness  = fitnesses[k];
			}
{'fraction_non_alphanumeric': 0.12895174708818635, 'fraction_numerical': 0.0024958402662229617, 'mean_word_length': 5.352708058124175, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 52, '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': '5263562', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1593, 'n_words': 327}