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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-01-17 06:45:10 +00:00
|
|
|
|
|
|
|
#include <tools/yulPhaser/Population.h>
|
|
|
|
|
2020-02-05 15:55:50 +00:00
|
|
|
#include <tools/yulPhaser/PairSelections.h>
|
2020-02-05 15:55:50 +00:00
|
|
|
#include <tools/yulPhaser/Selections.h>
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-05 15:51:43 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-02-11 21:25:17 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
2020-01-22 19:14:36 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-02-11 21:25:17 +00:00
|
|
|
using namespace solidity::util;
|
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)
|
|
|
|
{
|
2020-03-09 18:24:37 +00:00
|
|
|
_stream << _individual.fitness << " " << _individual.chromosome;
|
2020-01-17 06:45:10 +00:00
|
|
|
|
|
|
|
return _stream;
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:20:01 +00:00
|
|
|
bool phaser::isFitter(Individual const& a, Individual const& b)
|
|
|
|
{
|
2020-02-11 21:25:17 +00:00
|
|
|
return (
|
2020-02-05 15:18:53 +00:00
|
|
|
(a.fitness < b.fitness) ||
|
|
|
|
(a.fitness == b.fitness && a.chromosome.length() < b.chromosome.length()) ||
|
2020-09-11 18:18:09 +00:00
|
|
|
(a.fitness == b.fitness && a.chromosome.length() == b.chromosome.length() && a.chromosome.genes() < b.chromosome.genes())
|
2020-02-11 21:25:17 +00:00
|
|
|
);
|
2020-02-11 18:20:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 05:19:55 +00:00
|
|
|
Population Population::makeRandom(
|
2020-02-26 18:58:14 +00:00
|
|
|
shared_ptr<FitnessMetric> _fitnessMetric,
|
2020-02-06 05:19:55 +00:00
|
|
|
size_t _size,
|
|
|
|
function<size_t()> _chromosomeLengthGenerator
|
|
|
|
)
|
2020-01-17 06:45:10 +00:00
|
|
|
{
|
2020-02-05 15:18:53 +00:00
|
|
|
vector<Chromosome> chromosomes;
|
2020-01-17 06:45:10 +00:00
|
|
|
for (size_t i = 0; i < _size; ++i)
|
2020-02-05 15:18:53 +00:00
|
|
|
chromosomes.push_back(Chromosome::makeRandom(_chromosomeLengthGenerator()));
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-05 15:18:53 +00:00
|
|
|
return Population(move(_fitnessMetric), move(chromosomes));
|
2020-01-17 06:45:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 05:19:55 +00:00
|
|
|
Population Population::makeRandom(
|
2020-02-26 18:58:14 +00:00
|
|
|
shared_ptr<FitnessMetric> _fitnessMetric,
|
2020-02-06 05:19:55 +00:00
|
|
|
size_t _size,
|
|
|
|
size_t _minChromosomeLength,
|
|
|
|
size_t _maxChromosomeLength
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return makeRandom(
|
2020-02-05 14:58:35 +00:00
|
|
|
move(_fitnessMetric),
|
2020-02-06 05:19:55 +00:00
|
|
|
_size,
|
|
|
|
std::bind(uniformChromosomeLength, _minChromosomeLength, _maxChromosomeLength)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:55:50 +00:00
|
|
|
Population Population::select(Selection const& _selection) const
|
|
|
|
{
|
|
|
|
vector<Individual> selectedIndividuals;
|
|
|
|
for (size_t i: _selection.materialise(m_individuals.size()))
|
|
|
|
selectedIndividuals.emplace_back(m_individuals[i]);
|
|
|
|
|
|
|
|
return Population(m_fitnessMetric, selectedIndividuals);
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:55:50 +00:00
|
|
|
Population Population::mutate(Selection const& _selection, function<Mutation> _mutation) const
|
|
|
|
{
|
|
|
|
vector<Individual> mutatedIndividuals;
|
|
|
|
for (size_t i: _selection.materialise(m_individuals.size()))
|
|
|
|
mutatedIndividuals.emplace_back(_mutation(m_individuals[i].chromosome), *m_fitnessMetric);
|
|
|
|
|
|
|
|
return Population(m_fitnessMetric, mutatedIndividuals);
|
|
|
|
}
|
|
|
|
|
|
|
|
Population Population::crossover(PairSelection const& _selection, function<Crossover> _crossover) const
|
|
|
|
{
|
|
|
|
vector<Individual> crossedIndividuals;
|
|
|
|
for (auto const& [i, j]: _selection.materialise(m_individuals.size()))
|
|
|
|
{
|
2020-02-13 22:44:06 +00:00
|
|
|
auto childChromosome = _crossover(
|
2020-02-05 15:55:50 +00:00
|
|
|
m_individuals[i].chromosome,
|
|
|
|
m_individuals[j].chromosome
|
|
|
|
);
|
2020-02-13 22:44:06 +00:00
|
|
|
crossedIndividuals.emplace_back(move(childChromosome), *m_fitnessMetric);
|
2020-02-05 15:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Population(m_fitnessMetric, crossedIndividuals);
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:13:55 +00:00
|
|
|
tuple<Population, Population> Population::symmetricCrossoverWithRemainder(
|
|
|
|
PairSelection const& _selection,
|
|
|
|
function<SymmetricCrossover> _symmetricCrossover
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
vector<int> indexSelected(m_individuals.size(), false);
|
|
|
|
|
|
|
|
vector<Individual> crossedIndividuals;
|
|
|
|
for (auto const& [i, j]: _selection.materialise(m_individuals.size()))
|
|
|
|
{
|
|
|
|
auto children = _symmetricCrossover(
|
|
|
|
m_individuals[i].chromosome,
|
|
|
|
m_individuals[j].chromosome
|
|
|
|
);
|
|
|
|
crossedIndividuals.emplace_back(move(get<0>(children)), *m_fitnessMetric);
|
|
|
|
crossedIndividuals.emplace_back(move(get<1>(children)), *m_fitnessMetric);
|
|
|
|
indexSelected[i] = true;
|
|
|
|
indexSelected[j] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Individual> remainder;
|
|
|
|
for (size_t i = 0; i < indexSelected.size(); ++i)
|
|
|
|
if (!indexSelected[i])
|
|
|
|
remainder.emplace_back(m_individuals[i]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
Population(m_fitnessMetric, crossedIndividuals),
|
|
|
|
Population(m_fitnessMetric, remainder),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:46:26 +00:00
|
|
|
namespace solidity::phaser
|
|
|
|
{
|
|
|
|
|
2020-02-05 15:51:43 +00:00
|
|
|
Population operator+(Population _a, Population _b)
|
|
|
|
{
|
2020-02-05 14:58:35 +00:00
|
|
|
// This operator is meant to be used only with populations sharing the same metric (and, to make
|
|
|
|
// things simple, "the same" here means the same exact object in memory).
|
|
|
|
assert(_a.m_fitnessMetric == _b.m_fitnessMetric);
|
2020-02-05 15:51:43 +00:00
|
|
|
|
2020-03-26 03:46:26 +00:00
|
|
|
using ::operator+; // Import the std::vector concat operator from CommonData.h
|
2020-02-05 14:58:35 +00:00
|
|
|
return Population(_a.m_fitnessMetric, move(_a.m_individuals) + move(_b.m_individuals));
|
2020-02-05 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 03:46:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:14:30 +00:00
|
|
|
Population Population::combine(std::tuple<Population, Population> _populationPair)
|
|
|
|
{
|
|
|
|
return get<0>(_populationPair) + get<1>(_populationPair);
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:25:00 +00:00
|
|
|
bool Population::operator==(Population const& _other) const
|
|
|
|
{
|
2020-02-05 14:58:35 +00:00
|
|
|
// We consider populations identical only if they share the same exact instance of the metric.
|
|
|
|
// It might be possible to define some notion of equality for metric objects but it would
|
|
|
|
// be an overkill since mixing populations using different metrics is not a common use case.
|
|
|
|
return m_individuals == _other.m_individuals && m_fitnessMetric == _other.m_fitnessMetric;
|
2020-02-11 10:25:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:15:17 +00:00
|
|
|
vector<Individual> Population::chromosomesToIndividuals(
|
2020-02-26 18:58:14 +00:00
|
|
|
FitnessMetric& _fitnessMetric,
|
2020-02-05 14:15:17 +00:00
|
|
|
vector<Chromosome> _chromosomes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
vector<Individual> individuals;
|
|
|
|
for (auto& chromosome: _chromosomes)
|
2020-02-05 16:02:32 +00:00
|
|
|
individuals.emplace_back(move(chromosome), _fitnessMetric);
|
2020-02-05 14:15:17 +00:00
|
|
|
|
|
|
|
return individuals;
|
|
|
|
}
|
2020-02-05 15:04:17 +00:00
|
|
|
|
|
|
|
vector<Individual> Population::sortedIndividuals(vector<Individual> _individuals)
|
|
|
|
{
|
|
|
|
sort(_individuals.begin(), _individuals.end(), isFitter);
|
|
|
|
return _individuals;
|
|
|
|
}
|