mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add RandomPairSelection and PairMosaicSelection classes
This commit is contained in:
parent
44932dc85a
commit
f9f2bdb5f7
@ -145,6 +145,7 @@ set(yul_phaser_sources
|
||||
yulPhaser/Chromosome.cpp
|
||||
yulPhaser/FitnessMetrics.cpp
|
||||
yulPhaser/GeneticAlgorithms.cpp
|
||||
yulPhaser/PairSelections.cpp
|
||||
yulPhaser/Population.cpp
|
||||
yulPhaser/Program.cpp
|
||||
yulPhaser/Selections.cpp
|
||||
@ -156,6 +157,7 @@ set(yul_phaser_sources
|
||||
../tools/yulPhaser/Chromosome.cpp
|
||||
../tools/yulPhaser/FitnessMetrics.cpp
|
||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||
../tools/yulPhaser/PairSelections.cpp
|
||||
../tools/yulPhaser/Population.cpp
|
||||
../tools/yulPhaser/Program.cpp
|
||||
../tools/yulPhaser/Selections.cpp
|
||||
|
185
test/yulPhaser/PairSelections.cpp
Normal file
185
test/yulPhaser/PairSelections.cpp
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(PairSelectionsTest)
|
||||
BOOST_AUTO_TEST_SUITE(RandomPairSelectionTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_random_values_with_equal_probabilities)
|
||||
{
|
||||
constexpr int collectionSize = 10;
|
||||
constexpr int selectionSize = 100;
|
||||
constexpr double relativeTolerance = 0.1;
|
||||
constexpr double expectedValue = (collectionSize - 1) / 2.0;
|
||||
constexpr double variance = (collectionSize * collectionSize - 1) / 12.0;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(selectionSize).materialise(collectionSize);
|
||||
vector<size_t> samples;
|
||||
for (auto& [first, second]: pairs)
|
||||
{
|
||||
samples.push_back(first);
|
||||
samples.push_back(second);
|
||||
}
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_only_values_that_can_be_used_as_collection_indices)
|
||||
{
|
||||
const size_t collectionSize = 200;
|
||||
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(0.5).materialise(collectionSize);
|
||||
|
||||
BOOST_TEST(pairs.size() == 100);
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [&](auto const& pair){ return get<0>(pair) <= collectionSize; }));
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [&](auto const& pair){ return get<1>(pair) <= collectionSize; }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_never_return_a_pair_of_identical_indices)
|
||||
{
|
||||
vector<tuple<size_t, size_t>> pairs = RandomPairSelection(0.5).materialise(100);
|
||||
|
||||
BOOST_TEST(pairs.size() == 50);
|
||||
BOOST_TEST(all_of(pairs.begin(), pairs.end(), [](auto const& pair){ return get<0>(pair) != get<1>(pair); }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_number_of_pairs_thats_a_fraction_of_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0.0).materialise(10).size() == 0);
|
||||
BOOST_TEST(RandomPairSelection(0.3).materialise(10).size() == 3);
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(10).size() == 5);
|
||||
BOOST_TEST(RandomPairSelection(0.7).materialise(10).size() == 7);
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(10).size() == 10);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_support_number_of_pairs_bigger_than_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(5).size() == 10);
|
||||
BOOST_TEST(RandomPairSelection(1.5).materialise(10).size() == 15);
|
||||
BOOST_TEST(RandomPairSelection(10.0).materialise(10).size() == 100);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_the_number_of_pairs_to_the_nearest_integer)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0.49).materialise(3).size() == 1);
|
||||
BOOST_TEST(RandomPairSelection(0.50).materialise(3).size() == 2);
|
||||
BOOST_TEST(RandomPairSelection(0.51).materialise(3).size() == 2);
|
||||
|
||||
BOOST_TEST(RandomPairSelection(1.51).materialise(3).size() == 5);
|
||||
|
||||
BOOST_TEST(RandomPairSelection(0.01).materialise(2).size() == 0);
|
||||
BOOST_TEST(RandomPairSelection(0.01).materialise(3).size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_is_empty)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(0).empty());
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_has_one_element)
|
||||
{
|
||||
BOOST_TEST(RandomPairSelection(0).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(0.5).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(1.0).materialise(1).empty());
|
||||
BOOST_TEST(RandomPairSelection(2.0).materialise(1).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(PairMosaicSelectionTest)
|
||||
|
||||
using IndexPairs = vector<tuple<size_t, size_t>>;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise)
|
||||
{
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 0.5).materialise(4) == IndexPairs({{1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(4) == IndexPairs({{1, 1}, {1, 1}, {1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 2.0).materialise(4) == IndexPairs({{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(2) == IndexPairs({{1, 1}, {1, 1}}));
|
||||
|
||||
IndexPairs pairs1{{0, 1}, {1, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 0.5).materialise(4) == IndexPairs({{0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 1.0).materialise(4) == IndexPairs({{0, 1}, {1, 0}, {0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 2.0).materialise(4) == IndexPairs({{0, 1}, {1, 0}, {0, 1}, {1, 0}, {0, 1}, {1, 0}, {0, 1}, {1, 0}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs1, 1.0).materialise(2) == IndexPairs({{0, 1}, {1, 0}}));
|
||||
|
||||
IndexPairs pairs2{{3, 2}, {2, 3}, {1, 0}, {1, 1}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 0.5).materialise(4) == IndexPairs({{3, 2}, {2, 3}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 1.0).materialise(4) == IndexPairs({{3, 2}, {2, 3}, {1, 0}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs2, 2.0).materialise(4) == IndexPairs({{3, 2}, {2, 3}, {1, 0}, {1, 1}, {3, 2}, {2, 3}, {1, 0}, {1, 1}}));
|
||||
|
||||
IndexPairs pairs3{{1, 0}, {1, 1}, {1, 0}, {1, 1}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs3, 1.0).materialise(2) == IndexPairs({{1, 0}, {1, 1}}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_indices)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.49).materialise(5) == IndexPairs({{4, 4}, {3, 3}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.50).materialise(5) == IndexPairs({{4, 4}, {3, 3}, {2, 2}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.51).materialise(5) == IndexPairs({{4, 4}, {3, 3}, {2, 2}}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_is_empty)
|
||||
{
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}}, 1.0).materialise(0).empty());
|
||||
BOOST_TEST(PairMosaicSelection({{1, 1}, {3, 3}}, 2.0).materialise(0).empty());
|
||||
BOOST_TEST(PairMosaicSelection({{5, 5}, {4, 4}, {3, 3}, {2, 2}}, 0.5).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_pairs_if_collection_has_one_element)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.0).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 0.5).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 1.0).materialise(1).empty());
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 7.0).materialise(1).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_clamp_indices_at_collection_size)
|
||||
{
|
||||
IndexPairs pairs{{4, 4}, {3, 3}, {2, 2}, {1, 1}, {0, 0}};
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 1.0).materialise(4) == IndexPairs({{3, 3}, {3, 3}, {2, 2}, {1, 1}}));
|
||||
BOOST_TEST(PairMosaicSelection(pairs, 2.0).materialise(3) == IndexPairs({{2, 2}, {2, 2}, {2, 2}, {1, 1}, {0, 0}, {2, 2}}));
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
@ -16,3 +16,50 @@
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
vector<tuple<size_t, size_t>> RandomPairSelection::materialise(size_t _poolSize) const
|
||||
{
|
||||
if (_poolSize < 2)
|
||||
return {};
|
||||
|
||||
size_t count = static_cast<size_t>(round(_poolSize * m_selectionSize));
|
||||
|
||||
vector<tuple<size_t, size_t>> selection;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
size_t index1 = SimulationRNG::uniformInt(0, _poolSize - 1);
|
||||
size_t index2;
|
||||
do
|
||||
{
|
||||
index2 = SimulationRNG::uniformInt(0, _poolSize - 1);
|
||||
} while (index1 == index2);
|
||||
|
||||
selection.push_back({index1, index2});
|
||||
}
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
||||
vector<tuple<size_t, size_t>> PairMosaicSelection::materialise(size_t _poolSize) const
|
||||
{
|
||||
if (_poolSize < 2)
|
||||
return {};
|
||||
|
||||
size_t count = static_cast<size_t>(round(_poolSize * m_selectionSize));
|
||||
|
||||
vector<tuple<size_t, size_t>> selection;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
tuple<size_t, size_t> pair = m_pattern[i % m_pattern.size()];
|
||||
selection.push_back({min(get<0>(pair), _poolSize - 1), min(get<1>(pair), _poolSize - 1)});
|
||||
}
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
@ -50,4 +51,49 @@ public:
|
||||
virtual std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* A selection that selects pairs of random elements from a container. The resulting set of pairs
|
||||
* may contain the same pair more than once but does not contain pairs of duplicates. Always
|
||||
* selects as many pairs as the size of the container multiplied by @a _selectionSize (unless the
|
||||
* container is empty).
|
||||
*/
|
||||
class RandomPairSelection: public PairSelection
|
||||
{
|
||||
public:
|
||||
explicit RandomPairSelection(double _selectionSize):
|
||||
m_selectionSize(_selectionSize) {}
|
||||
|
||||
std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const override;
|
||||
|
||||
private:
|
||||
double m_selectionSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* A selection that selects pairs of elements at specific, fixed positions indicated by a repeating
|
||||
* "pattern". If the positions in the pattern exceed the size of the container, they are capped at
|
||||
* the maximum available position. Always selects as many pairs as the size of the container
|
||||
* multiplied by @a _selectionSize (unless the container is empty).
|
||||
*
|
||||
* E.g. if the pattern is {{0, 1}, {3, 9}} and collection size is 5, the selection will materialise
|
||||
* into {{0, 1}, {3, 4}, {0, 1}, {3, 4}, {0, 1}}. If the size is 3, it will be
|
||||
* {{0, 1}, {2, 2}, {0, 1}}.
|
||||
*/
|
||||
class PairMosaicSelection: public PairSelection
|
||||
{
|
||||
public:
|
||||
explicit PairMosaicSelection(std::vector<std::tuple<size_t, size_t>> _pattern, double _selectionSize = 1.0):
|
||||
m_pattern(move(_pattern)),
|
||||
m_selectionSize(_selectionSize)
|
||||
{
|
||||
assert(m_pattern.size() > 0 || _selectionSize == 0.0);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const override;
|
||||
|
||||
private:
|
||||
std::vector<std::tuple<size_t, size_t>> m_pattern;
|
||||
double m_selectionSize;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user