mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Base class for genetic algorithms
This commit is contained in:
parent
26bae6b459
commit
11bdf358df
@ -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/GeneticAlgorithms.cpp
|
||||||
yulPhaser/Population.cpp
|
yulPhaser/Population.cpp
|
||||||
yulPhaser/Program.cpp
|
yulPhaser/Program.cpp
|
||||||
yulPhaser/SimulationRNG.cpp
|
yulPhaser/SimulationRNG.cpp
|
||||||
@ -153,6 +154,7 @@ set(yul_phaser_sources
|
|||||||
# 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/Chromosome.cpp
|
../tools/yulPhaser/Chromosome.cpp
|
||||||
../tools/yulPhaser/FitnessMetrics.cpp
|
../tools/yulPhaser/FitnessMetrics.cpp
|
||||||
|
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||||
../tools/yulPhaser/Population.cpp
|
../tools/yulPhaser/Population.cpp
|
||||||
../tools/yulPhaser/Program.cpp
|
../tools/yulPhaser/Program.cpp
|
||||||
../tools/yulPhaser/SimulationRNG.cpp
|
../tools/yulPhaser/SimulationRNG.cpp
|
||||||
|
96
test/yulPhaser/GeneticAlgorithms.cpp
Normal file
96
test/yulPhaser/GeneticAlgorithms.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
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/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 <vector>
|
||||||
|
|
||||||
|
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_END()
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
}
|
@ -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/GeneticAlgorithms.h
|
||||||
|
yulPhaser/GeneticAlgorithms.cpp
|
||||||
yulPhaser/Population.h
|
yulPhaser/Population.h
|
||||||
yulPhaser/Population.cpp
|
yulPhaser/Population.cpp
|
||||||
yulPhaser/FitnessMetrics.h
|
yulPhaser/FitnessMetrics.h
|
||||||
|
32
tools/yulPhaser/GeneticAlgorithms.cpp
Normal file
32
tools/yulPhaser/GeneticAlgorithms.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/GeneticAlgorithms.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
66
tools/yulPhaser/GeneticAlgorithms.h
Normal file
66
tools/yulPhaser/GeneticAlgorithms.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
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 an abstract base class representing a genetic algorithm and its concrete implementations.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#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.
|
||||||
|
*/
|
||||||
|
class GeneticAlgorithm
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GeneticAlgorithm(Population _initialPopulation, std::ostream& _outputStream):
|
||||||
|
m_population(std::move(_initialPopulation)),
|
||||||
|
m_outputStream(_outputStream) {}
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user