mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add --min-chromosome-length and --max-chromosome-length options
This commit is contained in:
parent
3f7ada1689
commit
55ea92dbec
@ -62,14 +62,14 @@ GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLi
|
|||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
_arguments["algorithm"].as<Algorithm>(),
|
_arguments["algorithm"].as<Algorithm>(),
|
||||||
|
_arguments["min-chromosome-length"].as<size_t>(),
|
||||||
|
_arguments["max-chromosome-length"].as<size_t>(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
||||||
Options const& _options,
|
Options const& _options,
|
||||||
size_t _populationSize,
|
size_t _populationSize
|
||||||
size_t _minChromosomeLength,
|
|
||||||
size_t _maxChromosomeLength
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
assert(_populationSize > 0);
|
assert(_populationSize > 0);
|
||||||
@ -79,8 +79,8 @@ unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
|||||||
case Algorithm::Random:
|
case Algorithm::Random:
|
||||||
return make_unique<RandomAlgorithm>(RandomAlgorithm::Options{
|
return make_unique<RandomAlgorithm>(RandomAlgorithm::Options{
|
||||||
/* elitePoolSize = */ 1.0 / _populationSize,
|
/* elitePoolSize = */ 1.0 / _populationSize,
|
||||||
/* minChromosomeLength = */ _minChromosomeLength,
|
/* minChromosomeLength = */ _options.minChromosomeLength,
|
||||||
/* maxChromosomeLength = */ _maxChromosomeLength,
|
/* maxChromosomeLength = */ _options.maxChromosomeLength,
|
||||||
});
|
});
|
||||||
case Algorithm::GEWEP:
|
case Algorithm::GEWEP:
|
||||||
return make_unique<GenerationalElitistWithExclusivePools>(GenerationalElitistWithExclusivePools::Options{
|
return make_unique<GenerationalElitistWithExclusivePools>(GenerationalElitistWithExclusivePools::Options{
|
||||||
@ -88,8 +88,8 @@ unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
|||||||
/* crossoverPoolSize = */ 0.25,
|
/* crossoverPoolSize = */ 0.25,
|
||||||
/* randomisationChance = */ 0.9,
|
/* randomisationChance = */ 0.9,
|
||||||
/* deletionVsAdditionChance = */ 0.5,
|
/* deletionVsAdditionChance = */ 0.5,
|
||||||
/* percentGenesToRandomise = */ 1.0 / _maxChromosomeLength,
|
/* percentGenesToRandomise = */ 1.0 / _options.maxChromosomeLength,
|
||||||
/* percentGenesToAddOrDelete = */ 1.0 / _maxChromosomeLength,
|
/* percentGenesToAddOrDelete = */ 1.0 / _options.maxChromosomeLength,
|
||||||
});
|
});
|
||||||
default:
|
default:
|
||||||
assertThrow(false, solidity::util::Exception, "Invalid Algorithm value.");
|
assertThrow(false, solidity::util::Exception, "Invalid Algorithm value.");
|
||||||
@ -114,6 +114,8 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
|||||||
PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
|
_arguments["min-chromosome-length"].as<size_t>(),
|
||||||
|
_arguments["max-chromosome-length"].as<size_t>(),
|
||||||
_arguments.count("population") > 0 ?
|
_arguments.count("population") > 0 ?
|
||||||
_arguments["population"].as<vector<string>>() :
|
_arguments["population"].as<vector<string>>() :
|
||||||
vector<string>{},
|
vector<string>{},
|
||||||
@ -139,6 +141,8 @@ Population PopulationFactory::build(
|
|||||||
|
|
||||||
population = move(population) + buildRandom(
|
population = move(population) + buildRandom(
|
||||||
combinedSize,
|
combinedSize,
|
||||||
|
_options.minChromosomeLength,
|
||||||
|
_options.maxChromosomeLength,
|
||||||
_fitnessMetric
|
_fitnessMetric
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -162,14 +166,16 @@ Population PopulationFactory::buildFromStrings(
|
|||||||
|
|
||||||
Population PopulationFactory::buildRandom(
|
Population PopulationFactory::buildRandom(
|
||||||
size_t _populationSize,
|
size_t _populationSize,
|
||||||
|
size_t _minChromosomeLength,
|
||||||
|
size_t _maxChromosomeLength,
|
||||||
shared_ptr<FitnessMetric> _fitnessMetric
|
shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return Population::makeRandom(
|
return Population::makeRandom(
|
||||||
move(_fitnessMetric),
|
move(_fitnessMetric),
|
||||||
_populationSize,
|
_populationSize,
|
||||||
MinChromosomeLength,
|
_minChromosomeLength,
|
||||||
MaxChromosomeLength
|
_maxChromosomeLength
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,6 +264,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
|||||||
po::value<Algorithm>()->value_name("<NAME>")->default_value(Algorithm::GEWEP),
|
po::value<Algorithm>()->value_name("<NAME>")->default_value(Algorithm::GEWEP),
|
||||||
"Algorithm"
|
"Algorithm"
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
"min-chromosome-length",
|
||||||
|
po::value<size_t>()->value_name("<NUM>")->default_value(12),
|
||||||
|
"Minimum length of randomly generated chromosomes."
|
||||||
|
)
|
||||||
|
(
|
||||||
|
"max-chromosome-length",
|
||||||
|
po::value<size_t>()->value_name("<NUM>")->default_value(30),
|
||||||
|
"Maximum length of randomly generated chromosomes."
|
||||||
|
)
|
||||||
;
|
;
|
||||||
keywordDescription.add(algorithmDescription);
|
keywordDescription.add(algorithmDescription);
|
||||||
|
|
||||||
@ -360,9 +376,7 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
|||||||
|
|
||||||
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
||||||
algorithmOptions,
|
algorithmOptions,
|
||||||
population.individuals().size(),
|
population.individuals().size()
|
||||||
PopulationFactory::MinChromosomeLength,
|
|
||||||
PopulationFactory::MaxChromosomeLength
|
|
||||||
);
|
);
|
||||||
|
|
||||||
AlgorithmRunner algorithmRunner(population, buildAlgorithmRunnerOptions(_arguments), cout);
|
AlgorithmRunner algorithmRunner(population, buildAlgorithmRunnerOptions(_arguments), cout);
|
||||||
|
@ -64,15 +64,15 @@ public:
|
|||||||
struct Options
|
struct Options
|
||||||
{
|
{
|
||||||
Algorithm algorithm;
|
Algorithm algorithm;
|
||||||
|
size_t minChromosomeLength;
|
||||||
|
size_t maxChromosomeLength;
|
||||||
|
|
||||||
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::unique_ptr<GeneticAlgorithm> build(
|
static std::unique_ptr<GeneticAlgorithm> build(
|
||||||
Options const& _options,
|
Options const& _options,
|
||||||
size_t _populationSize,
|
size_t _populationSize
|
||||||
size_t _minChromosomeLength,
|
|
||||||
size_t _maxChromosomeLength
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -101,11 +101,10 @@ public:
|
|||||||
class PopulationFactory
|
class PopulationFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr size_t MinChromosomeLength = 12;
|
|
||||||
static constexpr size_t MaxChromosomeLength = 30;
|
|
||||||
|
|
||||||
struct Options
|
struct Options
|
||||||
{
|
{
|
||||||
|
size_t minChromosomeLength;
|
||||||
|
size_t maxChromosomeLength;
|
||||||
std::vector<std::string> population;
|
std::vector<std::string> population;
|
||||||
std::vector<size_t> randomPopulation;
|
std::vector<size_t> randomPopulation;
|
||||||
std::vector<std::string> populationFromFile;
|
std::vector<std::string> populationFromFile;
|
||||||
@ -123,6 +122,8 @@ public:
|
|||||||
);
|
);
|
||||||
static Population buildRandom(
|
static Population buildRandom(
|
||||||
size_t _populationSize,
|
size_t _populationSize,
|
||||||
|
size_t _minChromosomeLength,
|
||||||
|
size_t _maxChromosomeLength,
|
||||||
std::shared_ptr<FitnessMetric> _fitnessMetric
|
std::shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
);
|
);
|
||||||
static Population buildFromFile(
|
static Population buildFromFile(
|
||||||
|
Loading…
Reference in New Issue
Block a user