mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add --show-only-top-chromosome and --hide-round options
This commit is contained in:
parent
ec10a3d378
commit
c875b3d944
@ -78,6 +78,12 @@ protected:
|
|||||||
output << "Fitness:" << individual.fitness << ",";
|
output << "Fitness:" << individual.fitness << ",";
|
||||||
output << "optimisations:" << individual.chromosome;
|
output << "optimisations:" << individual.chromosome;
|
||||||
return output.str();
|
return output.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
string topChromosomePattern(size_t roundNumber, Individual const& individual) const
|
||||||
|
{
|
||||||
|
ostringstream output;
|
||||||
|
output << roundNumber << "\\|" << individualPattern(individual);
|
||||||
return output.str();
|
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.maxRounds = 1;
|
||||||
m_options.showInitialPopulation = false;
|
m_options.showInitialPopulation = false;
|
||||||
|
m_options.showOnlyTopChromosome = false;
|
||||||
|
m_options.showRoundInfo = true;
|
||||||
AlgorithmRunner runner(m_population, {}, m_options, m_output);
|
AlgorithmRunner runner(m_population, {}, m_options, m_output);
|
||||||
RandomisingAlgorithm algorithm;
|
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_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)
|
BOOST_FIXTURE_TEST_CASE(run_should_print_initial_population_if_requested, AlgorithmRunnerFixture)
|
||||||
{
|
{
|
||||||
m_options.maxRounds = 0;
|
m_options.maxRounds = 0;
|
||||||
m_options.showInitialPopulation = true;
|
m_options.showInitialPopulation = true;
|
||||||
|
m_options.showRoundInfo = false;
|
||||||
|
m_options.showOnlyTopChromosome = false;
|
||||||
RandomisingAlgorithm algorithm;
|
RandomisingAlgorithm algorithm;
|
||||||
|
|
||||||
AlgorithmRunner runner(m_population, {}, m_options, m_output);
|
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.maxRounds = 0;
|
||||||
m_options.showInitialPopulation = false;
|
m_options.showInitialPopulation = false;
|
||||||
|
m_options.showRoundInfo = false;
|
||||||
|
m_options.showOnlyTopChromosome = false;
|
||||||
RandomisingAlgorithm algorithm;
|
RandomisingAlgorithm algorithm;
|
||||||
|
|
||||||
AlgorithmRunner runner(m_population, {}, m_options, m_output);
|
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_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)
|
BOOST_FIXTURE_TEST_CASE(run_should_save_initial_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
|
||||||
{
|
{
|
||||||
m_options.maxRounds = 0;
|
m_options.maxRounds = 0;
|
||||||
|
@ -41,13 +41,36 @@ void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
|
|||||||
m_population = _algorithm.runNextRound(m_population);
|
m_population = _algorithm.runNextRound(m_population);
|
||||||
randomiseDuplicates();
|
randomiseDuplicates();
|
||||||
|
|
||||||
m_outputStream << "---------- ROUND " << round + 1 << " ----------" << endl;
|
printRoundSummary(round);
|
||||||
m_outputStream << m_population;
|
|
||||||
|
|
||||||
populationAutosave();
|
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
|
void AlgorithmRunner::printInitialPopulation() const
|
||||||
{
|
{
|
||||||
if (!m_options.showInitialPopulation)
|
if (!m_options.showInitialPopulation)
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include <tools/yulPhaser/Population.h>
|
#include <tools/yulPhaser/Population.h>
|
||||||
#include <tools/yulPhaser/ProgramCache.h>
|
#include <tools/yulPhaser/ProgramCache.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
@ -48,6 +49,8 @@ public:
|
|||||||
std::optional<size_t> minChromosomeLength = std::nullopt;
|
std::optional<size_t> minChromosomeLength = std::nullopt;
|
||||||
std::optional<size_t> maxChromosomeLength = std::nullopt;
|
std::optional<size_t> maxChromosomeLength = std::nullopt;
|
||||||
bool showInitialPopulation = false;
|
bool showInitialPopulation = false;
|
||||||
|
bool showOnlyTopChromosome = false;
|
||||||
|
bool showRoundInfo = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
AlgorithmRunner(
|
AlgorithmRunner(
|
||||||
@ -67,6 +70,9 @@ public:
|
|||||||
Population const& population() const { return m_population; }
|
Population const& population() const { return m_population; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void printRoundSummary(
|
||||||
|
size_t _round
|
||||||
|
) const;
|
||||||
void printInitialPopulation() const;
|
void printInitialPopulation() const;
|
||||||
void populationAutosave() const;
|
void populationAutosave() const;
|
||||||
void randomiseDuplicates();
|
void randomiseDuplicates();
|
||||||
|
@ -550,6 +550,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
|||||||
po::bool_switch(),
|
po::bool_switch(),
|
||||||
"Print the state of the population before the algorithm starts."
|
"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);
|
keywordDescription.add(outputDescription);
|
||||||
|
|
||||||
@ -603,6 +613,8 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c
|
|||||||
_arguments["min-chromosome-length"].as<size_t>(),
|
_arguments["min-chromosome-length"].as<size_t>(),
|
||||||
_arguments["max-chromosome-length"].as<size_t>(),
|
_arguments["max-chromosome-length"].as<size_t>(),
|
||||||
_arguments["show-initial-population"].as<bool>(),
|
_arguments["show-initial-population"].as<bool>(),
|
||||||
|
_arguments["show-only-top-chromosome"].as<bool>(),
|
||||||
|
!_arguments["hide-round"].as<bool>(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user