mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add --random-population option
This commit is contained in:
parent
d8e5f8f965
commit
af090876b5
@ -111,13 +111,43 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
|||||||
return make_unique<ProgramSize>(move(_program), _options.chromosomeRepetitions);
|
return make_unique<ProgramSize>(move(_program), _options.chromosomeRepetitions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
_arguments.count("random-population") > 0 ?
|
||||||
|
_arguments["random-population"].as<vector<size_t>>() :
|
||||||
|
vector<size_t>{},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Population PopulationFactory::build(
|
Population PopulationFactory::build(
|
||||||
|
Options const& _options,
|
||||||
|
shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
|
)
|
||||||
|
{
|
||||||
|
Population population(_fitnessMetric, vector<Chromosome>{});
|
||||||
|
|
||||||
|
size_t combinedSize = 0;
|
||||||
|
for (size_t populationSize: _options.randomPopulation)
|
||||||
|
combinedSize += populationSize;
|
||||||
|
|
||||||
|
population = move(population) + buildRandom(
|
||||||
|
combinedSize,
|
||||||
|
_fitnessMetric
|
||||||
|
);
|
||||||
|
|
||||||
|
return population;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Population PopulationFactory::buildRandom(
|
||||||
|
size_t _populationSize,
|
||||||
shared_ptr<FitnessMetric> _fitnessMetric
|
shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return Population::makeRandom(
|
return Population::makeRandom(
|
||||||
move(_fitnessMetric),
|
move(_fitnessMetric),
|
||||||
PopulationSize,
|
_populationSize,
|
||||||
MinChromosomeLength,
|
MinChromosomeLength,
|
||||||
MaxChromosomeLength
|
MaxChromosomeLength
|
||||||
);
|
);
|
||||||
@ -203,6 +233,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
|||||||
;
|
;
|
||||||
keywordDescription.add(algorithmDescription);
|
keywordDescription.add(algorithmDescription);
|
||||||
|
|
||||||
|
po::options_description populationDescription("POPULATION", lineLength, minDescriptionLength);
|
||||||
|
populationDescription.add_options()
|
||||||
|
(
|
||||||
|
"random-population",
|
||||||
|
po::value<vector<size_t>>()->value_name("<SIZE>"),
|
||||||
|
"The number of randomly generated chromosomes to be included in the initial population."
|
||||||
|
)
|
||||||
|
;
|
||||||
|
keywordDescription.add(populationDescription);
|
||||||
|
|
||||||
po::options_description metricsDescription("METRICS", lineLength, minDescriptionLength);
|
po::options_description metricsDescription("METRICS", lineLength, minDescriptionLength);
|
||||||
metricsDescription.add_options()
|
metricsDescription.add_options()
|
||||||
(
|
(
|
||||||
@ -265,11 +305,12 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
|||||||
{
|
{
|
||||||
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
||||||
auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments);
|
auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments);
|
||||||
|
auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments);
|
||||||
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
||||||
|
|
||||||
Program program = ProgramFactory::build(programOptions);
|
Program program = ProgramFactory::build(programOptions);
|
||||||
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(program));
|
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(program));
|
||||||
Population population = PopulationFactory::build(move(fitnessMetric));
|
Population population = PopulationFactory::build(populationOptions, move(fitnessMetric));
|
||||||
|
|
||||||
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
||||||
algorithmOptions,
|
algorithmOptions,
|
||||||
|
@ -101,11 +101,22 @@ public:
|
|||||||
class PopulationFactory
|
class PopulationFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr size_t PopulationSize = 20;
|
|
||||||
static constexpr size_t MinChromosomeLength = 12;
|
static constexpr size_t MinChromosomeLength = 12;
|
||||||
static constexpr size_t MaxChromosomeLength = 30;
|
static constexpr size_t MaxChromosomeLength = 30;
|
||||||
|
|
||||||
|
struct Options
|
||||||
|
{
|
||||||
|
std::vector<size_t> randomPopulation;
|
||||||
|
|
||||||
|
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
||||||
|
};
|
||||||
|
|
||||||
static Population build(
|
static Population build(
|
||||||
|
Options const& _options,
|
||||||
|
std::shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
|
);
|
||||||
|
static Population buildRandom(
|
||||||
|
size_t _populationSize,
|
||||||
std::shared_ptr<FitnessMetric> _fitnessMetric
|
std::shared_ptr<FitnessMetric> _fitnessMetric
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user