[yul-phaser] Chromosome: store step abbreviations instead of full names

This commit is contained in:
Kamil Śliwak 2020-09-11 18:53:47 +02:00
parent 2a707e7685
commit 952a9c6115
5 changed files with 48 additions and 66 deletions

View File

@ -63,11 +63,6 @@ vector<string> const ChrOmOsoMeSteps{
BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions")) BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions"))
BOOST_AUTO_TEST_SUITE(ChromosomeTest) BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
{
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps);
}
BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time) BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time)
{ {
SimulationRNG::reset(1); SimulationRNG::reset(1);
@ -95,6 +90,11 @@ BOOST_AUTO_TEST_CASE(makeRandom_should_use_every_possible_step_with_the_same_pro
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
} }
BOOST_AUTO_TEST_CASE(constructor_should_store_genes)
{
BOOST_TEST(Chromosome("ChrOmOsoMe").genes() == "ChrOmOsoMe");
}
BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps) BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
{ {
vector<string> steps = { vector<string> steps = {
@ -102,9 +102,8 @@ BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
BlockFlattener::name, BlockFlattener::name,
UnusedPruner::name, UnusedPruner::name,
}; };
Chromosome chromosome(steps);
BOOST_TEST(steps == chromosome.optimisationSteps()); BOOST_TEST(Chromosome(steps).genes() == "tfu");
} }
BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps) BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps)
@ -116,9 +115,9 @@ BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps)
UnusedPruner::name, UnusedPruner::name,
BlockFlattener::name, BlockFlattener::name,
}; };
Chromosome chromosome(steps);
BOOST_TEST(steps == chromosome.optimisationSteps()); BOOST_TEST(Chromosome(steps).genes() == "ttfuf");
BOOST_TEST(Chromosome("ttfuf").genes() == "ttfuf");
} }
BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_string_representation) BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_string_representation)
@ -133,6 +132,11 @@ BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_strin
BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighTLMNrmVatpud"); BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighTLMNrmVatpud");
} }
BOOST_AUTO_TEST_CASE(optimisationSteps_should_translate_chromosomes_genes_to_optimisation_step_names)
{
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps);
}
BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_probability) BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_probability)
{ {
SimulationRNG::reset(1); SimulationRNG::reset(1);

View File

@ -24,6 +24,7 @@
#include <libsolutil/CommonIO.h> #include <libsolutil/CommonIO.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
@ -117,12 +118,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_before_first_position
Chromosome mutatedChromosome = mutation(chromosome); Chromosome mutatedChromosome = mutation(chromosome);
BOOST_TEST(mutatedChromosome.length() > chromosome.length()); BOOST_TEST(mutatedChromosome.length() > chromosome.length());
BOOST_TEST(boost::ends_with(mutatedChromosome.genes(), chromosome.genes()));
vector<string> suffix(
mutatedChromosome.optimisationSteps().end() - static_cast<ptrdiff_t>(chromosome.length()),
mutatedChromosome.optimisationSteps().end()
);
BOOST_TEST(suffix == chromosome.optimisationSteps());
} }
BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position) BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position)
@ -133,12 +129,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position)
Chromosome mutatedChromosome = mutation(chromosome); Chromosome mutatedChromosome = mutation(chromosome);
BOOST_TEST(mutatedChromosome.length() > chromosome.length()); BOOST_TEST(mutatedChromosome.length() > chromosome.length());
BOOST_TEST(boost::starts_with(mutatedChromosome.genes(), chromosome.genes()));
vector<string> prefix(
mutatedChromosome.optimisationSteps().begin(),
mutatedChromosome.optimisationSteps().begin() + static_cast<ptrdiff_t>(chromosome.length())
);
BOOST_TEST(prefix == chromosome.optimisationSteps());
} }
BOOST_AUTO_TEST_CASE(geneAddition_should_return_identical_chromosome_if_probability_is_zero) BOOST_AUTO_TEST_CASE(geneAddition_should_return_identical_chromosome_if_probability_is_zero)

View File

