mirror of
				https://github.com/ethereum/solidity
				synced 2023-10-03 13:03:40 +00:00 
			
		
		
		
	[yul-phaser] Add --show-initial-population option
This commit is contained in:
		
							parent
							
								
									d6b96063f8
								
							
						
					
					
						commit
						ec10a3d378
					
				| @ -70,6 +70,7 @@ class AlgorithmRunnerFixture | |||||||
| protected: | protected: | ||||||
| 	// NOTE: Regexes here should not contain spaces because we strip them before matching
 | 	// NOTE: Regexes here should not contain spaces because we strip them before matching
 | ||||||
| 	regex RoundSummaryRegex{R"(-+ROUND\d+-+)"}; | 	regex RoundSummaryRegex{R"(-+ROUND\d+-+)"}; | ||||||
|  | 	regex InitialPopulationHeaderRegex{"-+INITIALPOPULATION-+"}; | ||||||
| 
 | 
 | ||||||
| 	string individualPattern(Individual const& individual) const | 	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) | BOOST_FIXTURE_TEST_CASE(run_should_print_round_summary_after_each_round, AlgorithmRunnerFixture) | ||||||
| { | { | ||||||
| 	m_options.maxRounds = 1; | 	m_options.maxRounds = 1; | ||||||
|  | 	m_options.showInitialPopulation = false; | ||||||
| 	AlgorithmRunner runner(m_population, {}, m_options, m_output); | 	AlgorithmRunner runner(m_population, {}, m_options, m_output); | ||||||
| 	RandomisingAlgorithm algorithm; | 	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_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) | BOOST_FIXTURE_TEST_CASE(run_should_save_initial_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture) | ||||||
| { | { | ||||||
| 	m_options.maxRounds = 0; | 	m_options.maxRounds = 0; | ||||||
|  | |||||||
| @ -31,6 +31,7 @@ using namespace solidity::phaser; | |||||||
| void AlgorithmRunner::run(GeneticAlgorithm& _algorithm) | void AlgorithmRunner::run(GeneticAlgorithm& _algorithm) | ||||||
| { | { | ||||||
| 	populationAutosave(); | 	populationAutosave(); | ||||||
|  | 	printInitialPopulation(); | ||||||
| 	cacheClear(); | 	cacheClear(); | ||||||
| 
 | 
 | ||||||
| 	for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round) | 	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 | void AlgorithmRunner::populationAutosave() const | ||||||
| { | { | ||||||
| 	if (!m_options.populationAutosaveFile.has_value()) | 	if (!m_options.populationAutosaveFile.has_value()) | ||||||
|  | |||||||
| @ -47,6 +47,7 @@ public: | |||||||
| 		bool randomiseDuplicates = false; | 		bool randomiseDuplicates = false; | ||||||
| 		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; | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	AlgorithmRunner( | 	AlgorithmRunner( | ||||||
| @ -66,6 +67,7 @@ public: | |||||||
| 	Population const& population() const { return m_population; } | 	Population const& population() const { return m_population; } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  | 	void printInitialPopulation() const; | ||||||
| 	void populationAutosave() const; | 	void populationAutosave() const; | ||||||
| 	void randomiseDuplicates(); | 	void randomiseDuplicates(); | ||||||
| 	void cacheClear(); | 	void cacheClear(); | ||||||
|  | |||||||
| @ -543,6 +543,16 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription() | |||||||
| 	; | 	; | ||||||
| 	keywordDescription.add(cacheDescription); | 	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; | 	po::positional_options_description positionalDescription; | ||||||
| 	positionalDescription.add("input-files", -1); | 	positionalDescription.add("input-files", -1); | ||||||
| 
 | 
 | ||||||
| @ -592,6 +602,7 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c | |||||||
| 		!_arguments["no-randomise-duplicates"].as<bool>(), | 		!_arguments["no-randomise-duplicates"].as<bool>(), | ||||||
| 		_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>(), | ||||||
| 	}; | 	}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user