From 59011fcde6586150148792e3309cd3e15fe9e74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 11 Mar 2020 23:45:46 +0100 Subject: [PATCH] [yul-phaser] Mutations: Add mutationSequence() --- test/yulPhaser/Mutations.cpp | 33 +++++++++++++++++++++++++++++++++ tools/yulPhaser/Mutations.cpp | 12 ++++++++++++ tools/yulPhaser/Mutations.h | 3 +++ 3 files changed, 48 insertions(+) diff --git a/test/yulPhaser/Mutations.cpp b/test/yulPhaser/Mutations.cpp index d98fa7604..33c623f84 100644 --- a/test/yulPhaser/Mutations.cpp +++ b/test/yulPhaser/Mutations.cpp @@ -212,6 +212,39 @@ BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_i BOOST_TEST(mutation(chromosome) == Chromosome("f")); } +BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations) +{ + Chromosome chromosome("aaaaa"); + function mutation = mutationSequence({ + geneSubstitution(3, Chromosome("g").optimisationSteps()[0]), + geneSubstitution(2, Chromosome("f").optimisationSteps()[0]), + geneSubstitution(1, Chromosome("c").optimisationSteps()[0]), + }); + + BOOST_TEST(mutation(chromosome) == Chromosome("acfga")); +} + +BOOST_AUTO_TEST_CASE(mutationSequence_apply_mutations_in_the_order_they_are_given) +{ + Chromosome chromosome("aa"); + 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]), + }); + + BOOST_TEST(mutation(chromosome) == Chromosome("fo")); +} + +BOOST_AUTO_TEST_CASE(mutationSequence_should_return_unmodified_chromosome_if_given_no_mutations) +{ + Chromosome chromosome("aa"); + function mutation = mutationSequence({}); + + BOOST_TEST(mutation(chromosome) == chromosome); +} + BOOST_AUTO_TEST_CASE(randomPointCrossover_should_swap_chromosome_parts_at_random_point) { function crossover = randomPointCrossover(); diff --git a/tools/yulPhaser/Mutations.cpp b/tools/yulPhaser/Mutations.cpp index 9cf741707..98689a810 100644 --- a/tools/yulPhaser/Mutations.cpp +++ b/tools/yulPhaser/Mutations.cpp @@ -95,6 +95,18 @@ function phaser::alternativeMutations( }; } +function phaser::mutationSequence(vector> _mutations) +{ + return [=](Chromosome const& _chromosome) + { + Chromosome mutatedChromosome = _chromosome; + for (size_t i = 0; i < _mutations.size(); ++i) + mutatedChromosome = _mutations[i](move(mutatedChromosome)); + + return mutatedChromosome; + }; +} + namespace { diff --git a/tools/yulPhaser/Mutations.h b/tools/yulPhaser/Mutations.h index 5554a1d2d..d545aa093 100644 --- a/tools/yulPhaser/Mutations.h +++ b/tools/yulPhaser/Mutations.h @@ -58,6 +58,9 @@ std::function alternativeMutations( std::function _mutation2 ); +/// Creates a mutation operator that sequentially applies all the operators given in @a _mutations. +std::function mutationSequence(std::vector> _mutations); + // CROSSOVER /// Creates a crossover operator that randomly selects a number between 0 and 1 and uses it as the