mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8325 from imapp-pl/yul-phaser-population-tweaks
[yul-phaser] Miscellaneous Population tweaks and improvements
This commit is contained in:
commit
357c936243
@ -21,8 +21,18 @@
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <libyul/optimiser/BlockFlattener.h>
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/ConditionalSimplifier.h>
|
||||
#include <libyul/optimiser/ExpressionInliner.h>
|
||||
#include <libyul/optimiser/ExpressionSimplifier.h>
|
||||
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
||||
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
||||
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
||||
#include <libyul/optimiser/FunctionHoister.h>
|
||||
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
||||
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/UnusedPruner.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
@ -39,25 +49,49 @@ namespace solidity::phaser::test
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(makeRandom_should_create_chromosome_with_random_optimisation_steps)
|
||||
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
|
||||
{
|
||||
constexpr uint32_t numSteps = 1000;
|
||||
vector<string> expectedSteps{
|
||||
ConditionalSimplifier::name,
|
||||
FunctionHoister::name,
|
||||
RedundantAssignEliminator::name,
|
||||
ForLoopConditionOutOfBody::name,
|
||||
Rematerialiser::name,
|
||||
ForLoopConditionOutOfBody::name,
|
||||
ExpressionSimplifier::name,
|
||||
ForLoopInitRewriter::name,
|
||||
LoopInvariantCodeMotion::name,
|
||||
ExpressionInliner::name
|
||||
};
|
||||
|
||||
auto chromosome1 = Chromosome::makeRandom(numSteps);
|
||||
auto chromosome2 = Chromosome::makeRandom(numSteps);
|
||||
BOOST_CHECK_EQUAL(chromosome1.length(), numSteps);
|
||||
BOOST_CHECK_EQUAL(chromosome2.length(), numSteps);
|
||||
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps);
|
||||
}
|
||||
|
||||
multiset<string> steps1;
|
||||
multiset<string> steps2;
|
||||
for (auto const& step: chromosome1.optimisationSteps())
|
||||
steps1.insert(step);
|
||||
for (auto const& step: chromosome2.optimisationSteps())
|
||||
steps2.insert(step);
|
||||
BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
BOOST_TEST(Chromosome::makeRandom(100) != Chromosome::makeRandom(100));
|
||||
}
|
||||
|
||||
// Check if steps are different and also if they're not just a permutation of the same set.
|
||||
// Technically they could be the same and still random but the probability is infinitesimally low.
|
||||
BOOST_TEST(steps1 != steps2);
|
||||
BOOST_AUTO_TEST_CASE(makeRandom_should_use_every_possible_step_with_the_same_probability)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
constexpr int samplesPerStep = 100;
|
||||
constexpr double relativeTolerance = 0.01;
|
||||
|
||||
map<string, size_t> stepIndices = enumerateOptmisationSteps();
|
||||
auto chromosome = Chromosome::makeRandom(stepIndices.size() * samplesPerStep);
|
||||
|
||||
vector<size_t> samples;
|
||||
for (auto& step: chromosome.optimisationSteps())
|
||||
samples.push_back(stepIndices.at(step));
|
||||
|
||||
const double expectedValue = (stepIndices.size() - 1) / 2.0;
|
||||
const double variance = (stepIndices.size() * stepIndices.size() - 1) / 12.0;
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
|
||||
|
@ -19,10 +19,21 @@
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
#include <regex>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
|
||||
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
|
||||
{
|
||||
vector<size_t> lengths;
|
||||
for (auto const& individual: _population.individuals())
|
||||
lengths.push_back(individual.chromosome.length());
|
||||
|
||||
return lengths;
|
||||
}
|
||||
|
||||
map<string, size_t> phaser::test::enumerateOptmisationSteps()
|
||||
{
|
||||
map<string, size_t> stepIndices;
|
||||
@ -32,3 +43,9 @@ map<string, size_t> phaser::test::enumerateOptmisationSteps()
|
||||
|
||||
return stepIndices;
|
||||
}
|
||||
|
||||
string phaser::test::stripWhitespace(string const& input)
|
||||
{
|
||||
regex whitespaceRegex("\\s+");
|
||||
return regex_replace(input, whitespaceRegex, "");
|
||||
}
|
||||
|
@ -28,6 +28,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <string>
|
||||
@ -37,11 +39,20 @@ namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
// CHROMOSOME AND POPULATION HELPERS
|
||||
|
||||
/// Returns a vector containing lengths of all chromosomes in the population (in the same order).
|
||||
std::vector<size_t> chromosomeLengths(Population const& _population);
|
||||
|
||||
/// Assigns indices from 0 to N to all optimisation steps available in the OptimiserSuite.
|
||||
/// This is a convenience helper to make it easier to test their distribution with tools made for
|
||||
/// integers.
|
||||
std::map<std::string, size_t> enumerateOptmisationSteps();
|
||||
|
||||
// STRING UTILITIES
|
||||
|
||||
/// Returns the input string with all the whitespace characters (spaces, line endings, etc.) removed.
|
||||
std::string stripWhitespace(std::string const& input);
|
||||
|
||||
// STATISTICAL UTILITIES
|
||||
|
||||
/// Calculates the mean value of a series of samples given in a vector.
|
||||
|
@ -19,11 +19,14 @@
|
||||
|
||||
#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;
|
||||
|
||||
@ -33,6 +36,18 @@ namespace solidity::phaser::test
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
|
||||
{
|
||||
CharStream sourceStream("{}", "");
|
||||
auto program = Program::load(sourceStream);
|
||||
|
||||
Population population1(program, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
|
||||
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
|
||||
|
||||
Population population2(program);
|
||||
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_available_optimisation_steps)
|
||||
{
|
||||
map<string, char> stepsAndAbbreviations = OptimiserSuite::stepNameToAbbreviationMap();
|
||||
@ -51,6 +66,20 @@ BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_ava
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(stripWhitespace_should_remove_all_whitespace_characters_from_a_string)
|
||||
{
|
||||
BOOST_TEST(stripWhitespace("") == "");
|
||||
BOOST_TEST(stripWhitespace(" ") == "");
|
||||
BOOST_TEST(stripWhitespace(" \n\t\v ") == "");
|
||||
|
||||
BOOST_TEST(stripWhitespace("abc") == "abc");
|
||||
BOOST_TEST(stripWhitespace(" abc") == "abc");
|
||||
BOOST_TEST(stripWhitespace("abc ") == "abc");
|
||||
BOOST_TEST(stripWhitespace(" a b c ") == "abc");
|
||||
BOOST_TEST(stripWhitespace(" a b\tc\n") == "abc");
|
||||
BOOST_TEST(stripWhitespace(" a b \n\n c \n\t\v") == "abc");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(mean_should_calculate_statistical_mean)
|
||||
{
|
||||
BOOST_TEST(mean<int>({0}) == 0.0);
|
||||
|
@ -15,6 +15,8 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
@ -53,10 +55,14 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(PopulationTest)
|
||||
class PopulationFixture
|
||||
{
|
||||
protected:
|
||||
PopulationFixture():
|
||||
m_sourceStream(SampleSourceCode, ""),
|
||||
m_program(Program::load(m_sourceStream)) {}
|
||||
|
||||
string const& sampleSourceCode =
|
||||
static constexpr char SampleSourceCode[] =
|
||||
"{\n"
|
||||
" let factor := 13\n"
|
||||
" {\n"
|
||||
@ -79,14 +85,50 @@ string const& sampleSourceCode =
|
||||
" if 0 { let y := 2 }\n"
|
||||
"}\n";
|
||||
|
||||
BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness)
|
||||
CharStream m_sourceStream;
|
||||
Program m_program;
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
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("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_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("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_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_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness, PopulationFixture)
|
||||
{
|
||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
||||
vector<Chromosome> chromosomes = {
|
||||
Chromosome::makeRandom(5),
|
||||
Chromosome::makeRandom(10),
|
||||
};
|
||||
Population population(Program::load(sourceStream), chromosomes);
|
||||
Population population(m_program, chromosomes);
|
||||
|
||||
BOOST_TEST(population.individuals().size() == 2);
|
||||
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
|
||||
@ -96,40 +138,88 @@ BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness
|
||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes)
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
|
||||
{
|
||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
||||
auto program = Program::load(sourceStream);
|
||||
auto population1 = Population::makeRandom(program, 100);
|
||||
auto population2 = Population::makeRandom(program, 100);
|
||||
size_t chromosomeCount = 30;
|
||||
size_t maxLength = 5;
|
||||
assert(chromosomeCount % maxLength == 0);
|
||||
|
||||
BOOST_TEST(population1.individuals().size() == 100);
|
||||
BOOST_TEST(population2.individuals().size() == 100);
|
||||
auto nextLength = [counter = 0, maxLength]() mutable { return counter++ % maxLength; };
|
||||
auto population = Population::makeRandom(m_program, chromosomeCount, nextLength);
|
||||
|
||||
int numMatchingPositions = 0;
|
||||
for (size_t i = 0; i < 100; ++i)
|
||||
if (population1.individuals()[i].chromosome == population2.individuals()[i].chromosome)
|
||||
++numMatchingPositions;
|
||||
|
||||
// Assume that the results are random if there are no more than 10 identical chromosomes on the
|
||||
// same positions. One duplicate is very unlikely but still possible after billions of runs
|
||||
// (especially for short chromosomes). For ten the probability is so small that we can ignore it.
|
||||
BOOST_TEST(numMatchingPositions < 10);
|
||||
// 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.
|
||||
for (size_t length = 0; length < maxLength; ++length)
|
||||
BOOST_TEST(
|
||||
count_if(
|
||||
population.individuals().begin(),
|
||||
population.individuals().end(),
|
||||
[&length](auto const& individual) { return individual.chromosome.length() == length; }
|
||||
) == chromosomeCount / maxLength
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(makeRandom_should_not_compute_fitness)
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_range, PopulationFixture)
|
||||
{
|
||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
||||
auto population = Population::makeRandom(Program::load(sourceStream), 5);
|
||||
auto population = Population::makeRandom(m_program, 100, 5, 10);
|
||||
BOOST_TEST(all_of(
|
||||
population.individuals().begin(),
|
||||
population.individuals().end(),
|
||||
[](auto const& individual){ return 5 <= individual.chromosome.length() && individual.chromosome.length() <= 10; }
|
||||
));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_use_random_chromosome_length, PopulationFixture)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
constexpr int populationSize = 200;
|
||||
constexpr int minLength = 5;
|
||||
constexpr int maxLength = 10;
|
||||
constexpr double relativeTolerance = 0.05;
|
||||
|
||||
auto population = Population::makeRandom(m_program, populationSize, minLength, maxLength);
|
||||
vector<size_t> samples = chromosomeLengths(population);
|
||||
|
||||
const double expectedValue = (maxLength + minLength) / 2.0;
|
||||
const double variance = ((maxLength - minLength + 1) * (maxLength - minLength + 1) - 1) / 12.0;
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes, PopulationFixture)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
constexpr int populationSize = 100;
|
||||
constexpr int chromosomeLength = 30;
|
||||
constexpr double relativeTolerance = 0.01;
|
||||
|
||||
map<string, size_t> stepIndices = enumerateOptmisationSteps();
|
||||
auto population = Population::makeRandom(m_program, populationSize, chromosomeLength, chromosomeLength);
|
||||
|
||||
vector<size_t> samples;
|
||||
for (auto& individual: population.individuals())
|
||||
for (auto& step: individual.chromosome.optimisationSteps())
|
||||
samples.push_back(stepIndices.at(step));
|
||||
|
||||
const double expectedValue = (stepIndices.size() - 1) / 2.0;
|
||||
const double variance = (stepIndices.size() * stepIndices.size() - 1) / 12.0;
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_program, 3, 5, 10);
|
||||
|
||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
|
||||
{
|
||||
stringstream output;
|
||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
||||
auto population = Population::makeRandom(Program::load(sourceStream), 5);
|
||||
auto population = Population::makeRandom(m_program, 5, 5, 10);
|
||||
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
|
||||
population.run(1, output);
|
||||
@ -137,23 +227,21 @@ BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
|
||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
|
||||
{
|
||||
stringstream output;
|
||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
||||
vector<Chromosome> chromosomes = {
|
||||
Chromosome({StructuralSimplifier::name}),
|
||||
Chromosome({BlockFlattener::name}),
|
||||
Chromosome({SSAReverser::name}),
|
||||
Chromosome({UnusedPruner::name}),
|
||||
Chromosome({StructuralSimplifier::name, BlockFlattener::name}),
|
||||
Chromosome(vector<string>{StructuralSimplifier::name}),
|
||||
Chromosome(vector<string>{BlockFlattener::name}),
|
||||
Chromosome(vector<string>{SSAReverser::name}),
|
||||
Chromosome(vector<string>{UnusedPruner::name}),
|
||||
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
|
||||
};
|
||||
auto program = Program::load(sourceStream);
|
||||
Population population(program, chromosomes);
|
||||
Population population(m_program, chromosomes);
|
||||
|
||||
size_t initialTopFitness[2] = {
|
||||
Population::measureFitness(chromosomes[0], program),
|
||||
Population::measureFitness(chromosomes[1], program),
|
||||
Population::measureFitness(chromosomes[0], m_program),
|
||||
Population::measureFitness(chromosomes[1], m_program),
|
||||
};
|
||||
|
||||
for (int i = 0; i < 6; ++i)
|
||||
@ -173,6 +261,15 @@ BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
|
||||
}
|
||||
}
|
||||
|
||||
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")})
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
@ -15,6 +15,8 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
||||
@ -30,7 +32,6 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
@ -51,12 +52,6 @@ namespace
|
||||
else
|
||||
return _block;
|
||||
}
|
||||
|
||||
string stripWhitespace(string const& input)
|
||||
{
|
||||
regex whitespaceRegex("\\s+");
|
||||
return regex_replace(input, whitespaceRegex, "");
|
||||
}
|
||||
}
|
||||
|
||||
namespace solidity::phaser::test
|
||||
|
@ -36,6 +36,12 @@ ostream& operator<<(ostream& _stream, Chromosome const& _chromosome);
|
||||
|
||||
}
|
||||
|
||||
Chromosome::Chromosome(string const& _optimisationSteps)
|
||||
{
|
||||
for (char abbreviation: _optimisationSteps)
|
||||
m_optimisationSteps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation));
|
||||
}
|
||||
|
||||
Chromosome Chromosome::makeRandom(size_t _length)
|
||||
{
|
||||
vector<string> steps;
|
||||
|
@ -42,6 +42,7 @@ public:
|
||||
Chromosome() = default;
|
||||
explicit Chromosome(std::vector<std::string> _optimisationSteps):
|
||||
m_optimisationSteps(std::move(_optimisationSteps)) {}
|
||||
explicit Chromosome(std::string const& _optimisationSteps);
|
||||
static Chromosome makeRandom(size_t _length);
|
||||
|
||||
size_t length() const { return m_optimisationSteps.size(); }
|
||||
|
@ -19,14 +19,17 @@
|
||||
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
namespace solidity::phaser
|
||||
@ -49,6 +52,17 @@ ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
|
||||
return _stream;
|
||||
}
|
||||
|
||||
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))
|
||||
);
|
||||
}
|
||||
|
||||
Population::Population(Program _program, vector<Chromosome> const& _chromosomes):
|
||||
m_program{move(_program)}
|
||||
{
|
||||
@ -56,15 +70,33 @@ Population::Population(Program _program, vector<Chromosome> const& _chromosomes)
|
||||
m_individuals.push_back({chromosome});
|
||||
}
|
||||
|
||||
Population Population::makeRandom(Program _program, size_t _size)
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
function<size_t()> _chromosomeLengthGenerator
|
||||
)
|
||||
{
|
||||
vector<Individual> individuals;
|
||||
for (size_t i = 0; i < _size; ++i)
|
||||
individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())});
|
||||
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())});
|
||||
|
||||
return Population(move(_program), individuals);
|
||||
}
|
||||
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
)
|
||||
{
|
||||
return makeRandom(
|
||||
move(_program),
|
||||
_size,
|
||||
std::bind(uniformChromosomeLength, _minChromosomeLength, _maxChromosomeLength)
|
||||
);
|
||||
}
|
||||
|
||||
size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program)
|
||||
{
|
||||
Program programCopy = _program;
|
||||
@ -86,6 +118,20 @@ void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
|
||||
}
|
||||
}
|
||||
|
||||
Population operator+(Population _a, Population _b)
|
||||
{
|
||||
assert(toString(_a.m_program) == toString(_b.m_program));
|
||||
|
||||
return Population(_a.m_program, 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);
|
||||
}
|
||||
|
||||
ostream& phaser::operator<<(ostream& _stream, Population const& _population)
|
||||
{
|
||||
auto individual = _population.m_individuals.begin();
|
||||
@ -111,12 +157,7 @@ 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(),
|
||||
[](auto const& a, auto const& b){ return a.fitness.value() < b.fitness.value(); }
|
||||
);
|
||||
|
||||
sort(m_individuals.begin(), m_individuals.end(), isFitter);
|
||||
randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2);
|
||||
}
|
||||
|
||||
@ -131,6 +172,6 @@ void Population::randomizeWorstChromosomes(
|
||||
auto individual = _individuals.begin() + (_individuals.size() - _count);
|
||||
for (; individual != _individuals.end(); ++individual)
|
||||
{
|
||||
*individual = {Chromosome::makeRandom(randomChromosomeLength())};
|
||||
*individual = {Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength))};
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,17 @@
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
class Population;
|
||||
|
||||
}
|
||||
|
||||
// This operator+ must be declared in the global namespace. Otherwise it would shadow global
|
||||
// operator+ overloads from CommonData.h (e.g. the one for vector) in the namespace it was declared in.
|
||||
solidity::phaser::Population operator+(solidity::phaser::Population _a, solidity::phaser::Population _b);
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
/**
|
||||
* Information describing the state of an individual member of the population during the course
|
||||
* of the genetic algorithm.
|
||||
@ -37,9 +48,17 @@ struct Individual
|
||||
Chromosome chromosome;
|
||||
std::optional<size_t> fitness = std::nullopt;
|
||||
|
||||
bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; }
|
||||
bool operator!=(Individual const& _other) const { return !(*this == _other); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& _stream, Individual const& _individual);
|
||||
};
|
||||
|
||||
/// Determines which individual is better by comparing fitness values. If fitness is the same
|
||||
/// takes into account all the other properties of the individual to make the comparison
|
||||
/// deterministic as long as the individuals are not equal.
|
||||
bool isFitter(Individual const& a, Individual const& b);
|
||||
|
||||
/**
|
||||
* Represents a changing set of individuals undergoing a genetic algorithm.
|
||||
* Each round of the algorithm involves mutating existing individuals, evaluating their fitness
|
||||
@ -55,19 +74,35 @@ public:
|
||||
static constexpr size_t MaxChromosomeLength = 30;
|
||||
|
||||
explicit Population(Program _program, std::vector<Chromosome> const& _chromosomes = {});
|
||||
static Population makeRandom(Program _program, size_t _size);
|
||||
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
std::function<size_t()> _chromosomeLengthGenerator
|
||||
);
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
);
|
||||
|
||||
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
||||
friend Population (::operator+)(Population _a, Population _b);
|
||||
|
||||
std::vector<Individual> const& individuals() const { return m_individuals; }
|
||||
|
||||
static size_t randomChromosomeLength() { return SimulationRNG::binomialInt(MaxChromosomeLength, 0.5); }
|
||||
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); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
|
||||
|
||||
private:
|
||||
explicit Population(Program _program, std::vector<Individual> _individuals = {}):
|
||||
explicit Population(Program _program, std::vector<Individual> _individuals):
|
||||
m_program{std::move(_program)},
|
||||
m_individuals{std::move(_individuals)} {}
|
||||
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
@ -70,7 +71,11 @@ CharStream loadSource(string const& _sourcePath)
|
||||
void runAlgorithm(string const& _sourcePath)
|
||||
{
|
||||
CharStream sourceCode = loadSource(_sourcePath);
|
||||
auto population = Population::makeRandom(Program::load(sourceCode), 10);
|
||||
auto population = Population::makeRandom(
|
||||
Program::load(sourceCode),
|
||||
10,
|
||||
bind(Population::binomialChromosomeLength, Population::MaxChromosomeLength)
|
||||
);
|
||||
population.run(nullopt, cout);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user