@ -48,7 +48,7 @@ Chromosome Chromosome::makeRandom(size_t _length)
ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome) ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome)
{ {
return _stream << Chromosome::stepsToGenes(_chromosome.m_optimisationSteps); return _stream << _chromosome.m_genes;
} }
vector<string> Chromosome::allStepNames() vector<string> Chromosome::allStepNames()

View File

@ -42,17 +42,19 @@ class Chromosome
public: public:
Chromosome() = default; Chromosome() = default;
explicit Chromosome(std::vector<std::string> _optimisationSteps): explicit Chromosome(std::vector<std::string> _optimisationSteps):
m_optimisationSteps(std::move(_optimisationSteps)) {} m_genes(stepsToGenes(_optimisationSteps)) {}
explicit Chromosome(std::string const& _genes): explicit Chromosome(std::string _genes):
m_optimisationSteps(genesToSteps(_genes)) {} m_genes(std::move(_genes)) {}
static Chromosome makeRandom(size_t _length); static Chromosome makeRandom(size_t _length);
size_t length() const { return m_optimisationSteps.size(); } size_t length() const { return m_genes.size(); }
std::vector<std::string> const& optimisationSteps() const { return m_optimisationSteps; } std::string const& genes() const { return m_genes; }
std::vector<std::string> optimisationSteps() const { return genesToSteps(m_genes); }
friend std::ostream& operator<<(std::ostream& _stream, Chromosome const& _chromosome); friend std::ostream& operator<<(std::ostream& _stream, Chromosome const& _chromosome);
bool operator==(Chromosome const& _other) const { return m_optimisationSteps == _other.m_optimisationSteps; } bool operator==(Chromosome const& _other) const { return m_genes == _other.m_genes; }
bool operator!=(Chromosome const& _other) const { return !(*this == _other); } bool operator!=(Chromosome const& _other) const { return !(*this == _other); }
static std::string const& randomOptimisationStep(); static std::string const& randomOptimisationStep();
@ -62,7 +64,7 @@ public:
private: private:
static std::vector<std::string> allStepNames(); static std::vector<std::string> allStepNames();
std::vector<std::string> m_optimisationSteps; std::string m_genes;
}; };
} }

View File

