mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add GenerationalElitistWithExclusivePools algorithm
This commit is contained in:
parent
7e80ac861f
commit
fc4fedb214
@ -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()
|
||||
|
@ -16,7 +16,9 @@
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Mutations.h>
|
||||
#include <tools/yulPhaser/Selections.h>
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
@ -48,3 +50,28 @@ void RandomAlgorithm::runNextRound()
|
||||
m_options.maxChromosomeLength
|
||||
);
|
||||
}
|
||||
|
||||
void GenerationalElitistWithExclusivePools::runNextRound()
|
||||
{
|
||||
double elitePoolSize = 1.0 - (m_options.mutationPoolSize + m_options.crossoverPoolSize);
|
||||
RangeSelection elite(0.0, elitePoolSize);
|
||||
|
||||
m_population =
|
||||
m_population.select(elite) +
|
||||
m_population.select(elite).mutate(
|
||||
RandomSelection(m_options.mutationPoolSize / elitePoolSize),
|
||||
alternativeMutations(
|
||||
m_options.randomisationChance,
|
||||
geneRandomisation(m_options.percentGenesToRandomise),
|
||||
alternativeMutations(
|
||||
m_options.deletionVsAdditionChance,
|
||||
geneDeletion(m_options.percentGenesToAddOrDelete),
|
||||
geneAddition(m_options.percentGenesToAddOrDelete)
|
||||
)
|
||||
)
|
||||
) +
|
||||
m_population.select(elite).crossover(
|
||||
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize / 2),
|
||||
randomPointCrossover()
|
||||
);
|
||||
}
|
||||
|
@ -112,4 +112,57 @@ private:
|
||||
Options m_options;
|
||||
};
|
||||
|
||||
/**
|
||||
* A generational, elitist genetic algorithm that replaces the population by mutating and crossing
|
||||
* over chromosomes from the elite.
|
||||
*
|
||||
* The elite consists of individuals not included in the crossover and mutation pools.
|
||||
* The crossover operator used is @a randomPointCrossover. The mutation operator is randomly chosen
|
||||
* from three possibilities: @a geneRandomisation, @a geneDeletion or @a geneAddition (with
|
||||
* configurable probabilities). Each mutation also has a parameter determining the chance of a gene
|
||||
* being affected by it.
|
||||
*/
|
||||
class GenerationalElitistWithExclusivePools: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
struct Options
|
||||
{
|
||||
double mutationPoolSize; ///< Percentage of population to regenerate using mutations in each round.
|
||||
double crossoverPoolSize; ///< Percentage of population to regenerate using crossover in each round.
|
||||
double randomisationChance; ///< The chance of choosing @a geneRandomisation as the mutation to perform
|
||||
double deletionVsAdditionChance; ///< The chance of choosing @a geneDeletion as the mutation if randomisation was not chosen.
|
||||
double percentGenesToRandomise; ///< The chance of any given gene being mutated in gene randomisation.
|
||||
double percentGenesToAddOrDelete; ///< The chance of a gene being added (or deleted) in gene addition (or deletion).
|
||||
|
||||
bool isValid() const
|
||||
{
|
||||
return (
|
||||
0 <= mutationPoolSize && mutationPoolSize <= 1.0 &&
|
||||
0 <= crossoverPoolSize && crossoverPoolSize <= 1.0 &&
|
||||
0 <= randomisationChance && randomisationChance <= 1.0 &&
|
||||
0 <= deletionVsAdditionChance && deletionVsAdditionChance <= 1.0 &&
|
||||
0 <= percentGenesToRandomise && percentGenesToRandomise <= 1.0 &&
|
||||
0 <= percentGenesToAddOrDelete && percentGenesToAddOrDelete <= 1.0 &&
|
||||
mutationPoolSize + crossoverPoolSize <= 1.0
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
GenerationalElitistWithExclusivePools(
|
||||
Population _initialPopulation,
|
||||
std::ostream& _outputStream,
|
||||
Options const& _options
|
||||
):
|
||||
GeneticAlgorithm(_initialPopulation, _outputStream),
|
||||
m_options(_options)
|
||||
{
|
||||
assert(_options.isValid());
|
||||
}
|
||||
|
||||
void runNextRound() override;
|
||||
|
||||
private:
|
||||
Options m_options;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user