From 2291cf78ac5afa42598facb7f8e1f8b2cf99558d Mon Sep 17 00:00:00 2001 From: cameel Date: Wed, 5 Feb 2020 17:02:32 +0100 Subject: [PATCH] [yul-phaser] Population: Add constructors to Individual to simplify initialization --- test/yulPhaser/Population.cpp | 28 ++++++++++++++-------------- tools/yulPhaser/Population.cpp | 5 +---- tools/yulPhaser/Population.h | 7 +++++++ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/test/yulPhaser/Population.cpp b/test/yulPhaser/Population.cpp index c4b127888..55c346575 100644 --- a/test/yulPhaser/Population.cpp +++ b/test/yulPhaser/Population.cpp @@ -53,32 +53,32 @@ BOOST_AUTO_TEST_SUITE(PopulationTest) BOOST_AUTO_TEST_CASE(isFitter_should_use_fitness_as_the_main_criterion) { - BOOST_TEST(isFitter(Individual{Chromosome("a"), 5}, Individual{Chromosome("a"), 10})); - BOOST_TEST(!isFitter(Individual{Chromosome("a"), 10}, Individual{Chromosome("a"), 5})); + BOOST_TEST(isFitter(Individual(Chromosome("a"), 5), Individual(Chromosome("a"), 10))); + BOOST_TEST(!isFitter(Individual(Chromosome("a"), 10), Individual(Chromosome("a"), 5))); - BOOST_TEST(isFitter(Individual{Chromosome("aaa"), 5}, Individual{Chromosome("aaaaa"), 10})); - BOOST_TEST(!isFitter(Individual{Chromosome("aaaaa"), 10}, Individual{Chromosome("aaa"), 5})); + BOOST_TEST(isFitter(Individual(Chromosome("aaa"), 5), Individual(Chromosome("aaaaa"), 10))); + BOOST_TEST(!isFitter(Individual(Chromosome("aaaaa"), 10), Individual(Chromosome("aaa"), 5))); - BOOST_TEST(isFitter(Individual{Chromosome("aaaaa"), 5}, Individual{Chromosome("aaa"), 10})); - BOOST_TEST(!isFitter(Individual{Chromosome("aaa"), 10}, Individual{Chromosome("aaaaa"), 5})); + BOOST_TEST(isFitter(Individual(Chromosome("aaaaa"), 5), Individual(Chromosome("aaa"), 10))); + BOOST_TEST(!isFitter(Individual(Chromosome("aaa"), 10), Individual(Chromosome("aaaaa"), 5))); } BOOST_AUTO_TEST_CASE(isFitter_should_use_alphabetical_order_when_fitness_is_the_same) { - BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("c"), 3})); - BOOST_TEST(!isFitter(Individual{Chromosome("c"), 3}, Individual{Chromosome("a"), 3})); + BOOST_TEST(isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("c"), 3))); + BOOST_TEST(!isFitter(Individual(Chromosome("c"), 3), Individual(Chromosome("a"), 3))); - BOOST_TEST(isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("aa"), 3})); - BOOST_TEST(!isFitter(Individual{Chromosome("aa"), 3}, Individual{Chromosome("a"), 3})); + BOOST_TEST(isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("aa"), 3))); + BOOST_TEST(!isFitter(Individual(Chromosome("aa"), 3), Individual(Chromosome("a"), 3))); - BOOST_TEST(isFitter(Individual{Chromosome("T"), 3}, Individual{Chromosome("a"), 3})); - BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("T"), 3})); + BOOST_TEST(isFitter(Individual(Chromosome("T"), 3), Individual(Chromosome("a"), 3))); + BOOST_TEST(!isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("T"), 3))); } BOOST_AUTO_TEST_CASE(isFitter_should_return_false_for_identical_individuals) { - BOOST_TEST(!isFitter(Individual{Chromosome("a"), 3}, Individual{Chromosome("a"), 3})); - BOOST_TEST(!isFitter(Individual{Chromosome("acT"), 0}, Individual{Chromosome("acT"), 0})); + BOOST_TEST(!isFitter(Individual(Chromosome("a"), 3), Individual(Chromosome("a"), 3))); + BOOST_TEST(!isFitter(Individual(Chromosome("acT"), 0), Individual(Chromosome("acT"), 0))); } BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_compute_fitness_and_sort_chromosomes, PopulationFixture) diff --git a/tools/yulPhaser/Population.cpp b/tools/yulPhaser/Population.cpp index 923e75e6a..2fed8b90d 100644 --- a/tools/yulPhaser/Population.cpp +++ b/tools/yulPhaser/Population.cpp @@ -157,10 +157,7 @@ vector Population::chromosomesToIndividuals( { vector individuals; for (auto& chromosome: _chromosomes) - { - size_t fitness = _fitnessMetric.evaluate(chromosome); - individuals.push_back({move(chromosome), fitness}); - } + individuals.emplace_back(move(chromosome), _fitnessMetric); return individuals; } diff --git a/tools/yulPhaser/Population.h b/tools/yulPhaser/Population.h index bd58c397a..bd15674d6 100644 --- a/tools/yulPhaser/Population.h +++ b/tools/yulPhaser/Population.h @@ -48,6 +48,13 @@ struct Individual Chromosome chromosome; size_t fitness; + Individual(Chromosome _chromosome, size_t _fitness): + chromosome(std::move(_chromosome)), + fitness(_fitness) {} + Individual(Chromosome _chromosome, FitnessMetric const& _fitnessMetric): + chromosome(std::move(_chromosome)), + fitness(_fitnessMetric.evaluate(chromosome)) {} + bool operator==(Individual const& _other) const { return fitness == _other.fitness && chromosome == _other.chromosome; } bool operator!=(Individual const& _other) const { return !(*this == _other); }