[yul-phaser] Add --show-initial-population option

This commit is contained in:
Kamil Śliwak 2020-02-27 02:04:09 +01:00
parent d6b96063f8
commit ec10a3d378
4 changed files with 52 additions and 0 deletions

View File

@ -70,6 +70,7 @@ class AlgorithmRunnerFixture
protected:
// NOTE: Regexes here should not contain spaces because we strip them before matching
regex RoundSummaryRegex{R"(-+ROUND\d+-+)"};
regex InitialPopulationHeaderRegex{"-+INITIALPOPULATION-+"};
string individualPattern(Individual const& individual) const
{
@ -133,6 +134,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRu
BOOST_FIXTURE_TEST_CASE(run_should_print_round_summary_after_each_round, AlgorithmRunnerFixture)
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
RandomisingAlgorithm algorithm;
@ -148,6 +150,33 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_round_summary_after_each_round, Algorit
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_print_initial_population_if_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 0;
m_options.showInitialPopulation = true;
RandomisingAlgorithm algorithm;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, InitialPopulationHeaderRegex));
for (auto const& individual: m_population.individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_not_print_initial_population_if_not_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 0;
m_options.showInitialPopulation = false;
RandomisingAlgorithm algorithm;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
runner.run(algorithm);
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_save_initial_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
{
m_options.maxRounds = 0;

View File

@ -31,6 +31,7 @@ using namespace solidity::phaser;
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
{
populationAutosave();
printInitialPopulation();
cacheClear();
for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round)
@ -47,6 +48,15 @@ void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
}
}
void AlgorithmRunner::printInitialPopulation() const
{
if (!m_options.showInitialPopulation)
return;
m_outputStream << "---------- INITIAL POPULATION ----------" << endl;
m_outputStream << m_population;
}
void AlgorithmRunner::populationAutosave() const
{
if (!m_options.populationAutosaveFile.has_value())

View File

@ -47,6 +47,7 @@ public:
bool randomiseDuplicates = false;
std::optional<size_t> minChromosomeLength = std::nullopt;
std::optional<size_t> maxChromosomeLength = std::nullopt;
bool showInitialPopulation = false;
};
AlgorithmRunner(
@ -66,6 +67,7 @@ public:
Population const& population() const { return m_population; }
private:
void printInitialPopulation() const;
void populationAutosave() const;
void randomiseDuplicates();
void cacheClear();

View File

@ -543,6 +543,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
;
keywordDescription.add(cacheDescription);
po::options_description outputDescription("OUTPUT", lineLength, minDescriptionLength);
outputDescription.add_options()
(
"show-initial-population",
po::bool_switch(),
"Print the state of the population before the algorithm starts."
)
;
keywordDescription.add(outputDescription);
po::positional_options_description positionalDescription;
positionalDescription.add("input-files", -1);
@ -592,6 +602,7 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c
!_arguments["no-randomise-duplicates"].as<bool>(),
_arguments["min-chromosome-length"].as<size_t>(),
_arguments["max-chromosome-length"].as<size_t>(),
_arguments["show-initial-population"].as<bool>(),
};
}