From 92b54d83a3388e80dd8a340aacda0e7e7858041e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 14 Feb 2020 19:49:45 +0100 Subject: [PATCH] [yul-phaser] Common: Add geneSubstitution() mutation --- test/yulPhaser/Common.cpp | 12 ++++++++++++ test/yulPhaser/Common.h | 5 +++++ test/yulPhaser/CommonTest.cpp | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/test/yulPhaser/Common.cpp b/test/yulPhaser/Common.cpp index 52cde9590..f9000921f 100644 --- a/test/yulPhaser/Common.cpp +++ b/test/yulPhaser/Common.cpp @@ -31,6 +31,18 @@ function phaser::test::wholeChromosomeReplacement(Chromosome _newChrom return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; }; } +function phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue) +{ + return [=](Chromosome const& _chromosome) + { + vector newGenes = _chromosome.optimisationSteps(); + assert(_geneIndex < newGenes.size()); + newGenes[_geneIndex] = _geneValue; + + return Chromosome(newGenes); + }; +} + vector phaser::test::chromosomeLengths(Population const& _population) { vector lengths; diff --git a/test/yulPhaser/Common.h b/test/yulPhaser/Common.h index cbec90f78..a59c7eed1 100644 --- a/test/yulPhaser/Common.h +++ b/test/yulPhaser/Common.h @@ -59,6 +59,11 @@ public: /// Mutation that always replaces the whole chromosome with the one specified in the parameter. std::function wholeChromosomeReplacement(Chromosome _newChromosome); +/// Mutation that always replaces the optimisation step at position @a _geneIndex with @a _geneValue. +/// +/// The chromosome must be long enough for this position to exist. +std::function geneSubstitution(size_t _geneIndex, std::string _geneValue); + // CHROMOSOME AND POPULATION HELPERS /// Returns a vector containing lengths of all chromosomes in the population (in the same order). diff --git a/test/yulPhaser/CommonTest.cpp b/test/yulPhaser/CommonTest.cpp index 3456a6640..efc511a80 100644 --- a/test/yulPhaser/CommonTest.cpp +++ b/test/yulPhaser/CommonTest.cpp @@ -46,6 +46,17 @@ BOOST_AUTO_TEST_CASE(wholeChromosomeReplacement_should_replace_whole_chromosome_ BOOST_TEST(mutation(Chromosome("ccc")) == Chromosome("aaa")); } +BOOST_AUTO_TEST_CASE(geneSubstitution_should_change_a_single_gene_at_a_given_index) +{ + Chromosome chromosome("aaccff"); + + function mutation1 = geneSubstitution(0, chromosome.optimisationSteps()[5]); + BOOST_TEST(mutation1(chromosome) == Chromosome("faccff")); + + function mutation2 = geneSubstitution(5, chromosome.optimisationSteps()[0]); + BOOST_TEST(mutation2(chromosome) == Chromosome("aaccfa")); +} + BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population) { shared_ptr fitnessMetric = make_shared();