diff --git a/test/yulPhaser/Phaser.cpp b/test/yulPhaser/Phaser.cpp index a99c464da..800301ecf 100644 --- a/test/yulPhaser/Phaser.cpp +++ b/test/yulPhaser/Phaser.cpp @@ -62,6 +62,7 @@ protected: Program m_program = get(Program::load(m_sourceStream)); FitnessMetricFactory::Options m_options = { /* metric = */ MetricChoice::CodeSize, + /* relativeMetricScale = */ 5, /* chromosomeRepetitions = */ 1, }; }; @@ -163,6 +164,18 @@ BOOST_FIXTURE_TEST_CASE(build_should_respect_chromosome_repetitions_option, Fitn BOOST_TEST(programSizeMetric->repetitionCount() == m_options.chromosomeRepetitions); } +BOOST_FIXTURE_TEST_CASE(build_should_set_relative_metric_scale, FitnessMetricFactoryFixture) +{ + m_options.metric = MetricChoice::RelativeCodeSize; + m_options.relativeMetricScale = 10; + unique_ptr metric = FitnessMetricFactory::build(m_options, m_program); + BOOST_REQUIRE(metric != nullptr); + + auto relativeProgramSizeMetric = dynamic_cast(metric.get()); + BOOST_REQUIRE(relativeProgramSizeMetric != nullptr); + BOOST_TEST(relativeProgramSizeMetric->fixedPointPrecision() == m_options.relativeMetricScale); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(PopulationFactoryTest) diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index e8f2cae50..a0a787f7f 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -139,6 +139,7 @@ FitnessMetricFactory::Options FitnessMetricFactory::Options::fromCommandLine(po: { return { _arguments["metric"].as(), + _arguments["relative-metric-scale"].as(), _arguments["chromosome-repetitions"].as(), }; } @@ -158,7 +159,7 @@ unique_ptr FitnessMetricFactory::build( case MetricChoice::RelativeCodeSize: return make_unique( move(_program), - 3, + _options.relativeMetricScale, _options.chromosomeRepetitions ); default: @@ -421,6 +422,18 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription() po::value()->value_name("")->default_value(MetricChoice::CodeSize), "Metric used to evaluate the fitness of a chromosome." ) + ( + "relative-metric-scale", + po::value()->value_name("")->default_value(3), + "Scaling factor for values produced by relative fitness metrics. \n" + "Since all metrics must produce integer values, the fractional part of the result is discarded. " + "To keep the numbers meaningful, a relative metric multiples its values by a scaling factor " + "and this option specifies the exponent of this factor. " + "For example with value of 3 the factor is 10^3 = 1000 and the metric will return " + "500 to represent 0.5, 1000 for 1.0, 2000 for 2.0 and so on. " + "Using a bigger factor allows discerning smaller relative differences between chromosomes " + "but makes the numbers less readable and may also lose precision if the numbers are very large." + ) ( "chromosome-repetitions", po::value()->value_name("")->default_value(1), diff --git a/tools/yulPhaser/Phaser.h b/tools/yulPhaser/Phaser.h index effa2ffe6..07f1eaea1 100644 --- a/tools/yulPhaser/Phaser.h +++ b/tools/yulPhaser/Phaser.h @@ -100,6 +100,7 @@ public: struct Options { MetricChoice metric; + size_t relativeMetricScale; size_t chromosomeRepetitions; static Options fromCommandLine(boost::program_options::variables_map const& _arguments);