mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Customizable chromosome length in makeRandom()
This commit is contained in:
@@ -55,15 +55,33 @@ Population::Population(Program _program, vector<Chromosome> const& _chromosomes)
|
||||
m_individuals.push_back({chromosome});
|
||||
}
|
||||
|
||||
Population Population::makeRandom(Program _program, size_t _size)
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
function<size_t()> _chromosomeLengthGenerator
|
||||
)
|
||||
{
|
||||
vector<Individual> individuals;
|
||||
for (size_t i = 0; i < _size; ++i)
|
||||
individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())});
|
||||
individuals.push_back({Chromosome::makeRandom(_chromosomeLengthGenerator())});
|
||||
|
||||
return Population(move(_program), individuals);
|
||||
}
|
||||
|
||||
Population Population::makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
)
|
||||
{
|
||||
return makeRandom(
|
||||
move(_program),
|
||||
_size,
|
||||
std::bind(uniformChromosomeLength, _minChromosomeLength, _maxChromosomeLength)
|
||||
);
|
||||
}
|
||||
|
||||
size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program)
|
||||
{
|
||||
Program programCopy = _program;
|
||||
@@ -130,6 +148,6 @@ void Population::randomizeWorstChromosomes(
|
||||
auto individual = _individuals.begin() + (_individuals.size() - _count);
|
||||
for (; individual != _individuals.end(); ++individual)
|
||||
{
|
||||
*individual = {Chromosome::makeRandom(randomChromosomeLength())};
|
||||
*individual = {Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength))};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,13 +55,25 @@ public:
|
||||
static constexpr size_t MaxChromosomeLength = 30;
|
||||
|
||||
explicit Population(Program _program, std::vector<Chromosome> const& _chromosomes = {});
|
||||
static Population makeRandom(Program _program, size_t _size);
|
||||
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
std::function<size_t()> _chromosomeLengthGenerator
|
||||
);
|
||||
static Population makeRandom(
|
||||
Program _program,
|
||||
size_t _size,
|
||||
size_t _minChromosomeLength,
|
||||
size_t _maxChromosomeLength
|
||||
);
|
||||
|
||||
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
||||
|
||||
std::vector<Individual> const& individuals() const { return m_individuals; }
|
||||
|
||||
static size_t randomChromosomeLength() { return SimulationRNG::binomialInt(MaxChromosomeLength, 0.5); }
|
||||
static size_t uniformChromosomeLength(size_t _min, size_t _max) { return SimulationRNG::uniformInt(_min, _max); }
|
||||
static size_t binomialChromosomeLength(size_t _max) { return SimulationRNG::binomialInt(_max, 0.5); }
|
||||
static size_t measureFitness(Chromosome const& _chromosome, Program const& _program);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
@@ -70,7 +71,11 @@ CharStream loadSource(string const& _sourcePath)
|
||||
void runAlgorithm(string const& _sourcePath)
|
||||
{
|
||||
CharStream sourceCode = loadSource(_sourcePath);
|
||||
auto population = Population::makeRandom(Program::load(sourceCode), 10);
|
||||
auto population = Population::makeRandom(
|
||||
Program::load(sourceCode),
|
||||
10,
|
||||
bind(Population::binomialChromosomeLength, Population::MaxChromosomeLength)
|
||||
);
|
||||
population.run(nullopt, cout);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user