Merge pull request #8326 from imapp-pl/yul-phaser-fitness-metrics

[yul-phaser] Fitness metrics
This commit is contained in:
chriseth 2020-02-24 13:34:58 +01:00 committed by GitHub
commit aa6a2b4706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 359 additions and 163 deletions

View File

@ -143,6 +143,7 @@ set(yul_phaser_sources
yulPhaser/Common.cpp
yulPhaser/CommonTest.cpp
yulPhaser/Chromosome.cpp
yulPhaser/FitnessMetrics.cpp
yulPhaser/Population.cpp
yulPhaser/Program.cpp
yulPhaser/SimulationRNG.cpp
@ -151,6 +152,7 @@ set(yul_phaser_sources
# My current workaround is just to include its source files here but this introduces
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
../tools/yulPhaser/Chromosome.cpp
../tools/yulPhaser/FitnessMetrics.cpp
../tools/yulPhaser/Population.cpp
../tools/yulPhaser/Program.cpp
../tools/yulPhaser/SimulationRNG.cpp

View File

@ -28,6 +28,8 @@
#pragma once
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/FitnessMetrics.h>
#include <tools/yulPhaser/Population.h>
#include <cassert>
@ -38,6 +40,18 @@
namespace solidity::phaser::test
{
/**
* Fitness metric that only takes into account the number of optimisation steps in the chromosome.
* Recommended for use in tests because it's much faster than ProgramSize metric and it's very
* easy to guess the result at a glance.
*/
class ChromosomeLengthMetric: public FitnessMetric
{
public:
using FitnessMetric::FitnessMetric;
size_t evaluate(Chromosome const& _chromosome) const override { return _chromosome.length(); }
};
// CHROMOSOME AND POPULATION HELPERS
/// Returns a vector containing lengths of all chromosomes in the population (in the same order).

View File

@ -19,14 +19,11 @@
#include <libyul/optimiser/Suite.h>
#include <liblangutil/CharStream.h>
#include <boost/test/unit_test.hpp>
#include <set>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace boost::test_tools;
@ -36,15 +33,21 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(CommonTest)
BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_length)
{
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome()) == 0);
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("a")) == 1);
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("aaaaa")) == 5);
}
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
{
CharStream sourceStream("{}", "");
auto program = Program::load(sourceStream);
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ChromosomeLengthMetric>();
Population population1(program, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
Population population1(fitnessMetric, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
Population population2(program);
Population population2(fitnessMetric);
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
}

View File

@ -0,0 +1,113 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tools/yulPhaser/FitnessMetrics.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <liblangutil/CharStream.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::yul;
namespace solidity::phaser::test
{
class FitnessMetricFixture
{
protected:
FitnessMetricFixture():
m_sourceStream(SampleSourceCode, ""),
m_program(Program::load(m_sourceStream)) {}
static constexpr char SampleSourceCode[] =
"{\n"
" function foo() -> result\n"
" {\n"
" let x := 1\n"
" result := 15\n"
" }\n"
" function bar() -> result\n"
" {\n"
" result := 15\n"
" }\n"
" mstore(foo(), bar())\n"
"}\n";
CharStream m_sourceStream;
Program m_program;
};
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(FitnessMetricsTest)
BOOST_AUTO_TEST_SUITE(ProgramSizeTest)
BOOST_FIXTURE_TEST_CASE(evaluate_should_compute_size_of_the_optimised_program, FitnessMetricFixture)
{
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
Program optimisedProgram = m_program;
optimisedProgram.optimise(chromosome.optimisationSteps());
assert(m_program.codeSize() != optimisedProgram.codeSize());
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) != m_program.codeSize());
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) == optimisedProgram.codeSize());
}
BOOST_FIXTURE_TEST_CASE(evaluate_should_repeat_the_optimisation_specified_number_of_times, FitnessMetricFixture)
{
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
Program programOptimisedOnce = m_program;
programOptimisedOnce.optimise(chromosome.optimisationSteps());
Program programOptimisedTwice = programOptimisedOnce;
programOptimisedTwice.optimise(chromosome.optimisationSteps());
assert(m_program.codeSize() != programOptimisedOnce.codeSize());
assert(m_program.codeSize() != programOptimisedTwice.codeSize());
assert(programOptimisedOnce.codeSize() != programOptimisedTwice.codeSize());
ProgramSize metric(m_program, 2);
BOOST_TEST(metric.evaluate(chromosome) != m_program.codeSize());
BOOST_TEST(metric.evaluate(chromosome) != programOptimisedOnce.codeSize());
BOOST_TEST(metric.evaluate(chromosome) == programOptimisedTwice.codeSize());
}
BOOST_FIXTURE_TEST_CASE(evaluate_should_not_optimise_if_number_of_repetitions_is_zero, FitnessMetricFixture)
{
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
Program optimisedProgram = m_program;
optimisedProgram.optimise(chromosome.optimisationSteps());
assert(m_program.codeSize() != optimisedProgram.codeSize());
ProgramSize metric(m_program, 0);
BOOST_TEST(metric.evaluate(chromosome) == m_program.codeSize());
BOOST_TEST(metric.evaluate(chromosome) != optimisedProgram.codeSize());
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -42,51 +42,10 @@ using namespace boost::unit_test::framework;
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
{
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)
@ -94,48 +53,52 @@ BOOST_AUTO_TEST_SUITE(PopulationTest)
BOOST_AUTO_TEST_CASE(isFitter_should_use_fitness_as_the_main_criterion)
{
BOOST_TEST(isFitter(Individual{Chromosome("a"), 5}, Individual{Chromosome("a"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 10}, Individual{Chromosome("a"), 5}));
BOOST_TEST(isFitter(Individual(Chromosome("a"), 5), Individual(Chromosome("a"), 10)));
BOOST_TEST(!isFitter(Individual(Chromosome("a"), 10), Individual(Chromosome("a"), 5)));
BOOST_TEST(isFitter(Individual{Chromosome("aaa"), 5}, Individual{Chromosome("aaaaa"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("aaaaa"), 10}, Individual{Chromosome("aaa"), 5}));
BOOST_TEST(isFitter(Individual(Chromosome("aaa"), 5), Individual(Chromosome("aaaaa"), 10)));
BOOST_TEST(!isFitter(Individual(Chromosome("aaaaa"), 10), Individual(Chromosome("aaa"), 5)));
BOOST_TEST(isFitter(Individual{Chromosome("aaaaa"), 5}, Individual{Chromosome("aaa"), 10}));
BOOST_TEST(!isFitter(Individual{Chromosome("aaa"), 10}, Individual{Chromosome("aaaaa"), 5}));
BOOST_TEST(isFitter(Individual(Chromosome("aaaaa"), 5), Individual(Chromosome("aaa"), 10)));
BOOST_TEST(!isFitter(Individual(Chromosome("aaa"), 10), Individual(Chromosome("aaaaa"), 5)));
}
BOOST_AUTO_TEST_CASE(isFitter_should_use_alphabetical_order_when_fitness_is_the_same)
{
BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("c"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("c"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("c"), 3)));
BOOST_TEST(!isFitter(Individual(Chromosome("c"), 3), Individual(Chromosome("a"), 3)));
BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("aa"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("aa"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("aa"), 3)));
BOOST_TEST(!isFitter(Individual(Chromosome("aa"), 3), Individual(Chromosome("a"), 3)));
BOOST_TEST(isFitter(Individual{Chromosome("T"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("T"), 3}));
BOOST_TEST(isFitter(Individual(Chromosome("T"), 3), Individual(Chromosome("a"), 3)));
BOOST_TEST(!isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("T"), 3)));
}
BOOST_AUTO_TEST_CASE(isFitter_should_return_false_for_identical_individuals)
{
BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("a"), 3}));
BOOST_TEST(!isFitter(Individual{Chromosome("acT"), 0}, Individual{Chromosome("acT"), 0}));
BOOST_TEST(!isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("a"), 3)));
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_compute_fitness_and_sort_chromosomes, PopulationFixture)
{
vector<Chromosome> chromosomes = {
Chromosome::makeRandom(5),
Chromosome::makeRandom(15),
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]);
BOOST_TEST(population.individuals()[1].chromosome == chromosomes[1]);
vector<Individual> const& individuals = population.individuals();
auto fitnessNotSet = [](auto const& individual){ return !individual.fitness.has_value(); };
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
BOOST_TEST(individuals.size() == 3);
BOOST_TEST(individuals[0].fitness == 5);
BOOST_TEST(individuals[1].fitness == 10);
BOOST_TEST(individuals[2].fitness == 15);
BOOST_TEST(individuals[0].chromosome == chromosomes[0]);
BOOST_TEST(individuals[1].chromosome == chromosomes[2]);
BOOST_TEST(individuals[2].chromosome == chromosomes[1]);
}
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
@ -145,7 +108,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 +124,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 +140,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 +158,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())
@ -209,22 +172,13 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromoso
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_program, 3, 5, 10);
auto population = Population::makeRandom(m_fitnessMetric, 3, 5, 10);
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
}
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
{
stringstream output;
auto population = Population::makeRandom(m_program, 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_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_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
@ -237,23 +191,21 @@ 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)
{
population.run(1, output);
BOOST_TEST(population.individuals().size() == 5);
BOOST_TEST(fitnessSet(population.individuals()[0]));
BOOST_TEST(fitnessSet(population.individuals()[1]));
size_t currentTopFitness[2] = {
population.individuals()[0].fitness.value(),
population.individuals()[1].fitness.value(),
population.individuals()[0].fitness,
population.individuals()[1].fitness,
};
BOOST_TEST(currentTopFitness[0] <= initialTopFitness[0]);
BOOST_TEST(currentTopFitness[1] <= initialTopFitness[1]);
@ -264,9 +216,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,6 +17,8 @@ add_executable(yul-phaser
yulPhaser/main.cpp
yulPhaser/Population.h
yulPhaser/Population.cpp
yulPhaser/FitnessMetrics.h
yulPhaser/FitnessMetrics.cpp
yulPhaser/Chromosome.h
yulPhaser/Chromosome.cpp
yulPhaser/Program.h

View File

@ -0,0 +1,30 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tools/yulPhaser/FitnessMetrics.h>
using namespace std;
using namespace solidity::phaser;
size_t ProgramSize::evaluate(Chromosome const& _chromosome) const
{
Program programCopy = m_program;
for (size_t i = 0; i < m_repetitionCount; ++i)
programCopy.optimise(_chromosome.optimisationSteps());
return programCopy.codeSize();
}

View File

@ -0,0 +1,67 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Contains an abstract base class representing a fitness metric and its concrete implementations.
*/
#pragma once
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/Program.h>
#include <cstddef>
namespace solidity::phaser
{
/**
* Abstract base class for fitness metrics.
*
* The main feature is the @a evaluate() method that can tell how good a given chromosome is.
* The lower the value, the better the fitness is. The result should be deterministic and depend
* only on the chromosome and metric's state (which is constant).
*/
class FitnessMetric
{
public:
FitnessMetric() = default;
FitnessMetric(FitnessMetric const&) = delete;
FitnessMetric& operator=(FitnessMetric const&) = delete;
virtual ~FitnessMetric() = default;
virtual size_t evaluate(Chromosome const& _chromosome) const = 0;
};
/**
* Fitness metric based on the size of a specific program after applying the optimisations from the
* chromosome to it.
*/
class ProgramSize: public FitnessMetric
{
public:
explicit ProgramSize(Program _program, size_t _repetitionCount = 1):
m_program(std::move(_program)),
m_repetitionCount(_repetitionCount) {}
size_t evaluate(Chromosome const& _chromosome) const override;
private:
Program m_program;
size_t m_repetitionCount;
};
}

View File

@ -17,7 +17,6 @@
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/Program.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/CommonIO.h>
@ -42,11 +41,7 @@ ostream& operator<<(ostream& _stream, Population const& _population);
ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
{
_stream << "Fitness: ";
if (_individual.fitness.has_value())
_stream << _individual.fitness.value();
else
_stream << "<NONE>";
_stream << "Fitness: " << _individual.fitness;
_stream << ", optimisations: " << _individual.chromosome;
return _stream;
@ -54,64 +49,46 @@ ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
bool phaser::isFitter(Individual const& a, Individual const& b)
{
assert(a.fitness.has_value() && b.fitness.has_value());
return (
(a.fitness.value() < b.fitness.value()) ||
(a.fitness.value() == b.fitness.value() && 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.fitness == b.fitness && a.chromosome.length() < b.chromosome.length()) ||
(a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && toString(a.chromosome) < toString(b.chromosome))
);
}
Population::Population(Program _program, vector<Chromosome> const& _chromosomes):
m_program{move(_program)}
{
for (auto const& chromosome: _chromosomes)
m_individuals.push_back({chromosome});
}
Population Population::makeRandom(
Program _program,
shared_ptr<FitnessMetric const> _fitnessMetric,
size_t _size,
function<size_t()> _chromosomeLengthGenerator
)
{
vector<Individual> individuals;
vector<Chromosome> chromosomes;
for (size_t i = 0; i < _size; ++i)
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())});
chromosomes.push_back(Chromosome::makeRandom(_chromosomeLengthGenerator()));
return Population(move(_program), individuals);
return Population(move(_fitnessMetric), move(chromosomes));
}
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();
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
{
doMutation();
doSelection();
doEvaluation();
_outputStream << "---------- ROUND " << round << " ----------" << endl;
_outputStream << *this;
@ -120,16 +97,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)
@ -146,22 +126,14 @@ void Population::doMutation()
// TODO: Implement mutation and crossover
}
void Population::doEvaluation()
{
for (auto& individual: m_individuals)
if (!individual.fitness.has_value())
individual.fitness = measureFitness(individual.chromosome, m_program);
}
void Population::doSelection()
{
assert(all_of(m_individuals.begin(), m_individuals.end(), [](auto& i){ return i.fitness.has_value(); }));
sort(m_individuals.begin(), m_individuals.end(), isFitter);
randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2);
randomizeWorstChromosomes(*m_fitnessMetric, m_individuals, m_individuals.size() / 2);
m_individuals = sortedIndividuals(move(m_individuals));
}
void Population::randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
vector<Individual>& _individuals,
size_t _count
)
@ -172,6 +144,26 @@ void Population::randomizeWorstChromosomes(
auto individual = _individuals.begin() + (_individuals.size() - _count);
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(
FitnessMetric const& _fitnessMetric,
vector<Chromosome> _chromosomes
)
{
vector<Individual> individuals;
for (auto& chromosome: _chromosomes)
individuals.emplace_back(move(chromosome), _fitnessMetric);
return individuals;
}
vector<Individual> Population::sortedIndividuals(vector<Individual> _individuals)
{
sort(_individuals.begin(), _individuals.end(), isFitter);
return _individuals;
}

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>
@ -46,7 +46,14 @@ namespace solidity::phaser
struct Individual
{
Chromosome chromosome;
std::optional<size_t> fitness = std::nullopt;
size_t fitness;
Individual(Chromosome _chromosome, size_t _fitness):
chromosome(std::move(_chromosome)),
fitness(_fitness) {}
Individual(Chromosome _chromosome, FitnessMetric const& _fitnessMetric):
chromosome(std::move(_chromosome)),
fitness(_fitnessMetric.evaluate(chromosome)) {}
bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; }
bool operator!=(Individual const& _other) const { return !(*this == _other); }
@ -64,24 +71,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 always ordered by their fitness (based on @_fitnessMetric and @a isFitter()).
* The fitness is computed using the metric as soon as an individual is inserted into the population.
*/
class Population
{
public:
static constexpr size_t MaxChromosomeLength = 30;
explicit Population(Program _program, std::vector<Chromosome> const& _chromosomes = {});
explicit Population(
std::shared_ptr<FitnessMetric const> _fitnessMetric,
std::vector<Chromosome> _chromosomes = {}
):
Population(
_fitnessMetric,
chromosomesToIndividuals(*_fitnessMetric, 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
@ -90,11 +104,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); }
@ -102,20 +116,25 @@ 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)},
m_individuals{std::move(_individuals)} {}
explicit Population(std::shared_ptr<FitnessMetric const> _fitnessMetric, std::vector<Individual> _individuals):
m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{sortedIndividuals(std::move(_individuals))} {}
void doMutation();
void doEvaluation();
void doSelection();
static void randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
std::vector<Individual>& _individuals,
size_t _count
);
static std::vector<Individual> chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric,
std::vector<Chromosome> _chromosomes
);
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), 5);
auto population = Population::makeRandom(
Program::load(sourceCode),
fitnessMetric,
10,
bind(Population::binomialChromosomeLength, Population::MaxChromosomeLength)
);