OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
IndividualTest.cpp
Go to the documentation of this file.
3 #include "gtest/gtest.h"
4 
5 #include "boost/smart_ptr.hpp"
6 
7 #include <string>
8 
9 namespace {
10 
11  // The fixture for testing class Foo.
12  class IndividualTest : public ::testing::Test {
13  protected:
14 
15  IndividualTest() {
16  // You can do set-up work for each test here.
17  lower_bound_ = -10.0;
18  upper_bound_ = 19.79;
19  }
20 
21  virtual ~IndividualTest() {
22  // You can do clean-up work that doesn't throw exceptions here.
23  }
24 
25  // If the constructor and destructor are not enough for setting up
26  // and cleaning up each test, you can define the following methods:
27 
28  virtual void SetUp() {
29  // Code here will be called immediately after the constructor (right
30  // before each test).
31  }
32 
33  virtual void TearDown() {
34  // Code here will be called immediately after each test (right
35  // before the destructor).
36  }
37 
38  boost::shared_ptr<Individual> createIndividual(size_t num_genes, std::string constraint="") {
39 
41  Individual::names_t names;
42  Individual::constraints_t constraints;
43  for(size_t i=0; i < num_genes; i++) {
44  bounds.push_back(
45  std::pair<double, double>(lower_bound_, upper_bound_));
46  names.push_back("dvar"+std::to_string(i));
47  }
48  if (constraint.empty() == false)
49  constraints.insert(std::pair<std::string,Expressions::Expr_t*>
50  ("constraint0",new Expressions::Expr_t(constraint)));
51 
52  boost::shared_ptr<Individual> ind(new Individual(bounds,names,constraints));
53  return ind;
54  }
55 
56  double lower_bound_;
57  double upper_bound_;
58  };
59 
60  TEST_F(IndividualTest, IndividualRespectsBounds) {
61 
62  size_t num_genes = 1;
63  boost::shared_ptr<Individual> ind = createIndividual(num_genes);
64  double gene = ind->genes_m[0];
65 
66  EXPECT_LE(lower_bound_, gene) << "gene should respect lower bound";
67  EXPECT_GE(upper_bound_, gene) << "gene should respect upper bound";
68 
69  size_t my_size = ind->genes_m.size();
70  EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
71  << "individual should only have " << num_genes << " gene(s)";
72 
73  }
74 
75  TEST_F(IndividualTest, IndividualHasCorrectNumberOfGenes) {
76 
77  size_t num_genes = 12;
78  boost::shared_ptr<Individual> ind = createIndividual(num_genes);
79 
80  size_t my_size = ind->genes_m.size();
81  EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
82  << "individual should only have " << num_genes << " gene(s)";
83 
84  }
85 
86  TEST_F(IndividualTest, IndividualRandomGene) {
87 
88  size_t num_genes = 1;
89  boost::shared_ptr<Individual> ind = createIndividual(num_genes);
90  double gene = ind->genes_m[0];
91  double new_gene = ind->new_gene(0);
92 
93  EXPECT_NE(gene, new_gene) << "new gene should be different";
94  }
95 
96  TEST_F(IndividualTest, IndividualConstraint) {
97 
98  size_t num_genes = 2;
99  double half = (lower_bound_ + upper_bound_) / 2.;
100  std::string constraint = "(dvar0 + dvar1)/2. <=" + std::to_string(half);
101 
102  // create several individuals to test
103  for (int i=0; i<10; i++) {
104  boost::shared_ptr<Individual> ind = createIndividual(num_genes,constraint);
105  double gene0 = ind->genes_m[0];
106  double gene1 = ind->genes_m[1];
107  EXPECT_LE((gene0+gene1)/2, half) << "constraint should be respected";
108  }
109  }
110 }
111 
112 int main(int argc, char **argv) {
113  ::testing::InitGoogleTest(&argc, argv);
114  return RUN_ALL_TESTS();
115 }
Expression to be evaluated in the framework.
Definition: Expression.h:75
void bounds(const PETE_Expr< T1 > &expr, Vektor< T2, D > &minval, Vektor< T2, D > &maxval)
int main(int argc, char *argv[])
Definition: Main.cpp:107
std::vector< std::string > names_t
gene names
Definition: Individual.h:28
Expressions::Named_t constraints_t
constraints
Definition: Individual.h:34
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition: Individual.h:32