Merge pull request #9783 from ethereum/yul-phaser-fix-slow-tests-for-classic-algorithm

[yul-phaser] Fix slow tests for classic genetic algorithm
This commit is contained in:
chriseth 2020-09-14 16:16:59 +02:00 committed by GitHub
commit fb205b719e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 110 additions and 107 deletions

View File

@ -47,12 +47,7 @@ using namespace solidity::util;
namespace solidity::phaser::test namespace solidity::phaser::test
{ {
BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions")) vector<string> const ChrOmOsoMeSteps{
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps)
{
vector<string> expectedSteps{
ConditionalSimplifier::name, ConditionalSimplifier::name,
FunctionHoister::name, FunctionHoister::name,
RedundantAssignEliminator::name, RedundantAssignEliminator::name,
@ -65,8 +60,8 @@ BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_step
ExpressionInliner::name ExpressionInliner::name
}; };
BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps); BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions"))
} BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time) BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time)
{ {
@ -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);
@ -151,6 +155,18 @@ BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_pr
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
} }
BOOST_AUTO_TEST_CASE(stepsToGenes_should_translate_optimisation_step_names_to_abbreviations)
{
BOOST_TEST(Chromosome::stepsToGenes({}) == "");
BOOST_TEST(Chromosome::stepsToGenes(ChrOmOsoMeSteps) == "ChrOmOsoMe");
}
BOOST_AUTO_TEST_CASE(genesToSteps_should_translate_optimisation_step_abbreviations_to_names)
{
BOOST_TEST(Chromosome::genesToSteps("") == vector<string>{});
BOOST_TEST(Chromosome::genesToSteps("ChrOmOsoMe") == ChrOmOsoMeSteps);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -213,8 +213,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(ClassicGeneticAlgorithmTest) BOOST_AUTO_TEST_SUITE(ClassicGeneticAlgorithmTest)
// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_proportional_to_fitness, ClassicGeneticAlgorithmFixture)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_proportional_to_fitness, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled())
{ {
constexpr double relativeTolerance = 0.1; constexpr double relativeTolerance = 0.1;
constexpr size_t populationSize = 1000; constexpr size_t populationSize = 1000;
@ -255,8 +254,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_individuals_with_probability_
BOOST_TEST(abs(meanSquaredError(newFitness, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(newFitness, expectedValue) - variance) < variance * relativeTolerance);
} }
// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_only_individuals_existing_in_the_original_population, ClassicGeneticAlgorithmFixture)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_select_only_individuals_existing_in_the_original_population, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled())
{ {
constexpr size_t populationSize = 1000; constexpr size_t populationSize = 1000;
auto population = Population::makeRandom(m_fitnessMetric, populationSize, 1, 10); auto population = Population::makeRandom(m_fitnessMetric, populationSize, 1, 10);
@ -300,8 +298,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_crossover, ClassicGeneticAlgorith
BOOST_TEST(totalCrossed >= 2); BOOST_TEST(totalCrossed >= 2);
} }
// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithmFixture)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled())
{ {
m_options.mutationChance = 0.6; m_options.mutationChance = 0.6;
ClassicGeneticAlgorithm algorithm(m_options); ClassicGeneticAlgorithm algorithm(m_options);
@ -330,8 +327,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_mutation, ClassicGeneticAlgorithm
BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance);
} }
// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithmFixture)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled())
{ {
m_options.deletionChance = 0.6; m_options.deletionChance = 0.6;
ClassicGeneticAlgorithm algorithm(m_options); ClassicGeneticAlgorithm algorithm(m_options);
@ -360,8 +356,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_deletion, ClassicGeneticAlgorithm
BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance); BOOST_TEST(abs(meanSquaredError(bernoulliTrials, expectedValue) - variance) < variance * relativeTolerance);
} }
// FIXME: This test runs *very* slowly (tens of seconds). Investigate, fix and re-enable. BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_addition, ClassicGeneticAlgorithmFixture)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_do_addition, ClassicGeneticAlgorithmFixture, *boost::unit_test::disabled())
{ {
m_options.additionChance = 0.6; m_options.additionChance = 0.6;
ClassicGeneticAlgorithm algorithm(m_options); ClassicGeneticAlgorithm algorithm(m_options);

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)
@ -218,10 +209,11 @@ BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_i
BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations) BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations)
{ {
Chromosome chromosome("aaaaa"); Chromosome chromosome("aaaaa");
vector<string> steps = Chromosome::genesToSteps("gfc");
function<Mutation> mutation = mutationSequence({ function<Mutation> mutation = mutationSequence({
geneSubstitution(3, Chromosome("g").optimisationSteps()[0]), geneSubstitution(3, steps[0]),
geneSubstitution(2, Chromosome("f").optimisationSteps()[0]), geneSubstitution(2, steps[1]),
geneSubstitution(1, Chromosome("c").optimisationSteps()[0]), geneSubstitution(1, steps[2]),
}); });
BOOST_TEST(mutation(chromosome) == Chromosome("acfga")); BOOST_TEST(mutation(chromosome) == Chromosome("acfga"));
@ -230,11 +222,12 @@ BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations)
BOOST_AUTO_TEST_CASE(mutationSequence_apply_mutations_in_the_order_they_are_given) BOOST_AUTO_TEST_CASE(mutationSequence_apply_mutations_in_the_order_they_are_given)
{ {
Chromosome chromosome("aa"); Chromosome chromosome("aa");
vector<string> steps = Chromosome::genesToSteps("gcfo");
function<Mutation> mutation = mutationSequence({ function<Mutation> mutation = mutationSequence({
geneSubstitution(0, Chromosome("g").optimisationSteps()[0]), geneSubstitution(0, steps[0]),
geneSubstitution(1, Chromosome("c").optimisationSteps()[0]), geneSubstitution(1, steps[1]),
geneSubstitution(0, Chromosome("f").optimisationSteps()[0]), geneSubstitution(0, steps[2]),
geneSubstitution(1, Chromosome("o").optimisationSteps()[0]), geneSubstitution(1, steps[3]),
}); });
BOOST_TEST(mutation(chromosome) == Chromosome("fo")); BOOST_TEST(mutation(chromosome) == Chromosome("fo"));

View File

@ -451,7 +451,7 @@ BOOST_AUTO_TEST_CASE(build_should_apply_prefix)
CharStream nestedSource("{{{let x:= 1}}}", ""); CharStream nestedSource("{{{let x:= 1}}}", "");
Program nestedProgram = get<Program>(Program::load(nestedSource)); Program nestedProgram = get<Program>(Program::load(nestedSource));
Program flatProgram = get<Program>(Program::load(nestedSource)); Program flatProgram = get<Program>(Program::load(nestedSource));
flatProgram.optimise(Chromosome("f").optimisationSteps()); flatProgram.optimise(Chromosome::genesToSteps("f"));
assert(toString(nestedProgram) != toString(flatProgram)); assert(toString(nestedProgram) != toString(flatProgram));
{ {

View File

@ -53,7 +53,7 @@ protected:
Program optimisedProgram(Program _program, string _abbreviatedOptimisationSteps) const Program optimisedProgram(Program _program, string _abbreviatedOptimisationSteps) const
{ {
Program result = move(_program); Program result = move(_program);
result.optimise(Chromosome(_abbreviatedOptimisationSteps).optimisationSteps()); result.optimise(Chromosome::genesToSteps(_abbreviatedOptimisationSteps));
return result; return result;
} }

View File

@ -37,12 +37,6 @@ ostream& operator<<(ostream& _stream, Chromosome const& _chromosome);
} }
Chromosome::Chromosome(string const& _optimisationSteps)
{
for (char abbreviation: _optimisationSteps)
m_optimisationSteps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation));
}
Chromosome Chromosome::makeRandom(size_t _length) Chromosome Chromosome::makeRandom(size_t _length)
{ {
vector<string> steps; vector<string> steps;
@ -54,10 +48,7 @@ Chromosome Chromosome::makeRandom(size_t _length)
ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome) ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome)
{ {
for (auto const& stepName: _chromosome.m_optimisationSteps) return _stream << _chromosome.m_genes;
_stream << OptimiserSuite::stepNameToAbbreviationMap().at(stepName);
return _stream;
} }
vector<string> Chromosome::allStepNames() vector<string> Chromosome::allStepNames()
@ -75,3 +66,21 @@ string const& Chromosome::randomOptimisationStep()
return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)]; return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)];
} }
string Chromosome::stepsToGenes(vector<string> const& _optimisationSteps)
{
string genes;
for (string const& stepName: _optimisationSteps)
genes.push_back(OptimiserSuite::stepNameToAbbreviationMap().at(stepName));
return genes;
}
vector<string> Chromosome::genesToSteps(string const& _genes)
{
vector<string> steps;
for (char abbreviation: _genes)
steps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation));
return steps;
}

View File

@ -42,24 +42,29 @@ 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& _optimisationSteps); explicit Chromosome(std::string _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();
static std::string stepsToGenes(std::vector<std::string> const& _optimisationSteps);
static std::vector<std::string> genesToSteps(std::string const& _genes);
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)};

View File

@ -54,7 +54,7 @@ bool phaser::isFitter(Individual const& a, Individual const& b)
return ( return (
(a.fitness < b.fitness) || (a.fitness < b.fitness) ||
(a.fitness == b.fitness && a.chromosome.length() < b.chromosome.length()) || (a.fitness == b.fitness && a.chromosome.length() < b.chromosome.length()) ||
(a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && toString(a.chromosome) < toString(b.chromosome)) (a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && a.chromosome.genes() < b.chromosome.genes())
); );
} }