diff --git a/test/yulPhaser/Population.cpp b/test/yulPhaser/Population.cpp index d0ee5a0c7..d2c11a886 100644 --- a/test/yulPhaser/Population.cpp +++ b/test/yulPhaser/Population.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -85,7 +86,7 @@ BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness Chromosome::makeRandom(5), Chromosome::makeRandom(10), }; - Population population(sourceStream, chromosomes); + Population population(Program::load(sourceStream), chromosomes); BOOST_TEST(population.individuals().size() == 2); BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]); @@ -98,8 +99,9 @@ BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes) { CharStream sourceStream(sampleSourceCode, current_test_case().p_name); - auto population1 = Population::makeRandom(sourceStream, 100); - auto population2 = Population::makeRandom(sourceStream, 100); + auto program = Program::load(sourceStream); + auto population1 = Population::makeRandom(program, 100); + auto population2 = Population::makeRandom(program, 100); BOOST_TEST(population1.individuals().size() == 100); BOOST_TEST(population2.individuals().size() == 100); @@ -118,7 +120,7 @@ BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes BOOST_AUTO_TEST_CASE(makeRandom_should_not_compute_fitness) { CharStream sourceStream(sampleSourceCode, current_test_case().p_name); - auto population = Population::makeRandom(sourceStream, 5); + auto population = Population::makeRandom(Program::load(sourceStream), 5); BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet)); } @@ -127,7 +129,7 @@ BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness) { stringstream output; CharStream sourceStream(sampleSourceCode, current_test_case().p_name); - auto population = Population::makeRandom(sourceStream, 5); + auto population = Population::makeRandom(Program::load(sourceStream), 5); assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet)); population.run(1, output); @@ -146,11 +148,12 @@ BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse) Chromosome({UnusedPruner::name}), Chromosome({StructuralSimplifier::name, BlockFlattener::name}), }; - Population population(sourceStream, chromosomes); + auto program = Program::load(sourceStream); + Population population(program, chromosomes); size_t initialTopFitness[2] = { - Population::measureFitness(chromosomes[0], sourceStream), - Population::measureFitness(chromosomes[1], sourceStream), + Population::measureFitness(chromosomes[0], program), + Population::measureFitness(chromosomes[1], program), }; for (int i = 0; i < 6; ++i) diff --git a/tools/yulPhaser/Population.cpp b/tools/yulPhaser/Population.cpp index ca6fe7891..a28db27a4 100644 --- a/tools/yulPhaser/Population.cpp +++ b/tools/yulPhaser/Population.cpp @@ -49,27 +49,27 @@ ostream& phaser::operator<<(ostream& _stream, Individual const& _individual) return _stream; } -Population::Population(CharStream _sourceCode, vector const& _chromosomes): - m_sourceCode{move(_sourceCode)} +Population::Population(Program _program, vector const& _chromosomes): + m_program{move(_program)} { for (auto const& chromosome: _chromosomes) m_individuals.push_back({chromosome}); } -Population Population::makeRandom(CharStream _sourceCode, size_t _size) +Population Population::makeRandom(Program _program, size_t _size) { vector individuals; for (size_t i = 0; i < _size; ++i) individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())}); - return Population(move(_sourceCode), individuals); + return Population(move(_program), individuals); } -size_t Population::measureFitness(Chromosome const& _chromosome, CharStream& _sourceCode) +size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program) { - auto program = Program::load(_sourceCode); - program.optimise(_chromosome.optimisationSteps()); - return program.codeSize(); + Program programCopy = _program; + programCopy.optimise(_chromosome.optimisationSteps()); + return programCopy.codeSize(); } void Population::run(optional _numRounds, ostream& _outputStream) @@ -88,8 +88,6 @@ void Population::run(optional _numRounds, ostream& _outputStream) ostream& phaser::operator<<(ostream& _stream, Population const& _population) { - _stream << "Stream name: " << _population.m_sourceCode.name() << endl; - auto individual = _population.m_individuals.begin(); for (; individual != _population.m_individuals.end(); ++individual) _stream << *individual << endl; @@ -106,7 +104,7 @@ void Population::doEvaluation() { for (auto& individual: m_individuals) if (!individual.fitness.has_value()) - individual.fitness = measureFitness(individual.chromosome, m_sourceCode); + individual.fitness = measureFitness(individual.chromosome, m_program); } void Population::doSelection() diff --git a/tools/yulPhaser/Population.h b/tools/yulPhaser/Population.h index 6e30b6193..e8a38dc14 100644 --- a/tools/yulPhaser/Population.h +++ b/tools/yulPhaser/Population.h @@ -18,10 +18,9 @@ #pragma once #include +#include #include -#include - #include #include #include @@ -47,7 +46,7 @@ struct Individual * and selecting the best ones for the next round. * * An individual is a sequence of optimiser steps represented by a @a Chromosome instance. The whole - * population is associated with a fixed Yul program. By loading the source code into a @a Program + * population is associated with a fixed Yul program. By applying the steps to the @a Program * instance the class can compute fitness of the individual. */ class Population @@ -55,21 +54,21 @@ class Population public: static constexpr size_t MaxChromosomeLength = 30; - explicit Population(langutil::CharStream _sourceCode, std::vector const& _chromosomes = {}); - static Population makeRandom(langutil::CharStream _sourceCode, size_t _size); + explicit Population(Program _program, std::vector const& _chromosomes = {}); + static Population makeRandom(Program _program, size_t _size); void run(std::optional _numRounds, std::ostream& _outputStream); std::vector const& individuals() const { return m_individuals; } static size_t randomChromosomeLength() { return binomialRandomInt(MaxChromosomeLength, 0.5); } - static size_t measureFitness(Chromosome const& _chromosome, langutil::CharStream& _sourceCode); + static size_t measureFitness(Chromosome const& _chromosome, Program const& _program); friend std::ostream& operator<<(std::ostream& _stream, Population const& _population); private: - explicit Population(langutil::CharStream _sourceCode, std::vector _individuals = {}): - m_sourceCode{std::move(_sourceCode)}, + explicit Population(Program _program, std::vector _individuals = {}): + m_program{std::move(_program)}, m_individuals{std::move(_individuals)} {} void doMutation(); @@ -81,8 +80,7 @@ private: size_t _count ); - langutil::CharStream m_sourceCode; - + Program m_program; std::vector m_individuals; }; diff --git a/tools/yulPhaser/main.cpp b/tools/yulPhaser/main.cpp index 6c72bd0f0..bb11b9fcf 100644 --- a/tools/yulPhaser/main.cpp +++ b/tools/yulPhaser/main.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -54,7 +55,8 @@ CharStream loadSource(string const& _sourcePath) void runAlgorithm(string const& _sourcePath) { - auto population = Population::makeRandom(loadSource(_sourcePath), 10); + CharStream sourceCode = loadSource(_sourcePath); + auto population = Population::makeRandom(Program::load(sourceCode), 10); population.run(nullopt, cout); }