[yul-phaser] Add GenerationalElitistWithExclusivePools algorithm

This commit is contained in:
Kamil Śliwak
2020-03-09 13:21:48 +01:00
parent 7e80ac861f
commit fc4fedb214
3 changed files with 182 additions and 0 deletions
+27
View File
@@ -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()
);
}
+53
View File
@@ -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;
};
}