[yul-phaser] Add --mode option

This commit is contained in:
Kamil Śliwak 2020-02-29 00:56:47 +01:00
parent 47c3b558f2
commit 1272a9335c
2 changed files with 43 additions and 6 deletions

View File

@ -46,6 +46,12 @@ namespace po = boost::program_options;
namespace namespace
{ {
map<PhaserMode, string> const PhaserModeToStringMap =
{
{PhaserMode::RunAlgorithm, "run-algorithm"},
};
map<string, PhaserMode> const StringToPhaserModeMap = invertMap(PhaserModeToStringMap);
map<Algorithm, string> const AlgorithmToStringMap = map<Algorithm, string> const AlgorithmToStringMap =
{ {
{Algorithm::Random, "random"}, {Algorithm::Random, "random"},
@ -71,6 +77,8 @@ map<string, MetricAggregatorChoice> const StringToMetricAggregatorChoiceMap = in
} }
istream& phaser::operator>>(istream& _inputStream, PhaserMode& _phaserMode) { return deserializeChoice(_inputStream, _phaserMode, StringToPhaserModeMap); }
ostream& phaser::operator<<(ostream& _outputStream, PhaserMode _phaserMode) { return serializeChoice(_outputStream, _phaserMode, PhaserModeToStringMap); }
istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); } istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); }
ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); } ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); }
istream& phaser::operator>>(istream& _inputStream, MetricChoice& _metric) { return deserializeChoice(_inputStream, _metric, StringToMetricChoiceMap); } istream& phaser::operator>>(istream& _inputStream, MetricChoice& _metric) { return deserializeChoice(_inputStream, _metric, StringToMetricChoiceMap); }
@ -348,7 +356,7 @@ void Phaser::main(int _argc, char** _argv)
initialiseRNG(arguments.value()); initialiseRNG(arguments.value());
runAlgorithm(arguments.value()); runPhaser(arguments.value());
} }
Phaser::CommandLineDescription Phaser::buildCommandLineDescription() Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
@ -392,6 +400,12 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
po::value<size_t>()->value_name("<NUM>"), po::value<size_t>()->value_name("<NUM>"),
"The number of rounds after which the algorithm should stop. (default=no limit)." "The number of rounds after which the algorithm should stop. (default=no limit)."
) )
(
"mode",
po::value<PhaserMode>()->value_name("<NAME>")->default_value(PhaserMode::RunAlgorithm),
"Mode of operation. The default is to run the algorithm but you can also tell phaser "
"to do something else with its parameters, e.g. just print the optimised programs and exit."
)
; ;
keywordDescription.add(generalDescription); keywordDescription.add(generalDescription);
@ -618,13 +632,12 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c
}; };
} }
void Phaser::runAlgorithm(po::variables_map const& _arguments) void Phaser::runPhaser(po::variables_map const& _arguments)
{ {
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments); auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
auto cacheOptions = ProgramCacheFactory::Options::fromCommandLine(_arguments); auto cacheOptions = ProgramCacheFactory::Options::fromCommandLine(_arguments);
auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments); auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments);
auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments); auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments);
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
vector<Program> programs = ProgramFactory::build(programOptions); vector<Program> programs = ProgramFactory::build(programOptions);
vector<shared_ptr<ProgramCache>> programCaches = ProgramCacheFactory::build(cacheOptions, programs); vector<shared_ptr<ProgramCache>> programCaches = ProgramCacheFactory::build(cacheOptions, programs);
@ -632,11 +645,23 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments)
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(programs), programCaches); unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(programs), programCaches);
Population population = PopulationFactory::build(populationOptions, move(fitnessMetric)); Population population = PopulationFactory::build(populationOptions, move(fitnessMetric));
if (_arguments["mode"].as<PhaserMode>() == PhaserMode::RunAlgorithm)
runAlgorithm(_arguments, move(population), move(programCaches));
}
void Phaser::runAlgorithm(
po::variables_map const& _arguments,
Population _population,
vector<shared_ptr<ProgramCache>> _programCaches
)
{
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build( unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
algorithmOptions, algorithmOptions,
population.individuals().size() _population.individuals().size()
); );
AlgorithmRunner algorithmRunner(population, move(programCaches), buildAlgorithmRunnerOptions(_arguments), cout); AlgorithmRunner algorithmRunner(move(_population), move(_programCaches), buildAlgorithmRunnerOptions(_arguments), cout);
algorithmRunner.run(*geneticAlgorithm); algorithmRunner.run(*geneticAlgorithm);
} }

View File

@ -47,6 +47,11 @@ class Population;
class Program; class Program;
class ProgramCache; class ProgramCache;
enum class PhaserMode
{
RunAlgorithm,
};
enum class Algorithm enum class Algorithm
{ {
Random, Random,
@ -67,6 +72,8 @@ enum class MetricAggregatorChoice
Minimum, Minimum,
}; };
std::istream& operator>>(std::istream& _inputStream, solidity::phaser::PhaserMode& _phaserMode);
std::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::PhaserMode _phaserMode);
std::istream& operator>>(std::istream& _inputStream, solidity::phaser::Algorithm& _algorithm); std::istream& operator>>(std::istream& _inputStream, solidity::phaser::Algorithm& _algorithm);
std::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::Algorithm _algorithm); std::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::Algorithm _algorithm);
std::istream& operator>>(std::istream& _inputStream, solidity::phaser::MetricChoice& _metric); std::istream& operator>>(std::istream& _inputStream, solidity::phaser::MetricChoice& _metric);
@ -223,7 +230,12 @@ private:
static void initialiseRNG(boost::program_options::variables_map const& _arguments); static void initialiseRNG(boost::program_options::variables_map const& _arguments);
static AlgorithmRunner::Options buildAlgorithmRunnerOptions(boost::program_options::variables_map const& _arguments); static AlgorithmRunner::Options buildAlgorithmRunnerOptions(boost::program_options::variables_map const& _arguments);
static void runAlgorithm(boost::program_options::variables_map const& _arguments); static void runPhaser(boost::program_options::variables_map const& _arguments);
static void runAlgorithm(
boost::program_options::variables_map const& _arguments,
Population _population,
std::vector<std::shared_ptr<ProgramCache>> _programCaches
);
}; };
} }