mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Add symmetricCrossoverWithRemainder()
This commit is contained in:
@@ -117,6 +117,37 @@ Population Population::crossover(PairSelection const& _selection, function<Cross
|
||||
return Population(m_fitnessMetric, crossedIndividuals);
|
||||
}
|
||||
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
||||
namespace solidity::phaser
|
||||
{
|
||||
|
||||
|
||||
@@ -100,6 +100,10 @@ public:
|
||||
Population select(Selection const& _selection) const;
|
||||
Population mutate(Selection const& _selection, std::function<Mutation> _mutation) const;
|
||||
Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const;
|
||||
std::tuple<Population, Population> symmetricCrossoverWithRemainder(
|
||||
PairSelection const& _selection,
|
||||
std::function<SymmetricCrossover> _symmetricCrossover
|
||||
) const;
|
||||
|
||||
friend Population operator+(Population _a, Population _b);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user