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:
@@ -15,6 +15,8 @@ install(TARGETS solidity-upgrade DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(yul-phaser
|
||||
yulPhaser/main.cpp
|
||||
yulPhaser/AlgorithmRunner.h
|
||||
yulPhaser/AlgorithmRunner.cpp
|
||||
yulPhaser/Phaser.h
|
||||
yulPhaser/Phaser.cpp
|
||||
yulPhaser/GeneticAlgorithms.h
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 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;
|
||||
|
||||
+17
-23
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <tools/yulPhaser/Phaser.h>
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
#include <tools/yulPhaser/Exceptions.h>
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
@@ -181,37 +182,30 @@ void Phaser::runAlgorithm(string const& _sourcePath, Algorithm _algorithm)
|
||||
maxChromosomeLength
|
||||
);
|
||||
|
||||
AlgorithmRunner algorithmRunner(population, cout);
|
||||
switch (_algorithm)
|
||||
{
|
||||
case Algorithm::Random:
|
||||
{
|
||||
RandomAlgorithm(
|
||||
population,
|
||||
cout,
|
||||
{
|
||||
/* elitePoolSize = */ 1.0 / populationSize,
|
||||
/* minChromosomeLength = */ minChromosomeLength,
|
||||
/* maxChromosomeLength = */ maxChromosomeLength,
|
||||
}
|
||||
).run();
|
||||
|
||||
RandomAlgorithm algorithm({
|
||||
/* elitePoolSize = */ 1.0 / populationSize,
|
||||
/* minChromosomeLength = */ minChromosomeLength,
|
||||
/* maxChromosomeLength = */ maxChromosomeLength,
|
||||
});
|
||||
algorithmRunner.run(algorithm);
|
||||
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();
|
||||
|
||||
GenerationalElitistWithExclusivePools algorithm({
|
||||
/* mutationPoolSize = */ 0.25,
|
||||
/* crossoverPoolSize = */ 0.25,
|
||||
/* randomisationChance = */ 0.9,
|
||||
/* deletionVsAdditionChance = */ 0.5,
|
||||
/* percentGenesToRandomise = */ 1.0 / maxChromosomeLength,
|
||||
/* percentGenesToAddOrDelete = */ 1.0 / maxChromosomeLength,
|
||||
});
|
||||
algorithmRunner.run(algorithm);
|
||||
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
|
||||
* 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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user