[yul-phaser] Population: Store fitness metric rather than program directly

- In the console app use ProgramSize metric when creating the population.
This commit is contained in:
cameel 2020-02-05 15:58:35 +01:00 committed by Kamil Śliwak
parent 85074e7a8a
commit 66fdc1c374
4 changed files with 45 additions and 73 deletions

View File

@ -58,35 +58,7 @@ namespace
class PopulationFixture
{
protected:
PopulationFixture():
m_sourceStream(SampleSourceCode, ""),
m_program(Program::load(m_sourceStream)) {}
static constexpr char SampleSourceCode[] =
"{\n"
" let factor := 13\n"
" {\n"
" if factor\n"
" {\n"
" let variable := add(1, 2)\n"
" }\n"
" let result := factor\n"
" }\n"
" let something := 6\n"
" {\n"
" {\n"
" {\n"
" let value := 15\n"
" }\n"
" }\n"
" }\n"
" let something_else := mul(mul(something, 1), add(factor, 0))\n"
" if 1 { let x := 1 }\n"
" if 0 { let y := 2 }\n"
"}\n";
CharStream m_sourceStream;
Program m_program;
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
};
BOOST_AUTO_TEST_SUITE(Phaser)
@ -128,7 +100,7 @@ BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitn
Chromosome::makeRandom(5),
Chromosome::makeRandom(10),
};
Population population(m_program, chromosomes);
Population population(m_fitnessMetric, chromosomes);
BOOST_TEST(population.individuals().size() == 2);
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
@ -145,7 +117,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_
assert(chromosomeCount % maxLength == 0);
auto nextLength = [counter = 0, maxLength]() mutable { return counter++ % maxLength; };
auto population = Population::makeRandom(m_program, chromosomeCount, nextLength);
auto population = Population::makeRandom(m_fitnessMetric, chromosomeCount, nextLength);
// We can't rely on the order since the population sorts its chromosomes immediately but
// we can check the number of occurrences of each length.
@ -161,7 +133,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_range, PopulationFixture)
{
auto population = Population::makeRandom(m_program, 100, 5, 10);
auto population = Population::makeRandom(m_fitnessMetric, 100, 5, 10);
BOOST_TEST(all_of(
population.individuals().begin(),
population.individuals().end(),
@ -177,7 +149,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_use_random_chromosome_length, Populati
constexpr int maxLength = 10;
constexpr double relativeTolerance = 0.05;
auto population = Population::makeRandom(m_program, populationSize, minLength, maxLength);
auto population = Population::makeRandom(m_fitnessMetric, populationSize, minLength, maxLength);
vector<size_t> samples = chromosomeLengths(population);
const double expectedValue = (maxLength + minLength) / 2.0;
@ -195,7 +167,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromoso
constexpr double relativeTolerance = 0.01;
map<string, size_t> stepIndices = enumerateOptmisationSteps();
auto population = Population::makeRandom(m_program, populationSize, chromosomeLength, chromosomeLength);
auto population = Population::makeRandom(m_fitnessMetric, populationSize, chromosomeLength, chromosomeLength);
vector<size_t> samples;
for (auto& individual: population.individuals())
@ -211,7 +183,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromoso
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
{
auto population = Population::makeRandom(m_program, 3, 5, 10);
auto population = Population::makeRandom(m_fitnessMetric, 3, 5, 10);
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
}
@ -219,7 +191,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
{
stringstream output;
auto population = Population::makeRandom(m_program, 5, 5, 10);
auto population = Population::makeRandom(m_fitnessMetric, 5, 5, 10);
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
population.run(1, output);
@ -237,11 +209,11 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, Po
Chromosome(vector<string>{UnusedPruner::name}),
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
};
Population population(m_program, chromosomes);
Population population(m_fitnessMetric, chromosomes);
size_t initialTopFitness[2] = {
Population::measureFitness(chromosomes[0], m_program),
Population::measureFitness(chromosomes[1], m_program),
m_fitnessMetric->evaluate(chromosomes[0]),
m_fitnessMetric->evaluate(chromosomes[1]),
};
for (int i = 0; i < 6; ++i)
@ -264,9 +236,9 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, Po
BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixture)
{
BOOST_CHECK_EQUAL(
Population(m_program, {Chromosome("ac"), Chromosome("cx")}) +
Population(m_program, {Chromosome("g"), Chromosome("h"), Chromosome("iI")}),
Population(m_program, {Chromosome("ac"), Chromosome("cx"), Chromosome("g"), Chromosome("h"), Chromosome("iI")})
Population(m_fitnessMetric, {Chromosome("ac"), Chromosome("cx")}) +
Population(m_fitnessMetric, {Chromosome("g"), Chromosome("h"), Chromosome("iI")}),
Population(m_fitnessMetric, {Chromosome("ac"), Chromosome("cx"), Chromosome("g"), Chromosome("h"), Chromosome("iI")})
);
}

View File

@ -17,7 +17,6 @@
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/Program.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/CommonIO.h>
@ -64,7 +63,7 @@ bool phaser::isFitter(Individual const& a, Individual const& b)
}
Population Population::makeRandom(
Program _program,
shared_ptr<FitnessMetric const> _fitnessMetric,
size_t _size,
function<size_t()> _chromosomeLengthGenerator
)
@ -73,30 +72,23 @@ Population Population::makeRandom(
for (size_t i = 0; i < _size; ++i)
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())});
return Population(move(_program), individuals);
return Population(move(_fitnessMetric), move(individuals));
}
Population Population::makeRandom(
Program _program,
shared_ptr<FitnessMetric const> _fitnessMetric,
size_t _size,
size_t _minChromosomeLength,
size_t _maxChromosomeLength
)
{
return makeRandom(
move(_program),
move(_fitnessMetric),
_size,
std::bind(uniformChromosomeLength, _minChromosomeLength, _maxChromosomeLength)
);
}
size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program)
{
Program programCopy = _program;
programCopy.optimise(_chromosome.optimisationSteps());
return programCopy.codeSize();
}
void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
{
doEvaluation();
@ -113,16 +105,19 @@ void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
Population operator+(Population _a, Population _b)
{
assert(toString(_a.m_program) == toString(_b.m_program));
// This operator is meant to be used only with populations sharing the same metric (and, to make
// things simple, "the same" here means the same exact object in memory).
assert(_a.m_fitnessMetric == _b.m_fitnessMetric);
return Population(_a.m_program, move(_a.m_individuals) + move(_b.m_individuals));
return Population(_a.m_fitnessMetric, move(_a.m_individuals) + move(_b.m_individuals));
}
bool Population::operator==(Population const& _other) const
{
// TODO: Comparing programs is pretty heavy but it's just a stopgap. It will soon be replaced
// by a comparison of fitness metric associated with the population (once metrics are introduced).
return m_individuals == _other.m_individuals && toString(m_program) == toString(_other.m_program);
// We consider populations identical only if they share the same exact instance of the metric.
// It might be possible to define some notion of equality for metric objects but it would
// be an overkill since mixing populations using different metrics is not a common use case.
return m_individuals == _other.m_individuals && m_fitnessMetric == _other.m_fitnessMetric;
}
ostream& phaser::operator<<(ostream& _stream, Population const& _population)
@ -143,7 +138,7 @@ void Population::doEvaluation()
{
for (auto& individual: m_individuals)
if (!individual.fitness.has_value())
individual.fitness = measureFitness(individual.chromosome, m_program);
individual.fitness = m_fitnessMetric->evaluate(individual.chromosome);
}
void Population::doSelection()

View File

@ -18,7 +18,7 @@
#pragma once
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/Program.h>
#include <tools/yulPhaser/FitnessMetrics.h>
#include <tools/yulPhaser/SimulationRNG.h>
#include <optional>
@ -64,28 +64,31 @@ bool isFitter(Individual const& a, Individual const& b);
* Each round of the algorithm involves mutating existing individuals, evaluating their fitness
* and selecting the best ones for the next round.
*
* An individual is a sequence of optimiser steps represented by a @a Chromosome instance. The whole
* population is associated with a fixed Yul program. By applying the steps to the @a Program
* instance the class can compute fitness of the individual.
* 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
* associated with the population.
*/
class Population
{
public:
static constexpr size_t MaxChromosomeLength = 30;
explicit Population(Program _program, std::vector<Chromosome> _chromosomes = {}):
explicit Population(
std::shared_ptr<FitnessMetric const> _fitnessMetric,
std::vector<Chromosome> _chromosomes = {}
):
Population(
std::move(_program),
std::move(_fitnessMetric),
chromosomesToIndividuals(std::move(_chromosomes))
) {}
static Population makeRandom(
Program _program,
std::shared_ptr<FitnessMetric const> _fitnessMetric,
size_t _size,
std::function<size_t()> _chromosomeLengthGenerator
);
static Population makeRandom(
Program _program,
std::shared_ptr<FitnessMetric const> _fitnessMetric,
size_t _size,
size_t _minChromosomeLength,
size_t _maxChromosomeLength
@ -94,11 +97,11 @@ public:
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
friend Population (::operator+)(Population _a, Population _b);
std::shared_ptr<FitnessMetric const> fitnessMetric() const { return m_fitnessMetric; }
std::vector<Individual> const& individuals() const { return m_individuals; }
static size_t uniformChromosomeLength(size_t _min, size_t _max) { return SimulationRNG::uniformInt(_min, _max); }
static size_t binomialChromosomeLength(size_t _max) { return SimulationRNG::binomialInt(_max, 0.5); }
static size_t measureFitness(Chromosome const& _chromosome, Program const& _program);
bool operator==(Population const& _other) const;
bool operator!=(Population const& _other) const { return !(*this == _other); }
@ -106,8 +109,8 @@ public:
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
private:
explicit Population(Program _program, std::vector<Individual> _individuals):
m_program{std::move(_program)},
explicit Population(std::shared_ptr<FitnessMetric const> _fitnessMetric, std::vector<Individual> _individuals):
m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{std::move(_individuals)} {}
void doMutation();
@ -123,7 +126,7 @@ private:
);
static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals);
Program m_program;
std::shared_ptr<FitnessMetric const> m_fitnessMetric;
std::vector<Individual> m_individuals;
};

View File

@ -17,6 +17,7 @@
#include <tools/yulPhaser/Exceptions.h>
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/FitnessMetrics.h>
#include <tools/yulPhaser/Program.h>
#include <tools/yulPhaser/SimulationRNG.h>
@ -71,8 +72,9 @@ CharStream loadSource(string const& _sourcePath)
void runAlgorithm(string const& _sourcePath)
{
CharStream sourceCode = loadSource(_sourcePath);
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ProgramSize>(Program::load(sourceCode));
auto population = Population::makeRandom(
Program::load(sourceCode),
fitnessMetric,
10,
bind(Population::binomialChromosomeLength, Population::MaxChromosomeLength)
);