2020-01-17 06:45:10 +00:00
|
|
|
/*
|
|
|
|
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/Population.h>
|
|
|
|
|
|
|
|
#include <tools/yulPhaser/Program.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
2020-01-22 19:14:36 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-01-17 06:45:10 +00:00
|
|
|
using namespace solidity::phaser;
|
|
|
|
|
|
|
|
namespace solidity::phaser
|
|
|
|
{
|
|
|
|
|
|
|
|
ostream& operator<<(ostream& _stream, Individual const& _individual);
|
|
|
|
ostream& operator<<(ostream& _stream, Population const& _population);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
|
|
|
|
{
|
|
|
|
_stream << "Fitness: ";
|
|
|
|
if (_individual.fitness.has_value())
|
|
|
|
_stream << _individual.fitness.value();
|
|
|
|
else
|
|
|
|
_stream << "<NONE>";
|
|
|
|
_stream << ", optimisations: " << _individual.chromosome;
|
|
|
|
|
|
|
|
return _stream;
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:26:54 +00:00
|
|
|
Population::Population(Program _program, vector<Chromosome> const& _chromosomes):
|
|
|
|
m_program{move(_program)}
|
2020-01-17 06:45:10 +00:00
|
|
|
{
|
|
|
|
for (auto const& chromosome: _chromosomes)
|
|
|
|
m_individuals.push_back({chromosome});
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:26:54 +00:00
|
|
|
Population Population::makeRandom(Program _program, size_t _size)
|
2020-01-17 06:45:10 +00:00
|
|
|
{
|
|
|
|
vector<Individual> individuals;
|
|
|
|
for (size_t i = 0; i < _size; ++i)
|
|
|
|
individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())});
|
|
|
|
|
2020-01-31 12:26:54 +00:00
|
|
|
return Population(move(_program), individuals);
|
2020-01-17 06:45:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 12:26:54 +00:00
|
|
|
size_t Population::measureFitness(Chromosome const& _chromosome, Program const& _program)
|
2020-01-17 06:45:10 +00:00
|
|
|
{
|
2020-01-31 12:26:54 +00:00
|
|
|
Program programCopy = _program;
|
|
|
|
programCopy.optimise(_chromosome.optimisationSteps());
|
|
|
|
return programCopy.codeSize();
|
2020-01-17 06:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
|
|
|
|
{
|
|
|
|
doEvaluation();
|
|
|
|
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
|
|
|
|
{
|
|
|
|
doMutation();
|
|
|
|
doSelection();
|
|
|
|
doEvaluation();
|
|
|
|
|
|
|
|
_outputStream << "---------- ROUND " << round << " ----------" << endl;
|
|
|
|
_outputStream << *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& phaser::operator<<(ostream& _stream, Population const& _population)
|
|
|
|
{
|
|
|
|
auto individual = _population.m_individuals.begin();
|
|
|
|
for (; individual != _population.m_individuals.end(); ++individual)
|
|
|
|
_stream << *individual << endl;
|
|
|
|
|
|
|
|
return _stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Population::doMutation()
|
|
|
|
{
|
|
|
|
// TODO: Implement mutation and crossover
|
|
|
|
}
|
|
|
|
|
|
|
|
void Population::doEvaluation()
|
|
|
|
{
|
|
|
|
for (auto& individual: m_individuals)
|
|
|
|
if (!individual.fitness.has_value())
|
2020-01-31 12:26:54 +00:00
|
|
|
individual.fitness = measureFitness(individual.chromosome, m_program);
|
2020-01-17 06:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Population::doSelection()
|
|
|
|
{
|
|
|
|
assert(all_of(m_individuals.begin(), m_individuals.end(), [](auto& i){ return i.fitness.has_value(); }));
|
|
|
|
|
|
|
|
sort(
|
|
|
|
m_individuals.begin(),
|
|
|
|
m_individuals.end(),
|
|
|
|
[](auto const& a, auto const& b){ return a.fitness.value() < b.fitness.value(); }
|
|
|
|
);
|
|
|
|
|
|
|
|
randomizeWorstChromosomes(m_individuals, m_individuals.size() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Population::randomizeWorstChromosomes(
|
|
|
|
vector<Individual>& _individuals,
|
|
|
|
size_t _count
|
|
|
|
)
|
|
|
|
{
|
|
|
|
assert(_individuals.size() >= _count);
|
|
|
|
// ASSUMPTION: _individuals is sorted in ascending order
|
|
|
|
|
|
|
|
auto individual = _individuals.begin() + (_individuals.size() - _count);
|
|
|
|
for (; individual != _individuals.end(); ++individual)
|
|
|
|
{
|
|
|
|
*individual = {Chromosome::makeRandom(randomChromosomeLength())};
|
|
|
|
}
|
|
|
|
}
|