OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
PopulationTest.cpp
Go to the documentation of this file.
1 //
2 // Test PopulationTest
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
23 #include "gtest/gtest.h"
24 
25 #include "boost/smart_ptr.hpp"
26 
27 namespace {
28 
29  // The fixture for testing class Foo.
30  class PopulationTest : public ::testing::Test {
31  protected:
32 
33  PopulationTest() {
34  // You can do set-up work for each test here.
35  population_.reset(new Population<Individual>());
36  }
37 
38  virtual ~PopulationTest() {
39  // You can do clean-up work that doesn't throw exceptions here.
40  }
41 
42  // If the constructor and destructor are not enough for setting up
43  // and cleaning up each test, you can define the following methods:
44 
45  virtual void SetUp() {
46  // Code here will be called immediately after the constructor (right
47  // before each test).
48  }
49 
50  virtual void TearDown() {
51  // Code here will be called immediately after each test (right
52  // before the destructor).
53  // population_->clean_population();
54  }
55 
56  boost::shared_ptr<Individual> createIndividual(size_t num_genes) {
57 
59  Individual::names_t names;
60  Individual::constraints_t constraints;
61  for(size_t i=0; i < num_genes; i++) {
62  bounds.push_back(std::pair<double, double>(0.0, 10.0));
63  names.push_back("dvar"+std::to_string(i));
64  }
65 
66  boost::shared_ptr<Individual> ind(new Individual(bounds,names,constraints));
67  return ind;
68  }
69 
70  // Objects declared here can be used by all tests in the test case
71  boost::scoped_ptr< Population<Individual> > population_;
72  };
73 
74  TEST_F(PopulationTest, AddOneIndividual) {
75 
76  boost::shared_ptr<Individual> ind = createIndividual(1);
77  unsigned int id = population_->add_individual(ind);
78  double gene = ind->genes_m[0];
79  double obj = ind->objectives_m[0];
80 
81  EXPECT_EQ(static_cast<size_t>(0), id) << "first individuals id should be 0";
82 
83  boost::shared_ptr<Individual> tmp = population_->get_individual(id);
84  EXPECT_EQ(0, tmp.get()) << "no committed individuals after insert";
85 
86  tmp = population_->get_staging(id);
87  EXPECT_EQ(gene, tmp->genes_m[0]) << "gene should have specified value";
88  EXPECT_EQ(obj, tmp->objectives_m[0]) << "objective should have specified value";
89 
90  size_t my_size = population_->size();
91  EXPECT_EQ(static_cast<size_t>(0), my_size)
92  << "no committed individuals, population should still be 0";
93 
94  }
95 
96  TEST_F(PopulationTest, CommitOneIndividual) {
97 
98  boost::shared_ptr<Individual> ind = createIndividual(1);
99  unsigned int id = population_->add_individual(ind);
100  double gene = ind->genes_m[0];
101  double obj = ind->objectives_m[0];
102 
103  EXPECT_EQ(static_cast<size_t>(0), id) << "first individuals id should be 0";
104 
105  population_->commit_individuals();
106 
107  boost::shared_ptr<Individual> tmp = population_->get_staging(id);
108  EXPECT_EQ(0, tmp.get()) << "no staging individuals after commit";
109 
110  tmp = population_->get_individual(id);
111  EXPECT_EQ(gene, tmp->genes_m[0]);
112  EXPECT_EQ(obj, tmp->objectives_m[0]);
113 
114  size_t my_size = population_->size();
115  EXPECT_EQ(static_cast<size_t>(1), my_size);
116 
117  }
118 
119  TEST_F(PopulationTest, KeepIndividuals) {
120 
121  boost::shared_ptr<Individual> ind1 = createIndividual(1);
122  boost::shared_ptr<Individual> ind2 = createIndividual(1);
123  boost::shared_ptr<Individual> ind3 = createIndividual(1);
124  boost::shared_ptr<Individual> ind4 = createIndividual(1);
125 
126  size_t id0 = population_->add_individual(ind1);
127  EXPECT_EQ(static_cast<size_t>(0), id0);
128  size_t id1 = population_->add_individual(ind2);
129  EXPECT_EQ(static_cast<size_t>(1), id1);
130  size_t id2 = population_->add_individual(ind3);
131  EXPECT_EQ(static_cast<size_t>(2), id2);
132  size_t id3 = population_->add_individual(ind4);
133  EXPECT_EQ(static_cast<size_t>(3), id3);
134 
135  population_->commit_individuals();
136 
137  std::set<unsigned int> survivors;
138  survivors.insert(id1);
139  survivors.insert(id3);
140 
141  population_->keepSurvivors(survivors);
142 
143  size_t size = population_->size();
144  EXPECT_EQ(survivors.size(), size);
145 
146  boost::shared_ptr<Individual> tmp = population_->get_individual(id1);
147  EXPECT_EQ(ind2->genes_m[0], tmp->genes_m[0]);
148  EXPECT_EQ(ind2->objectives_m[0], tmp->objectives_m[0]);
149  }
150 
151  TEST_F(PopulationTest, IDsContinuous) {
152 
153  boost::shared_ptr<Individual> ind0 = createIndividual(1);
154  boost::shared_ptr<Individual> ind1 = createIndividual(1);
155  boost::shared_ptr<Individual> ind2 = createIndividual(1);
156  boost::shared_ptr<Individual> ind3 = createIndividual(1);
157 
158  size_t id0 = population_->add_individual(ind0);
159  EXPECT_EQ(static_cast<size_t>(0), id0);
160  size_t id1 = population_->add_individual(ind1);
161  EXPECT_EQ(static_cast<size_t>(1), id1);
162  size_t id2 = population_->add_individual(ind2);
163  EXPECT_EQ(static_cast<size_t>(2), id2);
164  size_t id3 = population_->add_individual(ind3);
165  EXPECT_EQ(static_cast<size_t>(3), id3);
166 
167  unsigned int individual_to_be_removed_id = 1;
168  population_->remove_individual(ind1);
169 
170  boost::shared_ptr<Individual> newind = createIndividual(1);
171  unsigned int id_new = population_->add_individual(newind);
172  EXPECT_EQ(individual_to_be_removed_id, id_new);
173 
174  boost::shared_ptr<Individual> tmp = population_->get_staging(id_new);
175  EXPECT_EQ(newind->genes_m[0], tmp->genes_m[0]);
176  EXPECT_EQ(newind->objectives_m[0], tmp->objectives_m[0]);
177  }
178 
179  TEST_F(PopulationTest, FindNonExistingStaging) {
180 
181  boost::shared_ptr<Individual> tmp = population_->get_staging(124);
182  EXPECT_EQ(0, tmp.get());
183  }
184 
185  TEST_F(PopulationTest, FindNonExistingIndividual) {
186 
187  boost::shared_ptr<Individual> tmp = population_->get_individual(124);
188  EXPECT_EQ(0, tmp.get());
189  }
190 
191  TEST_F(PopulationTest, RepresentedCheck) {
192 
193  boost::shared_ptr<Individual> ind = createIndividual(1);
194  population_->add_individual(ind);
195 
196  population_->add_individual(ind);
197  population_->commit_individuals();
198 
199  bool represented = population_->isRepresentedInPopulation(ind->genes_m);
200  EXPECT_TRUE(represented);
201 
202  std::vector<double> tmp_genes;
203  tmp_genes.push_back(12312);
204  represented = population_->isRepresentedInPopulation(tmp_genes);
205  EXPECT_FALSE(represented);
206  }
207 
208 }
209 
210 int main(int argc, char **argv) {
211  ::testing::InitGoogleTest(&argc, argv);
212  return RUN_ALL_TESTS();
213 }
void bounds(const PETE_Expr< T1 > &expr, Vektor< T2, D > &minval, Vektor< T2, D > &maxval)
int main(int argc, char **argv)
std::vector< std::string > names_t
gene names
Definition: Individual.h:45
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition: Individual.h:49
Expressions::Named_t constraints_t
constraints
Definition: Individual.h:51