OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
IndividualTest.cpp
Go to the documentation of this file.
1//
2// Test IndividualTest
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#include <string>
28
29namespace {
30
31 // The fixture for testing class Foo.
32 class IndividualTest : public ::testing::Test {
33 protected:
34
35 IndividualTest() {
36 // You can do set-up work for each test here.
37 lower_bound_ = -10.0;
38 upper_bound_ = 19.79;
39 }
40
41 virtual ~IndividualTest() {
42 // You can do clean-up work that doesn't throw exceptions here.
43 }
44
45 // If the constructor and destructor are not enough for setting up
46 // and cleaning up each test, you can define the following methods:
47
48 virtual void SetUp() {
49 // Code here will be called immediately after the constructor (right
50 // before each test).
51 }
52
53 virtual void TearDown() {
54 // Code here will be called immediately after each test (right
55 // before the destructor).
56 }
57
58 boost::shared_ptr<Individual> createIndividual(size_t num_genes, std::string constraint="") {
59
62 Individual::constraints_t constraints;
63 for(size_t i=0; i < num_genes; i++) {
64 bounds.push_back(
65 std::pair<double, double>(lower_bound_, upper_bound_));
66 names.push_back("dvar"+std::to_string(i));
67 }
68 if (constraint.empty() == false)
69 constraints.insert(std::pair<std::string,Expressions::Expr_t*>
70 ("constraint0",new Expressions::Expr_t(constraint)));
71
72 boost::shared_ptr<Individual> ind(new Individual(bounds,names,constraints));
73 return ind;
74 }
75
76 double lower_bound_;
77 double upper_bound_;
78 };
79
80 TEST_F(IndividualTest, IndividualRespectsBounds) {
81
82 size_t num_genes = 1;
83 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
84 double gene = ind->genes_m[0];
85
86 EXPECT_LE(lower_bound_, gene) << "gene should respect lower bound";
87 EXPECT_GE(upper_bound_, gene) << "gene should respect upper bound";
88
89 size_t my_size = ind->genes_m.size();
90 EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
91 << "individual should only have " << num_genes << " gene(s)";
92
93 }
94
95 TEST_F(IndividualTest, IndividualHasCorrectNumberOfGenes) {
96
97 size_t num_genes = 12;
98 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
99
100 size_t my_size = ind->genes_m.size();
101 EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
102 << "individual should only have " << num_genes << " gene(s)";
103
104 }
105
106 TEST_F(IndividualTest, IndividualRandomGene) {
107
108 size_t num_genes = 1;
109 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
110 double gene = ind->genes_m[0];
111 double new_gene = ind->new_gene(0);
112
113 EXPECT_NE(gene, new_gene) << "new gene should be different";
114 }
115
116 TEST_F(IndividualTest, IndividualConstraint) {
117
118 size_t num_genes = 2;
119 double half = (lower_bound_ + upper_bound_) / 2.;
120 std::string constraint = "(dvar0 + dvar1)/2. <=" + std::to_string(half);
121
122 // create several individuals to test
123 for (int i=0; i<10; i++) {
124 boost::shared_ptr<Individual> ind = createIndividual(num_genes,constraint);
125 double gene0 = ind->genes_m[0];
126 double gene1 = ind->genes_m[1];
127 EXPECT_LE((gene0+gene1)/2, half) << "constraint should be respected";
128 }
129 }
130}
131
132int main(int argc, char **argv) {
133 ::testing::InitGoogleTest(&argc, argv);
134 return RUN_ALL_TESTS();
135}
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