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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <tools/yulPhaser/Chromosome.h>
|
2020-02-05 14:58:35 +00:00
|
|
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
2020-02-05 15:55:50 +00:00
|
|
|
#include <tools/yulPhaser/Mutations.h>
|
2020-02-15 02:03:22 +00:00
|
|
|
#include <tools/yulPhaser/SimulationRNG.h>
|
2020-01-17 06:45:10 +00:00
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <ostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace solidity::phaser
|
|
|
|
{
|
|
|
|
|
2020-02-05 15:55:50 +00:00
|
|
|
class PairSelection;
|
2020-02-05 15:55:50 +00:00
|
|
|
class Selection;
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
/**
|
|
|
|
* Information describing the state of an individual member of the population during the course
|
|
|
|
* of the genetic algorithm.
|
|
|
|
*/
|
|
|
|
struct Individual
|
|
|
|
{
|
|
|
|
Chromosome chromosome;
|
2020-02-05 15:18:53 +00:00
|
|
|
size_t fitness;
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-05 16:02:32 +00:00
|
|
|
Individual(Chromosome _chromosome, size_t _fitness):
|
|
|
|
chromosome(std::move(_chromosome)),
|
|
|
|
fitness(_fitness) {}
|
2020-02-26 18:58:14 +00:00
|
|
|
Individual(Chromosome _chromosome, FitnessMetric& _fitnessMetric):
|
2020-02-05 16:02:32 +00:00
|
|
|
chromosome(std::move(_chromosome)),
|
|
|
|
fitness(_fitnessMetric.evaluate(chromosome)) {}
|
|
|
|
|
2020-02-11 10:25:00 +00:00
|
|
|
bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; }
|
|
|
|
bool operator!=(Individual const& _other) const { return !(*this == _other); }
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& _stream, Individual const& _individual);
|
|
|
|
};
|
|
|
|
|
2020-02-11 21:25:17 +00:00
|
|
|
/// Determines which individual is better by comparing fitness values. If fitness is the same
|
|
|
|
/// takes into account all the other properties of the individual to make the comparison
|
|
|
|
/// deterministic as long as the individuals are not equal.
|
2020-02-11 18:20:01 +00:00
|
|
|
bool isFitter(Individual const& a, Individual const& b);
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
/**
|
2020-02-06 06:59:08 +00:00
|
|
|
* Represents a snapshot of a population undergoing a genetic algorithm. Consists of a set of
|
|
|
|
* chromosomes with associated fitness values.
|
2020-01-17 06:45:10 +00:00
|
|
|
*
|
2020-02-05 14:58:35 +00:00
|
|
|
* An individual is a sequence of optimiser steps represented by a @a Chromosome instance.
|
2020-02-05 15:49:50 +00:00
|
|
|
* Individuals are always ordered by their fitness (based on @_fitnessMetric and @a isFitter()).
|
2020-02-05 15:18:53 +00:00
|
|
|
* The fitness is computed using the metric as soon as an individual is inserted into the population.
|
2020-02-06 06:59:08 +00:00
|
|
|
*
|
|
|
|
* The population is immutable. Selections, mutations and crossover work by producing a new
|
|
|
|
* instance and copying the individuals.
|
2020-01-17 06:45:10 +00:00
|
|
|
*/
|
|
|
|
class Population
|
|
|
|
{
|
|
|
|
public:
|
2020-02-05 14:58:35 +00:00
|
|
|
explicit Population(
|
2020-02-26 18:58:14 +00:00
|
|
|
std::shared_ptr<FitnessMetric> _fitnessMetric,
|
2020-02-05 14:58:35 +00:00
|
|
|
std::vector<Chromosome> _chromosomes = {}
|
|
|
|
):
|
2020-02-05 14:15:17 +00:00
|
|
|
Population(
|
2020-02-05 15:18:53 +00:00
|
|
|
_fitnessMetric,
|
|
|
|
chromosomesToIndividuals(*_fitnessMetric, std::move(_chromosomes))
|
2020-02-05 14:15:17 +00:00
|
|
|
) {}
|
2020-03-11 02:01:09 +00:00
|
|
|
explicit Population(std::shared_ptr<FitnessMetric> _fitnessMetric, std::vector<Individual> _individuals):
|
|
|
|
m_fitnessMetric(std::move(_fitnessMetric)),
|
|
|
|
m_individuals{sortedIndividuals(std::move(_individuals))} {}
|
2020-02-06 05:19:55 +00:00
|
|
|
|
|
|
|
static Population makeRandom(
|
2020-02-26 18:58:14 +00:00
|
|
|
std::shared_ptr<FitnessMetric> _fitnessMetric,
|
2020-02-06 05:19:55 +00:00
|
|
|
size_t _size,
|
|
|
|
std::function<size_t()> _chromosomeLengthGenerator
|
|
|
|
);
|
|
|
|
static Population makeRandom(
|
2020-02-26 18:58:14 +00:00
|
|
|
std::shared_ptr<FitnessMetric> _fitnessMetric,
|
2020-02-06 05:19:55 +00:00
|
|
|
size_t _size,
|
|
|
|
size_t _minChromosomeLength,
|
|
|
|
size_t _maxChromosomeLength
|
|
|
|
);
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-05 15:55:50 +00:00
|
|
|
Population select(Selection const& _selection) const;
|
2020-02-05 15:55:50 +00:00
|
|
|
Population mutate(Selection const& _selection, std::function<Mutation> _mutation) const;
|
|
|
|
Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const;
|
2020-03-11 01:13:55 +00:00
|
|
|
std::tuple<Population, Population> symmetricCrossoverWithRemainder(
|
|
|
|
PairSelection const& _selection,
|
|
|
|
std::function<SymmetricCrossover> _symmetricCrossover
|
|
|
|
) const;
|
2020-03-26 03:46:26 +00:00
|
|
|
|
|
|
|
friend Population operator+(Population _a, Population _b);
|
2020-03-11 01:14:30 +00:00
|
|
|
static Population combine(std::tuple<Population, Population> _populationPair);
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-26 18:58:14 +00:00
|
|
|
std::shared_ptr<FitnessMetric> fitnessMetric() { return m_fitnessMetric; }
|
2020-01-17 06:45:10 +00:00
|
|
|
std::vector<Individual> const& individuals() const { return m_individuals; }
|
|
|
|
|
2020-02-06 05:19:55 +00:00
|
|
|
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); }
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-11 10:25:00 +00:00
|
|
|
bool operator==(Population const& _other) const;
|
|
|
|
bool operator!=(Population const& _other) const { return !(*this == _other); }
|
|
|
|
|
2020-01-17 06:45:10 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
|
|
|
|
|
|
|
|
private:
|
2020-02-05 14:15:17 +00:00
|
|
|
static std::vector<Individual> chromosomesToIndividuals(
|
2020-02-26 18:58:14 +00:00
|
|
|
FitnessMetric& _fitnessMetric,
|
2020-02-05 14:15:17 +00:00
|
|
|
std::vector<Chromosome> _chromosomes
|
|
|
|
);
|
2020-02-05 15:04:17 +00:00
|
|
|
static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals);
|
2020-01-17 06:45:10 +00:00
|
|
|
|
2020-02-26 18:58:14 +00:00
|
|
|
std::shared_ptr<FitnessMetric> m_fitnessMetric;
|
2020-01-17 06:45:10 +00:00
|
|
|
std::vector<Individual> m_individuals;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|