mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8421 from imapp-pl/yul-phaser-refactor-main
[yul-phaser] Refactoring in main
This commit is contained in:
commit
d45e6aa24c
@ -139,11 +139,13 @@ set(libyul_sources
|
||||
detect_stray_source_files("${libyul_sources}" "libyul/")
|
||||
|
||||
set(yul_phaser_sources
|
||||
yulPhaser/Common.h
|
||||
yulPhaser/TestHelpers.h
|
||||
yulPhaser/TestHelpers.cpp
|
||||
yulPhaser/TestHelpersTest.cpp
|
||||
yulPhaser/Common.cpp
|
||||
yulPhaser/CommonTest.cpp
|
||||
yulPhaser/Chromosome.cpp
|
||||
yulPhaser/FitnessMetrics.cpp
|
||||
yulPhaser/AlgorithmRunner.cpp
|
||||
yulPhaser/GeneticAlgorithms.cpp
|
||||
yulPhaser/Mutations.cpp
|
||||
yulPhaser/PairSelections.cpp
|
||||
@ -155,6 +157,7 @@ set(yul_phaser_sources
|
||||
# FIXME: yul-phaser is not a library so I can't just add it to target_link_libraries().
|
||||
# My current workaround is just to include its source files here but this introduces
|
||||
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
|
||||
../tools/yulPhaser/AlgorithmRunner.cpp
|
||||
../tools/yulPhaser/Chromosome.cpp
|
||||
../tools/yulPhaser/FitnessMetrics.cpp
|
||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||
|
99
test/yulPhaser/AlgorithmRunner.cpp
Normal file
99
test/yulPhaser/AlgorithmRunner.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::unit_test::framework;
|
||||
using namespace boost::test_tools;
|
||||
using namespace solidity::util;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
class DummyAlgorithm: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using GeneticAlgorithm::GeneticAlgorithm;
|
||||
Population runNextRound(Population _population) override
|
||||
{
|
||||
++m_currentRound;
|
||||
return _population;
|
||||
}
|
||||
|
||||
size_t m_currentRound = 0;
|
||||
};
|
||||
|
||||
class AlgorithmRunnerFixture
|
||||
{
|
||||
protected:
|
||||
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||
output_test_stream m_output;
|
||||
AlgorithmRunner::Options m_options;
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture)
|
||||
{
|
||||
m_options.maxRounds = 5;
|
||||
AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output);
|
||||
DummyAlgorithm algorithm;
|
||||
|
||||
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||
runner.run(algorithm);
|
||||
BOOST_TEST(algorithm.m_currentRound == 5);
|
||||
runner.run(algorithm);
|
||||
BOOST_TEST(algorithm.m_currentRound == 10);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
m_options.maxRounds = 1;
|
||||
AlgorithmRunner runner(
|
||||
// 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.
|
||||
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
|
||||
m_options,
|
||||
m_output
|
||||
);
|
||||
|
||||
DummyAlgorithm algorithm;
|
||||
|
||||
BOOST_TEST(m_output.is_empty());
|
||||
runner.run(algorithm);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1);
|
||||
runner.run(algorithm);
|
||||
runner.run(algorithm);
|
||||
runner.run(algorithm);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
@ -15,79 +15,115 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <tools/yulPhaser/Common.h>
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <regex>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::phaser;
|
||||
using namespace boost::test_tools;
|
||||
using namespace solidity::util;
|
||||
|
||||
function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
enum class TestEnum
|
||||
{
|
||||
A,
|
||||
B,
|
||||
AB,
|
||||
CD,
|
||||
EF,
|
||||
GH,
|
||||
};
|
||||
|
||||
map<TestEnum, string> const TestEnumToStringMap =
|
||||
{
|
||||
{TestEnum::A, "a"},
|
||||
{TestEnum::B, "b"},
|
||||
{TestEnum::AB, "a b"},
|
||||
{TestEnum::CD, "c-d"},
|
||||
{TestEnum::EF, "e f"},
|
||||
};
|
||||
map<string, TestEnum> const StringToTestEnumMap = invertMap(TestEnumToStringMap);
|
||||
|
||||
}
|
||||
|
||||
function<Mutation> phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue)
|
||||
{
|
||||
return [=](Chromosome const& _chromosome)
|
||||
{
|
||||
vector<string> newGenes = _chromosome.optimisationSteps();
|
||||
assert(_geneIndex < newGenes.size());
|
||||
newGenes[_geneIndex] = _geneValue;
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||
|
||||
return Chromosome(newGenes);
|
||||
};
|
||||
BOOST_AUTO_TEST_CASE(deserializeChoice_should_convert_string_to_enum)
|
||||
{
|
||||
istringstream aStream("a");
|
||||
TestEnum aResult;
|
||||
deserializeChoice(aStream, aResult, StringToTestEnumMap);
|
||||
BOOST_CHECK(aResult == TestEnum::A);
|
||||
BOOST_TEST(!aStream.fail());
|
||||
|
||||
istringstream bStream("b");
|
||||
TestEnum bResult;
|
||||
deserializeChoice(bStream, bResult, StringToTestEnumMap);
|
||||
BOOST_CHECK(bResult == TestEnum::B);
|
||||
BOOST_TEST(!bStream.fail());
|
||||
|
||||
istringstream cdStream("c-d");
|
||||
TestEnum cdResult;
|
||||
deserializeChoice(cdStream, cdResult, StringToTestEnumMap);
|
||||
BOOST_CHECK(cdResult == TestEnum::CD);
|
||||
BOOST_TEST(!cdStream.fail());
|
||||
}
|
||||
|
||||
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
|
||||
BOOST_AUTO_TEST_CASE(deserializeChoice_should_set_failbit_if_there_is_no_enum_corresponding_to_string)
|
||||
{
|
||||
vector<size_t> lengths;
|
||||
for (auto const& individual: _population.individuals())
|
||||
lengths.push_back(individual.chromosome.length());
|
||||
|
||||
return lengths;
|
||||
istringstream xyzStream("xyz");
|
||||
TestEnum xyzResult;
|
||||
deserializeChoice(xyzStream, xyzResult, StringToTestEnumMap);
|
||||
BOOST_TEST(xyzStream.fail());
|
||||
}
|
||||
|
||||
map<string, size_t> phaser::test::enumerateOptmisationSteps()
|
||||
BOOST_AUTO_TEST_CASE(deserializeChoice_does_not_have_to_support_strings_with_spaces)
|
||||
{
|
||||
map<string, size_t> stepIndices;
|
||||
size_t i = 0;
|
||||
for (auto const& nameAndAbbreviation: OptimiserSuite::stepNameToAbbreviationMap())
|
||||
stepIndices.insert({nameAndAbbreviation.first, i++});
|
||||
istringstream abStream("a b");
|
||||
TestEnum abResult;
|
||||
deserializeChoice(abStream, abResult, StringToTestEnumMap);
|
||||
BOOST_CHECK(abResult == TestEnum::A);
|
||||
BOOST_TEST(!abStream.fail());
|
||||
|
||||
return stepIndices;
|
||||
istringstream efStream("e f");
|
||||
TestEnum efResult;
|
||||
deserializeChoice(efStream, efResult, StringToTestEnumMap);
|
||||
BOOST_TEST(efStream.fail());
|
||||
}
|
||||
|
||||
size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2)
|
||||
BOOST_AUTO_TEST_CASE(serializeChoice_should_convert_enum_to_string)
|
||||
{
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < min(_chromosome1.length(), _chromosome2.length()); ++i)
|
||||
count += static_cast<int>(_chromosome1.optimisationSteps()[i] != _chromosome2.optimisationSteps()[i]);
|
||||
output_test_stream output;
|
||||
|
||||
return count + abs(static_cast<int>(_chromosome1.length() - _chromosome2.length()));
|
||||
serializeChoice(output, TestEnum::A, TestEnumToStringMap);
|
||||
BOOST_CHECK(output.is_equal("a"));
|
||||
BOOST_TEST(!output.fail());
|
||||
|
||||
serializeChoice(output, TestEnum::AB, TestEnumToStringMap);
|
||||
BOOST_CHECK(output.is_equal("a b"));
|
||||
BOOST_TEST(!output.fail());
|
||||
}
|
||||
|
||||
string phaser::test::stripWhitespace(string const& input)
|
||||
BOOST_AUTO_TEST_CASE(serializeChoice_should_set_failbit_if_there_is_no_string_corresponding_to_enum)
|
||||
{
|
||||
regex whitespaceRegex("\\s+");
|
||||
return regex_replace(input, whitespaceRegex, "");
|
||||
output_test_stream output;
|
||||
serializeChoice(output, TestEnum::GH, TestEnumToStringMap);
|
||||
BOOST_TEST(output.fail());
|
||||
}
|
||||
|
||||
size_t phaser::test::countSubstringOccurrences(string const& _inputString, string const& _substring)
|
||||
{
|
||||
assert(_substring.size() > 0);
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
size_t count = 0;
|
||||
size_t lastOccurrence = 0;
|
||||
while ((lastOccurrence = _inputString.find(_substring, lastOccurrence)) != string::npos)
|
||||
{
|
||||
++count;
|
||||
lastOccurrence += _substring.size();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
@ -15,19 +15,15 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
||||
#include <liblangutil/CharStream.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
@ -35,102 +31,58 @@
|
||||
using namespace std;
|
||||
using namespace boost::unit_test::framework;
|
||||
using namespace boost::test_tools;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
class DummyAlgorithm: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using GeneticAlgorithm::GeneticAlgorithm;
|
||||
void runNextRound() override { ++m_currentRound; }
|
||||
|
||||
size_t m_currentRound = 0;
|
||||
};
|
||||
|
||||
class GeneticAlgorithmFixture
|
||||
{
|
||||
protected:
|
||||
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||
output_test_stream m_output;
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmsTest)
|
||||
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, GeneticAlgorithmFixture)
|
||||
{
|
||||
DummyAlgorithm algorithm(Population(m_fitnessMetric), m_output);
|
||||
|
||||
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||
algorithm.run(10);
|
||||
BOOST_TEST(algorithm.m_currentRound == 10);
|
||||
algorithm.run(3);
|
||||
BOOST_TEST(algorithm.m_currentRound == 13);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, GeneticAlgorithmFixture)
|
||||
{
|
||||
// run() is allowed to print more but should at least print the first one
|
||||
|
||||
DummyAlgorithm algorithm(
|
||||
// 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.
|
||||
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
|
||||
m_output
|
||||
);
|
||||
|
||||
BOOST_TEST(m_output.is_empty());
|
||||
algorithm.run(1);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 1);
|
||||
algorithm.run(3);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(RandomAlgorithmTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_randomise_rest_of_population, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.5, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
RandomAlgorithm algorithm({0.5, 1, 1});
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.5, 7, 7});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
RandomAlgorithm algorithm({0.5, 7, 7});
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_replace_all_chromosomes_if_zero_size_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.0, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
RandomAlgorithm algorithm({0.0, 1, 1});
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_any_chromosomes_if_whole_population_is_the_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {1.0, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
RandomAlgorithm algorithm({1.0, 1, 1});
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@ -139,6 +91,7 @@ BOOST_AUTO_TEST_SUITE(GenerationalElitistWithExclusivePoolsTest)
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_of_population, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.2,
|
||||
@ -148,17 +101,17 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_o
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
GenerationalElitistWithExclusivePools algorithm(options);
|
||||
|
||||
algorithm.runNextRound();
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{0, 0, 3, 3, 3, 3, 3, 3, 3, 3}));
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{0, 0, 3, 3, 3, 3, 3, 3, 3, 3}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
assert(chromosomeLengths(population) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.2,
|
||||
@ -168,12 +121,11 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individ
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert(chromosomeLengths(algorithm.population()) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
GenerationalElitistWithExclusivePools algorithm(options);
|
||||
|
||||
algorithm.runNextRound();
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 3, 3, 7, 7}));
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 3, 3, 3, 3, 7, 7}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossover_pool_by_mutating_the_elite, GeneticAlgorithmFixture)
|
||||
@ -188,13 +140,13 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
|
||||
/* percentGenesToRandomise = */ 1.0,
|
||||
/* percentGenesToAddOrDelete = */ 1.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
GenerationalElitistWithExclusivePools algorithm(options);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
algorithm.runNextRound();
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
|
||||
BOOST_TEST((
|
||||
chromosomeLengths(algorithm.population()) ==
|
||||
chromosomeLengths(newPopulation) ==
|
||||
vector<size_t>{0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 11, 11, 11}
|
||||
));
|
||||
}
|
||||
@ -205,6 +157,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
|
||||
Population(m_fitnessMetric, {Chromosome("aa"), Chromosome("ff")}) +
|
||||
Population::makeRandom(m_fitnessMetric, 8, 6, 6)
|
||||
);
|
||||
assert((chromosomeLengths(population) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
|
||||
|
||||
GenerationalElitistWithExclusivePools::Options options = {
|
||||
/* mutationPoolSize = */ 0.0,
|
||||
@ -214,14 +167,13 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
|
||||
/* percentGenesToRandomise = */ 0.0,
|
||||
/* percentGenesToAddOrDelete = */ 0.0,
|
||||
};
|
||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
|
||||
GenerationalElitistWithExclusivePools algorithm(options);
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
algorithm.runNextRound();
|
||||
Population newPopulation = algorithm.runNextRound(population);
|
||||
|
||||
vector<Individual> const& newIndividuals = algorithm.population().individuals();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
|
||||
vector<Individual> const& newIndividuals = newPopulation.individuals();
|
||||
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
|
||||
for (auto& individual: newIndividuals)
|
||||
BOOST_TEST((
|
||||
individual.chromosome == Chromosome("aa") ||
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/Mutations.h>
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/Selections.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
|
93
test/yulPhaser/TestHelpers.cpp
Normal file
93
test/yulPhaser/TestHelpers.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
#include <regex>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
|
||||
{
|
||||
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
|
||||
}
|
||||
|
||||
function<Mutation> phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue)
|
||||
{
|
||||
return [=](Chromosome const& _chromosome)
|
||||
{
|
||||
vector<string> newGenes = _chromosome.optimisationSteps();
|
||||
assert(_geneIndex < newGenes.size());
|
||||
newGenes[_geneIndex] = _geneValue;
|
||||
|
||||
return Chromosome(newGenes);
|
||||
};
|
||||
}
|
||||
|
||||
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
|
||||
{
|
||||
vector<size_t> lengths;
|
||||
for (auto const& individual: _population.individuals())
|
||||
lengths.push_back(individual.chromosome.length());
|
||||
|
||||
return lengths;
|
||||
}
|
||||
|
||||
map<string, size_t> phaser::test::enumerateOptmisationSteps()
|
||||
{
|
||||
map<string, size_t> stepIndices;
|
||||
size_t i = 0;
|
||||
for (auto const& nameAndAbbreviation: OptimiserSuite::stepNameToAbbreviationMap())
|
||||
stepIndices.insert({nameAndAbbreviation.first, i++});
|
||||
|
||||
return stepIndices;
|
||||
}
|
||||
|
||||
size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2)
|
||||
{
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < min(_chromosome1.length(), _chromosome2.length()); ++i)
|
||||
count += static_cast<int>(_chromosome1.optimisationSteps()[i] != _chromosome2.optimisationSteps()[i]);
|
||||
|
||||
return count + abs(static_cast<int>(_chromosome1.length() - _chromosome2.length()));
|
||||
}
|
||||
|
||||
string phaser::test::stripWhitespace(string const& input)
|
||||
{
|
||||
regex whitespaceRegex("\\s+");
|
||||
return regex_replace(input, whitespaceRegex, "");
|
||||
}
|
||||
|
||||
size_t phaser::test::countSubstringOccurrences(string const& _inputString, string const& _substring)
|
||||
{
|
||||
assert(_substring.size() > 0);
|
||||
|
||||
size_t count = 0;
|
||||
size_t lastOccurrence = 0;
|
||||
while ((lastOccurrence = _inputString.find(_substring, lastOccurrence)) != string::npos)
|
||||
{
|
||||
++count;
|
||||
lastOccurrence += _substring.size();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
@ -31,7 +31,7 @@ namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||
BOOST_AUTO_TEST_SUITE(TestHelpersTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_length)
|
||||
{
|
@ -15,6 +15,11 @@ install(TARGETS solidity-upgrade DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(yul-phaser
|
||||
yulPhaser/main.cpp
|
||||
yulPhaser/Common.h
|
||||
yulPhaser/AlgorithmRunner.h
|
||||
yulPhaser/AlgorithmRunner.cpp
|
||||
yulPhaser/Phaser.h
|
||||
yulPhaser/Phaser.cpp
|
||||
yulPhaser/GeneticAlgorithms.h
|
||||
yulPhaser/GeneticAlgorithms.cpp
|
||||
yulPhaser/Population.h
|
||||
|
32
tools/yulPhaser/AlgorithmRunner.cpp
Normal file
32
tools/yulPhaser/AlgorithmRunner.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
void AlgorithmRunner::run(GeneticAlgorithm& _algorithm)
|
||||
{
|
||||
for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round)
|
||||
{
|
||||
m_population = _algorithm.runNextRound(m_population);
|
||||
|
||||
m_outputStream << "---------- ROUND " << round + 1 << " ----------" << endl;
|
||||
m_outputStream << m_population;
|
||||
}
|
||||
}
|
67
tools/yulPhaser/AlgorithmRunner.h
Normal file
67
tools/yulPhaser/AlgorithmRunner.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Contains the implementation of a class that manages the execution of a genetic algorithm.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
/**
|
||||
* Manages a population and executes a genetic algorithm on it. It's independent of the
|
||||
* implementation details of a specific algorithm which is pluggable via @a GeneticAlgorithm class.
|
||||
*
|
||||
* The class is also responsible for providing text feedback on the execution of the algorithm
|
||||
* to the associated output stream.
|
||||
*/
|
||||
class AlgorithmRunner
|
||||
{
|
||||
public:
|
||||
struct Options
|
||||
{
|
||||
std::optional<size_t> maxRounds = std::nullopt;
|
||||
};
|
||||
|
||||
AlgorithmRunner(
|
||||
Population _initialPopulation,
|
||||
Options _options,
|
||||
std::ostream& _outputStream
|
||||
):
|
||||
m_population(std::move(_initialPopulation)),
|
||||
m_options(std::move(_options)),
|
||||
m_outputStream(_outputStream) {}
|
||||
|
||||
void run(GeneticAlgorithm& _algorithm);
|
||||
|
||||
Options const& options() const { return m_options; }
|
||||
Population const& population() const { return m_population; }
|
||||
|
||||
private:
|
||||
Population m_population;
|
||||
Options m_options;
|
||||
std::ostream& m_outputStream;
|
||||
};
|
||||
|
||||
}
|
68
tools/yulPhaser/Common.h
Normal file
68
tools/yulPhaser/Common.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Miscellaneous utilities for use in yul-phaser.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
/// Reads a token from the input stream and translates it to a string using a map.
|
||||
/// Sets the failbit in the stream if there's no matching value in the map.
|
||||
template <typename C>
|
||||
std::istream& deserializeChoice(
|
||||
std::istream& _inputStream,
|
||||
C& _choice,
|
||||
std::map<std::string, C> const& _stringToValueMap
|
||||
)
|
||||
{
|
||||
std::string deserializedValue;
|
||||
_inputStream >> deserializedValue;
|
||||
|
||||
auto const& pair = _stringToValueMap.find(deserializedValue);
|
||||
if (pair != _stringToValueMap.end())
|
||||
_choice = pair->second;
|
||||
else
|
||||
_inputStream.setstate(std::ios_base::failbit);
|
||||
|
||||
return _inputStream;
|
||||
}
|
||||
|
||||
/// Translates a value to a string using a map and prints it to the output stream.
|
||||
/// Sets the failbit if the value is not in the map.
|
||||
template <typename C>
|
||||
std::ostream& serializeChoice(
|
||||
std::ostream& _outputStream,
|
||||
C const& _choice,
|
||||
std::map<C, std::string> const& _valueToStringMap
|
||||
)
|
||||
{
|
||||
auto const& pair = _valueToStringMap.find(_choice);
|
||||
if (pair != _valueToStringMap.end())
|
||||
_outputStream << pair->second;
|
||||
else
|
||||
_outputStream.setstate(std::ios_base::failbit);
|
||||
|
||||
return _outputStream;
|
||||
}
|
||||
|
||||
}
|
@ -23,42 +23,31 @@
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
void GeneticAlgorithm::run(optional<size_t> _numRounds)
|
||||
{
|
||||
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
|
||||
{
|
||||
runNextRound();
|
||||
|
||||
m_outputStream << "---------- ROUND " << round << " ----------" << endl;
|
||||
m_outputStream << m_population;
|
||||
}
|
||||
}
|
||||
|
||||
void RandomAlgorithm::runNextRound()
|
||||
Population RandomAlgorithm::runNextRound(Population _population)
|
||||
{
|
||||
RangeSelection elite(0.0, m_options.elitePoolSize);
|
||||
|
||||
Population elitePopulation = m_population.select(elite);
|
||||
size_t replacementCount = m_population.individuals().size() - elitePopulation.individuals().size();
|
||||
Population elitePopulation = _population.select(elite);
|
||||
size_t replacementCount = _population.individuals().size() - elitePopulation.individuals().size();
|
||||
|
||||
m_population =
|
||||
return
|
||||
move(elitePopulation) +
|
||||
Population::makeRandom(
|
||||
m_population.fitnessMetric(),
|
||||
_population.fitnessMetric(),
|
||||
replacementCount,
|
||||
m_options.minChromosomeLength,
|
||||
m_options.maxChromosomeLength
|
||||
);
|
||||
}
|
||||
|
||||
void GenerationalElitistWithExclusivePools::runNextRound()
|
||||
Population GenerationalElitistWithExclusivePools::runNextRound(Population _population)
|
||||
{
|
||||
double elitePoolSize = 1.0 - (m_options.mutationPoolSize + m_options.crossoverPoolSize);
|
||||
RangeSelection elite(0.0, elitePoolSize);
|
||||
|
||||
m_population =
|
||||
m_population.select(elite) +
|
||||
m_population.select(elite).mutate(
|
||||
return
|
||||
_population.select(elite) +
|
||||
_population.select(elite).mutate(
|
||||
RandomSelection(m_options.mutationPoolSize / elitePoolSize),
|
||||
alternativeMutations(
|
||||
m_options.randomisationChance,
|
||||
@ -70,7 +59,7 @@ void GenerationalElitistWithExclusivePools::runNextRound()
|
||||
)
|
||||
)
|
||||
) +
|
||||
m_population.select(elite).crossover(
|
||||
_population.select(elite).crossover(
|
||||
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize),
|
||||
randomPointCrossover()
|
||||
);
|
||||
|
@ -22,45 +22,25 @@
|
||||
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
/**
|
||||
* Abstract base class for genetic algorithms.
|
||||
*
|
||||
* The main feature is the @a run() method that executes the algorithm, updating the internal
|
||||
* population during each round and printing the results to the stream provided to the constructor.
|
||||
*
|
||||
* Derived classes can provide specific methods for updating the population by implementing
|
||||
* the @a runNextRound() method.
|
||||
* The main feature is the @a runNextRound() method that executes one round of the algorithm,
|
||||
* on the supplied population.
|
||||
*/
|
||||
class GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
GeneticAlgorithm(Population _initialPopulation, std::ostream& _outputStream):
|
||||
m_population(std::move(_initialPopulation)),
|
||||
m_outputStream(_outputStream) {}
|
||||
|
||||
GeneticAlgorithm() {}
|
||||
GeneticAlgorithm(GeneticAlgorithm const&) = delete;
|
||||
GeneticAlgorithm& operator=(GeneticAlgorithm const&) = delete;
|
||||
virtual ~GeneticAlgorithm() = default;
|
||||
|
||||
Population const& population() const { return m_population; }
|
||||
|
||||
void run(std::optional<size_t> _numRounds = std::nullopt);
|
||||
|
||||
/// The method that actually implements the algorithm. Should use @a m_population as input and
|
||||
/// replace it with the updated state after the round.
|
||||
virtual void runNextRound() = 0;
|
||||
|
||||
protected:
|
||||
Population m_population;
|
||||
|
||||
private:
|
||||
std::ostream& m_outputStream;
|
||||
virtual Population runNextRound(Population _population) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -95,18 +75,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
explicit RandomAlgorithm(
|
||||
Population _initialPopulation,
|
||||
std::ostream& _outputStream,
|
||||
Options const& _options
|
||||
):
|
||||
GeneticAlgorithm(_initialPopulation, _outputStream),
|
||||
explicit RandomAlgorithm(Options const& _options):
|
||||
m_options(_options)
|
||||
{
|
||||
assert(_options.isValid());
|
||||
}
|
||||
|
||||
void runNextRound() override;
|
||||
Population runNextRound(Population _population) override;
|
||||
|
||||
private:
|
||||
Options m_options;
|
||||
@ -148,18 +123,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
GenerationalElitistWithExclusivePools(
|
||||
Population _initialPopulation,
|
||||
std::ostream& _outputStream,
|
||||
Options const& _options
|
||||
):
|
||||
GeneticAlgorithm(_initialPopulation, _outputStream),
|
||||
GenerationalElitistWithExclusivePools(Options const& _options):
|
||||
m_options(_options)
|
||||
{
|
||||
assert(_options.isValid());
|
||||
}
|
||||
|
||||
void runNextRound() override;
|
||||
Population runNextRound(Population _population) override;
|
||||
|
||||
private:
|
||||
Options m_options;
|
||||
|
259
tools/yulPhaser/Phaser.cpp
Normal file
259
tools/yulPhaser/Phaser.cpp
Normal file
@ -0,0 +1,259 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/Phaser.h>
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
#include <tools/yulPhaser/Common.h>
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <liblangutil/CharStream.h>
|
||||
|
||||
#include <libsolutil/Assertions.h>
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
map<Algorithm, string> const AlgorithmToStringMap =
|
||||
{
|
||||
{Algorithm::Random, "random"},
|
||||
{Algorithm::GEWEP, "GEWEP"},
|
||||
};
|
||||
map<string, Algorithm> const StringToAlgorithmMap = invertMap(AlgorithmToStringMap);
|
||||
|
||||
}
|
||||
|
||||
istream& phaser::operator>>(istream& _inputStream, Algorithm& _algorithm) { return deserializeChoice(_inputStream, _algorithm, StringToAlgorithmMap); }
|
||||
ostream& phaser::operator<<(ostream& _outputStream, Algorithm _algorithm) { return serializeChoice(_outputStream, _algorithm, AlgorithmToStringMap); }
|
||||
|
||||
GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
||||
{
|
||||
return {
|
||||
_arguments["algorithm"].as<Algorithm>(),
|
||||
};
|
||||
}
|
||||
|
||||
unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
||||
Options const& _options,
|
||||
size_t _populationSize,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
)
|
||||
{
|
||||
assert(_populationSize > 0);
|
||||
|
||||
switch (_options.algorithm)
|
||||
{
|
||||
case Algorithm::Random:
|
||||
return make_unique<RandomAlgorithm>(RandomAlgorithm::Options{
|
||||
/* elitePoolSize = */ 1.0 / _populationSize,
|
||||
/* minChromosomeLength = */ _minChromosomeLength,
|
||||
/* maxChromosomeLength = */ _maxChromosomeLength,
|
||||
});
|
||||
case Algorithm::GEWEP:
|
||||
return make_unique<GenerationalElitistWithExclusivePools>(GenerationalElitistWithExclusivePools::Options{
|
||||
/* mutationPoolSize = */ 0.25,
|
||||
/* crossoverPoolSize = */ 0.25,
|
||||
/* randomisationChance = */ 0.9,
|
||||
/* deletionVsAdditionChance = */ 0.5,
|
||||
/* percentGenesToRandomise = */ 1.0 / _maxChromosomeLength,
|
||||
/* percentGenesToAddOrDelete = */ 1.0 / _maxChromosomeLength,
|
||||
});
|
||||
default:
|
||||
assertThrow(false, solidity::util::Exception, "Invalid Algorithm value.");
|
||||
}
|
||||
}
|
||||
|
||||
unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
||||
Program _program
|
||||
)
|
||||
{
|
||||
return make_unique<ProgramSize>(move(_program), RepetitionCount);
|
||||
}
|
||||
|
||||
Population PopulationFactory::build(
|
||||
shared_ptr<FitnessMetric> _fitnessMetric
|
||||
)
|
||||
{
|
||||
return Population::makeRandom(
|
||||
move(_fitnessMetric),
|
||||
PopulationSize,
|
||||
MinChromosomeLength,
|
||||
MaxChromosomeLength
|
||||
);
|
||||
}
|
||||
|
||||
ProgramFactory::Options ProgramFactory::Options::fromCommandLine(po::variables_map const& _arguments)
|
||||
{
|
||||
return {
|
||||
_arguments["input-file"].as<string>(),
|
||||
};
|
||||
}
|
||||
|
||||
Program ProgramFactory::build(Options const& _options)
|
||||
{
|
||||
CharStream sourceCode = loadSource(_options.inputFile);
|
||||
return Program::load(sourceCode);
|
||||
}
|
||||
|
||||
CharStream ProgramFactory::loadSource(string const& _sourcePath)
|
||||
{
|
||||
assertThrow(boost::filesystem::exists(_sourcePath), InvalidProgram, "Source file does not exist");
|
||||
|
||||
string sourceCode = readFileAsString(_sourcePath);
|
||||
return CharStream(sourceCode, _sourcePath);
|
||||
}
|
||||
|
||||
int Phaser::main(int _argc, char** _argv)
|
||||
{
|
||||
CommandLineParsingResult parsingResult = parseCommandLine(_argc, _argv);
|
||||
if (parsingResult.exitCode != 0)
|
||||
return parsingResult.exitCode;
|
||||
|
||||
initialiseRNG(parsingResult.arguments);
|
||||
|
||||
runAlgorithm(parsingResult.arguments);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
||||
{
|
||||
size_t const lineLength = po::options_description::m_default_line_length;
|
||||
size_t const minDescriptionLength = lineLength - 23;
|
||||
|
||||
po::options_description keywordDescription(
|
||||
"yul-phaser, a tool for finding the best sequence of Yul optimisation phases.\n"
|
||||
"\n"
|
||||
"Usage: yul-phaser [options] <file>\n"
|
||||
"Reads <file> as Yul code and tries to find the best order in which to run optimisation"
|
||||
" phases using a genetic algorithm.\n"
|
||||
"Example:\n"
|
||||
"yul-phaser program.yul\n"
|
||||
"\n"
|
||||
"Allowed options",
|
||||
lineLength,
|
||||
minDescriptionLength
|
||||
);
|
||||
|
||||
po::options_description generalDescription("GENERAL", lineLength, minDescriptionLength);
|
||||
generalDescription.add_options()
|
||||
("help", "Show help message and exit.")
|
||||
("input-file", po::value<string>()->required()->value_name("<PATH>"), "Input file.")
|
||||
("seed", po::value<uint32_t>()->value_name("<NUM>"), "Seed for the random number generator.")
|
||||
;
|
||||
keywordDescription.add(generalDescription);
|
||||
|
||||
po::options_description algorithmDescription("ALGORITHM", lineLength, minDescriptionLength);
|
||||
algorithmDescription.add_options()
|
||||
(
|
||||
"algorithm",
|
||||
po::value<Algorithm>()->value_name("<NAME>")->default_value(Algorithm::GEWEP),
|
||||
"Algorithm"
|
||||
)
|
||||
;
|
||||
keywordDescription.add(algorithmDescription);
|
||||
|
||||
|
||||
po::positional_options_description positionalDescription;
|
||||
positionalDescription.add("input-file", 1);
|
||||
|
||||
return {keywordDescription, positionalDescription};
|
||||
}
|
||||
|
||||
Phaser::CommandLineParsingResult Phaser::parseCommandLine(int _argc, char** _argv)
|
||||
{
|
||||
auto [keywordDescription, positionalDescription] = buildCommandLineDescription();
|
||||
|
||||
po::variables_map arguments;
|
||||
po::notify(arguments);
|
||||
|
||||
try
|
||||
{
|
||||
po::command_line_parser parser(_argc, _argv);
|
||||
parser.options(keywordDescription).positional(positionalDescription);
|
||||
po::store(parser.run(), arguments);
|
||||
}
|
||||
catch (po::error const & _exception)
|
||||
{
|
||||
cerr << _exception.what() << endl;
|
||||
return {1, move(arguments)};
|
||||
}
|
||||
|
||||
if (arguments.count("help") > 0)
|
||||
{
|
||||
cout << keywordDescription << endl;
|
||||
return {2, move(arguments)};
|
||||
}
|
||||
|
||||
if (arguments.count("input-file") == 0)
|
||||
{
|
||||
cerr << "Missing argument: input-file." << endl;
|
||||
return {1, move(arguments)};
|
||||
}
|
||||
|
||||
return {0, arguments};
|
||||
}
|
||||
|
||||
void Phaser::initialiseRNG(po::variables_map const& _arguments)
|
||||
{
|
||||
uint32_t seed;
|
||||
if (_arguments.count("seed") > 0)
|
||||
seed = _arguments["seed"].as<uint32_t>();
|
||||
else
|
||||
seed = SimulationRNG::generateSeed();
|
||||
|
||||
SimulationRNG::reset(seed);
|
||||
cout << "Random seed: " << seed << endl;
|
||||
}
|
||||
|
||||
void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
||||
{
|
||||
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
||||
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
||||
|
||||
Program program = ProgramFactory::build(programOptions);
|
||||
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(move(program));
|
||||
Population population = PopulationFactory::build(move(fitnessMetric));
|
||||
|
||||
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
||||
algorithmOptions,
|
||||
population.individuals().size(),
|
||||
PopulationFactory::MinChromosomeLength,
|
||||
PopulationFactory::MaxChromosomeLength
|
||||
);
|
||||
|
||||
AlgorithmRunner algorithmRunner(population, AlgorithmRunner::Options{}, cout);
|
||||
algorithmRunner.run(*geneticAlgorithm);
|
||||
}
|
153
tools/yulPhaser/Phaser.h
Normal file
153
tools/yulPhaser/Phaser.h
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Contains the main class that controls yul-phaser based on command-line parameters and
|
||||
* associated factories for building instances of phaser's components.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <istream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace solidity::langutil
|
||||
{
|
||||
|
||||
class CharStream;
|
||||
|
||||
}
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
class FitnessMetric;
|
||||
class GeneticAlgorithm;
|
||||
class Population;
|
||||
class Program;
|
||||
|
||||
enum class Algorithm
|
||||
{
|
||||
Random,
|
||||
GEWEP,
|
||||
};
|
||||
|
||||
std::istream& operator>>(std::istream& _inputStream, solidity::phaser::Algorithm& _algorithm);
|
||||
std::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::Algorithm _algorithm);
|
||||
|
||||
/**
|
||||
* Builds and validates instances of @a GeneticAlgorithm and its derived classes.
|
||||
*/
|
||||
class GeneticAlgorithmFactory
|
||||
{
|
||||
public:
|
||||
struct Options
|
||||
{
|
||||
Algorithm algorithm;
|
||||
|
||||
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
||||
};
|
||||
|
||||
static std::unique_ptr<GeneticAlgorithm> build(
|
||||
Options const& _options,
|
||||
size_t _populationSize,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds and validates instances of @a FitnessMetric and its derived classes.
|
||||
*/
|
||||
class FitnessMetricFactory
|
||||
{
|
||||
public:
|
||||
static constexpr size_t RepetitionCount = 5;
|
||||
|
||||
static std::unique_ptr<FitnessMetric> build(
|
||||
Program _program
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds and validates instances of @a Population.
|
||||
*/
|
||||
class PopulationFactory
|
||||
{
|
||||
public:
|
||||
static constexpr size_t PopulationSize = 20;
|
||||
static constexpr size_t MinChromosomeLength = 12;
|
||||
static constexpr size_t MaxChromosomeLength = 30;
|
||||
|
||||
static Population build(
|
||||
std::shared_ptr<FitnessMetric> _fitnessMetric
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds and validates instances of @a Program.
|
||||
*/
|
||||
class ProgramFactory
|
||||
{
|
||||
public:
|
||||
struct Options
|
||||
{
|
||||
std::string inputFile;
|
||||
|
||||
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
||||
};
|
||||
|
||||
static Program build(Options const& _options);
|
||||
|
||||
private:
|
||||
static langutil::CharStream loadSource(std::string const& _sourcePath);
|
||||
};
|
||||
|
||||
/**
|
||||
* Main class that controls yul-phaser based on command-line parameters. The class is responsible
|
||||
* for command-line parsing, initialisation of global objects (like the random number generator),
|
||||
* creating instances of main components using factories and feeding them into @a AlgorithmRunner.
|
||||
*/
|
||||
class Phaser
|
||||
{
|
||||
public:
|
||||
static int main(int argc, char** argv);
|
||||
|
||||
private:
|
||||
struct CommandLineDescription
|
||||
{
|
||||
boost::program_options::options_description keywordDescription;
|
||||
boost::program_options::positional_options_description positionalDescription;
|
||||
};
|
||||
|
||||
struct CommandLineParsingResult
|
||||
{
|
||||
int exitCode;
|
||||
boost::program_options::variables_map arguments;
|
||||
};
|
||||
|
||||
static CommandLineDescription buildCommandLineDescription();
|
||||
static CommandLineParsingResult parseCommandLine(int _argc, char** _argv);
|
||||
static void initialiseRNG(boost::program_options::variables_map const& _arguments);
|
||||
|
||||
static void runAlgorithm(boost::program_options::variables_map const& _arguments);
|
||||
};
|
||||
|
||||
}
|
@ -16,224 +16,19 @@
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <libsolutil/Assertions.h>
|
||||
#include <libsolutil/CommonIO.h>
|
||||
#include <liblangutil/CharStream.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <tools/yulPhaser/Phaser.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::phaser;
|
||||
using namespace solidity::util;
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
enum class Algorithm
|
||||
{
|
||||
Random,
|
||||
GEWEP
|
||||
};
|
||||
|
||||
istream& operator>>(istream& inputStream, Algorithm& algorithm)
|
||||
{
|
||||
string value;
|
||||
inputStream >> value;
|
||||
|
||||
if (value == "random")
|
||||
algorithm = Algorithm::Random;
|
||||
else if (value == "GEWEP")
|
||||
algorithm = Algorithm::GEWEP;
|
||||
else
|
||||
inputStream.setstate(ios_base::failbit);
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& outputStream, Algorithm algorithm)
|
||||
{
|
||||
if (algorithm == Algorithm::Random)
|
||||
outputStream << "random";
|
||||
else if (algorithm == Algorithm::GEWEP)
|
||||
outputStream << "GEWEP";
|
||||
else
|
||||
outputStream.setstate(ios_base::failbit);
|
||||
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct CommandLineParsingResult
|
||||
{
|
||||
int exitCode;
|
||||
po::variables_map arguments;
|
||||
};
|
||||
|
||||
|
||||
void initialiseRNG(po::variables_map const& arguments)
|
||||
{
|
||||
uint32_t seed;
|
||||
if (arguments.count("seed") > 0)
|
||||
seed = arguments["seed"].as<uint32_t>();
|
||||
else
|
||||
seed = SimulationRNG::generateSeed();
|
||||
|
||||
SimulationRNG::reset(seed);
|
||||
cout << "Random seed: " << seed << endl;
|
||||
}
|
||||
|
||||
CharStream loadSource(string const& _sourcePath)
|
||||
{
|
||||
assertThrow(boost::filesystem::exists(_sourcePath), InvalidProgram, "Source file does not exist");
|
||||
|
||||
string sourceCode = readFileAsString(_sourcePath);
|
||||
return CharStream(sourceCode, _sourcePath);
|
||||
}
|
||||
|
||||
void runAlgorithm(string const& _sourcePath, Algorithm _algorithm)
|
||||
{
|
||||
constexpr size_t populationSize = 20;
|
||||
constexpr size_t minChromosomeLength = 12;
|
||||
constexpr size_t maxChromosomeLength = 30;
|
||||
|
||||
CharStream sourceCode = loadSource(_sourcePath);
|
||||
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ProgramSize>(Program::load(sourceCode), 5);
|
||||
auto population = Population::makeRandom(
|
||||
fitnessMetric,
|
||||
populationSize,
|
||||
minChromosomeLength,
|
||||
maxChromosomeLength
|
||||
);
|
||||
|
||||
switch (_algorithm)
|
||||
{
|
||||
case Algorithm::Random:
|
||||
{
|
||||
RandomAlgorithm(
|
||||
population,
|
||||
cout,
|
||||
{
|
||||
/* elitePoolSize = */ 1.0 / populationSize,
|
||||
/* minChromosomeLength = */ minChromosomeLength,
|
||||
/* maxChromosomeLength = */ maxChromosomeLength,
|
||||
}
|
||||
).run();
|
||||
|
||||
break;
|
||||
}
|
||||
case Algorithm::GEWEP:
|
||||
{
|
||||
GenerationalElitistWithExclusivePools(
|
||||
population,
|
||||
cout,
|
||||
{
|
||||
/* mutationPoolSize = */ 0.25,
|
||||
/* crossoverPoolSize = */ 0.25,
|
||||
/* randomisationChance = */ 0.9,
|
||||
/* deletionVsAdditionChance = */ 0.5,
|
||||
/* percentGenesToRandomise = */ 1.0 / maxChromosomeLength,
|
||||
/* percentGenesToAddOrDelete = */ 1.0 / maxChromosomeLength,
|
||||
}
|
||||
).run();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CommandLineParsingResult parseCommandLine(int argc, char** argv)
|
||||
{
|
||||
po::options_description description(
|
||||
"yul-phaser, a tool for finding the best sequence of Yul optimisation phases.\n"
|
||||
"\n"
|
||||
"Usage: yul-phaser [options] <file>\n"
|
||||
"Reads <file> as Yul code and tries to find the best order in which to run optimisation"
|
||||
" phases using a genetic algorithm.\n"
|
||||
"Example:\n"
|
||||
"yul-phaser program.yul\n"
|
||||
"\n"
|
||||
"Allowed options",
|
||||
po::options_description::m_default_line_length,
|
||||
po::options_description::m_default_line_length - 23
|
||||
);
|
||||
|
||||
description.add_options()
|
||||
("help", "Show help message and exit.")
|
||||
("input-file", po::value<string>()->required(), "Input file")
|
||||
("seed", po::value<uint32_t>(), "Seed for the random number generator")
|
||||
(
|
||||
"algorithm",
|
||||
po::value<Algorithm>()->default_value(Algorithm::GEWEP),
|
||||
"Algorithm"
|
||||
)
|
||||
;
|
||||
|
||||
po::positional_options_description positionalDescription;
|
||||
po::variables_map arguments;
|
||||
positionalDescription.add("input-file", 1);
|
||||
po::notify(arguments);
|
||||
|
||||
try
|
||||
{
|
||||
po::command_line_parser parser(argc, argv);
|
||||
parser.options(description).positional(positionalDescription);
|
||||
po::store(parser.run(), arguments);
|
||||
}
|
||||
catch (po::error const & _exception)
|
||||
{
|
||||
cerr << _exception.what() << endl;
|
||||
return {1, move(arguments)};
|
||||
}
|
||||
|
||||
if (arguments.count("help") > 0)
|
||||
{
|
||||
cout << description << endl;
|
||||
return {2, move(arguments)};
|
||||
}
|
||||
|
||||
if (arguments.count("input-file") == 0)
|
||||
{
|
||||
cerr << "Missing argument: input-file." << endl;
|
||||
return {1, move(arguments)};
|
||||
}
|
||||
|
||||
return {0, arguments};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CommandLineParsingResult parsingResult = parseCommandLine(argc, argv);
|
||||
if (parsingResult.exitCode != 0)
|
||||
return parsingResult.exitCode;
|
||||
|
||||
initialiseRNG(parsingResult.arguments);
|
||||
|
||||
try
|
||||
{
|
||||
runAlgorithm(
|
||||
parsingResult.arguments["input-file"].as<string>(),
|
||||
parsingResult.arguments["algorithm"].as<Algorithm>()
|
||||
);
|
||||
return solidity::phaser::Phaser::main(argc, argv);
|
||||
}
|
||||
catch (InvalidProgram const& _exception)
|
||||
catch (solidity::phaser::InvalidProgram const& exception)
|
||||
{
|
||||
cerr << "ERROR: " << _exception.what() << endl;
|
||||
std::cerr << "ERROR: " << exception.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user