[yul-phaser] Change the design of crossover operators so that they produce a single chromosome rather than a pair

This commit is contained in:
Kamil Śliwak
2020-03-09 13:21:48 +01:00
parent a3e97108c5
commit 763bdb1d51
6 changed files with 74 additions and 65 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ void GenerationalElitistWithExclusivePools::runNextRound()
)
) +
m_population.select(elite).crossover(
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize / 2),
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize),
randomPointCrossover()
);
}
+4 -10
View File
@@ -98,7 +98,7 @@ function<Mutation> phaser::alternativeMutations(
namespace
{
ChromosomePair buildChromosomesBySwappingParts(
Chromosome buildChromosomesBySwappingParts(
Chromosome const& _chromosome1,
Chromosome const& _chromosome2,
size_t _crossoverPoint
@@ -110,15 +110,9 @@ ChromosomePair buildChromosomesBySwappingParts(
auto begin1 = _chromosome1.optimisationSteps().begin();
auto begin2 = _chromosome2.optimisationSteps().begin();
return ChromosomePair(
Chromosome(
vector<string>(begin1, begin1 + _crossoverPoint) +
vector<string>(begin2 + _crossoverPoint, _chromosome2.optimisationSteps().end())
),
Chromosome(
vector<string>(begin2, begin2 + _crossoverPoint) +
vector<string>(begin1 + _crossoverPoint, _chromosome1.optimisationSteps().end())
)
return Chromosome(
vector<string>(begin1, begin1 + _crossoverPoint) +
vector<string>(begin2 + _crossoverPoint, _chromosome2.optimisationSteps().end())
);
}
+4 -7
View File
@@ -28,10 +28,8 @@
namespace solidity::phaser
{
using ChromosomePair = std::tuple<Chromosome, Chromosome>;
using Mutation = Chromosome(Chromosome const&);
using Crossover = ChromosomePair(Chromosome const&, Chromosome const&);
using Crossover = Chromosome(Chromosome const&, Chromosome const&);
// MUTATIONS
@@ -64,10 +62,9 @@ std::function<Mutation> alternativeMutations(
std::function<Crossover> randomPointCrossover();
/// Creates a crossover operator that always chooses a point that lies at @a _crossoverPoint
/// percent of the length of the shorter chromosome. Then creates a pair of chromosomes by
/// splitting both inputs at the crossover point and stitching the resulting parts. The first
/// output is created from the first half or first input and the second half of the second input
/// The second output from the remaining two halves.
/// percent of the length of the shorter chromosome. Then creates a new chromosome by
/// splitting both inputs at the crossover point and stitching output from the first half or first
/// input and the second half of the second input.
///
/// Avoids selecting position 0 (since this just produces a chromosome identical to the second one)
/// unless there is no other choice (i.e. one of the chromosomes is empty).
+2 -3
View File
@@ -108,12 +108,11 @@ Population Population::crossover(PairSelection const& _selection, function<Cross
vector<Individual> crossedIndividuals;
for (auto const& [i, j]: _selection.materialise(m_individuals.size()))
{
auto [childChromosome1, childChromosome2] = _crossover(
auto childChromosome = _crossover(
m_individuals[i].chromosome,
m_individuals[j].chromosome
);
crossedIndividuals.emplace_back(move(childChromosome1), *m_fitnessMetric);
crossedIndividuals.emplace_back(move(childChromosome2), *m_fitnessMetric);
crossedIndividuals.emplace_back(move(childChromosome), *m_fitnessMetric);
}
return Population(m_fitnessMetric, crossedIndividuals);