[yul-phaser] Population: Add operator+()

This commit is contained in:
cameel 2020-02-05 16:51:43 +01:00 committed by Kamil Śliwak
parent ecb30c670f
commit d9c5e2dc9f
3 changed files with 29 additions and 0 deletions

View File

@ -261,6 +261,15 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, Po
}
}
BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixture)
{
BOOST_CHECK_EQUAL(
Population(m_program, {Chromosome("ac"), Chromosome("cx")}) +
Population(m_program, {Chromosome("g"), Chromosome("h"), Chromosome("iI")}),
Population(m_program, {Chromosome("ac"), Chromosome("cx"), Chromosome("g"), Chromosome("h"), Chromosome("iI")})
);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

View File

@ -19,6 +19,7 @@
#include <tools/yulPhaser/Program.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/CommonIO.h>
#include <algorithm>
@ -117,6 +118,13 @@ void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
}
}
Population operator+(Population _a, Population _b)
{
assert(toString(_a.m_program) == toString(_b.m_program));
return Population(_a.m_program, move(_a.m_individuals) + move(_b.m_individuals));
}
ostream& phaser::operator<<(ostream& _stream, Population const& _population)
{
auto individual = _population.m_individuals.begin();

View File

@ -28,6 +28,17 @@
namespace solidity::phaser
{
class Population;
}
// This operator+ must be declared in the global namespace. Otherwise it would shadow global
// operator+ overloads from CommonData.h (e.g. the one for vector) in the namespace it was declared in.
solidity::phaser::Population operator+(solidity::phaser::Population _a, solidity::phaser::Population _b);
namespace solidity::phaser
{
/**
* Information describing the state of an individual member of the population during the course
* of the genetic algorithm.
@ -74,6 +85,7 @@ public:
);
void run(std::optional<size_t> _numRounds, std::ostream& _outputStream);
friend Population (::operator+)(Population _a, Population _b);
std::vector<Individual> const& individuals() const { return m_individuals; }