[yul-phaser] Add RelativeProgramSize metric

This commit is contained in:
Kamil Śliwak
2020-03-18 16:28:15 +01:00
parent 7edbbe4edd
commit 0e03839e62
3 changed files with 95 additions and 0 deletions
+17
View File
@@ -17,6 +17,8 @@
#include <tools/yulPhaser/FitnessMetrics.h>
#include <cmath>
using namespace std;
using namespace solidity::phaser;
@@ -33,3 +35,18 @@ size_t ProgramSize::evaluate(Chromosome const& _chromosome)
{
return optimisedProgram(_chromosome).codeSize();
}
size_t RelativeProgramSize::evaluate(Chromosome const& _chromosome)
{
size_t const scalingFactor = pow(10, m_fixedPointPrecision);
size_t unoptimisedSize = optimisedProgram(Chromosome("")).codeSize();
if (unoptimisedSize == 0)
return scalingFactor;
size_t optimisedSize = optimisedProgram(_chromosome).codeSize();
return static_cast<size_t>(round(
static_cast<double>(optimisedSize) / unoptimisedSize * scalingFactor
));
}
+26
View File
@@ -87,4 +87,30 @@ public:
size_t evaluate(Chromosome const& _chromosome) override;
};
/**
* Fitness metric based on the size of a specific program after applying the optimisations from the
* chromosome to it in relation to the original, unoptimised program.
*
* Since metric values are integers, the class multiplies the ratio by 10^@a _fixedPointPrecision
* before rounding it.
*/
class RelativeProgramSize: public ProgramBasedMetric
{
public:
explicit RelativeProgramSize(
Program _program,
size_t _fixedPointPrecision,
size_t _repetitionCount = 1
):
ProgramBasedMetric(std::move(_program), _repetitionCount),
m_fixedPointPrecision(_fixedPointPrecision) {}
size_t fixedPointPrecision() const { return m_fixedPointPrecision; }
size_t evaluate(Chromosome const& _chromosome) override;
private:
size_t m_fixedPointPrecision;
};
}