[yul-phaser] AlgorithmRunner: Add support for ProgramCache

This commit is contained in:
Kamil Śliwak 2020-02-27 00:55:24 +01:00
parent 259f738f17
commit e2ff9698d3
4 changed files with 63 additions and 8 deletions

View File

@ -20,6 +20,8 @@
#include <tools/yulPhaser/AlgorithmRunner.h>
#include <tools/yulPhaser/Common.h>
#include <liblangutil/CharStream.h>
#include <libsolutil/CommonIO.h>
#include <boost/filesystem.hpp>
@ -29,6 +31,7 @@
using namespace std;
using namespace boost::unit_test::framework;
using namespace boost::test_tools;
using namespace solidity::langutil;
using namespace solidity::util;
namespace fs = boost::filesystem;
@ -92,7 +95,7 @@ BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest)
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture)
{
m_options.maxRounds = 5;
AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output);
AlgorithmRunner runner(Population(m_fitnessMetric), {}, m_options, m_output);
CountingAlgorithm algorithm;
@ -112,6 +115,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixt
// NOTE: Chromosomes chosen so that they're not substrings of each other and are not
// words likely to appear in the output in normal circumstances.
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
{},
m_options,
m_output
);
@ -131,7 +135,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_save_initial_population_to_file_if_autosave_f
{
m_options.maxRounds = 0;
m_options.populationAutosaveFile = m_autosavePath;
AlgorithmRunner runner(m_population, m_options, m_output);
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));
runner.run(m_algorithm);
@ -145,7 +149,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_save_population_to_file_if_autosave_file_spec
{
m_options.maxRounds = 1;
m_options.populationAutosaveFile = m_autosavePath;
AlgorithmRunner runner(m_population, m_options, m_output);
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));
runner.run(m_algorithm);
@ -159,7 +163,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_overwrite_existing_file_if_autosave_file_spec
{
m_options.maxRounds = 5;
m_options.populationAutosaveFile = m_autosavePath;
AlgorithmRunner runner(m_population, m_options, m_output);
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));
vector<string> originalContent = {"Original content"};
@ -180,7 +184,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_save_population_to_file_if_autosave_file_
{
m_options.maxRounds = 5;
m_options.populationAutosaveFile = nullopt;
AlgorithmRunner runner(m_population, m_options, m_output);
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));
runner.run(m_algorithm);
@ -198,7 +202,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_randomise_duplicate_chromosomes_if_requested,
m_options.randomiseDuplicates = true;
m_options.minChromosomeLength = 50;
m_options.maxChromosomeLength = 50;
AlgorithmRunner runner(population, m_options, m_output);
AlgorithmRunner runner(population, {}, m_options, m_output);
runner.run(algorithm);
@ -227,7 +231,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_randomise_duplicate_chromosomes_if_not_re
m_options.maxRounds = 1;
m_options.randomiseDuplicates = false;
AlgorithmRunner runner(population, m_options, m_output);
AlgorithmRunner runner(population, {}, m_options, m_output);
runner.run(algorithm);
@ -237,6 +241,33 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_randomise_duplicate_chromosomes_if_not_re
BOOST_TEST(runner.population().individuals()[2].chromosome == duplicate);
}
BOOST_FIXTURE_TEST_CASE(run_should_clear_cache_at_the_beginning_and_update_it_before_each_round, AlgorithmRunnerFixture)
{
CharStream sourceStream = CharStream("{}", current_test_case().p_name);
vector<shared_ptr<ProgramCache>> caches = {
make_shared<ProgramCache>(get<Program>(Program::load(sourceStream))),
make_shared<ProgramCache>(get<Program>(Program::load(sourceStream))),
};
m_options.maxRounds = 10;
AlgorithmRunner runner(Population(m_fitnessMetric), caches, m_options, m_output);
CountingAlgorithm algorithm;
BOOST_TEST(algorithm.m_currentRound == 0);
BOOST_TEST(caches[0]->currentRound() == 0);
BOOST_TEST(caches[1]->currentRound() == 0);
runner.run(algorithm);
BOOST_TEST(algorithm.m_currentRound == 10);
BOOST_TEST(caches[0]->currentRound() == 10);
BOOST_TEST(caches[1]->currentRound() == 10);
runner.run(algorithm);
BOOST_TEST(algorithm.m_currentRound == 20);
BOOST_TEST(caches[0]->currentRound() == 10);
BOOST_TEST(caches[1]->currentRound() == 10);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

View File

@ -31,9 +31,12 @@ using namespace solidity::phaser;
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
{
populationAutosave();
cacheClear();
for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round)
{
cacheStartRound(round + 1);
m_population = _algorithm.runNextRound(m_population);
randomiseDuplicates();
@ -66,6 +69,20 @@ void AlgorithmRunner::populationAutosave() const
);
}
void AlgorithmRunner::cacheClear()
{
for (auto& cache: m_programCaches)
if (cache != nullptr)
cache->clear();
}
void AlgorithmRunner::cacheStartRound(size_t _roundNumber)
{
for (auto& cache: m_programCaches)
if (cache != nullptr)
cache->startRound(_roundNumber);
}
void AlgorithmRunner::randomiseDuplicates()
{
if (m_options.randomiseDuplicates)

View File

@ -22,6 +22,7 @@
#include <tools/yulPhaser/GeneticAlgorithms.h>
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/ProgramCache.h>
#include <optional>
#include <ostream>
@ -50,10 +51,12 @@ public:
AlgorithmRunner(
Population _initialPopulation,
std::vector<std::shared_ptr<ProgramCache>> _programCaches,
Options _options,
std::ostream& _outputStream
):
m_population(std::move(_initialPopulation)),
m_programCaches(std::move(_programCaches)),
m_options(std::move(_options)),
m_outputStream(_outputStream) {}
@ -65,6 +68,9 @@ public:
private:
void populationAutosave() const;
void randomiseDuplicates();
void cacheClear();
void cacheStartRound(size_t _roundNumber);
static Population randomiseDuplicates(
Population _population,
size_t _minChromosomeLength,
@ -72,6 +78,7 @@ private:
);
Population m_population;
std::vector<std::shared_ptr<ProgramCache>> m_programCaches;
Options m_options;
std::ostream& m_outputStream;
};

View File

@ -577,6 +577,6 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments)
population.individuals().size()
);
AlgorithmRunner algorithmRunner(population, buildAlgorithmRunnerOptions(_arguments), cout);
AlgorithmRunner algorithmRunner(population, vector<shared_ptr<ProgramCache>>(programs.size(), nullptr), buildAlgorithmRunnerOptions(_arguments), cout);
algorithmRunner.run(*geneticAlgorithm);
}