2020-02-05 13:56:55 +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-02-05 13:56:55 +00:00
|
|
|
|
|
|
|
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
2020-02-05 23:51:11 +00:00
|
|
|
#include <tools/yulPhaser/Mutations.h>
|
2020-02-05 13:58:48 +00:00
|
|
|
#include <tools/yulPhaser/Selections.h>
|
2020-02-05 23:51:11 +00:00
|
|
|
#include <tools/yulPhaser/PairSelections.h>
|
2020-02-05 13:56:55 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2020-03-11 02:38:31 +00:00
|
|
|
using namespace solidity;
|
2020-02-05 13:56:55 +00:00
|
|
|
using namespace solidity::phaser;
|
|
|
|
|
2020-03-11 02:38:31 +00:00
|
|
|
function<Crossover> phaser::buildCrossoverOperator(
|
|
|
|
CrossoverChoice _choice,
|
|
|
|
optional<double> _uniformCrossoverSwapChance
|
|
|
|
)
|
|
|
|
{
|
|
|
|
switch (_choice)
|
|
|
|
{
|
|
|
|
case CrossoverChoice::SinglePoint:
|
|
|
|
return randomPointCrossover();
|
|
|
|
case CrossoverChoice::TwoPoint:
|
|
|
|
return randomTwoPointCrossover();
|
|
|
|
case CrossoverChoice::Uniform:
|
|
|
|
assert(_uniformCrossoverSwapChance.has_value());
|
|
|
|
return uniformCrossover(_uniformCrossoverSwapChance.value());
|
|
|
|
default:
|
|
|
|
assertThrow(false, solidity::util::Exception, "Invalid CrossoverChoice value.");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function<SymmetricCrossover> phaser::buildSymmetricCrossoverOperator(
|
|
|
|
CrossoverChoice _choice,
|
|
|
|
optional<double> _uniformCrossoverSwapChance
|
|
|
|
)
|
|
|
|
{
|
|
|
|
switch (_choice)
|
|
|
|
{
|
|
|
|
case CrossoverChoice::SinglePoint:
|
|
|
|
return symmetricRandomPointCrossover();
|
|
|
|
case CrossoverChoice::TwoPoint:
|
|
|
|
return symmetricRandomTwoPointCrossover();
|
|
|
|
case CrossoverChoice::Uniform:
|
|
|
|
assert(_uniformCrossoverSwapChance.has_value());
|
|
|
|
return symmetricUniformCrossover(_uniformCrossoverSwapChance.value());
|
|
|
|
default:
|
|
|
|
assertThrow(false, solidity::util::Exception, "Invalid CrossoverChoice value.");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
Population RandomAlgorithm::runNextRound(Population _population)
|
2020-02-05 13:58:48 +00:00
|
|
|
{
|
|
|
|
RangeSelection elite(0.0, m_options.elitePoolSize);
|
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
Population elitePopulation = _population.select(elite);
|
|
|
|
size_t replacementCount = _population.individuals().size() - elitePopulation.individuals().size();
|
2020-02-05 13:58:48 +00:00
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
return
|
2022-08-23 17:28:45 +00:00
|
|
|
std::move(elitePopulation) +
|
2020-02-05 13:58:48 +00:00
|
|
|
Population::makeRandom(
|
2020-02-21 15:10:07 +00:00
|
|
|
_population.fitnessMetric(),
|
2020-02-05 13:58:48 +00:00
|
|
|
replacementCount,
|
|
|
|
m_options.minChromosomeLength,
|
|
|
|
m_options.maxChromosomeLength
|
|
|
|
);
|
|
|
|
}
|
2020-02-05 23:51:11 +00:00
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
Population GenerationalElitistWithExclusivePools::runNextRound(Population _population)
|
2020-02-05 23:51:11 +00:00
|
|
|
{
|
|
|
|
double elitePoolSize = 1.0 - (m_options.mutationPoolSize + m_options.crossoverPoolSize);
|
2020-03-11 01:29:20 +00:00
|
|
|
|
|
|
|
RangeSelection elitePool(0.0, elitePoolSize);
|
|
|
|
RandomSelection mutationPoolFromElite(m_options.mutationPoolSize / elitePoolSize);
|
|
|
|
RandomPairSelection crossoverPoolFromElite(m_options.crossoverPoolSize / elitePoolSize);
|
|
|
|
|
|
|
|
std::function<Mutation> mutationOperator = alternativeMutations(
|
|
|
|
m_options.randomisationChance,
|
|
|
|
geneRandomisation(m_options.percentGenesToRandomise),
|
|
|
|
alternativeMutations(
|
|
|
|
m_options.deletionVsAdditionChance,
|
|
|
|
geneDeletion(m_options.percentGenesToAddOrDelete),
|
|
|
|
geneAddition(m_options.percentGenesToAddOrDelete)
|
|
|
|
)
|
|
|
|
);
|
2020-03-11 02:39:29 +00:00
|
|
|
std::function<Crossover> crossoverOperator = buildCrossoverOperator(
|
|
|
|
m_options.crossover,
|
|
|
|
m_options.uniformCrossoverSwapChance
|
|
|
|
);
|
2020-02-05 23:51:11 +00:00
|
|
|
|
2020-02-21 15:10:07 +00:00
|
|
|
return
|
2020-03-11 01:29:20 +00:00
|
|
|
_population.select(elitePool) +
|
|
|
|
_population.select(elitePool).mutate(mutationPoolFromElite, mutationOperator) +
|
|
|
|
_population.select(elitePool).crossover(crossoverPoolFromElite, crossoverOperator);
|
2020-02-05 23:51:11 +00:00
|
|
|
}
|
2020-03-11 01:15:25 +00:00
|
|
|
|
|
|
|
Population ClassicGeneticAlgorithm::runNextRound(Population _population)
|
|
|
|
{
|
|
|
|
Population elite = _population.select(RangeSelection(0.0, m_options.elitePoolSize));
|
|
|
|
Population rest = _population.select(RangeSelection(m_options.elitePoolSize, 1.0));
|
|
|
|
|
|
|
|
Population selectedPopulation = select(_population, rest.individuals().size());
|
|
|
|
|
2020-03-11 02:39:29 +00:00
|
|
|
std::function<SymmetricCrossover> crossoverOperator = buildSymmetricCrossoverOperator(
|
|
|
|
m_options.crossover,
|
|
|
|
m_options.uniformCrossoverSwapChance
|
|
|
|
);
|
|
|
|
|
2020-03-11 01:15:25 +00:00
|
|
|
Population crossedPopulation = Population::combine(
|
|
|
|
selectedPopulation.symmetricCrossoverWithRemainder(
|
|
|
|
PairsFromRandomSubset(m_options.crossoverChance),
|
2020-03-11 02:39:29 +00:00
|
|
|
crossoverOperator
|
2020-03-11 01:15:25 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
std::function<Mutation> mutationOperator = mutationSequence({
|
|
|
|
geneRandomisation(m_options.mutationChance),
|
|
|
|
geneDeletion(m_options.deletionChance),
|
|
|
|
geneAddition(m_options.additionChance),
|
|
|
|
});
|
|
|
|
|
|
|
|
RangeSelection all(0.0, 1.0);
|
|
|
|
Population mutatedPopulation = crossedPopulation.mutate(all, mutationOperator);
|
|
|
|
|
|
|
|
return elite + mutatedPopulation;
|
|
|
|
}
|
|
|
|
|
|
|
|
Population ClassicGeneticAlgorithm::select(Population _population, size_t _selectionSize)
|
|
|
|
{
|
|
|
|
if (_population.individuals().size() == 0)
|
|
|
|
return _population;
|
|
|
|
|
|
|
|
size_t maxFitness = 0;
|
|
|
|
for (auto const& individual: _population.individuals())
|
|
|
|
maxFitness = max(maxFitness, individual.fitness);
|
|
|
|
|
|
|
|
size_t rouletteRange = 0;
|
|
|
|
for (auto const& individual: _population.individuals())
|
|
|
|
// Add 1 to make sure that every chromosome has non-zero probability of being chosen
|
|
|
|
rouletteRange += maxFitness + 1 - individual.fitness;
|
|
|
|
|
|
|
|
vector<Individual> selectedIndividuals;
|
|
|
|
for (size_t i = 0; i < _selectionSize; ++i)
|
|
|
|
{
|
2020-06-03 11:52:37 +00:00
|
|
|
size_t ball = SimulationRNG::uniformInt(0, rouletteRange - 1);
|
2020-03-11 01:15:25 +00:00
|
|
|
|
|
|
|
size_t cumulativeFitness = 0;
|
|
|
|
for (auto const& individual: _population.individuals())
|
|
|
|
{
|
|
|
|
size_t pocketSize = maxFitness + 1 - individual.fitness;
|
|
|
|
if (ball < cumulativeFitness + pocketSize)
|
|
|
|
{
|
|
|
|
selectedIndividuals.push_back(individual);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cumulativeFitness += pocketSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(selectedIndividuals.size() == _selectionSize);
|
|
|
|
return Population(_population.fitnessMetric(), selectedIndividuals);
|
|
|
|
}
|