@ -120,19 +120,14 @@ ChromosomePair fixedPointSwap(
assert(_crossoverPoint <= _chromosome1.length()); assert(_crossoverPoint <= _chromosome1.length());
assert(_crossoverPoint <= _chromosome2.length()); assert(_crossoverPoint <= _chromosome2.length());
auto begin1 = _chromosome1.optimisationSteps().begin();
auto begin2 = _chromosome2.optimisationSteps().begin();
auto end1 = _chromosome1.optimisationSteps().end();
auto end2 = _chromosome2.optimisationSteps().end();
return { return {
Chromosome( Chromosome(
vector<string>(begin1, begin1 + static_cast<ptrdiff_t>(_crossoverPoint)) + _chromosome1.genes().substr(0, _crossoverPoint) +
vector<string>(begin2 + static_cast<ptrdiff_t>(_crossoverPoint), end2) _chromosome2.genes().substr(_crossoverPoint, _chromosome2.length() - _crossoverPoint)
), ),
Chromosome( Chromosome(
vector<string>(begin2, begin2 + static_cast<ptrdiff_t>(_crossoverPoint)) + _chromosome2.genes().substr(0, _crossoverPoint) +
vector<string>(begin1 + static_cast<ptrdiff_t>(_crossoverPoint), end1) _chromosome1.genes().substr(_crossoverPoint, _chromosome1.length() - _crossoverPoint)
), ),
}; };
} }
@ -197,24 +192,19 @@ ChromosomePair fixedTwoPointSwap(
assert(_crossoverPoint2 <= _chromosome1.length()); assert(_crossoverPoint2 <= _chromosome1.length());
assert(_crossoverPoint2 <= _chromosome2.length()); assert(_crossoverPoint2 <= _chromosome2.length());
auto lowPoint = static_cast<ptrdiff_t>(min(_crossoverPoint1, _crossoverPoint2)); size_t lowPoint = min(_crossoverPoint1, _crossoverPoint2);
auto highPoint = static_cast<ptrdiff_t>(max(_crossoverPoint1, _crossoverPoint2)); size_t highPoint = max(_crossoverPoint1, _crossoverPoint2);
auto begin1 = _chromosome1.optimisationSteps().begin();
auto begin2 = _chromosome2.optimisationSteps().begin();
auto end1 = _chromosome1.optimisationSteps().end();
auto end2 = _chromosome2.optimisationSteps().end();
return { return {
Chromosome( Chromosome(
vector<string>(begin1, begin1 + lowPoint) + _chromosome1.genes().substr(0, lowPoint) +
vector<string>(begin2 + lowPoint, begin2 + highPoint) + _chromosome2.genes().substr(lowPoint, highPoint - lowPoint) +
vector<string>(begin1 + highPoint, end1) _chromosome1.genes().substr(highPoint, _chromosome1.length() - highPoint)
), ),
Chromosome( Chromosome(
vector<string>(begin2, begin2 + lowPoint) + _chromosome2.genes().substr(0, lowPoint) +
vector<string>(begin1 + lowPoint, begin1 + highPoint) + _chromosome1.genes().substr(lowPoint, highPoint - lowPoint) +
vector<string>(begin2 + highPoint, end2) _chromosome2.genes().substr(highPoint, _chromosome2.length() - highPoint)
), ),
}; };
} }
@ -258,42 +248,37 @@ namespace
ChromosomePair uniformSwap(Chromosome const& _chromosome1, Chromosome const& _chromosome2, double _swapChance) ChromosomePair uniformSwap(Chromosome const& _chromosome1, Chromosome const& _chromosome2, double _swapChance)
{ {
vector<string> steps1; string steps1;
vector<string> steps2; string steps2;
size_t minLength = min(_chromosome1.length(), _chromosome2.length()); size_t minLength = min(_chromosome1.length(), _chromosome2.length());
for (size_t i = 0; i < minLength; ++i) for (size_t i = 0; i < minLength; ++i)
if (SimulationRNG::bernoulliTrial(_swapChance)) if (SimulationRNG::bernoulliTrial(_swapChance))
{ {
steps1.push_back(_chromosome2.optimisationSteps()[i]); steps1.push_back(_chromosome2.genes()[i]);
steps2.push_back(_chromosome1.optimisationSteps()[i]); steps2.push_back(_chromosome1.genes()[i]);
} }
else else
{ {
steps1.push_back(_chromosome1.optimisationSteps()[i]); steps1.push_back(_chromosome1.genes()[i]);
steps2.push_back(_chromosome2.optimisationSteps()[i]); steps2.push_back(_chromosome2.genes()[i]);
} }
auto begin1 = _chromosome1.optimisationSteps().begin();
auto begin2 = _chromosome2.optimisationSteps().begin();
auto end1 = _chromosome1.optimisationSteps().end();
auto end2 = _chromosome2.optimisationSteps().end();
bool swapTail = SimulationRNG::bernoulliTrial(_swapChance); bool swapTail = SimulationRNG::bernoulliTrial(_swapChance);
if (_chromosome1.length() > minLength) if (_chromosome1.length() > minLength)
{ {
if (swapTail) if (swapTail)
steps2.insert(steps2.end(), begin1 + static_cast<ptrdiff_t>(minLength), end1); steps2 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
else else
steps1.insert(steps1.end(), begin1 + static_cast<ptrdiff_t>(minLength), end1); steps1 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
} }
if (_chromosome2.length() > minLength) if (_chromosome2.length() > minLength)
{ {
if (swapTail) if (swapTail)
steps1.insert(steps1.end(), begin2 + static_cast<ptrdiff_t>(minLength), end2); steps1 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
else else
steps2.insert(steps2.end(), begin2 + static_cast<ptrdiff_t>(minLength), end2); steps2 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
} }
return {Chromosome(steps1), Chromosome(steps2)}; return {Chromosome(steps1), Chromosome(steps2)};