diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index 3d45db1e8..768088e49 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -111,13 +111,43 @@ unique_ptr FitnessMetricFactory::build( return make_unique(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{}, + }; +} + Population PopulationFactory::build( + Options const& _options, + shared_ptr _fitnessMetric +) +{ + Population population(_fitnessMetric, vector{}); + + 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 ) { return Population::makeRandom( move(_fitnessMetric), - PopulationSize, + _populationSize, MinChromosomeLength, MaxChromosomeLength ); @@ -203,6 +233,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription() ; keywordDescription.add(algorithmDescription); + po::options_description populationDescription("POPULATION", lineLength, minDescriptionLength); + populationDescription.add_options() + ( + "random-population", + po::value>()->value_name(""), + "The number of randomly generated chromosomes to be included in the initial population." + ) + ; + keywordDescription.add(populationDescription); + po::options_description metricsDescription("METRICS", lineLength, minDescriptionLength); metricsDescription.add_options() ( @@ -265,11 +305,12 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments) { auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments); auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments); + auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments); auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments); Program program = ProgramFactory::build(programOptions); unique_ptr fitnessMetric = FitnessMetricFactory::build(metricOptions, move(program)); - Population population = PopulationFactory::build(move(fitnessMetric)); + Population population = PopulationFactory::build(populationOptions, move(fitnessMetric)); unique_ptr geneticAlgorithm = GeneticAlgorithmFactory::build( algorithmOptions, diff --git a/tools/yulPhaser/Phaser.h b/tools/yulPhaser/Phaser.h index ac6f70def..4df0109f7 100644 --- a/tools/yulPhaser/Phaser.h +++ b/tools/yulPhaser/Phaser.h @@ -101,11 +101,22 @@ public: class PopulationFactory { public: - static constexpr size_t PopulationSize = 20; static constexpr size_t MinChromosomeLength = 12; static constexpr size_t MaxChromosomeLength = 30; + struct Options + { + std::vector randomPopulation; + + static Options fromCommandLine(boost::program_options::variables_map const& _arguments); + }; + static Population build( + Options const& _options, + std::shared_ptr _fitnessMetric + ); + static Population buildRandom( + size_t _populationSize, std::shared_ptr _fitnessMetric ); };