From 2a707e76858a56fbc552625e39f2224655e29dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 11 Sep 2020 18:33:25 +0200 Subject: [PATCH 1/4] [yul-phaser] Chromosome: Add stepsToGenes() and genesToSteps() --- test/yulPhaser/Chromosome.cpp | 40 +++++++++++++++++++++------------ test/yulPhaser/Mutations.cpp | 16 +++++++------ test/yulPhaser/Phaser.cpp | 2 +- test/yulPhaser/ProgramCache.cpp | 2 +- tools/yulPhaser/Chromosome.cpp | 29 +++++++++++++++--------- tools/yulPhaser/Chromosome.h | 5 ++++- 6 files changed, 60 insertions(+), 34 deletions(-) diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp index d47c556c5..824fe695e 100644 --- a/test/yulPhaser/Chromosome.cpp +++ b/test/yulPhaser/Chromosome.cpp @@ -47,25 +47,25 @@ using namespace solidity::util; namespace solidity::phaser::test { +vector 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 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{}); + BOOST_TEST(Chromosome::genesToSteps("ChrOmOsoMe") == ChrOmOsoMeSteps); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/test/yulPhaser/Mutations.cpp b/test/yulPhaser/Mutations.cpp index 1f2bfef44..1fe947f00 100644 --- a/test/yulPhaser/Mutations.cpp +++ b/test/yulPhaser/Mutations.cpp @@ -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 steps = Chromosome::genesToSteps("gfc"); function 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 steps = Chromosome::genesToSteps("gcfo"); function 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")); diff --git a/test/yulPhaser/Phaser.cpp b/test/yulPhaser/Phaser.cpp index 673c43824..013f3a9e0 100644 --- a/test/yulPhaser/Phaser.cpp +++ b/test/yulPhaser/Phaser.cpp @@ -451,7 +451,7 @@ BOOST_AUTO_TEST_CASE(build_should_apply_prefix) CharStream nestedSource("{{{let x:= 1}}}", ""); Program nestedProgram = get(Program::load(nestedSource)); Program flatProgram = get(Program::load(nestedSource)); - flatProgram.optimise(Chromosome("f").optimisationSteps()); + flatProgram.optimise(Chromosome::genesToSteps("f")); assert(toString(nestedProgram) != toString(flatProgram)); { diff --git a/test/yulPhaser/ProgramCache.cpp b/test/yulPhaser/ProgramCache.cpp index 7fb74fca9..3a84aad67 100644 --- a/test/yulPhaser/ProgramCache.cpp +++ b/test/yulPhaser/ProgramCache.cpp @@ -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; } diff --git a/tools/yulPhaser/Chromosome.cpp b/tools/yulPhaser/Chromosome.cpp index 453c62d39..b7048ae61 100644 --- a/tools/yulPhaser/Chromosome.cpp +++ b/tools/yulPhaser/Chromosome.cpp @@ -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 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 Chromosome::allStepNames() @@ -75,3 +66,21 @@ string const& Chromosome::randomOptimisationStep() return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)]; } + +string Chromosome::stepsToGenes(vector const& _optimisationSteps) +{ + string genes; + for (string const& stepName: _optimisationSteps) + genes.push_back(OptimiserSuite::stepNameToAbbreviationMap().at(stepName)); + + return genes; +} + +vector Chromosome::genesToSteps(string const& _genes) +{ + vector steps; + for (char abbreviation: _genes) + steps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation)); + + return steps; +} diff --git a/tools/yulPhaser/Chromosome.h b/tools/yulPhaser/Chromosome.h index 95f3ae58e..e97042041 100644 --- a/tools/yulPhaser/Chromosome.h +++ b/tools/yulPhaser/Chromosome.h @@ -43,7 +43,8 @@ public: Chromosome() = default; explicit Chromosome(std::vector _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 const& _optimisationSteps); + static std::vector genesToSteps(std::string const& _genes); private: static std::vector allStepNames(); From 952a9c6115a17f5dc97b14565258e25b75e07116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 11 Sep 2020 18:53:47 +0200 Subject: [PATCH 2/4] [yul-phaser] Chromosome: store step abbreviations instead of full names --- test/yulPhaser/Chromosome.cpp | 22 +++++++------ test/yulPhaser/Mutations.cpp | 15 ++------- tools/yulPhaser/Chromosome.cpp | 2 +- tools/yulPhaser/Chromosome.h | 16 +++++---- tools/yulPhaser/Mutations.cpp | 59 +++++++++++++--------------------- 5 files changed, 48 insertions(+), 66 deletions(-) diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp index 824fe695e..509ad0036 100644 --- a/test/yulPhaser/Chromosome.cpp +++ b/test/yulPhaser/Chromosome.cpp @@ -63,11 +63,6 @@ vector const ChrOmOsoMeSteps{ 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) -{ - BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps); -} - BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time) { SimulationRNG::reset(1); @@ -95,6 +90,11 @@ BOOST_AUTO_TEST_CASE(makeRandom_should_use_every_possible_step_with_the_same_pro BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); } +BOOST_AUTO_TEST_CASE(constructor_should_store_genes) +{ + BOOST_TEST(Chromosome("ChrOmOsoMe").genes() == "ChrOmOsoMe"); +} + BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps) { vector steps = { @@ -102,9 +102,8 @@ BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps) BlockFlattener::name, UnusedPruner::name, }; - Chromosome chromosome(steps); - BOOST_TEST(steps == chromosome.optimisationSteps()); + BOOST_TEST(Chromosome(steps).genes() == "tfu"); } BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps) @@ -116,9 +115,9 @@ BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps) UnusedPruner::name, BlockFlattener::name, }; - Chromosome chromosome(steps); - BOOST_TEST(steps == chromosome.optimisationSteps()); + BOOST_TEST(Chromosome(steps).genes() == "ttfuf"); + BOOST_TEST(Chromosome("ttfuf").genes() == "ttfuf"); } BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_string_representation) @@ -133,6 +132,11 @@ BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_strin BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighTLMNrmVatpud"); } +BOOST_AUTO_TEST_CASE(optimisationSteps_should_translate_chromosomes_genes_to_optimisation_step_names) +{ + BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps); +} + BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_probability) { SimulationRNG::reset(1); diff --git a/test/yulPhaser/Mutations.cpp b/test/yulPhaser/Mutations.cpp index 1fe947f00..204efa603 100644 --- a/test/yulPhaser/Mutations.cpp +++ b/test/yulPhaser/Mutations.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -117,12 +118,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_before_first_position Chromosome mutatedChromosome = mutation(chromosome); BOOST_TEST(mutatedChromosome.length() > chromosome.length()); - - vector suffix( - mutatedChromosome.optimisationSteps().end() - static_cast(chromosome.length()), - mutatedChromosome.optimisationSteps().end() - ); - BOOST_TEST(suffix == chromosome.optimisationSteps()); + BOOST_TEST(boost::ends_with(mutatedChromosome.genes(), chromosome.genes())); } BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position) @@ -133,12 +129,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position) Chromosome mutatedChromosome = mutation(chromosome); BOOST_TEST(mutatedChromosome.length() > chromosome.length()); - - vector prefix( - mutatedChromosome.optimisationSteps().begin(), - mutatedChromosome.optimisationSteps().begin() + static_cast(chromosome.length()) - ); - BOOST_TEST(prefix == chromosome.optimisationSteps()); + BOOST_TEST(boost::starts_with(mutatedChromosome.genes(), chromosome.genes())); } BOOST_AUTO_TEST_CASE(geneAddition_should_return_identical_chromosome_if_probability_is_zero) diff --git a/tools/yulPhaser/Chromosome.cpp b/tools/yulPhaser/Chromosome.cpp index b7048ae61..c0e401800 100644 --- a/tools/yulPhaser/Chromosome.cpp +++ b/tools/yulPhaser/Chromosome.cpp @@ -48,7 +48,7 @@ Chromosome Chromosome::makeRandom(size_t _length) ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome) { - return _stream << Chromosome::stepsToGenes(_chromosome.m_optimisationSteps); + return _stream << _chromosome.m_genes; } vector Chromosome::allStepNames() diff --git a/tools/yulPhaser/Chromosome.h b/tools/yulPhaser/Chromosome.h index e97042041..0e7d1bb3f 100644 --- a/tools/yulPhaser/Chromosome.h +++ b/tools/yulPhaser/Chromosome.h @@ -42,17 +42,19 @@ class Chromosome public: Chromosome() = default; explicit Chromosome(std::vector _optimisationSteps): - m_optimisationSteps(std::move(_optimisationSteps)) {} - explicit Chromosome(std::string const& _genes): - m_optimisationSteps(genesToSteps(_genes)) {} + m_genes(stepsToGenes(_optimisationSteps)) {} + explicit Chromosome(std::string _genes): + m_genes(std::move(_genes)) {} static Chromosome makeRandom(size_t _length); - size_t length() const { return m_optimisationSteps.size(); } - std::vector const& optimisationSteps() const { return m_optimisationSteps; } + size_t length() const { return m_genes.size(); } + std::string const& genes() const { return m_genes; } + + std::vector optimisationSteps() const { return genesToSteps(m_genes); } friend std::ostream& operator<<(std::ostream& _stream, Chromosome const& _chromosome); - bool operator==(Chromosome const& _other) const { return m_optimisationSteps == _other.m_optimisationSteps; } + bool operator==(Chromosome const& _other) const { return m_genes == _other.m_genes; } bool operator!=(Chromosome const& _other) const { return !(*this == _other); } static std::string const& randomOptimisationStep(); @@ -62,7 +64,7 @@ public: private: static std::vector allStepNames(); - std::vector m_optimisationSteps; + std::string m_genes; }; } diff --git a/tools/yulPhaser/Mutations.cpp b/tools/yulPhaser/Mutations.cpp index f9d51744e..61614408d 100644 --- a/tools/yulPhaser/Mutations.cpp +++ b/tools/yulPhaser/Mutations.cpp @@ -120,19 +120,14 @@ ChromosomePair fixedPointSwap( assert(_crossoverPoint <= _chromosome1.length()); assert(_crossoverPoint <= _chromosome2.length()); - auto begin1 = _chromosome1.optimisationSteps().begin(); - auto begin2 = _chromosome2.optimisationSteps().begin(); - auto end1 = _chromosome1.optimisationSteps().end(); - auto end2 = _chromosome2.optimisationSteps().end(); - return { Chromosome( - vector(begin1, begin1 + static_cast(_crossoverPoint)) + - vector(begin2 + static_cast(_crossoverPoint), end2) + _chromosome1.genes().substr(0, _crossoverPoint) + + _chromosome2.genes().substr(_crossoverPoint, _chromosome2.length() - _crossoverPoint) ), Chromosome( - vector(begin2, begin2 + static_cast(_crossoverPoint)) + - vector(begin1 + static_cast(_crossoverPoint), end1) + _chromosome2.genes().substr(0, _crossoverPoint) + + _chromosome1.genes().substr(_crossoverPoint, _chromosome1.length() - _crossoverPoint) ), }; } @@ -197,24 +192,19 @@ ChromosomePair fixedTwoPointSwap( assert(_crossoverPoint2 <= _chromosome1.length()); assert(_crossoverPoint2 <= _chromosome2.length()); - auto lowPoint = static_cast(min(_crossoverPoint1, _crossoverPoint2)); - auto highPoint = static_cast(max(_crossoverPoint1, _crossoverPoint2)); - - auto begin1 = _chromosome1.optimisationSteps().begin(); - auto begin2 = _chromosome2.optimisationSteps().begin(); - auto end1 = _chromosome1.optimisationSteps().end(); - auto end2 = _chromosome2.optimisationSteps().end(); + size_t lowPoint = min(_crossoverPoint1, _crossoverPoint2); + size_t highPoint = max(_crossoverPoint1, _crossoverPoint2); return { Chromosome( - vector(begin1, begin1 + lowPoint) + - vector(begin2 + lowPoint, begin2 + highPoint) + - vector(begin1 + highPoint, end1) + _chromosome1.genes().substr(0, lowPoint) + + _chromosome2.genes().substr(lowPoint, highPoint - lowPoint) + + _chromosome1.genes().substr(highPoint, _chromosome1.length() - highPoint) ), Chromosome( - vector(begin2, begin2 + lowPoint) + - vector(begin1 + lowPoint, begin1 + highPoint) + - vector(begin2 + highPoint, end2) + _chromosome2.genes().substr(0, lowPoint) + + _chromosome1.genes().substr(lowPoint, highPoint - lowPoint) + + _chromosome2.genes().substr(highPoint, _chromosome2.length() - highPoint) ), }; } @@ -258,42 +248,37 @@ namespace ChromosomePair uniformSwap(Chromosome const& _chromosome1, Chromosome const& _chromosome2, double _swapChance) { - vector steps1; - vector steps2; + string steps1; + string steps2; size_t minLength = min(_chromosome1.length(), _chromosome2.length()); for (size_t i = 0; i < minLength; ++i) if (SimulationRNG::bernoulliTrial(_swapChance)) { - steps1.push_back(_chromosome2.optimisationSteps()[i]); - steps2.push_back(_chromosome1.optimisationSteps()[i]); + steps1.push_back(_chromosome2.genes()[i]); + steps2.push_back(_chromosome1.genes()[i]); } else { - steps1.push_back(_chromosome1.optimisationSteps()[i]); - steps2.push_back(_chromosome2.optimisationSteps()[i]); + steps1.push_back(_chromosome1.genes()[i]); + steps2.push_back(_chromosome2.genes()[i]); } - auto begin1 = _chromosome1.optimisationSteps().begin(); - auto begin2 = _chromosome2.optimisationSteps().begin(); - auto end1 = _chromosome1.optimisationSteps().end(); - auto end2 = _chromosome2.optimisationSteps().end(); - bool swapTail = SimulationRNG::bernoulliTrial(_swapChance); if (_chromosome1.length() > minLength) { if (swapTail) - steps2.insert(steps2.end(), begin1 + static_cast(minLength), end1); + steps2 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength); else - steps1.insert(steps1.end(), begin1 + static_cast(minLength), end1); + steps1 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength); } if (_chromosome2.length() > minLength) { if (swapTail) - steps1.insert(steps1.end(), begin2 + static_cast(minLength), end2); + steps1 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength); else - steps2.insert(steps2.end(), begin2 + static_cast(minLength), end2); + steps2 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength); } return {Chromosome(steps1), Chromosome(steps2)}; From e024032a67e5b00498fa55c5288b1f44deecff5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 11 Sep 2020 20:07:51 +0200 Subject: [PATCH 3/4] Revert "[yul-phaser] Temporarily disable very slow tests for the classic algorithm" This reverts commit b23f7d879009306c294438ef47b216722d8457ea. --- test/yulPhaser/GeneticAlgorithms.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/test/yulPhaser/GeneticAlgorithms.cpp b/test/yulPhaser/GeneticAlgorithms.cpp index cfac86db0..16d5cc654 100644 --- a/test/yulPhaser/GeneticAlgorithms.cpp +++ b/test/yulPhaser/GeneticAlgorithms.cpp @@ -213,8 +213,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(ClassicGeneticAlgorithmTest) -// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. -BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_proportional_to_fitness, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled()) +BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_proportional_to_fitness, ClassicGeneticAlgorithmFixture) { constexpr double relativeTolerance = 0.1; constexpr size_t populationSize = 1000; @@ -255,8 +254,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_ BOOST_TEST(abs(meanSquaredError(newFitness, expectedValue) - variance) < variance * relativeTolerance); } -// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. -BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_only_individuals_existing_in_the_original_population, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled()) +BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_only_individuals_existing_in_the_original_population, ClassicGeneticAlgorithmFixture) { constexpr size_t populationSize = 1000; auto population = Population::makeRandom(m_fitnessMetric, populationSize, 1, 10); @@ -300,8 +298,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_crossover, ClassicGeneticAlgorith BOOST_TEST(totalCrossed >= 2); } -// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. -BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled()) +BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithmFixture) { m_options.mutationChance = 0.6; ClassicGeneticAlgorithm algorithm(m_options); @@ -330,8 +327,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithm BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance); } -// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. -BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled()) +BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithmFixture) { m_options.deletionChance = 0.6; ClassicGeneticAlgorithm algorithm(m_options); @@ -360,8 +356,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithm BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance); } -// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. -BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_addition, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled()) +BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_addition, ClassicGeneticAlgorithmFixture) { m_options.additionChance = 0.6; ClassicGeneticAlgorithm algorithm(m_options); From 47f5ee42c97371efed7aa74a350c5e84bb66b04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 11 Sep 2020 20:18:09 +0200 Subject: [PATCH 4/4] [yul-phaser] isFitter(): Switch from toString() to genes() to make chromosome comparisons a tiny bit faster - toString() uses a stream for conversion while genes() returns a direct reference to the string, without copies in between. The speed up is very small compared to the improvement from switching to storing a string of abbreviations instead of a vector of step names inside chromosomes but there's basically no downside to this change so it's still worth it. --- tools/yulPhaser/Population.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/yulPhaser/Population.cpp b/tools/yulPhaser/Population.cpp index c4b506369..1dba2e2fd 100644 --- a/tools/yulPhaser/Population.cpp +++ b/tools/yulPhaser/Population.cpp @@ -54,7 +54,7 @@ bool phaser::isFitter(Individual const& a, Individual const& b) return ( (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)) + (a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && a.chromosome.genes() < b.chromosome.genes()) ); }