[yul-phaser] Add --show-only-top-chromosome and --hide-round options

This commit is contained in:
Kamil Śliwak 2020-02-27 02:07:48 +01:00
parent ec10a3d378
commit c875b3d944
4 changed files with 144 additions and 3 deletions

View File

@ -78,6 +78,12 @@ protected:
output << "Fitness:" << individual.fitness << ",";
output << "optimisations:" << individual.chromosome;
return output.str();
}
string topChromosomePattern(size_t roundNumber, Individual const& individual) const
{
ostringstream output;
output << roundNumber << "\\|" << individualPattern(individual);
return output.str();
}
@ -135,6 +141,8 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_round_summary_after_each_round, Algorit
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = false;
m_options.showRoundInfo = true;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
RandomisingAlgorithm algorithm;
@ -150,10 +158,83 @@ 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_not_print_round_summary_if_not_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = false;
m_options.showRoundInfo = false;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
RandomisingAlgorithm algorithm;
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex("")));
for (auto const& individual: runner.population().individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_not_print_population_if_its_empty, AlgorithmRunnerFixture)
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = false;
m_options.showRoundInfo = true;
AlgorithmRunner runner(Population(m_fitnessMetric), {}, m_options, m_output);
RandomisingAlgorithm algorithm;
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, RoundSummaryRegex));
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_print_only_top_chromosome_if_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = true;
m_options.showRoundInfo = true;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
RandomisingAlgorithm algorithm;
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex(topChromosomePattern(1, runner.population().individuals()[0]))));
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_not_print_round_number_for_top_chromosome_if_round_info_not_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 1;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = true;
m_options.showRoundInfo = false;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
RandomisingAlgorithm algorithm;
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(runner.population().individuals()[0]))));
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_not_print_population_if_its_empty_and_only_top_chromosome_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 3;
m_options.showRoundInfo = true;
m_options.showInitialPopulation = false;
m_options.showOnlyTopChromosome = true;
AlgorithmRunner runner(Population(m_fitnessMetric), {}, m_options, m_output);
RandomisingAlgorithm algorithm;
runner.run(algorithm);
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;
m_options.showRoundInfo = false;
m_options.showOnlyTopChromosome = false;
RandomisingAlgorithm algorithm;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
@ -169,6 +250,8 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_print_initial_population_if_not_requested
{
m_options.maxRounds = 0;
m_options.showInitialPopulation = false;
m_options.showRoundInfo = false;
m_options.showOnlyTopChromosome = false;
RandomisingAlgorithm algorithm;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
@ -177,6 +260,23 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_print_initial_population_if_not_requested
BOOST_TEST(m_output.peek() == EOF);
}
BOOST_FIXTURE_TEST_CASE(run_should_print_whole_initial_population_even_if_only_top_chromosome_requested, AlgorithmRunnerFixture)
{
m_options.maxRounds = 0;
m_options.showInitialPopulation = true;
m_options.showRoundInfo = false;
m_options.showOnlyTopChromosome = 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_save_initial_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
{
m_options.maxRounds = 0;

View File

@ -41,13 +41,36 @@ void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
m_population = _algorithm.runNextRound(m_population);
randomiseDuplicates();
m_outputStream << "---------- ROUND " << round + 1 << " ----------" << endl;
m_outputStream << m_population;
printRoundSummary(round);
populationAutosave();
}
}
void AlgorithmRunner::printRoundSummary(
size_t _round
) const
{
if (!m_options.showOnlyTopChromosome)
{
if (m_options.showRoundInfo)
{
m_outputStream << "---------- ROUND " << _round + 1;
m_outputStream << " ----------" << endl;
}
else if (m_population.individuals().size() > 0)
m_outputStream << endl;
m_outputStream << m_population;
}
else if (m_population.individuals().size() > 0)
{
if (m_options.showRoundInfo)
m_outputStream << setw(5) << _round + 1 << " | ";
m_outputStream << m_population.individuals()[0] << endl;
}
}
void AlgorithmRunner::printInitialPopulation() const
{
if (!m_options.showInitialPopulation)

View File

@ -24,6 +24,7 @@
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/ProgramCache.h>
#include <chrono>
#include <optional>
#include <ostream>
@ -48,6 +49,8 @@ public:
std::optional<size_t> minChromosomeLength = std::nullopt;
std::optional<size_t> maxChromosomeLength = std::nullopt;
bool showInitialPopulation = false;
bool showOnlyTopChromosome = false;
bool showRoundInfo = true;
};
AlgorithmRunner(
@ -67,6 +70,9 @@ public:
Population const& population() const { return m_population; }
private:
void printRoundSummary(
size_t _round
) const;
void printInitialPopulation() const;
void populationAutosave() const;
void randomiseDuplicates();

View File

@ -550,6 +550,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
po::bool_switch(),
"Print the state of the population before the algorithm starts."
)
(
"show-only-top-chromosome",
po::bool_switch(),
"Print only the best chromosome found in each round rather than the whole population."
)
(
"hide-round",
po::bool_switch(),
"Hide information about the current round (round number and elapsed time)."
)
;
keywordDescription.add(outputDescription);
@ -603,6 +613,8 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c
_arguments["min-chromosome-length"].as<size_t>(),
_arguments["max-chromosome-length"].as<size_t>(),
_arguments["show-initial-population"].as<bool>(),
_arguments["show-only-top-chromosome"].as<bool>(),
!_arguments["hide-round"].as<bool>(),
};
}