mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8328 from imapp-pl/yul-phaser-gewep-algorithm
[yul-phaser] GEWEP algorithm
This commit is contained in:
@@ -145,6 +145,8 @@ set(yul_phaser_sources
|
||||
yulPhaser/Chromosome.cpp
|
||||
yulPhaser/FitnessMetrics.cpp
|
||||
yulPhaser/GeneticAlgorithms.cpp
|
||||
yulPhaser/Mutations.cpp
|
||||
yulPhaser/PairSelections.cpp
|
||||
yulPhaser/Population.cpp
|
||||
yulPhaser/Program.cpp
|
||||
yulPhaser/Selections.cpp
|
||||
@@ -156,6 +158,8 @@ set(yul_phaser_sources
|
||||
../tools/yulPhaser/Chromosome.cpp
|
||||
../tools/yulPhaser/FitnessMetrics.cpp
|
||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||
../tools/yulPhaser/Mutations.cpp
|
||||
../tools/yulPhaser/PairSelections.cpp
|
||||
../tools/yulPhaser/Population.cpp
|
||||
../tools/yulPhaser/Program.cpp
|
||||
../tools/yulPhaser/Selections.cpp
|
||||
|
||||
@@ -24,6 +24,24 @@
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -44,6 +62,15 @@ map<string, size_t> phaser::test::enumerateOptmisationSteps()
|
||||
return stepIndices;
|
||||
}
|
||||
|
||||
size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2)
|
||||
{
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < min(_chromosome1.length(), _chromosome2.length()); ++i)
|
||||
count += static_cast<int>(_chromosome1.optimisationSteps()[i] != _chromosome2.optimisationSteps()[i]);
|
||||
|
||||
return count + abs(static_cast<int>(_chromosome1.length() - _chromosome2.length()));
|
||||
}
|
||||
|
||||
string phaser::test::stripWhitespace(string const& input)
|
||||
{
|
||||
regex whitespaceRegex("\\s+");
|
||||
|
||||
@@ -30,9 +30,11 @@
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/Mutations.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -52,11 +54,26 @@ public:
|
||||
size_t evaluate(Chromosome const& _chromosome) const override { return _chromosome.length(); }
|
||||
};
|
||||
|
||||
// MUTATIONS
|
||||
|
||||
/// 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).
|
||||
std::vector<size_t> chromosomeLengths(Population const& _population);
|
||||
|
||||
/// Returns the number of genes that differ between two chromosomes.
|
||||
/// If the chromnosomes have different lengths, the positions that are present in only one of them
|
||||
/// are counted as mismatches.
|
||||
size_t countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2);
|
||||
|
||||
/// Assigns indices from 0 to N to all optimisation steps available in the OptimiserSuite.
|
||||
/// This is a convenience helper to make it easier to test their distribution with tools made for
|
||||
/// integers.
|
||||
|
||||
@@ -40,6 +40,23 @@ BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_le
|
||||
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("aaaaa")) == 5);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(wholeChromosomeReplacement_should_replace_whole_chromosome_with_another)
|
||||
{
|
||||
function<Mutation> mutation = wholeChromosomeReplacement(Chromosome("aaa"));
|
||||
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>();
|
||||
@@ -51,6 +68,34 @@ BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_
|
||||
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(countDifferences_should_return_zero_for_identical_chromosomes)
|
||||
{
|
||||
BOOST_TEST(countDifferences(Chromosome(), Chromosome()) == 0);
|
||||
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("a")) == 0);
|
||||
BOOST_TEST(countDifferences(Chromosome("afxT"), Chromosome("afxT")) == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(countDifferences_should_count_mismatched_positions_in_chromosomes_of_the_same_length)
|
||||
{
|
||||
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("f")) == 1);
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("ac")) == 1);
|
||||
BOOST_TEST(countDifferences(Chromosome("ac"), Chromosome("cc")) == 1);
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("cc")) == 2);
|
||||
BOOST_TEST(countDifferences(Chromosome("afxT"), Chromosome("Txfa")) == 4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(countDifferences_should_count_missing_characters_as_differences)
|
||||
{
|
||||
BOOST_TEST(countDifferences(Chromosome(""), Chromosome("a")) == 1);
|
||||
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("")) == 1);
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("")) == 2);
|
||||
BOOST_TEST(countDifferences(Chromosome("aaa"), Chromosome("")) == 3);
|
||||
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("aaaa")) == 2);
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("aacc")) == 2);
|
||||
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("cccc")) == 4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_available_optimisation_steps)
|
||||
{
|
||||
map<string, char> stepsAndAbbreviations = OptimiserSuite::stepNameToAbbreviationMap();
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
@@ -132,6 +133,107 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_any_chromosomes_if_whole
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(GenerationalElitistWithExclusivePoolsTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_of_population, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.2,
|
||||
/* crossoverPoolSize = */ 0.2,
|
||||
/* randomisationChance = */ 0.0,
|
||||
/* deletionVsAdditionChance = */ 1.0,
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{0, 0, 3, 3, 3, 3, 3, 3, 3, 3}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.2,
|
||||
/* crossoverPoolSize = */ 0.2,
|
||||
/* randomisationChance = */ 0.0,
|
||||
/* deletionVsAdditionChance = */ 0.0,
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert(chromosomeLengths(algorithm.population()) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 3, 3, 7, 7}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossover_pool_by_mutating_the_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 20, 5, 5);
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.8,
|
||||
/* crossoverPoolSize = */ 0.0,
|
||||
/* randomisationChance = */ 0.5,
|
||||
/* deletionVsAdditionChance = */ 0.5,
|
||||
/* percentGenesToRandomise = */ 1.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
algorithm.runNextRound();
|
||||
|
||||
BOOST_TEST((
|
||||
chromosomeLengths(algorithm.population()) ==
|
||||
vector<size_t>{0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 11, 11, 11}
|
||||
));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossover_pool_by_crossing_over_the_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = (
|
||||
Population(m_fitnessMetric, {Chromosome("aa"), Chromosome("ff")}) +
|
||||
Population::makeRandom(m_fitnessMetric, 8, 6, 6)
|
||||
);
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.0,
|
||||
/* crossoverPoolSize = */ 0.8,
|
||||
/* randomisationChance = */ 0.0,
|
||||
/* deletionVsAdditionChance = */ 0.0,
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 0.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
algorithm.runNextRound();
|
||||
|
||||
vector<Individual> const& newIndividuals = algorithm.population().individuals();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
|
||||
for (auto& individual: newIndividuals)
|
||||
BOOST_TEST((
|
||||
individual.chromosome == Chromosome("aa") ||
|
||||
individual.chromosome == Chromosome("af") ||
|
||||
individual.chromosome == Chromosome("fa") ||
|
||||
individual.chromosome == Chromosome("ff")
|
||||
));
|
||||
BOOST_TEST(any_of(newIndividuals.begin() + 2, newIndividuals.end(), [](auto& individual){
|
||||
return individual.chromosome != Chromosome("aa") && individual.chromosome != Chromosome("ff");
|
||||
}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Mutations.h>
|
||||
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(MutationsTest)
|
||||
BOOST_AUTO_TEST_SUITE(GeneRandomisationTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneRandomisation_should_iterate_over_genes_and_replace_them_with_random_ones_with_given_probability)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejs");
|
||||
function<Mutation> mutation01 = geneRandomisation(0.1);
|
||||
function<Mutation> mutation05 = geneRandomisation(0.5);
|
||||
function<Mutation> mutation10 = geneRandomisation(1.0);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
BOOST_TEST(countDifferences(mutation01(chromosome), chromosome), 2);
|
||||
BOOST_TEST(countDifferences(mutation05(chromosome), chromosome), 5);
|
||||
BOOST_TEST(countDifferences(mutation10(chromosome), chromosome), 7);
|
||||
SimulationRNG::reset(2);
|
||||
BOOST_TEST(countDifferences(mutation01(chromosome), chromosome), 1);
|
||||
BOOST_TEST(countDifferences(mutation05(chromosome), chromosome), 3);
|
||||
BOOST_TEST(countDifferences(mutation10(chromosome), chromosome), 9);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneRandomisation_should_return_identical_chromosome_if_probability_is_zero)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejsrmV");
|
||||
function<Mutation> mutation = geneRandomisation(0.0);
|
||||
|
||||
BOOST_TEST(mutation(chromosome) == chromosome);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneDeletion_should_iterate_over_genes_and_delete_them_with_given_probability)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejs");
|
||||
function<Mutation> mutation01 = geneDeletion(0.1);
|
||||
function<Mutation> mutation05 = geneDeletion(0.5);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
// fcCUnDvejs
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace("fcCU Dvejs")));
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace(" D ejs")));
|
||||
SimulationRNG::reset(2);
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace("fcUnDvejs")));
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace(" Un s")));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneDeletion_should_return_identical_chromosome_if_probability_is_zero)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejsrmV");
|
||||
function<Mutation> mutation = geneDeletion(0.0);
|
||||
|
||||
BOOST_TEST(mutation(chromosome) == chromosome);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneDeletion_should_delete_all_genes_if_probability_is_one)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejsrmV");
|
||||
function<Mutation> mutation = geneDeletion(1.0);
|
||||
|
||||
BOOST_TEST(mutation(chromosome) == Chromosome(""));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_iterate_over_gene_positions_and_insert_new_genes_with_given_probability)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejs");
|
||||
function<Mutation> mutation01 = geneAddition(0.1);
|
||||
function<Mutation> mutation05 = geneAddition(0.5);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
// f c C U n D v e j s
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace(" f c C UC n D v e jx s"))); // 20% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("j f cu C U ne D v eI j sf"))); // 50% more
|
||||
SimulationRNG::reset(2);
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace(" f cu C U n D v e j s"))); // 10% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("L f ce Cv U n D v e jO s"))); // 40% more
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_before_first_position)
|
||||
{
|
||||
SimulationRNG::reset(7);
|
||||
Chromosome chromosome("fcCUnDvejs");
|
||||
function<Mutation> mutation = geneAddition(0.1);
|
||||
|
||||
Chromosome mutatedChromosome = mutation(chromosome);
|
||||
BOOST_TEST(mutatedChromosome.length() > chromosome.length());
|
||||
|
||||
vector<string> suffix(
|
||||
mutatedChromosome.optimisationSteps().end() - chromosome.length(),
|
||||
mutatedChromosome.optimisationSteps().end()
|
||||
);
|
||||
BOOST_TEST(suffix == chromosome.optimisationSteps());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position)
|
||||
{
|
||||
SimulationRNG::reset(81);
|
||||
Chromosome chromosome("fcCUnDvejs");
|
||||
function<Mutation> mutation = geneAddition(0.1);
|
||||
|
||||
Chromosome mutatedChromosome = mutation(chromosome);
|
||||
BOOST_TEST(mutatedChromosome.length() > chromosome.length());
|
||||
|
||||
vector<string> prefix(
|
||||
mutatedChromosome.optimisationSteps().begin(),
|
||||
mutatedChromosome.optimisationSteps().begin() + chromosome.length()
|
||||
);
|
||||
BOOST_TEST(prefix == chromosome.optimisationSteps());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_return_identical_chromosome_if_probability_is_zero)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejsrmV");
|
||||
function<Mutation> mutation = geneAddition(0.0);
|
||||
|
||||
BOOST_TEST(mutation(chromosome) == chromosome);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_insert_genes_at_all_positions_if_probability_is_one)
|
||||
{
|
||||
Chromosome chromosome("fcCUnDvejsrmV");
|
||||
function<Mutation> mutation = geneAddition(1.0);
|
||||
|
||||
Chromosome mutatedChromosome = mutation(chromosome);
|
||||
BOOST_TEST(mutatedChromosome.length() == chromosome.length() * 2 + 1);
|
||||
|
||||
vector<string> originalGenes;
|
||||
for (size_t i = 0; i < mutatedChromosome.length() - 1; ++i)
|
||||
if (i % 2 == 1)
|
||||
originalGenes.push_back(mutatedChromosome.optimisationSteps()[i]);
|
||||
|
||||
BOOST_TEST(Chromosome(originalGenes) == chromosome);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(alternativeMutations_should_choose_between_mutations_with_given_probability)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
Chromosome chromosome("a");
|
||||
function<Mutation> mutation = alternativeMutations(
|
||||
0.8,
|
||||
wholeChromosomeReplacement(Chromosome("c")),
|
||||
wholeChromosomeReplacement(Chromosome("f"))
|
||||
);
|
||||
|
||||
size_t cCount = 0;
|
||||
size_t fCount = 0;
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
{
|
||||
Chromosome mutatedChromosome = mutation(chromosome);
|
||||
cCount += static_cast<int>(mutatedChromosome == Chromosome("c"));
|
||||
fCount += static_cast<int>(mutatedChromosome == Chromosome("f"));
|
||||
}
|
||||
|
||||
// This particular seed results in 7 "c"s out of 10 which looks plausible given the 80% chance.
|
||||
BOOST_TEST(cCount == 7);
|
||||
BOOST_TEST(fCount == 3);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_first_mutation_if_probability_is_one)
|
||||
{
|
||||
Chromosome chromosome("a");
|
||||
function<Mutation> mutation = alternativeMutations(
|
||||
1.0,
|
||||
wholeChromosomeReplacement(Chromosome("c")),
|
||||
wholeChromosomeReplacement(Chromosome("f"))
|
||||
);
|
||||
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
BOOST_TEST(mutation(chromosome) == Chromosome("c"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_if_probability_is_zero)
|
||||
{
|
||||
Chromosome chromosome("a");
|
||||
function<Mutation> mutation = alternativeMutations(
|
||||
0.0,
|
||||
wholeChromosomeReplacement(Chromosome("c")),
|
||||
wholeChromosomeReplacement(Chromosome("f"))
|
||||
);
|
||||
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
BOOST_TEST(mutation(chromosome) == Chromosome("f"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_swap_chromosome_parts_at_random_point)
|
||||
{
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
Chromosome result1 = crossover(Chromosome("aaaaaaaaaa"), Chromosome("cccccc"));
|
||||
BOOST_TEST(result1 == Chromosome("aaaccc"));
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
Chromosome result2 = crossover(Chromosome("cccccc"), Chromosome("aaaaaaaaaa"));
|
||||
BOOST_TEST(result2 == Chromosome("cccaaaaaaa"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_only_consider_points_available_on_both_chromosomes)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
for (size_t i = 0; i < 30; ++i)
|
||||
{
|
||||
Chromosome result1 = crossover(Chromosome("aaa"), Chromosome("TTTTTTTTTTTTTTTTTTTT"));
|
||||
Chromosome result2 = crossover(Chromosome("TTTTTTTTTTTTTTTTTTTT"), Chromosome("aaa"));
|
||||
BOOST_TEST((
|
||||
result1 == Chromosome("TTTTTTTTTTTTTTTTTTTT") ||
|
||||
result1 == Chromosome("aTTTTTTTTTTTTTTTTTTT") ||
|
||||
result1 == Chromosome("aaTTTTTTTTTTTTTTTTTT") ||
|
||||
result1 == Chromosome("aaaTTTTTTTTTTTTTTTTT")
|
||||
));
|
||||
BOOST_TEST((
|
||||
result2 == Chromosome("aaa") ||
|
||||
result2 == Chromosome("Taa") ||
|
||||
result2 == Chromosome("TTa") ||
|
||||
result2 == Chromosome("TTT")
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_never_split_at_position_zero_if_chromosomes_are_splittable)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
for (size_t i = 0; i < 30; ++i)
|
||||
{
|
||||
Chromosome result1 = crossover(Chromosome("aa"), Chromosome("TTTTTTTTTTTTTTTTTTTT"));
|
||||
Chromosome result2 = crossover(Chromosome("TTTTTTTTTTTTTTTTTTTT"), Chromosome("aa"));
|
||||
BOOST_TEST(result1 != Chromosome("TTTTTTTTTTTTTTTTTTTT"));
|
||||
BOOST_TEST(result2 != Chromosome("aa"));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_never_split_at_position_zero_if_chromosomes_are_not_empty)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
for (size_t i = 0; i < 30; ++i)
|
||||
{
|
||||
Chromosome result1 = crossover(Chromosome("a"), Chromosome("T"));
|
||||
Chromosome result2 = crossover(Chromosome("T"), Chromosome("a"));
|
||||
BOOST_TEST(result1 == Chromosome("a"));
|
||||
BOOST_TEST(result2 == Chromosome("T"));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_work_even_if_one_chromosome_is_unsplittable)
|
||||
{
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
BOOST_CHECK(crossover(Chromosome("ff"), Chromosome("a")) == Chromosome("f"));
|
||||
BOOST_CHECK(crossover(Chromosome("a"), Chromosome("ff")) == Chromosome("af"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomPointCrossover_should_split_at_position_zero_only_if_at_least_one_chromosome_is_empty)
|
||||
{
|
||||
Chromosome empty("");
|
||||
Chromosome unsplittable("a");
|
||||
Chromosome splittable("aaaa");
|
||||
function<Crossover> crossover = randomPointCrossover();
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
BOOST_CHECK(crossover(empty, empty) == empty);
|
||||
BOOST_CHECK(crossover(unsplittable, empty) == empty);
|
||||
BOOST_CHECK(crossover(empty, unsplittable) == unsplittable);
|
||||
BOOST_CHECK(crossover(splittable, empty) == empty);
|
||||
BOOST_CHECK(crossover(empty, splittable) == splittable);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_swap_chromosome_parts_at_given_point)
|
||||
{
|
||||
Chromosome result1 = fixedPointCrossover(0.8)(Chromosome("aaaaaaaaaa"), Chromosome("cccccccccc"));
|
||||
Chromosome result2 = fixedPointCrossover(0.8)(Chromosome("cccccccccc"), Chromosome("aaaaaaaaaa"));
|
||||
BOOST_TEST(result1 == Chromosome("aaaaaaaacc"));
|
||||
BOOST_TEST(result2 == Chromosome("ccccccccaa"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_determine_crossover_point_based_on_length_of_shorter_chromosome)
|
||||
{
|
||||
Chromosome result1 = fixedPointCrossover(0.4)(Chromosome("aaaaa"), Chromosome("cccccccccc"));
|
||||
Chromosome result2 = fixedPointCrossover(0.4)(Chromosome("cccccccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result1 == Chromosome("aacccccccc"));
|
||||
BOOST_TEST(result2 == Chromosome("ccaaa"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_round_split_point)
|
||||
{
|
||||
Chromosome result1 = fixedPointCrossover(0.49)(Chromosome("aaaaa"), Chromosome("ccccc"));
|
||||
Chromosome result2 = fixedPointCrossover(0.49)(Chromosome("ccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result1 == Chromosome("aaccc"));
|
||||
BOOST_TEST(result2 == Chromosome("ccaaa"));
|
||||
|
||||
Chromosome result3 = fixedPointCrossover(0.50)(Chromosome("aaaaa"), Chromosome("ccccc"));
|
||||
Chromosome result4 = fixedPointCrossover(0.50)(Chromosome("ccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result3 == Chromosome("aaacc"));
|
||||
BOOST_TEST(result4 == Chromosome("cccaa"));
|
||||
|
||||
Chromosome result5 = fixedPointCrossover(0.51)(Chromosome("aaaaa"), Chromosome("ccccc"));
|
||||
Chromosome result6 = fixedPointCrossover(0.51)(Chromosome("ccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result5 == Chromosome("aaacc"));
|
||||
BOOST_TEST(result6 == Chromosome("cccaa"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_split_at_position_zero_if_explicitly_requested)
|
||||
{
|
||||
Chromosome result1 = fixedPointCrossover(0.0)(Chromosome("aaaaa"), Chromosome("cccccccccc"));
|
||||
Chromosome result2 = fixedPointCrossover(0.0)(Chromosome("cccccccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result1 == Chromosome("cccccccccc"));
|
||||
BOOST_TEST(result2 == Chromosome("aaaaa"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_split_at_end_of_shorter_chromosome_if_crossover_point_is_after_last_position)
|
||||
{
|
||||
Chromosome result1 = fixedPointCrossover(1.0)(Chromosome("aaaaa"), Chromosome("cccccccccc"));
|
||||
Chromosome result2 = fixedPointCrossover(1.0)(Chromosome("cccccccccc"), Chromosome("aaaaa"));
|
||||
BOOST_TEST(result1 == Chromosome("aaaaaccccc"));
|
||||
BOOST_TEST(result2 == Chromosome("ccccc"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_select_correct_split_point_for_unsplittable_chromosomes)
|
||||
{
|
||||
function<Crossover> crossover00 = fixedPointCrossover(0.0);
|
||||
BOOST_CHECK(crossover00(Chromosome("fff"), Chromosome("a")) == Chromosome("a"));
|
||||
BOOST_CHECK(crossover00(Chromosome("a"), Chromosome("fff")) == Chromosome("fff"));
|
||||
|
||||
BOOST_CHECK(crossover00(Chromosome("f"), Chromosome("a")) == Chromosome("a"));
|
||||
|
||||
function<Crossover> crossover10 = fixedPointCrossover(1.0);
|
||||
BOOST_CHECK(crossover10(Chromosome("fff"), Chromosome("a")) == Chromosome("f"));
|
||||
BOOST_CHECK(crossover10(Chromosome("a"), Chromosome("fff")) == Chromosome("aff"));
|
||||
|
||||
BOOST_CHECK(crossover10(Chromosome("f"), Chromosome("a")) == Chromosome("f"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fixedPointCrossover_should_always_use_position_zero_as_split_point_when_chromosome_empty)
|
||||
{
|
||||
Chromosome empty("");
|
||||
Chromosome unsplittable("f");
|
||||
Chromosome splittable("aaaa");
|
||||
|
||||
function<Crossover> crossover00 = fixedPointCrossover(0.0);
|
||||
BOOST_CHECK(crossover00(empty, empty) == empty);
|
||||
BOOST_CHECK(crossover00(unsplittable, empty) == empty);
|
||||
BOOST_CHECK(crossover00(empty, unsplittable) == unsplittable);
|
||||
BOOST_CHECK(crossover00(splittable, empty) == empty);
|
||||
BOOST_CHECK(crossover00(empty, splittable) == splittable);
|
||||
|
||||
function<Crossover> crossover10 = fixedPointCrossover(1.0);
|
||||
BOOST_CHECK(crossover10(empty, empty) == empty);
|
||||
BOOST_CHECK(crossover10(unsplittable, empty) == empty);
|
||||
BOOST_CHECK(crossover10(empty, unsplittable) == unsplittable);
|
||||
BOOST_CHECK(crossover10(splittable, empty) == empty);
|
||||
BOOST_CHECK(crossover10(empty, splittable) == splittable);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(PairSelectionsTest)
|
||||
BOOST_AUTO_TEST_SUITE(RandomPairSelectionTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_random_values_with_equal_probabilities)
|
||||
{
|
||||
constexpr int collectionSize = 10;
|
||||
constexpr int selectionSize = 100;
|
||||
constexpr double relativeTolerance = 0.1;
|
||||
constexpr double expectedValue = (collectionSize - 1) / 2.0;
|
||||
constexpr double variance = (collectionSize * collectionSize - 1) / 12.0;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(selectionSize).materialise(collectionSize);
|
||||
vector<size_t> samples;
|
||||
for (auto& [first, second]: pairs)
|
||||
{
|
||||
samples.push_back(first);
|
||||
samples.push_back(second);
|
||||
}
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_only_values_that_can_be_used_as_collection_indices)
|
||||
{
|
||||
const size_t collectionSize = 200;
|
||||
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(0.5).materialise(collectionSize);
|
||||
|
||||
BOOST_TEST(pairs.size() == 100);
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [&](auto const& pair){ return get<0>(pair) <= collectionSize; }));
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [&](auto const& pair){ return get<1>(pair) <= collectionSize; }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_never_return_a_pair_of_identical_indices)
|
||||
{
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(0.5).materialise(100);
|
||||
|
||||
BOOST_TEST(pairs.size() == 50);
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [](auto const& pair){ return get<0>(pair) != get<1>(pair); }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_number_of_pairs_thats_a_fraction_of_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0.0).materialise(10).size() == 0);
|
||||
BOOST_TEST(RandomPairSelection(0.3).materialise(10).size() == 3);
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(10).size() == 5);
|
||||
BOOST_TEST(RandomPairSelection(0.7).materialise(10).size() == 7);
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(10).size() == 10);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_support_number_of_pairs_bigger_than_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(5).size() == 10);
|
||||
BOOST_TEST(RandomPairSelection(1.5).materialise(10).size() == 15);
|
||||
BOOST_TEST(RandomPairSelection(10.0).materialise(10).size() == 100);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_the_number_of_pairs_to_the_nearest_integer)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0.49).materialise(3).size() == 1);
|
||||
BOOST_TEST(RandomPairSelection(0.50).materialise(3).size() == 2);
|
||||
BOOST_TEST(RandomPairSelection(0.51).materialise(3).size() == 2);
|
||||
|
||||
BOOST_TEST(RandomPairSelection(1.51).materialise(3).size() == 5);
|
||||
|
||||
BOOST_TEST(RandomPairSelection(0.01).materialise(2).size() == 0);
|
||||
BOOST_TEST(RandomPairSelection(0.01).materialise(3).size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_is_empty)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_has_one_element)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(1).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(PairMosaicSelectionTest)
|
||||
|
||||
using IndexPairs = vector<tuple<size_t, size_t>>;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise)
|
||||
{
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 0.5).materialise(4) == IndexPairs({{1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(4) == IndexPairs({{1, 1}, {1, 1}, {1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 2.0).materialise(4) == IndexPairs({{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(2) == IndexPairs({{1, 1}, {1, 1}}));
|
||||
|
||||
IndexPairs pairs1{{0, 1}, {1, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 0.5).materialise(4) == IndexPairs({{0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 1.0).materialise(4) == IndexPairs({{0, 1}, {1, 0}, {0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 2.0).materialise(4) == IndexPairs({{0, 1}, {1, 0}, {0, 1}, {1, 0}, {0, 1}, {1, 0}, {0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 1.0).materialise(2) == IndexPairs({{0, 1}, {1, 0}}));
|
||||
|
||||
IndexPairs pairs2{{3, 2}, {2, 3}, {1, 0}, {1, 1}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 0.5).materialise(4) == IndexPairs({{3, 2}, {2, 3}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 1.0).materialise(4) == IndexPairs({{3, 2}, {2, 3}, {1, 0}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 2.0).materialise(4) == IndexPairs({{3, 2}, {2, 3}, {1, 0}, {1, 1}, {3, 2}, {2, 3}, {1, 0}, {1, 1}}));
|
||||
|
||||
IndexPairs pairs3{{1, 0}, {1, 1}, {1, 0}, {1, 1}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs3, 1.0).materialise(2) == IndexPairs({{1, 0}, {1, 1}}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_indices)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.49).materialise(5) == IndexPairs({{4, 4}, {3, 3}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.50).materialise(5) == IndexPairs({{4, 4}, {3, 3}, {2, 2}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.51).materialise(5) == IndexPairs({{4, 4}, {3, 3}, {2, 2}}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_is_empty)
|
||||
{
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(0).empty());
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}, {3, 3}}, 2.0).materialise(0).empty());
|
||||
BOOST_TEST(PairMosaicSelection({{5, 5}, {4, 4}, {3, 3}, {2, 2}}, 0.5).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_has_one_element)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.0).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.5).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 1.0).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 7.0).materialise(1).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_clamp_indices_at_collection_size)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 1.0).materialise(4) == IndexPairs({{3, 3}, {3, 3}, {2, 2}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 2.0).materialise(3) == IndexPairs({{2, 2}, {2, 2}, {2, 2}, {1, 1}, {0, 0}, {2, 2}}));
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
#include <tools/yulPhaser/Selections.h>
|
||||
@@ -31,6 +32,7 @@
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <cmath>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
@@ -226,6 +228,70 @@ BOOST_FIXTURE_TEST_CASE(select_should_return_empty_population_if_selection_is_em
|
||||
BOOST_TEST(population.select(selection).individuals().empty());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(mutate_should_return_population_containing_individuals_indicated_by_selection_with_mutation_applied, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("cc"), Chromosome("gg"), Chromosome("hh")});
|
||||
RangeSelection selection(0.25, 0.75);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{1, 2}));
|
||||
|
||||
Population expectedPopulation(m_fitnessMetric, {Chromosome("fc"), Chromosome("fg")});
|
||||
|
||||
BOOST_TEST(population.mutate(selection, geneSubstitution(0, BlockFlattener::name)) == expectedPopulation);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(mutate_should_include_duplicates_if_selection_contains_duplicates, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("aa")});
|
||||
RangeSelection selection(0.0, 1.0);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{0, 1}));
|
||||
|
||||
BOOST_TEST(
|
||||
population.mutate(selection, geneSubstitution(0, BlockFlattener::name)) ==
|
||||
Population(m_fitnessMetric, {Chromosome("fa"), Chromosome("fa")})
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(mutate_should_return_empty_population_if_selection_is_empty, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("cc")});
|
||||
RangeSelection selection(0.0, 0.0);
|
||||
assert(selection.materialise(population.individuals().size()).empty());
|
||||
|
||||
BOOST_TEST(population.mutate(selection, geneSubstitution(0, BlockFlattener::name)).individuals().empty());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(crossover_should_return_population_containing_individuals_indicated_by_selection_with_crossover_applied, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("cc"), Chromosome("gg"), Chromosome("hh")});
|
||||
PairMosaicSelection selection({{0, 1}, {2, 1}}, 1.0);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<tuple<size_t, size_t>>{{0, 1}, {2, 1}, {0, 1}, {2, 1}}));
|
||||
|
||||
Population expectedPopulation(m_fitnessMetric, {Chromosome("ac"), Chromosome("ac"), Chromosome("gc"), Chromosome("gc")});
|
||||
|
||||
BOOST_TEST(population.crossover(selection, fixedPointCrossover(0.5)) == expectedPopulation);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(crossover_should_include_duplicates_if_selection_contains_duplicates, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("aa")});
|
||||
PairMosaicSelection selection({{0, 0}, {1, 1}}, 2.0);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<tuple<size_t, size_t>>{{0, 0}, {1, 1}, {0, 0}, {1, 1}}));
|
||||
|
||||
BOOST_TEST(
|
||||
population.crossover(selection, fixedPointCrossover(0.5)) ==
|
||||
Population(m_fitnessMetric, {Chromosome("aa"), Chromosome("aa"), Chromosome("aa"), Chromosome("aa")})
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(crossover_should_return_empty_population_if_selection_is_empty, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("aa"), Chromosome("cc")});
|
||||
PairMosaicSelection selection({}, 0.0);
|
||||
assert(selection.materialise(population.individuals().size()).empty());
|
||||
|
||||
BOOST_TEST(population.crossover(selection, fixedPointCrossover(0.5)).individuals().empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user