[yul-phaser] Population: Evaluate fitness immediately when an individual is added or modified

- This removes the explicit evaluation phase.
- Fitness is no longer optional in Individual
This commit is contained in:
cameel 2020-02-05 16:18:53 +01:00 committed by Kamil Śliwak
parent 66fdc1c374
commit 76842ac3fd
3 changed files with 32 additions and 64 deletions

View File

@ -42,19 +42,6 @@ using namespace boost::unit_test::framework;
namespace solidity::phaser::test namespace solidity::phaser::test
{ {
namespace
{
bool fitnessNotSet(Individual const& individual)
{
return !individual.fitness.has_value();
}
bool fitnessSet(Individual const& individual)
{
return individual.fitness.has_value();
}
}
class PopulationFixture class PopulationFixture
{ {
protected: protected:
@ -94,7 +81,7 @@ BOOST_AUTO_TEST_CASE(isFitter_should_return_false_for_identical_individuals)
BOOST_TEST(!isFitter(Individual{Chromosome("acT"), 0}, Individual{Chromosome("acT"), 0})); BOOST_TEST(!isFitter(Individual{Chromosome("acT"), 0}, Individual{Chromosome("acT"), 0}));
} }
BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness, PopulationFixture) BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_compute_fitness, PopulationFixture)
{ {
vector<Chromosome> chromosomes = { vector<Chromosome> chromosomes = {
Chromosome::makeRandom(5), Chromosome::makeRandom(5),
@ -106,8 +93,8 @@ BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitn
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]); BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
BOOST_TEST(population.individuals()[1].chromosome == chromosomes[1]); BOOST_TEST(population.individuals()[1].chromosome == chromosomes[1]);
auto fitnessNotSet = [](auto const& individual){ return !individual.fitness.has_value(); }; BOOST_TEST(population.individuals()[0].fitness == m_fitnessMetric->evaluate(population.individuals()[0].chromosome));
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet)); BOOST_TEST(population.individuals()[1].fitness == m_fitnessMetric->evaluate(population.individuals()[1].chromosome));
} }
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture) BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
@ -181,22 +168,13 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromoso
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
} }
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture) BOOST_FIXTURE_TEST_CASE(makeRandom_should_compute_fitness, PopulationFixture)
{ {
auto population = Population::makeRandom(m_fitnessMetric, 3, 5, 10); auto population = Population::makeRandom(m_fitnessMetric, 3, 5, 10);
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet)); BOOST_TEST(population.individuals()[0].fitness == m_fitnessMetric->evaluate(population.individuals()[0].chromosome));
} BOOST_TEST(population.individuals()[1].fitness == m_fitnessMetric->evaluate(population.individuals()[1].chromosome));
BOOST_TEST(population.individuals()[2].fitness == m_fitnessMetric->evaluate(population.individuals()[2].chromosome));
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
{
stringstream output;
auto population = Population::makeRandom(m_fitnessMetric, 5, 5, 10);
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
population.run(1, output);
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
} }
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture) BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
@ -220,12 +198,10 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, Po
{ {
population.run(1, output); population.run(1, output);
BOOST_TEST(population.individuals().size() == 5); BOOST_TEST(population.individuals().size() == 5);
BOOST_TEST(fitnessSet(population.individuals()[0]));
BOOST_TEST(fitnessSet(population.individuals()[1]));
size_t currentTopFitness[2] = { size_t currentTopFitness[2] = {
population.individuals()[0].fitness.value(), population.individuals()[0].fitness,
population.individuals()[1].fitness.value(), population.individuals()[1].fitness,
}; };
BOOST_TEST(currentTopFitness[0] <= initialTopFitness[0]); BOOST_TEST(currentTopFitness[0] <= initialTopFitness[0]);
BOOST_TEST(currentTopFitness[1] <= initialTopFitness[1]); BOOST_TEST(currentTopFitness[1] <= initialTopFitness[1]);

View File

@ -41,11 +41,7 @@ ostream& operator<<(ostream& _stream, Population const& _population);
ostream& phaser::operator<<(ostream& _stream, Individual const& _individual) ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
{ {
_stream << "Fitness: "; _stream << "Fitness: " << _individual.fitness;
if (_individual.fitness.has_value())
_stream << _individual.fitness.value();
else
_stream << "<NONE>";
_stream << ", optimisations: " << _individual.chromosome; _stream << ", optimisations: " << _individual.chromosome;
return _stream; return _stream;
@ -53,12 +49,10 @@ ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
bool phaser::isFitter(Individual const& a, Individual const& b) bool phaser::isFitter(Individual const& a, Individual const& b)
{ {
assert(a.fitness.has_value() && b.fitness.has_value());
return ( return (
(a.fitness.value() < b.fitness.value()) || (a.fitness < b.fitness) ||
(a.fitness.value() == b.fitness.value() && a.chromosome.length() < b.chromosome.length()) || (a.fitness == b.fitness && a.chromosome.length() < b.chromosome.length()) ||
(a.fitness.value() == b.fitness.value() && a.chromosome.length() == b.chromosome.length() && toString(a.chromosome) < toString(b.chromosome)) (a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && toString(a.chromosome) < toString(b.chromosome))
); );
} }
@ -68,11 +62,11 @@ Population Population::makeRandom(
function<size_t()> _chromosomeLengthGenerator function<size_t()> _chromosomeLengthGenerator
) )
{ {
vector<Individual> individuals; vector<Chromosome> chromosomes;
for (size_t i = 0; i < _size; ++i) for (size_t i = 0; i < _size; ++i)
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())}); chromosomes.push_back(Chromosome::makeRandom(_chromosomeLengthGenerator()));
return Population(move(_fitnessMetric), move(individuals)); return Population(move(_fitnessMetric), move(chromosomes));
} }
Population Population::makeRandom( Population Population::makeRandom(
@ -91,12 +85,10 @@ Population Population::makeRandom(
void Population::run(optional<size_t> _numRounds, ostream& _outputStream) void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
{ {
doEvaluation();
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round) for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
{ {
doMutation(); doMutation();
doSelection(); doSelection();
doEvaluation();
_outputStream << "---------- ROUND " << round << " ----------" << endl; _outputStream << "---------- ROUND " << round << " ----------" << endl;
_outputStream << *this; _outputStream << *this;
@ -134,20 +126,14 @@ void Population::doMutation()
// TODO: Implement mutation and crossover // TODO: Implement mutation and crossover
} }
void Population::doEvaluation()
{
for (auto& individual: m_individuals)
if (!individual.fitness.has_value())
individual.fitness = m_fitnessMetric->evaluate(individual.chromosome);
}
void Population::doSelection() void Population::doSelection()
{ {
m_individuals = sortedIndividuals(move(m_individuals)); m_individuals = sortedIndividuals(move(m_individuals));
randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2); randomizeWorstChromosomes(*m_fitnessMetric, m_individuals, m_individuals.size() / 2);
} }
void Population::randomizeWorstChromosomes( void Population::randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
vector<Individual>& _individuals, vector<Individual>& _individuals,
size_t _count size_t _count
) )
@ -158,25 +144,29 @@ void Population::randomizeWorstChromosomes(
auto individual = _individuals.begin() + (_individuals.size() - _count); auto individual = _individuals.begin() + (_individuals.size() - _count);
for (; individual != _individuals.end(); ++individual) for (; individual != _individuals.end(); ++individual)
{ {
*individual = {Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength))}; auto chromosome = Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength));
size_t fitness = _fitnessMetric.evaluate(chromosome);
*individual = {move(chromosome), fitness};
} }
} }
vector<Individual> Population::chromosomesToIndividuals( vector<Individual> Population::chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric,
vector<Chromosome> _chromosomes vector<Chromosome> _chromosomes
) )
{ {
vector<Individual> individuals; vector<Individual> individuals;
for (auto& chromosome: _chromosomes) for (auto& chromosome: _chromosomes)
individuals.push_back({move(chromosome)}); {
size_t fitness = _fitnessMetric.evaluate(chromosome);
individuals.push_back({move(chromosome), fitness});
}
return individuals; return individuals;
} }
vector<Individual> Population::sortedIndividuals(vector<Individual> _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); sort(_individuals.begin(), _individuals.end(), isFitter);
return _individuals; return _individuals;
} }

