mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] AlgorithmRunner: Create a structure to store runner's options
This commit is contained in:
parent
b8244f6a43
commit
2110bf10cf
@ -50,6 +50,7 @@ class AlgorithmRunnerFixture
|
|||||||
protected:
|
protected:
|
||||||
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||||
output_test_stream m_output;
|
output_test_stream m_output;
|
||||||
|
AlgorithmRunner::Options m_options;
|
||||||
};
|
};
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
@ -57,33 +58,38 @@ BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest)
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture)
|
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture)
|
||||||
{
|
{
|
||||||
AlgorithmRunner runner(Population(m_fitnessMetric), m_output);
|
m_options.maxRounds = 5;
|
||||||
|
AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output);
|
||||||
DummyAlgorithm algorithm;
|
DummyAlgorithm algorithm;
|
||||||
|
|
||||||
BOOST_TEST(algorithm.m_currentRound == 0);
|
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||||
runner.run(algorithm, 10);
|
runner.run(algorithm);
|
||||||
|
BOOST_TEST(algorithm.m_currentRound == 5);
|
||||||
|
runner.run(algorithm);
|
||||||
BOOST_TEST(algorithm.m_currentRound == 10);
|
BOOST_TEST(algorithm.m_currentRound == 10);
|
||||||
runner.run(algorithm, 3);
|
|
||||||
BOOST_TEST(algorithm.m_currentRound == 13);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixture)
|
BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixture)
|
||||||
{
|
{
|
||||||
// run() is allowed to print more but should at least print the first one
|
// run() is allowed to print more but should at least print the first one
|
||||||
|
|
||||||
|
m_options.maxRounds = 1;
|
||||||
AlgorithmRunner runner(
|
AlgorithmRunner runner(
|
||||||
// NOTE: Chromosomes chosen so that they're not substrings of each other and are not
|
// 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.
|
// words likely to appear in the output in normal circumstances.
|
||||||
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
|
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
|
||||||
|
m_options,
|
||||||
m_output
|
m_output
|
||||||
);
|
);
|
||||||
|
|
||||||
DummyAlgorithm algorithm;
|
DummyAlgorithm algorithm;
|
||||||
|
|
||||||
BOOST_TEST(m_output.is_empty());
|
BOOST_TEST(m_output.is_empty());
|
||||||
runner.run(algorithm, 1);
|
runner.run(algorithm);
|
||||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1);
|
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1);
|
||||||
runner.run(algorithm, 3);
|
runner.run(algorithm);
|
||||||
|
runner.run(algorithm);
|
||||||
|
runner.run(algorithm);
|
||||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4);
|
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,9 +20,9 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace solidity::phaser;
|
using namespace solidity::phaser;
|
||||||
|
|
||||||
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm, optional<size_t> _numRounds)
|
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
|
||||||
{
|
{
|
||||||
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
|
for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round)
|
||||||
{
|
{
|
||||||
m_population = _algorithm.runNextRound(m_population);
|
m_population = _algorithm.runNextRound(m_population);
|
||||||
|
|
||||||
|
@ -39,19 +39,28 @@ namespace solidity::phaser
|
|||||||
class AlgorithmRunner
|
class AlgorithmRunner
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
struct Options
|
||||||
|
{
|
||||||
|
std::optional<size_t> maxRounds = std::nullopt;
|
||||||
|
};
|
||||||
|
|
||||||
AlgorithmRunner(
|
AlgorithmRunner(
|
||||||
Population _initialPopulation,
|
Population _initialPopulation,
|
||||||
|
Options _options,
|
||||||
std::ostream& _outputStream
|
std::ostream& _outputStream
|
||||||
):
|
):
|
||||||
m_population(std::move(_initialPopulation)),
|
m_population(std::move(_initialPopulation)),
|
||||||
|
m_options(std::move(_options)),
|
||||||
m_outputStream(_outputStream) {}
|
m_outputStream(_outputStream) {}
|
||||||
|
|
||||||
void run(GeneticAlgorithm& _algorithm, std::optional<size_t> _numRounds = std::nullopt);
|
void run(GeneticAlgorithm& _algorithm);
|
||||||
|
|
||||||
|
Options const& options() const { return m_options; }
|
||||||
Population const& population() const { return m_population; }
|
Population const& population() const { return m_population; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Population m_population;
|
Population m_population;
|
||||||
|
Options m_options;
|
||||||
std::ostream& m_outputStream;
|
std::ostream& m_outputStream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -182,7 +182,7 @@ void Phaser::runAlgorithm(string const& _sourcePath, Algorithm _algorithm)
|
|||||||
maxChromosomeLength
|
maxChromosomeLength
|
||||||
);
|
);
|
||||||
|
|
||||||
AlgorithmRunner algorithmRunner(population, cout);
|
AlgorithmRunner algorithmRunner(population, AlgorithmRunner::Options{}, cout);
|
||||||
switch (_algorithm)
|
switch (_algorithm)
|
||||||
{
|
{
|
||||||
case Algorithm::Random:
|
case Algorithm::Random:
|
||||||
|
Loading…
Reference in New Issue
Block a user