[yul-phaser] Chromosome: Add stepsToGenes() and genesToSteps()

This commit is contained in:
Kamil Śliwak 2020-09-11 18:33:25 +02:00
parent 72f8a753a9
commit 2a707e7685
6 changed files with 60 additions and 34 deletions

View File

@ -47,25 +47,25 @@ using namespace solidity::util;
namespace solidity::phaser::test
{
vector<string> const ChrOmOsoMeSteps{
ConditionalSimplifier::name,
FunctionHoister::name,
RedundantAssignEliminator::name,
ForLoopConditionOutOfBody::name,
Rematerialiser::name,
ForLoopConditionOutOfBody::name,
ExpressionSimplifier::name,
ForLoopInitRewriter::name,
LoopInvariantCodeMotion::name,
ExpressionInliner::name
};
BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions"))
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
{
vector<string> expectedSteps{
ConditionalSimplifier::name,
FunctionHoister::name,
RedundantAssignEliminator::name,
ForLoopConditionOutOfBody::name,
Rematerialiser::name,
ForLoopConditionOutOfBody::name,
ExpressionSimplifier::name,
ForLoopInitRewriter::name,
LoopInvariantCodeMotion::name,
ExpressionInliner::name
};
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps);
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps);
}
BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time)
@ -151,6 +151,18 @@ BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_pr
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
}
BOOST_AUTO_TEST_CASE(stepsToGenes_should_translate_optimisation_step_names_to_abbreviations)
{
BOOST_TEST(Chromosome::stepsToGenes({}) == "");
BOOST_TEST(Chromosome::stepsToGenes(ChrOmOsoMeSteps) == "ChrOmOsoMe");
}
BOOST_AUTO_TEST_CASE(genesToSteps_should_translate_optimisation_step_abbreviations_to_names)
{
BOOST_TEST(Chromosome::genesToSteps("") == vector<string>{});
BOOST_TEST(Chromosome::genesToSteps("ChrOmOsoMe") == ChrOmOsoMeSteps);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

View File

@ -218,10 +218,11 @@ BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_i
BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations)
{
Chromosome chromosome("aaaaa");
vector<string> steps = Chromosome::genesToSteps("gfc");
function<Mutation> mutation = mutationSequence({
geneSubstitution(3, Chromosome("g").optimisationSteps()[0]),
geneSubstitution(2, Chromosome("f").optimisationSteps()[0]),
geneSubstitution(1, Chromosome("c").optimisationSteps()[0]),
geneSubstitution(3, steps[0]),
geneSubstitution(2, steps[1]),
geneSubstitution(1, steps[2]),
});
BOOST_TEST(mutation(chromosome) == Chromosome("acfga"));
@ -230,11 +231,12 @@ BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations)
BOOST_AUTO_TEST_CASE(mutationSequence_apply_mutations_in_the_order_they_are_given)
{
Chromosome chromosome("aa");
vector<string> steps = Chromosome::genesToSteps("gcfo");
function<Mutation> mutation = mutationSequence({
geneSubstitution(0, Chromosome("g").optimisationSteps()[0]),
geneSubstitution(1, Chromosome("c").optimisationSteps()[0]),
geneSubstitution(0, Chromosome("f").optimisationSteps()[0]),
geneSubstitution(1, Chromosome("o").optimisationSteps()[0]),
geneSubstitution(0, steps[0]),
geneSubstitution(1, steps[1]),
geneSubstitution(0, steps[2]),
geneSubstitution(1, steps[3]),
});
BOOST_TEST(mutation(chromosome) == Chromosome("fo"));

View File

@ -451,7 +451,7 @@ BOOST_AUTO_TEST_CASE(build_should_apply_prefix)
CharStream nestedSource("{{{let x:= 1}}}", "");
Program nestedProgram = get<Program>(Program::load(nestedSource));
Program flatProgram = get<Program>(Program::load(nestedSource));
flatProgram.optimise(Chromosome("f").optimisationSteps());
flatProgram.optimise(Chromosome::genesToSteps("f"));
assert(toString(nestedProgram) != toString(flatProgram));
{

View File

@ -53,7 +53,7 @@ protected:
Program optimisedProgram(Program _program, string _abbreviatedOptimisationSteps) const
{
Program result = move(_program);
result.optimise(Chromosome(_abbreviatedOptimisationSteps).optimisationSteps());
result.optimise(Chromosome::genesToSteps(_abbreviatedOptimisationSteps));
return result;
}

View File

@ -37,12 +37,6 @@ 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;
@ -54,10 +48,7 @@ Chromosome Chromosome::makeRandom(size_t _length)
ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome)
{
for (auto const& stepName: _chromosome.m_optimisationSteps)
_stream << OptimiserSuite::stepNameToAbbreviationMap().at(stepName);
return _stream;
return _stream << Chromosome::stepsToGenes(_chromosome.m_optimisationSteps);
}
vector<string> Chromosome::allStepNames()
@ -75,3 +66,21 @@ string const& Chromosome::randomOptimisationStep()
return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)];
}
string Chromosome::stepsToGenes(vector<string> const& _optimisationSteps)
{
string genes;
for (string const& stepName: _optimisationSteps)
genes.push_back(OptimiserSuite::stepNameToAbbreviationMap().at(stepName));
return genes;
}
vector<string> Chromosome::genesToSteps(string const& _genes)
{
vector<string> steps;
for (char abbreviation: _genes)
steps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation));
return steps;
}

View File

@ -43,7 +43,8 @@ public:
Chromosome() = default;
explicit Chromosome(std::vector<std::string> _optimisationSteps):
m_optimisationSteps(std::move(_optimisationSteps)) {}
explicit Chromosome(std::string const& _optimisationSteps);
explicit Chromosome(std::string const& _genes):
m_optimisationSteps(genesToSteps(_genes)) {}
static Chromosome makeRandom(size_t _length);
size_t length() const { return m_optimisationSteps.size(); }
@ -55,6 +56,8 @@ public:
bool operator!=(Chromosome const& _other) const { return !(*this == _other); }
static std::string const& randomOptimisationStep();
static std::string stepsToGenes(std::vector<std::string> const& _optimisationSteps);
static std::vector<std::string> genesToSteps(std::string const& _genes);
private:
static std::vector<std::string> allStepNames();