mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add FitnessMetricCombination and derived classes for average, sum, min and max
This commit is contained in:
parent
0e03839e62
commit
e4a360947b
@ -78,6 +78,21 @@ protected:
|
||||
Program m_optimisedProgram = optimisedProgram(m_program);
|
||||
};
|
||||
|
||||
class FitnessMetricCombinationFixture: public ProgramBasedMetricFixture
|
||||
{
|
||||
protected:
|
||||
vector<shared_ptr<FitnessMetric>> m_simpleMetrics = {
|
||||
make_shared<ProgramSize>(m_program, 1),
|
||||
make_shared<ProgramSize>(m_program, 2),
|
||||
make_shared<ProgramSize>(m_program, 3),
|
||||
};
|
||||
vector<size_t> m_fitness = {
|
||||
m_simpleMetrics[0]->evaluate(m_chromosome),
|
||||
m_simpleMetrics[1]->evaluate(m_chromosome),
|
||||
m_simpleMetrics[2]->evaluate(m_chromosome),
|
||||
};
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(FitnessMetricsTest)
|
||||
BOOST_AUTO_TEST_SUITE(ProgramBasedMetricTest)
|
||||
@ -173,6 +188,45 @@ BOOST_FIXTURE_TEST_CASE(evaluate_should_multiply_the_result_by_scaling_factor, P
|
||||
BOOST_TEST(RelativeProgramSize(m_program, 4).evaluate(m_chromosome) == round(10000.0 * sizeRatio));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(FitnessMetricCombinationTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(FitnessMetricAverage_evaluate_should_compute_average_of_values_returned_by_metrics_passed_to_it, FitnessMetricCombinationFixture)
|
||||
{
|
||||
FitnessMetricAverage metric(m_simpleMetrics);
|
||||
|
||||
assert(m_simpleMetrics.size() == 3);
|
||||
BOOST_TEST(metric.evaluate(m_chromosome) == (m_fitness[0] + m_fitness[1] + m_fitness[2]) / 3);
|
||||
BOOST_TEST(metric.metrics() == m_simpleMetrics);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(FitnessMetricSum_evaluate_should_compute_sum_of_values_returned_by_metrics_passed_to_it, FitnessMetricCombinationFixture)
|
||||
{
|
||||
FitnessMetricSum metric(m_simpleMetrics);
|
||||
|
||||
assert(m_simpleMetrics.size() == 3);
|
||||
BOOST_TEST(metric.evaluate(m_chromosome) == m_fitness[0] + m_fitness[1] + m_fitness[2]);
|
||||
BOOST_TEST(metric.metrics() == m_simpleMetrics);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(FitnessMetricMaximum_evaluate_should_compute_maximum_of_values_returned_by_metrics_passed_to_it, FitnessMetricCombinationFixture)
|
||||
{
|
||||
FitnessMetricMaximum metric(m_simpleMetrics);
|
||||
|
||||
assert(m_simpleMetrics.size() == 3);
|
||||
BOOST_TEST(metric.evaluate(m_chromosome) == max(m_fitness[0], max(m_fitness[1], m_fitness[2])));
|
||||
BOOST_TEST(metric.metrics() == m_simpleMetrics);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(FitnessMetricMinimum_evaluate_should_compute_minimum_of_values_returned_by_metrics_passed_to_it, FitnessMetricCombinationFixture)
|
||||
{
|
||||
FitnessMetricMinimum metric(m_simpleMetrics);
|
||||
|
||||
assert(m_simpleMetrics.size() == 3);
|
||||
BOOST_TEST(metric.evaluate(m_chromosome) == min(m_fitness[0], min(m_fitness[1], m_fitness[2])));
|
||||
BOOST_TEST(metric.metrics() == m_simpleMetrics);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
@ -50,3 +50,47 @@ size_t RelativeProgramSize::evaluate(Chromosome const& _chromosome)
|
||||
static_cast<double>(optimisedSize) / unoptimisedSize * scalingFactor
|
||||
));
|
||||
}
|
||||
|
||||
size_t FitnessMetricAverage::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
assert(m_metrics.size() > 0);
|
||||
|
||||
size_t total = m_metrics[0]->evaluate(_chromosome);
|
||||
for (size_t i = 1; i < m_metrics.size(); ++i)
|
||||
total += m_metrics[i]->evaluate(_chromosome);
|
||||
|
||||
return total / m_metrics.size();
|
||||
}
|
||||
|
||||
size_t FitnessMetricSum::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
assert(m_metrics.size() > 0);
|
||||
|
||||
size_t total = m_metrics[0]->evaluate(_chromosome);
|
||||
for (size_t i = 1; i < m_metrics.size(); ++i)
|
||||
total += m_metrics[i]->evaluate(_chromosome);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
size_t FitnessMetricMaximum::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
assert(m_metrics.size() > 0);
|
||||
|
||||
size_t maximum = m_metrics[0]->evaluate(_chromosome);
|
||||
for (size_t i = 1; i < m_metrics.size(); ++i)
|
||||
maximum = max(maximum, m_metrics[i]->evaluate(_chromosome));
|
||||
|
||||
return maximum;
|
||||
}
|
||||
|
||||
size_t FitnessMetricMinimum::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
assert(m_metrics.size() > 0);
|
||||
|
||||
size_t minimum = m_metrics[0]->evaluate(_chromosome);
|
||||
for (size_t i = 1; i < m_metrics.size(); ++i)
|
||||
minimum = min(minimum, m_metrics[i]->evaluate(_chromosome));
|
||||
|
||||
return minimum;
|
||||
}
|
||||
|
@ -113,4 +113,60 @@ private:
|
||||
size_t m_fixedPointPrecision;
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract base class for fitness metrics that compute their value based on values of multiple
|
||||
* other, nested metrics.
|
||||
*/
|
||||
class FitnessMetricCombination: public FitnessMetric
|
||||
{
|
||||
public:
|
||||
explicit FitnessMetricCombination(std::vector<std::shared_ptr<FitnessMetric>> _metrics):
|
||||
m_metrics(std::move(_metrics)) {}
|
||||
|
||||
std::vector<std::shared_ptr<FitnessMetric>> const& metrics() const { return m_metrics; }
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<FitnessMetric>> m_metrics;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fitness metric that returns the average of values of its nested metrics.
|
||||
*/
|
||||
class FitnessMetricAverage: public FitnessMetricCombination
|
||||
{
|
||||
public:
|
||||
using FitnessMetricCombination::FitnessMetricCombination;
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fitness metric that returns the sum of values of its nested metrics.
|
||||
*/
|
||||
class FitnessMetricSum: public FitnessMetricCombination
|
||||
{
|
||||
public:
|
||||
using FitnessMetricCombination::FitnessMetricCombination;
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fitness metric that returns the highest of values of its nested metrics.
|
||||
*/
|
||||
class FitnessMetricMaximum: public FitnessMetricCombination
|
||||
{
|
||||
public:
|
||||
using FitnessMetricCombination::FitnessMetricCombination;
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fitness metric that returns the lowest of values of its nested metrics.
|
||||
*/
|
||||
class FitnessMetricMinimum: public FitnessMetricCombination
|
||||
{
|
||||
public:
|
||||
using FitnessMetricCombination::FitnessMetricCombination;
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user