[yul-phaser] Make the Population constructor that takes individuals public and use it to speed up some operations

This commit is contained in:
Kamil Śliwak 2020-03-11 03:01:09 +01:00
parent 2750bb9b90
commit 7381068dcc
3 changed files with 23 additions and 7 deletions

View File

@ -104,6 +104,23 @@ BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_compute_fitness_and_
BOOST_TEST(individuals[2].chromosome == chromosomes[1]);
}
BOOST_FIXTURE_TEST_CASE(constructor_should_accept_individuals_without_recalculating_fitness, PopulationFixture)
{
vector<Individual> customIndividuals = {
Individual(Chromosome("aaaccc"), 20),
Individual(Chromosome("aaa"), 10),
Individual(Chromosome("aaaf"), 30),
};
assert(customIndividuals[0].fitness != m_fitnessMetric->evaluate(customIndividuals[0].chromosome));
assert(customIndividuals[1].fitness != m_fitnessMetric->evaluate(customIndividuals[1].chromosome));
assert(customIndividuals[2].fitness != m_fitnessMetric->evaluate(customIndividuals[2].chromosome));
Population population(m_fitnessMetric, customIndividuals);
vector<Individual> expectedIndividuals{customIndividuals[1], customIndividuals[0], customIndividuals[2]};
BOOST_TEST(population.individuals() == expectedIndividuals);
}
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
{
size_t chromosomeCount = 30;

View File

@ -187,16 +187,16 @@ Population AlgorithmRunner::randomiseDuplicates(
if (_population.individuals().size() == 0)
return _population;
vector<Chromosome> chromosomes{_population.individuals()[0].chromosome};
vector<Individual> individuals{_population.individuals()[0]};
size_t duplicateCount = 0;
for (size_t i = 1; i < _population.individuals().size(); ++i)
if (_population.individuals()[i].chromosome == _population.individuals()[i - 1].chromosome)
++duplicateCount;
else
chromosomes.push_back(_population.individuals()[i].chromosome);
individuals.push_back(_population.individuals()[i]);
return (
Population(_population.fitnessMetric(), chromosomes) +
Population(_population.fitnessMetric(), individuals) +
Population::makeRandom(_population.fitnessMetric(), duplicateCount, _minChromosomeLength, _maxChromosomeLength)
);
}

View File

@ -81,6 +81,9 @@ public:
_fitnessMetric,
chromosomesToIndividuals(*_fitnessMetric, std::move(_chromosomes))
) {}
explicit Population(std::shared_ptr<FitnessMetric> _fitnessMetric, std::vector<Individual> _individuals):
m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{sortedIndividuals(std::move(_individuals))} {}
static Population makeRandom(
std::shared_ptr<FitnessMetric> _fitnessMetric,
@ -112,10 +115,6 @@ public:
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
private:
explicit Population(std::shared_ptr<FitnessMetric> _fitnessMetric, std::vector<Individual> _individuals):
m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{sortedIndividuals(std::move(_individuals))} {}
static std::vector<Individual> chromosomesToIndividuals(
FitnessMetric& _fitnessMetric,
std::vector<Chromosome> _chromosomes