View File

@ -46,7 +46,7 @@ namespace solidity::phaser
struct Individual struct Individual
{ {
Chromosome chromosome; Chromosome chromosome;
std::optional<size_t> fitness = std::nullopt; size_t fitness;
bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; } bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; }
bool operator!=(Individual const& _other) const { return !(*this == _other); } bool operator!=(Individual const& _other) const { return !(*this == _other); }
@ -67,6 +67,7 @@ bool isFitter(Individual const& a, Individual const& b);
* An individual is a sequence of optimiser steps represented by a @a Chromosome instance. * An individual is a sequence of optimiser steps represented by a @a Chromosome instance.
* Individuals are stored together with a fitness value that can be computed by the fitness metric * Individuals are stored together with a fitness value that can be computed by the fitness metric
* associated with the population. * associated with the population.
* The fitness is computed using the metric as soon as an individual is inserted into the population.
*/ */
class Population class Population
{ {
@ -78,8 +79,8 @@ public:
std::vector<Chromosome> _chromosomes = {} std::vector<Chromosome> _chromosomes = {}
): ):
Population( Population(
std::move(_fitnessMetric), _fitnessMetric,
chromosomesToIndividuals(std::move(_chromosomes)) chromosomesToIndividuals(*_fitnessMetric, std::move(_chromosomes))
) {} ) {}
static Population makeRandom( static Population makeRandom(
@ -114,14 +115,15 @@ private:
m_individuals{std::move(_individuals)} {} m_individuals{std::move(_individuals)} {}
void doMutation(); void doMutation();
void doEvaluation();
void doSelection(); void doSelection();
static void randomizeWorstChromosomes( static void randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
std::vector<Individual>& _individuals, std::vector<Individual>& _individuals,
size_t _count size_t _count
); );
static std::vector<Individual> chromosomesToIndividuals( static std::vector<Individual> chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric,
std::vector<Chromosome> _chromosomes std::vector<Chromosome> _chromosomes
); );
static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals); static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals);