mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Extract the code that controls execution of algorithm rounds from GeneticAlgorithm into AlgorithmRunner
This commit is contained in:
parent
0c1b88508e
commit
b8244f6a43
@ -144,6 +144,7 @@ set(yul_phaser_sources
|
|||||||
yulPhaser/CommonTest.cpp
|
yulPhaser/CommonTest.cpp
|
||||||
yulPhaser/Chromosome.cpp
|
yulPhaser/Chromosome.cpp
|
||||||
yulPhaser/FitnessMetrics.cpp
|
yulPhaser/FitnessMetrics.cpp
|
||||||
|
yulPhaser/AlgorithmRunner.cpp
|
||||||
yulPhaser/GeneticAlgorithms.cpp
|
yulPhaser/GeneticAlgorithms.cpp
|
||||||
yulPhaser/Mutations.cpp
|
yulPhaser/Mutations.cpp
|
||||||
yulPhaser/PairSelections.cpp
|
yulPhaser/PairSelections.cpp
|
||||||
@ -155,6 +156,7 @@ set(yul_phaser_sources
|
|||||||
# FIXME: yul-phaser is not a library so I can't just add it to target_link_libraries().
|
# 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
|
# 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.
|
# 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/Chromosome.cpp
|
||||||
../tools/yulPhaser/FitnessMetrics.cpp
|
../tools/yulPhaser/FitnessMetrics.cpp
|
||||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||||
|
93
test/yulPhaser/AlgorithmRunner.cpp
Normal file
93
test/yulPhaser/AlgorithmRunner.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/Common.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;
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
|
BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest)
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture)
|
||||||
|
{
|
||||||
|
AlgorithmRunner runner(Population(m_fitnessMetric), m_output);
|
||||||
|
DummyAlgorithm algorithm;
|
||||||
|
|
||||||
|
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||||
|
runner.run(algorithm, 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)
|
||||||
|
{
|
||||||
|
// run() is allowed to print more but should at least print the first one
|
||||||
|
|
||||||
|
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_output
|
||||||
|
);
|
||||||
|
|
||||||
|
DummyAlgorithm algorithm;
|
||||||
|
|
||||||
|
BOOST_TEST(m_output.is_empty());
|
||||||
|
runner.run(algorithm, 1);
|
||||||
|
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1);
|
||||||
|
runner.run(algorithm, 3);
|
||||||
|
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
}
|
@ -20,14 +20,10 @@
|
|||||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||||
#include <tools/yulPhaser/Population.h>
|
#include <tools/yulPhaser/Population.h>
|
||||||
#include <tools/yulPhaser/Program.h>
|
|
||||||
|
|
||||||
#include <liblangutil/CharStream.h>
|
|
||||||
|
|
||||||
#include <libsolutil/CommonIO.h>
|
#include <libsolutil/CommonIO.h>
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <boost/test/tools/output_test_stream.hpp>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -35,102 +31,58 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace boost::unit_test::framework;
|
using namespace boost::unit_test::framework;
|
||||||
using namespace boost::test_tools;
|
using namespace boost::test_tools;
|
||||||
using namespace solidity::langutil;
|
|
||||||
using namespace solidity::util;
|
|
||||||
|
|
||||||
namespace solidity::phaser::test
|
namespace solidity::phaser::test
|
||||||
{
|
{
|
||||||
|
|
||||||
class DummyAlgorithm: public GeneticAlgorithm
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using GeneticAlgorithm::GeneticAlgorithm;
|
|
||||||
void runNextRound() override { ++m_currentRound; }
|
|
||||||
|
|
||||||
size_t m_currentRound = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class GeneticAlgorithmFixture
|
class GeneticAlgorithmFixture
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||||
output_test_stream m_output;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmsTest)
|
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_AUTO_TEST_SUITE(RandomAlgorithmTest)
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_randomise_rest_of_population, GeneticAlgorithmFixture)
|
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);
|
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(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
RandomAlgorithm algorithm({0.5, 1, 1});
|
||||||
|
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
|
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)
|
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);
|
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(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
RandomAlgorithm algorithm({0.5, 7, 7});
|
||||||
|
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
|
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)
|
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);
|
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(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
RandomAlgorithm algorithm({0.0, 1, 1});
|
||||||
|
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
|
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)
|
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);
|
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(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
RandomAlgorithm algorithm({1.0, 1, 1});
|
||||||
|
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
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)
|
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);
|
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 = {
|
GenerationalElitistWithExclusivePools::Options options = {
|
||||||
/* mutationPoolSize = */ 0.2,
|
/* mutationPoolSize = */ 0.2,
|
||||||
@ -148,17 +101,17 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_o
|
|||||||
/* percentGenesToRandomise = */ 0.0,
|
/* percentGenesToRandomise = */ 0.0,
|
||||||
/* percentGenesToAddOrDelete = */ 1.0,
|
/* percentGenesToAddOrDelete = */ 1.0,
|
||||||
};
|
};
|
||||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
GenerationalElitistWithExclusivePools algorithm(options);
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
|
||||||
|
|
||||||
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)
|
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);
|
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 = {
|
GenerationalElitistWithExclusivePools::Options options = {
|
||||||
/* mutationPoolSize = */ 0.2,
|
/* mutationPoolSize = */ 0.2,
|
||||||
@ -168,12 +121,11 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individ
|
|||||||
/* percentGenesToRandomise = */ 0.0,
|
/* percentGenesToRandomise = */ 0.0,
|
||||||
/* percentGenesToAddOrDelete = */ 1.0,
|
/* percentGenesToAddOrDelete = */ 1.0,
|
||||||
};
|
};
|
||||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
GenerationalElitistWithExclusivePools algorithm(options);
|
||||||
assert(chromosomeLengths(algorithm.population()) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
|
|
||||||
|
|
||||||
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)
|
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,
|
/* percentGenesToRandomise = */ 1.0,
|
||||||
/* percentGenesToAddOrDelete = */ 1.0,
|
/* percentGenesToAddOrDelete = */ 1.0,
|
||||||
};
|
};
|
||||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
GenerationalElitistWithExclusivePools algorithm(options);
|
||||||
|
|
||||||
SimulationRNG::reset(1);
|
SimulationRNG::reset(1);
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
|
|
||||||
BOOST_TEST((
|
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}
|
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(m_fitnessMetric, {Chromosome("aa"), Chromosome("ff")}) +
|
||||||
Population::makeRandom(m_fitnessMetric, 8, 6, 6)
|
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 = {
|
GenerationalElitistWithExclusivePools::Options options = {
|
||||||
/* mutationPoolSize = */ 0.0,
|
/* mutationPoolSize = */ 0.0,
|
||||||
@ -214,14 +167,13 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
|
|||||||
/* percentGenesToRandomise = */ 0.0,
|
/* percentGenesToRandomise = */ 0.0,
|
||||||
/* percentGenesToAddOrDelete = */ 0.0,
|
/* percentGenesToAddOrDelete = */ 0.0,
|
||||||
};
|
};
|
||||||
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
|
GenerationalElitistWithExclusivePools algorithm(options);
|
||||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
|
|
||||||
|
|
||||||
SimulationRNG::reset(1);
|
SimulationRNG::reset(1);
|
||||||
algorithm.runNextRound();
|
Population newPopulation = algorithm.runNextRound(population);
|
||||||
|
|
||||||
vector<Individual> const& newIndividuals = algorithm.population().individuals();
|
vector<Individual> const& newIndividuals = newPopulation.individuals();
|
||||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
|
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
|
||||||
for (auto& individual: newIndividuals)
|
for (auto& individual: newIndividuals)
|
||||||
BOOST_TEST((
|
BOOST_TEST((
|
||||||
individual.chromosome == Chromosome("aa") ||
|
individual.chromosome == Chromosome("aa") ||
|
||||||
|
@ -15,6 +15,8 @@ install(TARGETS solidity-upgrade DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
|||||||
|
|
||||||
add_executable(yul-phaser
|
add_executable(yul-phaser
|
||||||
yulPhaser/main.cpp
|
yulPhaser/main.cpp
|
||||||
|
yulPhaser/AlgorithmRunner.h
|
||||||
|
yulPhaser/AlgorithmRunner.cpp
|
||||||
yulPhaser/Phaser.h
|
yulPhaser/Phaser.h
|
||||||
yulPhaser/Phaser.cpp
|
yulPhaser/Phaser.cpp
|
||||||
yulPhaser/GeneticAlgorithms.h
|
yulPhaser/GeneticAlgorithms.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, optional<size_t> _numRounds)
|
||||||
|
{
|
||||||
|
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
|
||||||
|
{
|
||||||
|
m_population = _algorithm.runNextRound(m_population);
|
||||||
|
|
||||||
|
m_outputStream << "---------- ROUND " << round << " ----------" << endl;
|
||||||
|
m_outputStream << m_population;
|
||||||
|
}
|
||||||
|
}
|
58
tools/yulPhaser/AlgorithmRunner.h
Normal file
58
tools/yulPhaser/AlgorithmRunner.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
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:
|
||||||
|
AlgorithmRunner(
|
||||||
|
Population _initialPopulation,
|
||||||
|
std::ostream& _outputStream
|
||||||
|
):
|
||||||
|
m_population(std::move(_initialPopulation)),
|
||||||
|
m_outputStream(_outputStream) {}
|
||||||
|
|
||||||
|
void run(GeneticAlgorithm& _algorithm, std::optional<size_t> _numRounds = std::nullopt);
|
||||||
|
|
||||||
|
Population const& population() const { return m_population; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Population m_population;
|
||||||
|
std::ostream& m_outputStream;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -23,42 +23,31 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace solidity::phaser;
|
using namespace solidity::phaser;
|
||||||
|
|
||||||
void GeneticAlgorithm::run(optional<size_t> _numRounds)
|
Population RandomAlgorithm::runNextRound(Population _population)
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
RangeSelection elite(0.0, m_options.elitePoolSize);
|
RangeSelection elite(0.0, m_options.elitePoolSize);
|
||||||
|
|
||||||
Population elitePopulation = m_population.select(elite);
|
Population elitePopulation = _population.select(elite);
|
||||||
size_t replacementCount = m_population.individuals().size() - elitePopulation.individuals().size();
|
size_t replacementCount = _population.individuals().size() - elitePopulation.individuals().size();
|
||||||
|
|
||||||
m_population =
|
return
|
||||||
move(elitePopulation) +
|
move(elitePopulation) +
|
||||||
Population::makeRandom(
|
Population::makeRandom(
|
||||||
m_population.fitnessMetric(),
|
_population.fitnessMetric(),
|
||||||
replacementCount,
|
replacementCount,
|
||||||
m_options.minChromosomeLength,
|
m_options.minChromosomeLength,
|
||||||
m_options.maxChromosomeLength
|
m_options.maxChromosomeLength
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerationalElitistWithExclusivePools::runNextRound()
|
Population GenerationalElitistWithExclusivePools::runNextRound(Population _population)
|
||||||
{
|
{
|
||||||
double elitePoolSize = 1.0 - (m_options.mutationPoolSize + m_options.crossoverPoolSize);
|
double elitePoolSize = 1.0 - (m_options.mutationPoolSize + m_options.crossoverPoolSize);
|
||||||
RangeSelection elite(0.0, elitePoolSize);
|
RangeSelection elite(0.0, elitePoolSize);
|
||||||
|
|
||||||
m_population =
|
return
|
||||||
m_population.select(elite) +
|
_population.select(elite) +
|
||||||
m_population.select(elite).mutate(
|
_population.select(elite).mutate(
|
||||||
RandomSelection(m_options.mutationPoolSize / elitePoolSize),
|
RandomSelection(m_options.mutationPoolSize / elitePoolSize),
|
||||||
alternativeMutations(
|
alternativeMutations(
|
||||||
m_options.randomisationChance,
|
m_options.randomisationChance,
|
||||||
@ -70,7 +59,7 @@ void GenerationalElitistWithExclusivePools::runNextRound()
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
) +
|
) +
|
||||||
m_population.select(elite).crossover(
|
_population.select(elite).crossover(
|
||||||
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize),
|
RandomPairSelection(m_options.crossoverPoolSize / elitePoolSize),
|
||||||
randomPointCrossover()
|
randomPointCrossover()
|
||||||
);
|
);
|
||||||
|
@ -22,45 +22,25 @@
|
|||||||
|
|
||||||
#include <tools/yulPhaser/Population.h>
|
#include <tools/yulPhaser/Population.h>
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <ostream>
|
|
||||||
|
|
||||||
namespace solidity::phaser
|
namespace solidity::phaser
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for genetic algorithms.
|
* Abstract base class for genetic algorithms.
|
||||||
*
|
* The main feature is the @a runNextRound() method that executes one round of the algorithm,
|
||||||
* The main feature is the @a run() method that executes the algorithm, updating the internal
|
* on the supplied population.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
class GeneticAlgorithm
|
class GeneticAlgorithm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GeneticAlgorithm(Population _initialPopulation, std::ostream& _outputStream):
|
GeneticAlgorithm() {}
|
||||||
m_population(std::move(_initialPopulation)),
|
|
||||||
m_outputStream(_outputStream) {}
|
|
||||||
|
|
||||||
GeneticAlgorithm(GeneticAlgorithm const&) = delete;
|
GeneticAlgorithm(GeneticAlgorithm const&) = delete;
|
||||||
GeneticAlgorithm& operator=(GeneticAlgorithm const&) = delete;
|
GeneticAlgorithm& operator=(GeneticAlgorithm const&) = delete;
|
||||||
virtual ~GeneticAlgorithm() = default;
|
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
|
/// The method that actually implements the algorithm. Should use @a m_population as input and
|
||||||
/// replace it with the updated state after the round.
|
/// replace it with the updated state after the round.
|
||||||
virtual void runNextRound() = 0;
|
virtual Population runNextRound(Population _population) = 0;
|
||||||
|
|
||||||
protected:
|
|
||||||
Population m_population;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::ostream& m_outputStream;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,18 +75,13 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit RandomAlgorithm(
|
explicit RandomAlgorithm(Options const& _options):
|
||||||
Population _initialPopulation,
|
|
||||||
std::ostream& _outputStream,
|
|
||||||
Options const& _options
|
|
||||||
):
|
|
||||||
GeneticAlgorithm(_initialPopulation, _outputStream),
|
|
||||||
m_options(_options)
|
m_options(_options)
|
||||||
{
|
{
|
||||||
assert(_options.isValid());
|
assert(_options.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void runNextRound() override;
|
Population runNextRound(Population _population) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Options m_options;
|
Options m_options;
|
||||||
@ -148,18 +123,13 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GenerationalElitistWithExclusivePools(
|
GenerationalElitistWithExclusivePools(Options const& _options):
|
||||||
Population _initialPopulation,
|
|
||||||
std::ostream& _outputStream,
|
|
||||||
Options const& _options
|
|
||||||
):
|
|
||||||
GeneticAlgorithm(_initialPopulation, _outputStream),
|
|
||||||
m_options(_options)
|
m_options(_options)
|
||||||
{
|
{
|
||||||
assert(_options.isValid());
|
assert(_options.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void runNextRound() override;
|
Population runNextRound(Population _population) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Options m_options;
|
Options m_options;
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <tools/yulPhaser/Phaser.h>
|
#include <tools/yulPhaser/Phaser.h>
|
||||||
|
|
||||||
|
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||||
#include <tools/yulPhaser/Exceptions.h>
|
#include <tools/yulPhaser/Exceptions.h>
|
||||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||||
@ -181,37 +182,30 @@ void Phaser::runAlgorithm(string const& _sourcePath, Algorithm _algorithm)
|
|||||||
maxChromosomeLength
|
maxChromosomeLength
|
||||||
);
|
);
|
||||||
|
|
||||||
|
AlgorithmRunner algorithmRunner(population, cout);
|
||||||
switch (_algorithm)
|
switch (_algorithm)
|
||||||
{
|
{
|
||||||
case Algorithm::Random:
|
case Algorithm::Random:
|
||||||
{
|
{
|
||||||
RandomAlgorithm(
|
RandomAlgorithm algorithm({
|
||||||
population,
|
/* elitePoolSize = */ 1.0 / populationSize,
|
||||||
cout,
|
/* minChromosomeLength = */ minChromosomeLength,
|
||||||
{
|
/* maxChromosomeLength = */ maxChromosomeLength,
|
||||||
/* elitePoolSize = */ 1.0 / populationSize,
|
});
|
||||||
/* minChromosomeLength = */ minChromosomeLength,
|
algorithmRunner.run(algorithm);
|
||||||
/* maxChromosomeLength = */ maxChromosomeLength,
|
|
||||||
}
|
|
||||||
).run();
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Algorithm::GEWEP:
|
case Algorithm::GEWEP:
|
||||||
{
|
{
|
||||||
GenerationalElitistWithExclusivePools(
|
GenerationalElitistWithExclusivePools algorithm({
|
||||||
population,
|
/* mutationPoolSize = */ 0.25,
|
||||||
cout,
|
/* crossoverPoolSize = */ 0.25,
|
||||||
{
|
/* randomisationChance = */ 0.9,
|
||||||
/* mutationPoolSize = */ 0.25,
|
/* deletionVsAdditionChance = */ 0.5,
|
||||||
/* crossoverPoolSize = */ 0.25,
|
/* percentGenesToRandomise = */ 1.0 / maxChromosomeLength,
|
||||||
/* randomisationChance = */ 0.9,
|
/* percentGenesToAddOrDelete = */ 1.0 / maxChromosomeLength,
|
||||||
/* deletionVsAdditionChance = */ 0.5,
|
});
|
||||||
/* percentGenesToRandomise = */ 1.0 / maxChromosomeLength,
|
algorithmRunner.run(algorithm);
|
||||||
/* percentGenesToAddOrDelete = */ 1.0 / maxChromosomeLength,
|
|
||||||
}
|
|
||||||
).run();
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ std::ostream& operator<<(std::ostream& _outputStream, solidity::phaser::Algorith
|
|||||||
/**
|
/**
|
||||||
* Main class that controls yul-phaser based on command-line parameters. The class is responsible
|
* 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),
|
* for command-line parsing, initialisation of global objects (like the random number generator),
|
||||||
* creating instances of main components and running the genetic algorithm.
|
* creating instances of main components and feeding them into @a AlgorithmRunner.
|
||||||
*/
|
*/
|
||||||
class Phaser
|
class Phaser
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user