mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Customizable chromosome length in makeRandom()
This commit is contained in:
parent
e771f00971
commit
806891f494
@ -15,6 +15,8 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
@ -106,10 +108,59 @@ BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitn
|
||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_generator, PopulationFixture)
|
||||
{
|
||||
size_t chromosomeCount = 30;
|
||||
size_t maxLength = 5;
|
||||
assert(chromosomeCount % maxLength == 0);
|
||||
|
||||
auto nextLength = [counter = 0, maxLength]() mutable { return counter++ % maxLength; };
|
||||
auto population = Population::makeRandom(m_program, chromosomeCount, nextLength);
|
||||
|
||||
// We can't rely on the order since the population sorts its chromosomes immediately but
|
||||
// we can check the number of occurrences of each length.
|
||||
for (size_t length = 0; length < maxLength; ++length)
|
||||
BOOST_TEST(
|
||||
count_if(
|
||||
population.individuals().begin(),
|
||||
population.individuals().end(),
|
||||
[&length](auto const& individual) { return individual.chromosome.length() == length; }
|
||||
) == chromosomeCount / maxLength
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_get_chromosome_lengths_from_specified_range, PopulationFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_program, 100, 5, 10);
|
||||
BOOST_TEST(all_of(
|
||||
population.individuals().begin(),
|
||||
population.individuals().end(),
|
||||
[](auto const& individual){ return 5 <= individual.chromosome.length() && individual.chromosome.length() <= 10; }
|
||||
));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_use_random_chromosome_length, PopulationFixture)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
constexpr int populationSize = 200;
|
||||
constexpr int minLength = 5;
|
||||
constexpr int maxLength = 10;
|
||||
constexpr double relativeTolerance = 0.05;
|
||||
|
||||
auto population = Population::makeRandom(m_program, populationSize, minLength, maxLength);
|
||||
vector<size_t> samples = chromosomeLengths(population);
|
||||
|
||||
const double expectedValue = (maxLength + minLength) / 2.0;
|
||||
const double variance = ((maxLength - minLength + 1) * (maxLength - minLength + 1) - 1) / 12.0;
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes, PopulationFixture)
|
||||
{
|
||||
auto population1 = Population::makeRandom(m_program, 100);
|
||||
auto population2 = Population::makeRandom(m_program, 100);
|
||||
auto population1 = Population::makeRandom(m_program, 100, 30, 30);
|
||||
auto population2 = Population::makeRandom(m_program, 100, 30, 30);
|
||||
|
||||
BOOST_TEST(population1.individuals().size() == 100);
|
||||
BOOST_TEST(population2.individuals().size() == 100);
|
||||
@ -127,7 +178,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromoso
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_program, 5);
|
||||
auto population = Population::makeRandom(m_program, 3, 5, 10);
|
||||
|
||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
}
|
||||
@ -135,7 +186,7 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
|
||||
{
|
||||
stringstream output;
|
||||
auto population = Population::makeRandom(m_program, 5);
|
||||
auto population = Population::makeRandom(m_program, 5, 5, 10);
|
||||
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||
|
||||
population.run(1, output);
|
||||
|
@ -55,15 +55,33 @@ Population::Population(Program _program, vector<Chromosome> const& _chromosomes)
|
||||
m_individuals.push_back({chromosome});
|
||||
}
|
||||
|
||||
Population Population::makeRandom(Program _program, size_t _size)
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
function<size_t()> _chromosomeLengthGenerator
|
||||
)
|
||||
{
|
||||
vector<Individual> individuals;
|
||||
for (size_t i = 0; i < _size; ++i)
|
||||
individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())});
|
||||
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())});
|
||||
|
||||
return Population(move(_program), individuals);
|
||||
}
|
||||
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
)
|
||||
{
|
||||
return makeRandom(
|
||||
move(_program),
|
||||
_size,
|
||||
std::bind(uniformChromosomeLength, _minChromosomeLength, _maxChromosomeLength)
|
||||
);
|
||||
}
|
||||
|
||||
size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program)
|
||||
{
|
||||
Program programCopy = _program;
|
||||
@ -130,6 +148,6 @@ void Population::randomizeWorstChromosomes(
|
||||
auto individual = _individuals.begin() + (_individuals.size() - _count);
|
||||
for (; individual != _individuals.end(); ++individual)
|
||||
{
|
||||
*individual = {Chromosome::makeRandom(randomChromosomeLength())};
|
||||
*individual = {Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength))};
|
||||
}
|
||||
}
|
||||
|
@ -55,13 +55,25 @@ public:
|
||||
static constexpr size_t MaxChromosomeLength = 30;
|
||||
|
||||
explicit Population(Program _program, std::vector<Chromosome> const& _chromosomes = {});
|
||||
static Population makeRandom(Program _program, size_t _size);
|
||||
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
std::function<size_t()> _chromosomeLengthGenerator
|
||||
);
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
);
|
||||
|
||||
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
||||
|
||||
std::vector<Individual> const& individuals() const { return m_individuals; }
|
||||
|
||||
static size_t randomChromosomeLength() { return SimulationRNG::binomialInt(MaxChromosomeLength, 0.5); }
|
||||
static size_t uniformChromosomeLength(size_t _min, size_t _max) { return SimulationRNG::uniformInt(_min, _max); }
|
||||
static size_t binomialChromosomeLength(size_t _max) { return SimulationRNG::binomialInt(_max, 0.5); }
|
||||
static size_t measureFitness(Chromosome const& _chromosome, Program const& _program);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
@ -70,7 +71,11 @@ CharStream loadSource(string const& _sourcePath)
|
||||
void runAlgorithm(string const& _sourcePath)
|
||||
{
|
||||
CharStream sourceCode = loadSource(_sourcePath);
|
||||
auto population = Population::makeRandom(Program::load(sourceCode), 10);
|
||||
auto population = Population::makeRandom(
|
||||
Program::load(sourceCode),
|
||||
10,
|
||||
bind(Population::binomialChromosomeLength, Population::MaxChromosomeLength)
|
||||
);
|
||||
population.run(nullopt, cout);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user