mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Common: Add ChromosomeLengthMetric
This commit is contained in:
parent
2238919c76
commit
751caf0ed3
@ -28,6 +28,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
|
||||
#include <cassert>
|
||||
@ -38,6 +40,18 @@
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
/**
|
||||
* Fitness metric that only takes into account the number of optimisation steps in the chromosome.
|
||||
* Recommended for use in tests because it's much faster than ProgramSize metric and it's very
|
||||
* easy to guess the result at a glance.
|
||||
*/
|
||||
class ChromosomeLengthMetric: public FitnessMetric
|
||||
{
|
||||
public:
|
||||
using FitnessMetric::FitnessMetric;
|
||||
size_t evaluate(Chromosome const& _chromosome) const override { return _chromosome.length(); }
|
||||
};
|
||||
|
||||
// CHROMOSOME AND POPULATION HELPERS
|
||||
|
||||
/// Returns a vector containing lengths of all chromosomes in the population (in the same order).
|
||||
|
@ -19,14 +19,11 @@
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
#include <liblangutil/CharStream.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <set>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::yul;
|
||||
using namespace boost::test_tools;
|
||||
|
||||
@ -36,15 +33,21 @@ namespace solidity::phaser::test
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_length)
|
||||
{
|
||||
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome()) == 0);
|
||||
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("a")) == 1);
|
||||
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("aaaaa")) == 5);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
|
||||
{
|
||||
CharStream sourceStream("{}", "");
|
||||
auto program = Program::load(sourceStream);
|
||||
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||
|
||||
Population population1(program, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
|
||||
Population population1(fitnessMetric, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
|
||||
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
|
||||
|
||||
Population population2(program);
|
||||
Population population2(fitnessMetric);
|
||||
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user