[yul-phaser] Common: Add geneSubstitution() mutation

This commit is contained in:
Kamil Śliwak 2020-02-14 19:49:45 +01:00
parent c941eaf5d6
commit 92b54d83a3
3 changed files with 28 additions and 0 deletions

View File

@ -31,6 +31,18 @@ function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChrom
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
}
function<Mutation> phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue)
{
return [=](Chromosome const& _chromosome)
{
vector<string> newGenes = _chromosome.optimisationSteps();
assert(_geneIndex < newGenes.size());
newGenes[_geneIndex] = _geneValue;
return Chromosome(newGenes);
};
}
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
{
vector<size_t> lengths;

View File

@ -59,6 +59,11 @@ public:
/// Mutation that always replaces the whole chromosome with the one specified in the parameter.
std::function<Mutation> 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<Mutation> 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).

View File

@ -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<Mutation> mutation1 = geneSubstitution(0, chromosome.optimisationSteps()[5]);
BOOST_TEST(mutation1(chromosome) == Chromosome("faccff"));
function<Mutation> 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> fitnessMetric = make_shared<ChromosomeLengthMetric>();