[yul-phaser] Population: Remove no longer used methods for running algorithm steps

- They have been superseded by objects from GeneticAlgorithms.h
This commit is contained in:
Kamil Śliwak
2020-02-25 16:06:16 +01:00
parent 67fbafab8f
commit e1b8b64f05
3 changed files with 5 additions and 87 deletions
-41
View File
@@ -84,18 +84,6 @@ Population Population::makeRandom(
);
}
void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
{
for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round)
{
doMutation();
doSelection();
_outputStream << "---------- ROUND " << round << " ----------" << endl;
_outputStream << *this;
}
}
Population Population::select(Selection const& _selection) const
{
vector<Individual> selectedIndividuals;
@@ -131,35 +119,6 @@ ostream& phaser::operator<<(ostream& _stream, Population const& _population)
return _stream;
}
void Population::doMutation()
{
// TODO: Implement mutation and crossover
}
void Population::doSelection()
{
randomizeWorstChromosomes(*m_fitnessMetric, m_individuals, m_individuals.size() / 2);
m_individuals = sortedIndividuals(move(m_individuals));
}
void Population::randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
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)
{
auto chromosome = Chromosome::makeRandom(binomialChromosomeLength(MaxChromosomeLength));
size_t fitness = _fitnessMetric.evaluate(chromosome);
*individual = {move(chromosome), fitness};
}
}
vector<Individual> Population::chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric,
vector<Chromosome> _chromosomes
+5 -14
View File
@@ -69,19 +69,19 @@ struct Individual
bool isFitter(Individual const& a, Individual const& b);
/**
* 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.
* Represents a snapshot of a population undergoing a genetic algorithm. Consists of a set of
* chromosomes with associated fitness values.
*
* An individual is a sequence of optimiser steps represented by a @a Chromosome instance.
* Individuals are always ordered by their fitness (based on @_fitnessMetric and @a isFitter()).
* The fitness is computed using the metric as soon as an individual is inserted into the population.
*
* The population is immutable. Selections, mutations and crossover work by producing a new
* instance and copying the individuals.
*/
class Population
{
public:
static constexpr size_t MaxChromosomeLength = 30;
explicit Population(
std::shared_ptr<FitnessMetric const> _fitnessMetric,
std::vector<Chromosome> _chromosomes = {}
@@ -103,7 +103,6 @@ public:
size_t _maxChromosomeLength
);
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
Population select(Selection const& _selection) const;
friend Population (::operator+)(Population _a, Population _b);
@@ -123,14 +122,6 @@ private:
m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{sortedIndividuals(std::move(_individuals))} {}
void doMutation();
void doSelection();
static void randomizeWorstChromosomes(
FitnessMetric const& _fitnessMetric,
std::vector<Individual>& _individuals,
size_t _count
);
static std::vector<Individual> chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric,
std::vector<Chromosome> _chromosomes