std::vector<Genome*> StrongIterativeReplacingGA::breedMutateSelect(
	std::vector<Genome*> initialPopulation,
	std::vector<Fitness>& populationFitnesses,
	std::vector<ObjectiveFunction*> objectives,
	std::string speciesNode
) {
	std::vector<Genome*> newPopulation, children;
	std::vector<unsigned int> parentIndices(2, 0);

	for (unsigned int i = 0; i < initialPopulation.size(); i++)
		newPopulation.push_back(new Genome(initialPopulation[i]));

	for (unsigned int i = 0; i < initialPopulation.size(); i++) {
		parentIndices[0] = i;
		parentIndices[1] = this->getParent(
			initialPopulation,
			populationFitnesses
		);

		std::vector<Genome*> parents;
		for (unsigned int k = 0; k < parentIndices.size(); k++)
			parents.push_back(initialPopulation[parentIndices[k]]);

		children = this->produceChildren(parents, speciesNode);

		for (unsigned int k = 0; k < children.size(); k++) {
			Fitness childFitness = this->evaluateFitness(
				children[k],
				objectives
			);

			if (
				childFitness >
				populationFitnesses[parentIndices[k]]
			) {
				delete(newPopulation[parentIndices[k]]);
				newPopulation[parentIndices[k]] = children[k];
				populationFitnesses[parentIndices[k]] =
					childFitness;
			} else {
				delete(children[k]);
			}
		}
	}

	if (this->scramble)
		this->scramblePopulation(newPopulation, populationFitnesses);

	return(newPopulation);
}
Example #2
0
std::vector<Genome*> ReplacingGA::breedMutateSelect(
	std::vector<Genome*> initialPopulation,
	std::vector<float>& populationFitnesses,
	std::vector<ObjectiveFunction*> objectives,
	std::string speciesNode
) {
	std::vector<Genome*> newPopulation, children, parents;
	std::vector<unsigned int> parentIndices(2, 0);
	std::vector<float> newFitnesses(populationFitnesses);

	for (unsigned int i = 0; i < initialPopulation.size(); i++)
		newPopulation.push_back(new Genome(initialPopulation[i]));

	for (unsigned int i = 0; i < initialPopulation.size()/2; i++) {
		for (int k = 0; k < 2; k++) {
			parentIndices[k] = this->getParent(
				initialPopulation,
				populationFitnesses
			);
			parents.push_back(initialPopulation[parentIndices[k]]);
		}

		children = this->produceChildren(parents, speciesNode);
		parents.clear();

		for (unsigned int k = 0; k < children.size(); k++) {
			float childFitness = this->evaluateFitness(
				children[k],
				objectives
			);

			if (childFitness > newFitnesses[parentIndices[k]]) {
				delete(newPopulation[parentIndices[k]]);
				newPopulation[parentIndices[k]] = children[k];
				newFitnesses[parentIndices[k]] = childFitness;
			} else {
				delete(children[k]);
			}
		}
	}

	populationFitnesses = newFitnesses;
	return(newPopulation);
}