mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Add select() method
This commit is contained in:
parent
83b8ab8012
commit
4665b7a7e4
@ -20,6 +20,7 @@
|
|||||||
#include <tools/yulPhaser/Chromosome.h>
|
#include <tools/yulPhaser/Chromosome.h>
|
||||||
#include <tools/yulPhaser/Population.h>
|
#include <tools/yulPhaser/Population.h>
|
||||||
#include <tools/yulPhaser/Program.h>
|
#include <tools/yulPhaser/Program.h>
|
||||||
|
#include <tools/yulPhaser/Selections.h>
|
||||||
|
|
||||||
#include <libyul/optimiser/BlockFlattener.h>
|
#include <libyul/optimiser/BlockFlattener.h>
|
||||||
#include <libyul/optimiser/SSAReverser.h>
|
#include <libyul/optimiser/SSAReverser.h>
|
||||||
@ -222,6 +223,41 @@ BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixt
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(select_should_return_population_containing_individuals_indicated_by_selection, PopulationFixture)
|
||||||
|
{
|
||||||
|
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c"), Chromosome("g"), Chromosome("h")});
|
||||||
|
RangeSelection selection(0.25, 0.75);
|
||||||
|
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{1, 2}));
|
||||||
|
|
||||||
|
BOOST_TEST(
|
||||||
|
population.select(selection) ==
|
||||||
|
Population(m_fitnessMetric, {population.individuals()[1].chromosome, population.individuals()[2].chromosome})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(select_should_include_duplicates_if_selection_contains_duplicates, PopulationFixture)
|
||||||
|
{
|
||||||
|
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c")});
|
||||||
|
MosaicSelection selection({0, 1}, 2.0);
|
||||||
|
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{0, 1, 0, 1}));
|
||||||
|
|
||||||
|
BOOST_TEST(population.select(selection) == Population(m_fitnessMetric, {
|
||||||
|
population.individuals()[0].chromosome,
|
||||||
|
population.individuals()[1].chromosome,
|
||||||
|
population.individuals()[0].chromosome,
|
||||||
|
population.individuals()[1].chromosome,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(select_should_return_empty_population_if_selection_is_empty, PopulationFixture)
|
||||||
|
{
|
||||||
|
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c")});
|
||||||
|
RangeSelection selection(0.0, 0.0);
|
||||||
|
assert(selection.materialise(population.individuals().size()).empty());
|
||||||
|
|
||||||
|
BOOST_TEST(population.select(selection).individuals().empty());
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <tools/yulPhaser/Population.h>
|
#include <tools/yulPhaser/Population.h>
|
||||||
|
|
||||||
|
#include <tools/yulPhaser/Selections.h>
|
||||||
|
|
||||||
#include <libsolutil/CommonData.h>
|
#include <libsolutil/CommonData.h>
|
||||||
#include <libsolutil/CommonIO.h>
|
#include <libsolutil/CommonIO.h>
|
||||||
@ -95,6 +96,15 @@ void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
Population operator+(Population _a, Population _b)
|
Population operator+(Population _a, Population _b)
|
||||||
{
|
{
|
||||||
// This operator is meant to be used only with populations sharing the same metric (and, to make
|
// This operator is meant to be used only with populations sharing the same metric (and, to make
|
||||||
|
@ -39,6 +39,8 @@ solidity::phaser::Population operator+(solidity::phaser::Population _a, solidity
|
|||||||
namespace solidity::phaser
|
namespace solidity::phaser
|
||||||
{
|
{
|
||||||
|
|
||||||
|
class Selection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Information describing the state of an individual member of the population during the course
|
* Information describing the state of an individual member of the population during the course
|
||||||
* of the genetic algorithm.
|
* of the genetic algorithm.
|
||||||
@ -102,6 +104,7 @@ public:
|
|||||||
);
|
);
|
||||||
|
|
||||||
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
|
||||||
|
Population select(Selection const& _selection) const;
|
||||||
friend Population (::operator+)(Population _a, Population _b);
|
friend Population (::operator+)(Population _a, Population _b);
|
||||||
|
|
||||||
std::shared_ptr<FitnessMetric const> fitnessMetric() const { return m_fitnessMetric; }
|
std::shared_ptr<FitnessMetric const> fitnessMetric() const { return m_fitnessMetric; }
|
||||||
|
Loading…
Reference in New Issue
Block a user