[yul-phaser] Add --metric option

This commit is contained in:
Kamil Śliwak 2020-02-25 18:56:07 +01:00
parent e4a360947b
commit 8e64c5c6f0
3 changed files with 46 additions and 4 deletions

View File

@ -61,6 +61,7 @@ protected:
CharStream m_sourceStream = CharStream("{}", "");
Program m_program = get<Program>(Program::load(m_sourceStream));
FitnessMetricFactory::Options m_options = {
/* metric = */ MetricChoice::CodeSize,
/* chromosomeRepetitions = */ 1,
};
};
@ -141,16 +142,18 @@ BOOST_AUTO_TEST_SUITE(FitnessMetricFactoryTest)
BOOST_FIXTURE_TEST_CASE(build_should_create_metric_of_the_right_type, FitnessMetricFactoryFixture)
{
m_options.metric = MetricChoice::RelativeCodeSize;
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, m_program);
BOOST_REQUIRE(metric != nullptr);
auto programSizeMetric = dynamic_cast<ProgramSize*>(metric.get());
BOOST_REQUIRE(programSizeMetric != nullptr);
BOOST_TEST(toString(programSizeMetric->program()) == toString(m_program));
auto relativeProgramSizeMetric = dynamic_cast<RelativeProgramSize*>(metric.get());
BOOST_REQUIRE(relativeProgramSizeMetric != nullptr);
BOOST_TEST(toString(relativeProgramSizeMetric->program()) == toString(m_program));
}
BOOST_FIXTURE_TEST_CASE(build_should_respect_chromosome_repetitions_option, FitnessMetricFactoryFixture)
{
m_options.metric = MetricChoice::CodeSize;
m_options.chromosomeRepetitions = 5;
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, m_program);
BOOST_REQUIRE(metric != nullptr);

View File

@ -53,10 +53,19 @@ map<Algorithm, string> const AlgorithmToStringMap =
};
map<string, Algorithm> const StringToAlgorithmMap = invertMap(AlgorithmToStringMap);
map<MetricChoice, string> MetricChoiceToStringMap =
{
{MetricChoice::CodeSize, "code-size"},
{MetricChoice::RelativeCodeSize, "relative-code-size"},
};
map<string, MetricChoice> const StringToMetricChoiceMap = invertMap(MetricChoiceToStringMap);
}
istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); }
ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); }
istream& phaser::operator>>(istream& _inputStream, MetricChoice& _metric) { return deserializeChoice(_inputStream, _metric, StringToMetricChoiceMap); }
ostream& phaser::operator<<(ostream& _outputStream, MetricChoice _metric) { return serializeChoice(_outputStream, _metric, MetricChoiceToStringMap); }
GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLine(po::variables_map const& _arguments)
{
@ -129,6 +138,7 @@ unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
FitnessMetricFactory::Options FitnessMetricFactory::Options::fromCommandLine(po::variables_map const& _arguments)
{
return {
_arguments["metric"].as<MetricChoice>(),
_arguments["chromosome-repetitions"].as<size_t>(),
};
}
@ -138,7 +148,22 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
Program _program
)
{
return make_unique<ProgramSize>(move(_program), _options.chromosomeRepetitions);
switch (_options.metric)
{
case MetricChoice::CodeSize:
return make_unique<ProgramSize>(
move(_program),
_options.chromosomeRepetitions
);
case MetricChoice::RelativeCodeSize:
return make_unique<RelativeProgramSize>(
move(_program),
3,
_options.chromosomeRepetitions
);
default:
assertThrow(false, solidity::util::Exception, "Invalid MetricChoice value.");
}
}
PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)
@ -391,6 +416,11 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
po::options_description metricsDescription("METRICS", lineLength, minDescriptionLength);
metricsDescription.add_options()
(
"metric",
po::value<MetricChoice>()->value_name("<NAME>")->default_value(MetricChoice::CodeSize),
"Metric used to evaluate the fitness of a chromosome."
)
(
"chromosome-repetitions",
po::value<size_t>()->value_name("<COUNT>")->default_value(1),

View File

@ -52,8 +52,16 @@ enum class Algorithm
GEWEP,
};
enum class MetricChoice
{
CodeSize,
RelativeCodeSize,
};
std::istream& operator>>(std::istream& _inputStream, 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::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::MetricChoice _metric);
/**
* Builds and validates instances of @a GeneticAlgorithm and its derived classes.
@ -91,6 +99,7 @@ class FitnessMetricFactory
public:
struct Options
{
MetricChoice metric;
size_t chromosomeRepetitions;
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);