[yul-phaser] GeneticAlgorithms: Add methods for selecting a crossover operator

This commit is contained in:
Kamil Śliwak 2020-03-11 03:38:31 +01:00
parent 1ada2a52fb
commit ad89b477c8
2 changed files with 59 additions and 0 deletions

View File

@ -21,8 +21,47 @@
#include <tools/yulPhaser/PairSelections.h>
using namespace std;
using namespace solidity;
using namespace solidity::phaser;
function<Crossover> phaser::buildCrossoverOperator(
CrossoverChoice _choice,
optional<double> _uniformCrossoverSwapChance
)
{
switch (_choice)
{
case CrossoverChoice::SinglePoint:
return randomPointCrossover();
case CrossoverChoice::TwoPoint:
return randomTwoPointCrossover();
case CrossoverChoice::Uniform:
assert(_uniformCrossoverSwapChance.has_value());
return uniformCrossover(_uniformCrossoverSwapChance.value());
default:
assertThrow(false, solidity::util::Exception, "Invalid CrossoverChoice value.");
};
}
function<SymmetricCrossover> phaser::buildSymmetricCrossoverOperator(
CrossoverChoice _choice,
optional<double> _uniformCrossoverSwapChance
)
{
switch (_choice)
{
case CrossoverChoice::SinglePoint:
return symmetricRandomPointCrossover();
case CrossoverChoice::TwoPoint:
return symmetricRandomTwoPointCrossover();
case CrossoverChoice::Uniform:
assert(_uniformCrossoverSwapChance.has_value());
return symmetricUniformCrossover(_uniformCrossoverSwapChance.value());
default:
assertThrow(false, solidity::util::Exception, "Invalid CrossoverChoice value.");
};
}
Population RandomAlgorithm::runNextRound(Population _population)
{
RangeSelection elite(0.0, m_options.elitePoolSize);

View File

@ -20,11 +20,31 @@
#pragma once
#include <tools/yulPhaser/Mutations.h>
#include <tools/yulPhaser/Population.h>
#include <optional>
namespace solidity::phaser
{
enum class CrossoverChoice
{
SinglePoint,
TwoPoint,
Uniform,
};
std::function<Crossover> buildCrossoverOperator(
CrossoverChoice _choice,
std::optional<double> _uniformCrossoverSwapChance
);
std::function<SymmetricCrossover> buildSymmetricCrossoverOperator(
CrossoverChoice _choice,
std::optional<double> _uniformCrossoverSwapChance
);
/**
* Abstract base class for genetic algorithms.
* The main feature is the @a runNextRound() method that executes one round of the algorithm,