mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Mutations: Add mutationSequence()
This commit is contained in:
parent
0837a62d5c
commit
59011fcde6
@ -212,6 +212,39 @@ BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_i
|
|||||||
BOOST_TEST(mutation(chromosome) == Chromosome("f"));
|
BOOST_TEST(mutation(chromosome) == Chromosome("f"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations)
|
||||||
|
{
|
||||||
|
Chromosome chromosome("aaaaa");
|
||||||
|
function<Mutation> 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> 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> mutation = mutationSequence({});
|
||||||
|
|
||||||
|
BOOST_TEST(mutation(chromosome) == chromosome);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_swap_chromosome_parts_at_random_point)
|
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_swap_chromosome_parts_at_random_point)
|
||||||
{
|
{
|
||||||
function<Crossover> crossover = randomPointCrossover();
|
function<Crossover> crossover = randomPointCrossover();
|
||||||
|
@ -95,6 +95,18 @@ function<Mutation> phaser::alternativeMutations(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function<Mutation> phaser::mutationSequence(vector<function<Mutation>> _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
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -58,6 +58,9 @@ std::function<Mutation> alternativeMutations(
|
|||||||
std::function<Mutation> _mutation2
|
std::function<Mutation> _mutation2
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// Creates a mutation operator that sequentially applies all the operators given in @a _mutations.
|
||||||
|
std::function<Mutation> mutationSequence(std::vector<std::function<Mutation>> _mutations);
|
||||||
|
|
||||||
// CROSSOVER
|
// CROSSOVER
|
||||||
|
|
||||||
/// Creates a crossover operator that randomly selects a number between 0 and 1 and uses it as the
|
/// Creates a crossover operator that randomly selects a number between 0 and 1 and uses it as the
|
||||||
|
Loading…
Reference in New Issue
Block a user