mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
785f65d0f5
- I need some sample .yul files for testing but I see that existing tests generally have source code hard-coded in them rather than in standalone .yul files. There are lots of .yul files but they seem to be automatically processed by a special test case rather loaded ad-hoc by manually created tests. - Program and Population required a file name until now. I'm making them accept loaded source code to be able to give them data hard-coded in a test.
90 lines
2.8 KiB
C++
90 lines
2.8 KiB
C++
/*
|
|
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/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <tools/yulPhaser/Chromosome.h>
|
|
#include <tools/yulPhaser/Random.h>
|
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <vector>
|
|
|
|
namespace solidity::phaser
|
|
{
|
|
|
|
/**
|
|
* Information describing the state of an individual member of the population during the course
|
|
* of the genetic algorithm.
|
|
*/
|
|
struct Individual
|
|
{
|
|
Chromosome chromosome;
|
|
std::optional<size_t> fitness = std::nullopt;
|
|
|
|
friend std::ostream& operator<<(std::ostream& _stream, Individual const& _individual);
|
|
};
|
|
|
|
/**
|
|
* Represents a changing set of individuals undergoing a genetic algorithm.
|
|
* Each round of the algorithm involves mutating existing individuals, evaluating their fitness
|
|
* and selecting the best ones for the next round.
|
|
*
|
|
* An individual is a sequence of optimiser steps represented by a @a Chromosome instance. The whole
|
|
* population is associated with a fixed Yul program. By loading the source code into a @a Program
|
|
* instance the class can compute fitness of the individual.
|
|
*/
|
|
class Population
|
|
{
|
|
public:
|
|
static constexpr size_t MaxChromosomeLength = 30;
|
|
|
|
explicit Population(langutil::CharStream _sourceCode, std::vector<Chromosome> const& _chromosomes = {});
|
|
static Population makeRandom(langutil::CharStream _sourceCode, size_t _size);
|
|
|
|
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
|
|
|
std::vector<Individual> const& individuals() const { return m_individuals; }
|
|
|
|
static size_t randomChromosomeLength() { return binomialRandomInt(MaxChromosomeLength, 0.5); }
|
|
static size_t measureFitness(Chromosome const& _chromosome, langutil::CharStream& _sourceCode);
|
|
|
|
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
|
|
|
|
private:
|
|
explicit Population(langutil::CharStream _sourceCode, std::vector<Individual> _individuals = {}):
|
|
m_sourceCode{std::move(_sourceCode)},
|
|
m_individuals{std::move(_individuals)} {}
|
|
|
|
void doMutation();
|
|
void doEvaluation();
|
|
void doSelection();
|
|
|
|
static void randomizeWorstChromosomes(
|
|
std::vector<Individual>& _individuals,
|
|
size_t _count
|
|
);
|
|
|
|
langutil::CharStream m_sourceCode;
|
|
|
|
std::vector<Individual> m_individuals;
|
|
};
|
|
|
|
}
|