[yul-phaser] Population: Extract sorting from doSelection() into sortIndividuals() in Population class

This commit is contained in:
cameel 2020-02-05 16:04:17 +01:00 committed by Kamil Śliwak
parent 41f36f421d
commit 85074e7a8a
2 changed files with 10 additions and 3 deletions

View File

@ -148,9 +148,7 @@ void Population::doEvaluation()
void Population::doSelection() void Population::doSelection()
{ {
assert(all_of(m_individuals.begin(), m_individuals.end(), [](auto& i){ return i.fitness.has_value(); })); m_individuals = sortedIndividuals(move(m_individuals));
sort(m_individuals.begin(), m_individuals.end(), isFitter);
randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2); randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2);
} }
@ -179,3 +177,11 @@ vector<Individual> Population::chromosomesToIndividuals(
return individuals; return individuals;
} }
vector<Individual> Population::sortedIndividuals(vector<Individual> _individuals)
{
assert(all_of(_individuals.begin(), _individuals.end(), [](auto& i){ return i.fitness.has_value(); }));
sort(_individuals.begin(), _individuals.end(), isFitter);
return _individuals;
}

View File

@ -121,6 +121,7 @@ private:
static std::vector<Individual> chromosomesToIndividuals( static std::vector<Individual> chromosomesToIndividuals(
std::vector<Chromosome> _chromosomes std::vector<Chromosome> _chromosomes
); );
static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals);
Program m_program; Program m_program;
std::vector<Individual> m_individuals; std::vector<Individual> m_individuals;